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