Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,25 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
from fastapi import FastAPI, HTTPException, Query
|
4 |
from fastapi.responses import StreamingResponse
|
5 |
-
from pydantic import BaseModel
|
6 |
from openai import AsyncOpenAI
|
7 |
-
from typing import Optional
|
8 |
|
9 |
-
|
10 |
-
logging.basicConfig(level=logging.DEBUG)
|
11 |
-
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
description="API for streaming AI responses with model selection and publisher via URL",
|
16 |
-
version="1.0.0"
|
17 |
-
)
|
18 |
-
|
19 |
-
|
20 |
-
class GenerateRequest(BaseModel):
|
21 |
-
prompt: str
|
22 |
-
publisher: Optional[str] = None # Allow publisher in the body if needed
|
23 |
-
|
24 |
-
async def generate_ai_response(prompt: str, model: str, publisher: Optional[str]):
|
25 |
-
logger.debug(f"Received prompt: {prompt}, model: {model}, publisher: {publisher}")
|
26 |
-
|
27 |
-
# Configuration for AI endpoint
|
28 |
token = os.getenv("GITHUB_TOKEN")
|
29 |
-
endpoint = os.getenv("AI_SERVER_URL", "https://models.github.ai/inference")
|
30 |
-
default_publisher = os.getenv("DEFAULT_PUBLISHER", "abdullahalioo") # Fallback publisher
|
31 |
-
|
32 |
if not token:
|
33 |
-
logger.error("GitHub token not configured")
|
34 |
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
client = AsyncOpenAI(
|
38 |
-
base_url=endpoint,
|
39 |
-
api_key=token,
|
40 |
-
default_headers={"X-Publisher": final_publisher} # Pass publisher as header
|
41 |
-
)
|
42 |
|
43 |
try:
|
44 |
stream = await client.chat.completions.create(
|
45 |
messages=[
|
46 |
-
{"role": "system", "content": "You are a helpful assistant named Orion
|
47 |
{"role": "user", "content": prompt}
|
48 |
],
|
49 |
model=model,
|
@@ -57,55 +33,18 @@ async def generate_ai_response(prompt: str, model: str, publisher: Optional[str]
|
|
57 |
yield chunk.choices[0].delta.content
|
58 |
|
59 |
except Exception as err:
|
60 |
-
|
61 |
-
|
62 |
-
error_msg = str(err)
|
63 |
-
if "unknown_model" in error_msg.lower():
|
64 |
-
raise HTTPException(status_code=400, detail=f"AI server error: {error_msg}")
|
65 |
-
yield f"Error: {error_msg}"
|
66 |
-
raise HTTPException(status_code=500, detail=f"AI generation failed: {error_msg}")
|
67 |
|
68 |
-
@app.post("/generate"
|
69 |
-
async def generate_response(
|
70 |
-
|
71 |
-
prompt: Optional[str] = Query(None, description="The input text prompt for the AI"),
|
72 |
-
publisher: Optional[str] = Query(None, description="Publisher identifier (optional, defaults to DEFAULT_PUBLISHER env var)"),
|
73 |
-
request: Optional[GenerateRequest] = None
|
74 |
-
):
|
75 |
-
"""
|
76 |
-
Generate a streaming AI response based on the provided prompt, model, and publisher.
|
77 |
-
|
78 |
-
- **model**: The AI model to use (e.g., DeepSeek-V3-0324)
|
79 |
-
- **prompt**: The input text prompt for the AI (query param or body)
|
80 |
-
- **publisher**: The publisher identifier (optional, defaults to DEFAULT_PUBLISHER env var)
|
81 |
-
"""
|
82 |
-
logger.debug(f"Request received - model: {model}, prompt: {prompt}, publisher: {publisher}, body: {request}")
|
83 |
-
|
84 |
-
# Determine prompt source: query parameter or request body
|
85 |
-
final_prompt = prompt if prompt is not None else (request.prompt if request is not None else None)
|
86 |
-
# Determine publisher source: query parameter or request body
|
87 |
-
final_publisher = publisher if publisher is not None else (request.publisher if request is not None else None)
|
88 |
-
|
89 |
-
if not final_prompt or not final_prompt.strip():
|
90 |
-
logger.error("Prompt cannot be empty")
|
91 |
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
92 |
|
93 |
-
if not model or not model.strip():
|
94 |
-
logger.error("Model cannot be empty")
|
95 |
-
raise HTTPException(status_code=400, detail="Model cannot be empty")
|
96 |
-
|
97 |
return StreamingResponse(
|
98 |
-
generate_ai_response(
|
99 |
media_type="text/event-stream"
|
100 |
)
|
101 |
|
102 |
-
@app.get("/models", summary="List available models")
|
103 |
-
async def list_models():
|
104 |
-
"""
|
105 |
-
List all available models supported by the AI server.
|
106 |
-
"""
|
107 |
-
return {"models": VALID_MODELS}
|
108 |
-
|
109 |
def get_app():
|
110 |
-
return app
|
111 |
-
|
|
|
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:
|
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,
|
|
|
33 |
yield chunk.choices[0].delta.content
|
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
|
|