Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,19 @@
|
|
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 |
-
async def generate_ai_response(prompt: str
|
|
|
9 |
token = os.getenv("GITHUB_TOKEN")
|
10 |
if not token:
|
11 |
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
12 |
|
13 |
endpoint = "https://models.github.ai/inference"
|
|
|
|
|
14 |
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
15 |
|
16 |
try:
|
@@ -31,20 +34,17 @@ async def generate_ai_response(prompt: str, model: str):
|
|
31 |
|
32 |
except Exception as err:
|
33 |
yield f"Error: {str(err)}"
|
34 |
-
raise HTTPException(status_code=500, detail=
|
35 |
|
36 |
@app.post("/generate")
|
37 |
-
async def generate_response(
|
38 |
-
prompt:
|
39 |
-
|
40 |
-
):
|
41 |
-
if not prompt or not model:
|
42 |
-
raise HTTPException(status_code=400, detail="Prompt and model must be provided")
|
43 |
|
44 |
return StreamingResponse(
|
45 |
-
generate_ai_response(prompt
|
46 |
media_type="text/event-stream"
|
47 |
)
|
48 |
|
49 |
def get_app():
|
50 |
-
return app
|
|
|
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 |
+
async def generate_ai_response(prompt: str):
|
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 |
|
19 |
try:
|
|
|
34 |
|
35 |
except Exception as err:
|
36 |
yield f"Error: {str(err)}"
|
37 |
+
raise HTTPException(status_code=500, detail="AI generation failed")
|
38 |
|
39 |
@app.post("/generate")
|
40 |
+
async def generate_response(prompt: str):
|
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 |
|
49 |
def get_app():
|
50 |
+
return app
|