Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,23 @@
|
|
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 |
-
model = "openai/gpt-4.1-mini" # Unofficial model name
|
16 |
|
17 |
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
18 |
|
@@ -37,12 +42,12 @@ 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
|
3 |
from fastapi.responses import StreamingResponse
|
4 |
+
from pydantic import BaseModel
|
5 |
from openai import AsyncOpenAI
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
+
# Define a request model for the prompt and optional model name
|
10 |
+
class GenerateRequest(BaseModel):
|
11 |
+
prompt: str
|
12 |
+
model: str | None = "openai/gpt-4.1-mini" # Default model
|
13 |
+
|
14 |
+
async def generate_ai_response(prompt: str, model: str):
|
15 |
# Configuration for unofficial GitHub AI endpoint
|
16 |
token = os.getenv("GITHUB_TOKEN")
|
17 |
if not token:
|
18 |
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
19 |
|
20 |
endpoint = "https://models.github.ai/inference"
|
|
|
21 |
|
22 |
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
23 |
|
|
|
42 |
raise HTTPException(status_code=500, detail="AI generation failed")
|
43 |
|
44 |
@app.post("/generate")
|
45 |
+
async def generate_response(request: GenerateRequest):
|
46 |
+
if not request.prompt:
|
47 |
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
48 |
|
49 |
return StreamingResponse(
|
50 |
+
generate_ai_response(request.prompt, request.model),
|
51 |
media_type="text/event-stream"
|
52 |
)
|
53 |
|