Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,25 @@
|
|
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 |
-
|
10 |
-
|
11 |
-
model: str # Model is required, no default
|
12 |
-
|
13 |
-
async def generate_ai_response(prompt: str, model: str):
|
14 |
token = os.getenv("GITHUB_TOKEN")
|
15 |
if not token:
|
16 |
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
17 |
|
18 |
endpoint = "https://models.github.ai/inference"
|
|
|
|
|
19 |
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
20 |
|
21 |
try:
|
22 |
stream = await client.chat.completions.create(
|
23 |
messages=[
|
24 |
-
{"role": "system", "content": "You are a helpful assistant
|
25 |
{"role": "user", "content": prompt}
|
26 |
],
|
27 |
model=model,
|
@@ -36,17 +34,15 @@ async def generate_ai_response(prompt: str, model: str):
|
|
36 |
|
37 |
except Exception as err:
|
38 |
yield f"Error: {str(err)}"
|
39 |
-
raise HTTPException(status_code=500, detail=
|
40 |
|
41 |
@app.post("/generate")
|
42 |
-
async def generate_response(
|
43 |
-
if not
|
44 |
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
45 |
-
if not request.model:
|
46 |
-
raise HTTPException(status_code=400, detail="Model must be specified")
|
47 |
|
48 |
return StreamingResponse(
|
49 |
-
generate_ai_response(
|
50 |
media_type="text/event-stream"
|
51 |
)
|
52 |
|
|
|
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 , model: 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:
|
20 |
stream = await client.chat.completions.create(
|
21 |
messages=[
|
22 |
+
{"role": "system", "content": "You are a helpful assistant named Orion and made by Abdullah Ali"},
|
23 |
{"role": "user", "content": prompt}
|
24 |
],
|
25 |
model=model,
|
|
|
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 , model: 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 , model),
|
46 |
media_type="text/event-stream"
|
47 |
)
|
48 |
|