Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
-
|
| 5 |
from pydantic import BaseModel
|
|
|
|
| 6 |
|
| 7 |
# Initialize FastAPI app
|
| 8 |
app = FastAPI()
|
|
@@ -16,41 +17,49 @@ token = os.getenv("GITHUB_TOKEN")
|
|
| 16 |
if not token:
|
| 17 |
raise ValueError("GITHUB_TOKEN environment variable not set")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
async def stream_response(prompt: str):
|
| 24 |
try:
|
| 25 |
-
# Create streaming chat completion
|
| 26 |
-
|
| 27 |
-
model="gpt-4", # Replace with the model you're using (e.g., gpt-3.5-turbo or gpt-4)
|
| 28 |
messages=[
|
| 29 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 30 |
{"role": "user", "content": prompt}
|
| 31 |
],
|
| 32 |
temperature=1.0,
|
| 33 |
top_p=1.0,
|
| 34 |
-
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# Yield each chunk
|
| 38 |
-
for chunk in
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
except Exception as err:
|
| 44 |
yield f"Error: {str(err)}"
|
| 45 |
|
| 46 |
-
# Endpoint to handle
|
| 47 |
@app.post("/generate")
|
| 48 |
async def generate_response(request: PromptRequest):
|
| 49 |
try:
|
| 50 |
-
# Return a StreamingResponse with the async generator
|
| 51 |
return StreamingResponse(
|
| 52 |
stream_response(request.prompt),
|
| 53 |
-
media_type="text/event-stream"
|
| 54 |
)
|
| 55 |
except Exception as err:
|
| 56 |
raise HTTPException(status_code=500, detail=f"Server error: {str(err)}")
|
|
@@ -58,4 +67,4 @@ async def generate_response(request: PromptRequest):
|
|
| 58 |
# Health check endpoint for Hugging Face Spaces
|
| 59 |
@app.get("/")
|
| 60 |
async def health_check():
|
| 61 |
-
return {"status": "healthy"}
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
+
from openai import AsyncOpenAI
|
| 5 |
from pydantic import BaseModel
|
| 6 |
+
import httpx
|
| 7 |
|
| 8 |
# Initialize FastAPI app
|
| 9 |
app = FastAPI()
|
|
|
|
| 17 |
if not token:
|
| 18 |
raise ValueError("GITHUB_TOKEN environment variable not set")
|
| 19 |
|
| 20 |
+
# Use environment variables for endpoint and model, with fallbacks
|
| 21 |
+
endpoint = os.getenv("API_ENDPOINT", "https://api.openai.com/v1") # Fallback to OpenAI-compatible endpoint
|
| 22 |
+
model = os.getenv("MODEL_NAME", "gpt-4o-mini") # Default to a known model
|
| 23 |
|
| 24 |
+
# Initialize AsyncOpenAI with a custom HTTP client to avoid proxies issue
|
| 25 |
+
client = AsyncOpenAI(
|
| 26 |
+
base_url=endpoint,
|
| 27 |
+
api_key=token,
|
| 28 |
+
http_client=httpx.AsyncClient() # Explicitly use httpx.AsyncClient without proxies
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Async generator to stream chunks
|
| 32 |
async def stream_response(prompt: str):
|
| 33 |
try:
|
| 34 |
+
# Create streaming chat completion
|
| 35 |
+
stream = await client.chat.completions.create(
|
|
|
|
| 36 |
messages=[
|
| 37 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 38 |
{"role": "user", "content": prompt}
|
| 39 |
],
|
| 40 |
temperature=1.0,
|
| 41 |
top_p=1.0,
|
| 42 |
+
model=model,
|
| 43 |
+
stream=True
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Yield each chunk as it arrives
|
| 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:
|
| 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")
|
| 58 |
async def generate_response(request: PromptRequest):
|
| 59 |
try:
|
|
|
|
| 60 |
return StreamingResponse(
|
| 61 |
stream_response(request.prompt),
|
| 62 |
+
media_type="text/event-stream"
|
| 63 |
)
|
| 64 |
except Exception as err:
|
| 65 |
raise HTTPException(status_code=500, detail=f"Server error: {str(err)}")
|
|
|
|
| 67 |
# Health check endpoint for Hugging Face Spaces
|
| 68 |
@app.get("/")
|
| 69 |
async def health_check():
|
| 70 |
+
return {"status": "healthy"}
|