Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,137 +8,127 @@ from collections import defaultdict
|
|
8 |
app = FastAPI()
|
9 |
|
10 |
# Define available models
|
11 |
-
|
12 |
AVAILABLE_MODELS = {
|
13 |
-
"openai/gpt-4.1": "OpenAI GPT-4.1",
|
14 |
-
"openai/gpt-4.1-mini": "OpenAI GPT-4.1-mini",
|
15 |
-
"openai/gpt-4.1-nano": "OpenAI GPT-4.1-nano",
|
16 |
-
"openai/gpt-4o": "OpenAI GPT-4o",
|
17 |
-
"openai/gpt-4o-mini": "OpenAI GPT-4o mini",
|
18 |
-
"openai/o4-mini": "OpenAI o4-mini",
|
19 |
-
"microsoft/MAI-DS-R1": "MAI-DS-R1",
|
20 |
-
"microsoft/Phi-3.5-MoE-instruct": "Phi-3.5-MoE instruct (128k)",
|
21 |
-
"microsoft/Phi-3.5-mini-instruct": "Phi-3.5-mini instruct (128k)",
|
22 |
-
"microsoft/Phi-3.5-vision-instruct": "Phi-3.5-vision instruct (128k)",
|
23 |
-
"microsoft/Phi-3-medium-128k-instruct": "Phi-3-medium instruct (128k)",
|
24 |
-
"microsoft/Phi-3-medium-4k-instruct": "Phi-3-medium instruct (4k)",
|
25 |
-
"microsoft/Phi-3-mini-128k-instruct": "Phi-3-mini instruct (128k)",
|
26 |
-
"microsoft/Phi-3-small-128k-instruct": "Phi-3-small instruct (128k)",
|
27 |
-
"microsoft/Phi-3-small-8k-instruct": "Phi-3-small instruct (8k)",
|
28 |
-
"microsoft/Phi-4": "Phi-4",
|
29 |
-
"microsoft/Phi-4-mini-instruct": "Phi-4-mini-instruct",
|
30 |
-
"microsoft/Phi-4-multimodal-instruct": "Phi-4-multimodal-instruct",
|
31 |
-
"ai21-labs/AI21-Jamba-1.5-Large": "AI21 Jamba 1.5 Large",
|
32 |
-
"ai21-labs/AI21-Jamba-1.5-Mini": "AI21 Jamba 1.5 Mini",
|
33 |
-
"mistral-ai/Codestral-2501": "Codestral 25.01",
|
34 |
-
"cohere/Cohere-command-r": "Cohere Command R",
|
35 |
-
"cohere/Cohere-command-r-08-2024": "Cohere Command R 08-2024",
|
36 |
-
"cohere/Cohere-command-r-plus": "Cohere Command R+",
|
37 |
-
"cohere/Cohere-command-r-plus-08-2024": "Cohere Command R+ 08-2024",
|
38 |
-
"deepseek/DeepSeek-R1": "DeepSeek-R1",
|
39 |
-
"deepseek/DeepSeek-V3-0324": "DeepSeek-V3-0324",
|
40 |
-
"meta/Llama-3.2-11B-Vision-Instruct": "Llama-3.2-11B-Vision-Instruct",
|
41 |
-
"meta/Llama-3.2-90B-Vision-Instruct": "Llama-3.2-90B-Vision-Instruct",
|
42 |
-
"meta/Llama-3.3-70B-Instruct": "Llama-3.3-70B-Instruct",
|
43 |
-
"meta/Llama-4-Maverick-17B-128E-Instruct-FP8": "Llama 4 Maverick 17B 128E Instruct FP8",
|
44 |
-
"meta/Llama-4-Scout-17B-16E-Instruct": "Llama 4 Scout 17B 16E Instruct",
|
45 |
-
"meta/Meta-Llama-3.1-405B-Instruct": "Meta-Llama-3.1-405B-Instruct",
|
46 |
-
"meta/Meta-Llama-3.1-70B-Instruct": "Meta-Llama-3.1-70B-Instruct",
|
47 |
-
"meta/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
|
48 |
-
"meta/Meta-Llama-3-70B-Instruct": "Meta-Llama-3-70B-Instruct",
|
49 |
-
"meta/Meta-Llama-3-8B-Instruct": "Meta-Llama-3-8B-Instruct",
|
50 |
-
"mistral-ai/Ministral-3B": "Ministral 3B",
|
51 |
-
"mistral-ai/Mistral-Large-2411": "Mistral Large 24.11",
|
52 |
-
"mistral-ai/Mistral-Nemo": "Mistral Nemo",
|
53 |
-
"mistral-ai/Mistral-large-2407": "Mistral Large (2407)",
|
54 |
-
"mistral-ai/Mistral-small": "Mistral Small",
|
55 |
-
"cohere/cohere-command-a": "Cohere Command A",
|
56 |
-
"core42/jais-30b-chat": "JAIS 30b Chat",
|
57 |
-
"mistral-ai/mistral-small-2503": "Mistral Small 3.1"
|
58 |
}
|
59 |
|
60 |
# Chat memory (in-memory)
|
61 |
-
|
62 |
chat_histories = defaultdict(list)
|
63 |
MAX_HISTORY = 100 # limit memory to avoid crashes
|
64 |
|
65 |
# Generate response stream
|
66 |
-
|
67 |
async def generate_ai_response(chat_id: str, model: str):
|
68 |
-
token = os.getenv("GITHUB_TOKEN")
|
69 |
-
if not token:
|
70 |
-
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
raise HTTPException(status_code=500, detail="AI generation failed")
|
108 |
-
```
|
109 |
|
110 |
# Chat endpoint
|
111 |
-
|
112 |
@app.post("/generate")
|
113 |
-
async def
|
114 |
-
|
115 |
-
prompt: str = Query(..., description="User message"),
|
116 |
-
model: str = Query("openai/gpt-4.1-mini", description="Model to use")
|
117 |
):
|
118 |
-
if not prompt:
|
119 |
-
raise HTTPException(
|
120 |
|
121 |
-
|
122 |
-
chat_histories[chat_id]
|
123 |
-
chat_histories[chat_id] = chat_histories[chat_id][-MAX_HISTORY:]
|
124 |
|
125 |
-
return StreamingResponse(
|
126 |
-
|
127 |
-
|
128 |
-
)
|
129 |
-
```
|
130 |
|
131 |
# Optional: reset chat history
|
132 |
-
|
133 |
@app.post("/reset")
|
134 |
async def reset_chat(chat_id: str = Query(..., description="ID of chat to reset")):
|
135 |
-
if chat_id in chat_histories:
|
136 |
-
chat_histories
|
137 |
-
return {"message": f"Chat {chat_id} history reset."}
|
138 |
-
else:
|
139 |
-
raise HTTPException(status_code=404, detail="Chat ID not found")
|
140 |
|
141 |
# For ASGI servers like Uvicorn
|
142 |
-
|
143 |
-
|
144 |
-
return app
|
|
|
8 |
app = FastAPI()
|
9 |
|
10 |
# Define available models
|
|
|
11 |
AVAILABLE_MODELS = {
|
12 |
+
"openai/gpt-4.1": "OpenAI GPT-4.1",
|
13 |
+
"openai/gpt-4.1-mini": "OpenAI GPT-4.1-mini",
|
14 |
+
"openai/gpt-4.1-nano": "OpenAI GPT-4.1-nano",
|
15 |
+
"openai/gpt-4o": "OpenAI GPT-4o",
|
16 |
+
"openai/gpt-4o-mini": "OpenAI GPT-4o mini",
|
17 |
+
"openai/o4-mini": "OpenAI o4-mini",
|
18 |
+
"microsoft/MAI-DS-R1": "MAI-DS-R1",
|
19 |
+
"microsoft/Phi-3.5-MoE-instruct": "Phi-3.5-MoE instruct (128k)",
|
20 |
+
"microsoft/Phi-3.5-mini-instruct": "Phi-3.5-mini instruct (128k)",
|
21 |
+
"microsoft/Phi-3.5-vision-instruct": "Phi-3.5-vision instruct (128k)",
|
22 |
+
"microsoft/Phi-3-medium-128k-instruct": "Phi-3-medium instruct (128k)",
|
23 |
+
"microsoft/Phi-3-medium-4k-instruct": "Phi-3-medium instruct (4k)",
|
24 |
+
"microsoft/Phi-3-mini-128k-instruct": "Phi-3-mini instruct (128k)",
|
25 |
+
"microsoft/Phi-3-small-128k-instruct": "Phi-3-small instruct (128k)",
|
26 |
+
"microsoft/Phi-3-small-8k-instruct": "Phi-3-small instruct (8k)",
|
27 |
+
"microsoft/Phi-4": "Phi-4",
|
28 |
+
"microsoft/Phi-4-mini-instruct": "Phi-4-mini-instruct",
|
29 |
+
"microsoft/Phi-4-multimodal-instruct": "Phi-4-multimodal-instruct",
|
30 |
+
"ai21-labs/AI21-Jamba-1.5-Large": "AI21 Jamba 1.5 Large",
|
31 |
+
"ai21-labs/AI21-Jamba-1.5-Mini": "AI21 Jamba 1.5 Mini",
|
32 |
+
"mistral-ai/Codestral-2501": "Codestral 25.01",
|
33 |
+
"cohere/Cohere-command-r": "Cohere Command R",
|
34 |
+
"cohere/Cohere-command-r-08-2024": "Cohere Command R 08-2024",
|
35 |
+
"cohere/Cohere-command-r-plus": "Cohere Command R+",
|
36 |
+
"cohere/Cohere-command-r-plus-08-2024": "Cohere Command R+ 08-2024",
|
37 |
+
"deepseek/DeepSeek-R1": "DeepSeek-R1",
|
38 |
+
"deepseek/DeepSeek-V3-0324": "DeepSeek-V3-0324",
|
39 |
+
"meta/Llama-3.2-11B-Vision-Instruct": "Llama-3.2-11B-Vision-Instruct",
|
40 |
+
"meta/Llama-3.2-90B-Vision-Instruct": "Llama-3.2-90B-Vision-Instruct",
|
41 |
+
"meta/Llama-3.3-70B-Instruct": "Llama-3.3-70B-Instruct",
|
42 |
+
"meta/Llama-4-Maverick-17B-128E-Instruct-FP8": "Llama 4 Maverick 17B 128E Instruct FP8",
|
43 |
+
"meta/Llama-4-Scout-17B-16E-Instruct": "Llama 4 Scout 17B 16E Instruct",
|
44 |
+
"meta/Meta-Llama-3.1-405B-Instruct": "Meta-Llama-3.1-405B-Instruct",
|
45 |
+
"meta/Meta-Llama-3.1-70B-Instruct": "Meta-Llama-3.1-70B-Instruct",
|
46 |
+
"meta/Meta-Llama-3.1-8B-Instruct": "Meta-Llama-3.1-8B-Instruct",
|
47 |
+
"meta/Meta-Llama-3-70B-Instruct": "Meta-Llama-3-70B-Instruct",
|
48 |
+
"meta/Meta-Llama-3-8B-Instruct": "Meta-Llama-3-8B-Instruct",
|
49 |
+
"mistral-ai/Ministral-3B": "Ministral 3B",
|
50 |
+
"mistral-ai/Mistral-Large-2411": "Mistral Large 24.11",
|
51 |
+
"mistral-ai/Mistral-Nemo": "Mistral Nemo",
|
52 |
+
"mistral-ai/Mistral-large-2407": "Mistral Large (2407)",
|
53 |
+
"mistral-ai/Mistral-small": "Mistral Small",
|
54 |
+
"cohere/cohere-command-a": "Cohere Command A",
|
55 |
+
"core42/jais-30b-chat": "JAIS 30b Chat",
|
56 |
+
"mistral-ai/mistral-small-2503": "Mistral Small 3.1"
|
57 |
}
|
58 |
|
59 |
# Chat memory (in-memory)
|
|
|
60 |
chat_histories = defaultdict(list)
|
61 |
MAX_HISTORY = 100 # limit memory to avoid crashes
|
62 |
|
63 |
# Generate response stream
|
|
|
64 |
async def generate_ai_response(chat_id: str, model: str):
|
65 |
+
token = os.getenv("GITHUB_TOKEN")
|
66 |
+
if not token:
|
67 |
+
raise HTTPException(status_code=500, detail="GitHub token not configured")
|
68 |
+
|
69 |
+
endpoint = "https://models.github.ai/inference"
|
70 |
+
|
71 |
+
if model not in AVAILABLE_MODELS:
|
72 |
+
raise HTTPException(
|
73 |
+
status_code=400,
|
74 |
+
detail=f"Model not available. Choose from: {', '.join(AVAILABLE_MODELS.keys())}"
|
75 |
+
)
|
76 |
+
|
77 |
+
client = AsyncOpenAI(base_url=endpoint, api_key=token)
|
78 |
+
|
79 |
+
try:
|
80 |
+
stream = await asyncio.wait_for(
|
81 |
+
client.chat.completions.create(
|
82 |
+
messages=chat_histories[chat_id],
|
83 |
+
model=model,
|
84 |
+
temperature=1.0,
|
85 |
+
top_p=1.0,
|
86 |
+
stream=True
|
87 |
+
),
|
88 |
+
timeout=60 # Prevent hangs
|
89 |
+
)
|
90 |
+
|
91 |
+
async for chunk in stream:
|
92 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
93 |
+
content = chunk.choices[0].delta.content
|
94 |
+
yield content
|
95 |
+
chat_histories[chat_id].append({"role": "assistant", "content": content})
|
96 |
+
chat_histories[chat_id] = chat_histories[chat_id][-MAX_HISTORY:]
|
97 |
+
|
98 |
+
except asyncio.TimeoutError:
|
99 |
+
yield "Error: Response timed out."
|
100 |
+
raise HTTPException(status_code=504, detail="Model timed out.")
|
101 |
+
except Exception as err:
|
102 |
+
yield f"Error: {str(err)}"
|
103 |
+
raise HTTPException(status_code=500, detail="AI generation failed")
|
|
|
|
|
104 |
|
105 |
# Chat endpoint
|
|
|
106 |
@app.post("/generate")
|
107 |
+
async def generate_response(
|
108 |
+
chat_id: str = Query(..., description="Unique chat ID"),
|
109 |
+
prompt: str = Query(..., description="User message"),
|
110 |
+
model: str = Query("openai/gpt-4.1-mini", description="Model to use")
|
111 |
):
|
112 |
+
if not prompt:
|
113 |
+
raise HTTPException(status_code=400, detail="Prompt cannot be empty")
|
114 |
|
115 |
+
chat_histories[chat_id].append({"role": "user", "content": prompt})
|
116 |
+
chat_histories[chat_id] = chat_histories[chat_id][-MAX_HISTORY:]
|
|
|
117 |
|
118 |
+
return StreamingResponse(
|
119 |
+
generate_ai_response(chat_id, model),
|
120 |
+
media_type="text/event-stream"
|
121 |
+
)
|
|
|
122 |
|
123 |
# Optional: reset chat history
|
|
|
124 |
@app.post("/reset")
|
125 |
async def reset_chat(chat_id: str = Query(..., description="ID of chat to reset")):
|
126 |
+
if chat_id in chat_histories:
|
127 |
+
chat_histories[chat_id].clear()
|
128 |
+
return {"message": f"Chat {chat_id} history reset."}
|
129 |
+
else:
|
130 |
+
raise HTTPException(status_code=404, detail="Chat ID not found")
|
131 |
|
132 |
# For ASGI servers like Uvicorn
|
133 |
+
def get_app():
|
134 |
+
return app
|
|