Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ from fastapi import FastAPI, HTTPException
|
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
from pydantic import BaseModel
|
| 6 |
-
import asyncio
|
| 7 |
|
| 8 |
# Initialize FastAPI app
|
| 9 |
app = FastAPI()
|
|
@@ -16,9 +15,18 @@ class PromptRequest(BaseModel):
|
|
| 16 |
token = os.getenv("GITHUB_TOKEN")
|
| 17 |
if not token:
|
| 18 |
raise ValueError("GITHUB_TOKEN environment variable not set")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Async generator to stream chunks
|
| 24 |
async def stream_response(prompt: str):
|
|
@@ -39,10 +47,11 @@ async def stream_response(prompt: str):
|
|
| 39 |
async for chunk in stream:
|
| 40 |
if chunk.choices and len(chunk.choices) > 0:
|
| 41 |
content = chunk.choices[0].delta.content or ""
|
| 42 |
-
yield content
|
|
|
|
| 43 |
|
| 44 |
except Exception as err:
|
| 45 |
-
yield f"Error: {err}"
|
| 46 |
|
| 47 |
# Endpoint to handle prompt and stream response
|
| 48 |
@app.post("/generate")
|
|
@@ -51,8 +60,12 @@ async def generate_response(request: PromptRequest):
|
|
| 51 |
# Return a StreamingResponse with the async generator
|
| 52 |
return StreamingResponse(
|
| 53 |
stream_response(request.prompt),
|
| 54 |
-
media_type="text/
|
| 55 |
)
|
| 56 |
except Exception as err:
|
| 57 |
-
raise HTTPException(status_code=500, detail=f"Server error: {err}")
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
from pydantic import BaseModel
|
|
|
|
| 6 |
|
| 7 |
# Initialize FastAPI app
|
| 8 |
app = FastAPI()
|
|
|
|
| 15 |
token = os.getenv("GITHUB_TOKEN")
|
| 16 |
if not token:
|
| 17 |
raise ValueError("GITHUB_TOKEN environment variable not set")
|
| 18 |
+
|
| 19 |
+
# Use the correct endpoint for GitHub Models or fallback to a compatible OpenAI-like API
|
| 20 |
+
endpoint = os.getenv("API_ENDPOINT", "https://api.github.com/models") # Adjust based on GitHub Models documentation
|
| 21 |
+
model = os.getenv("MODEL_NAME", "gpt-4o-mini") # Use a valid model name, e.g., gpt-4o-mini or equivalent
|
| 22 |
+
|
| 23 |
+
# Initialize AsyncOpenAI client without proxies to avoid TypeError
|
| 24 |
+
client = AsyncOpenAI(
|
| 25 |
+
base_url=endpoint,
|
| 26 |
+
api_key=token,
|
| 27 |
+
# Explicitly disable proxies if not needed
|
| 28 |
+
http_client=None # Avoid passing unexpected kwargs like proxies
|
| 29 |
+
)
|
| 30 |
|
| 31 |
# Async generator to stream chunks
|
| 32 |
async def stream_response(prompt: str):
|
|
|
|
| 47 |
async for chunk in stream:
|
| 48 |
if chunk.choices and len(chunk.choices) > 0:
|
| 49 |
content = chunk.choices[0].delta.content or ""
|
| 50 |
+
if content: # Only yield non-empty content
|
| 51 |
+
yield content
|
| 52 |
|
| 53 |
except Exception as err:
|
| 54 |
+
yield f"Error: {str(err)}"
|
| 55 |
|
| 56 |
# Endpoint to handle prompt and stream response
|
| 57 |
@app.post("/generate")
|
|
|
|
| 60 |
# Return a StreamingResponse with the async generator
|
| 61 |
return StreamingResponse(
|
| 62 |
stream_response(request.prompt),
|
| 63 |
+
media_type="text/event-stream" # Use text/event-stream for streaming
|
| 64 |
)
|
| 65 |
except Exception as err:
|
| 66 |
+
raise HTTPException(status_code=500, detail=f"Server error: {str(err)}")
|
| 67 |
|
| 68 |
+
# Health check endpoint for Hugging Face Spaces
|
| 69 |
+
@app.get("/")
|
| 70 |
+
async def health_check():
|
| 71 |
+
return {"status": "healthy"}
|