Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Configuration for unofficial GitHub AI endpoint
|
| 10 |
token = os.getenv("GITHUB_TOKEN")
|
| 11 |
if not token:
|
| 12 |
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
| 13 |
|
| 14 |
endpoint = "https://models.github.ai/inference"
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
| 18 |
|
|
@@ -37,12 +47,15 @@ async def generate_ai_response(prompt: str):
|
|
| 37 |
raise HTTPException(status_code=500, detail="AI generation failed")
|
| 38 |
|
| 39 |
@app.post("/generate")
|
| 40 |
-
async def generate_response(
|
|
|
|
|
|
|
|
|
|
| 41 |
if not prompt:
|
| 42 |
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
| 43 |
|
| 44 |
return StreamingResponse(
|
| 45 |
-
generate_ai_response(prompt),
|
| 46 |
media_type="text/event-stream"
|
| 47 |
)
|
| 48 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException, Query
|
| 3 |
from fastapi.responses import StreamingResponse
|
| 4 |
from openai import AsyncOpenAI
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Define available models (you can expand this list)
|
| 9 |
+
AVAILABLE_MODELS = {
|
| 10 |
+
"openai/gpt-4.1-mini": "OpenAI GPT-4 Mini",
|
| 11 |
+
"deepseek/DeepSeek-V3-0324": "DeepSeek V3 0324",
|
| 12 |
+
# Add more models as needed
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
async def generate_ai_response(prompt: str, model: str):
|
| 16 |
# Configuration for unofficial GitHub AI endpoint
|
| 17 |
token = os.getenv("GITHUB_TOKEN")
|
| 18 |
if not token:
|
| 19 |
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
| 20 |
|
| 21 |
endpoint = "https://models.github.ai/inference"
|
| 22 |
+
|
| 23 |
+
# Validate the model
|
| 24 |
+
if model not in AVAILABLE_MODELS:
|
| 25 |
+
raise HTTPException(status_code=400, detail=f"Model not available. Choose from: {', '.join(AVAILABLE_MODELS.keys())}")
|
| 26 |
|
| 27 |
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
| 28 |
|
|
|
|
| 47 |
raise HTTPException(status_code=500, detail="AI generation failed")
|
| 48 |
|
| 49 |
@app.post("/generate")
|
| 50 |
+
async def generate_response(
|
| 51 |
+
prompt: str = Query(..., description="The prompt for the AI"),
|
| 52 |
+
model: str = Query("openai/gpt-4.1-mini", description="The model to use for generation")
|
| 53 |
+
):
|
| 54 |
if not prompt:
|
| 55 |
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
| 56 |
|
| 57 |
return StreamingResponse(
|
| 58 |
+
generate_ai_response(prompt, model),
|
| 59 |
media_type="text/event-stream"
|
| 60 |
)
|
| 61 |
|