bahi-bh commited on
Commit
04c4bf7
·
verified ·
1 Parent(s): fcf0bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -102
app.py CHANGED
@@ -3,7 +3,6 @@ from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from pydantic import BaseModel
5
  from typing import List, Optional
6
-
7
  import asyncio
8
  import json
9
  import time
@@ -26,19 +25,13 @@ logger = logging.getLogger(__name__)
26
 
27
  API_KEY = "sk-your-secret-key"
28
 
29
- # timeout بسيط فقط لمنع التعليق الأبدي
30
- REQUEST_TIMEOUT = 45
31
-
32
- # retry خفيف
33
- MAX_RETRIES = 2
34
-
35
  # =====================================================
36
  # FASTAPI
37
  # =====================================================
38
 
39
  app = FastAPI(
40
  title="Universal AI Gateway",
41
- version="5.0.0"
42
  )
43
 
44
  # =====================================================
@@ -77,7 +70,7 @@ def verify_api_key(req: Request):
77
 
78
  auth = req.headers.get("Authorization")
79
 
80
- # السماح للاختبار
81
  if not auth:
82
  return True
83
 
@@ -107,7 +100,7 @@ async def root():
107
  return {
108
  "status": "online",
109
  "service": "Universal AI Gateway",
110
- "version": "5.0.0"
111
  }
112
 
113
  # =====================================================
@@ -118,11 +111,7 @@ async def root():
118
  async def get_models():
119
 
120
  models_data = []
121
- added_models = set()
122
-
123
- # =================================================
124
- # DYNAMIC MODELS FROM G4F
125
- # =================================================
126
 
127
  try:
128
 
@@ -134,7 +123,7 @@ async def get_models():
134
 
135
  model_name = str(model)
136
 
137
- if model_name not in added_models:
138
 
139
  models_data.append({
140
  "id": model_name,
@@ -143,43 +132,43 @@ async def get_models():
143
  "owned_by": "g4f"
144
  })
145
 
146
- added_models.add(model_name)
147
 
148
  except Exception as e:
149
 
150
  logger.error(f"Models error: {e}")
151
 
152
  # =================================================
153
- # IMPORTANT WORKING MODELS
154
  # =================================================
155
 
156
- important_models = [
157
 
158
- # Cohere family
159
  "command-r",
160
  "command-r-plus",
161
  "command-r7b",
162
  "command-a",
163
 
164
- # Kimi
165
- "kimi-k2",
166
-
167
  # DeepSeek
168
  "deepseek-chat",
169
  "deepseek-v3",
170
  "deepseek-r1",
171
 
172
- # Gemini
173
- "gemini-2.5-flash",
174
 
175
  # GPT
176
  "gpt-4",
177
  "gpt-4o",
 
 
 
178
  ]
179
 
180
- for model in important_models:
181
 
182
- if model not in added_models:
183
 
184
  models_data.append({
185
  "id": model,
@@ -188,7 +177,7 @@ async def get_models():
188
  "owned_by": "g4f"
189
  })
190
 
191
- added_models.add(model)
192
 
193
  return {
194
  "object": "list",
@@ -196,64 +185,6 @@ async def get_models():
196
  "total": len(models_data)
197
  }
198
 
199
- # =====================================================
200
- # SAFE COMPLETION
201
- # =====================================================
202
-
203
- async def safe_completion(
204
- model,
205
- messages,
206
- stream=False
207
- ):
208
-
209
- last_error = None
210
-
211
- for attempt in range(MAX_RETRIES):
212
-
213
- try:
214
-
215
- logger.info(
216
- f"Attempt {attempt + 1} | model={model}"
217
- )
218
-
219
- client = Client()
220
-
221
- response = await asyncio.wait_for(
222
- asyncio.to_thread(
223
- client.chat.completions.create,
224
- model=model,
225
- messages=messages,
226
- stream=stream
227
- ),
228
- timeout=REQUEST_TIMEOUT
229
- )
230
-
231
- logger.info(
232
- f"Success | model={model}"
233
- )
234
-
235
- return response
236
-
237
- except asyncio.TimeoutError:
238
-
239
- last_error = "Request timeout"
240
-
241
- logger.warning(
242
- f"Timeout | model={model}"
243
- )
244
-
245
- except Exception as e:
246
-
247
- last_error = e
248
-
249
- logger.warning(
250
- f"Attempt failed {attempt + 1} | {e}"
251
- )
252
-
253
- await asyncio.sleep(1)
254
-
255
- raise Exception(last_error)
256
-
257
  # =====================================================
258
  # CHAT COMPLETIONS
259
  # =====================================================
@@ -288,7 +219,16 @@ async def chat_completions(
288
 
289
  try:
290
 
291
- response = await safe_completion(
 
 
 
 
 
 
 
 
 
292
  model=body.model,
293
  messages=messages,
294
  stream=True
@@ -302,12 +242,12 @@ async def chat_completions(
302
 
303
  try:
304
 
305
- content = ""
306
-
307
  # تجاهل image models
308
  if hasattr(chunk, "images"):
309
  continue
310
 
 
 
311
  if (
312
  hasattr(chunk, "choices")
313
  and chunk.choices
@@ -352,16 +292,8 @@ async def chat_completions(
352
  # stream فارغ
353
  if not has_content:
354
 
355
- error_payload = {
356
- "error": {
357
- "message": "Empty stream",
358
- "type": "empty_stream"
359
- }
360
- }
361
-
362
- yield (
363
- f"data: "
364
- f"{json.dumps(error_payload)}\n\n"
365
  )
366
 
367
  final_payload = {
@@ -417,10 +349,12 @@ async def chat_completions(
417
 
418
  try:
419
 
420
- response = await safe_completion(
 
 
 
421
  model=body.model,
422
- messages=messages,
423
- stream=False
424
  )
425
 
426
  assistant_message = ""
 
3
  from fastapi.responses import StreamingResponse, JSONResponse
4
  from pydantic import BaseModel
5
  from typing import List, Optional
 
6
  import asyncio
7
  import json
8
  import time
 
25
 
26
  API_KEY = "sk-your-secret-key"
27
 
 
 
 
 
 
 
28
  # =====================================================
29
  # FASTAPI
30
  # =====================================================
31
 
32
  app = FastAPI(
33
  title="Universal AI Gateway",
34
+ version="FINAL-5.1.0"
35
  )
36
 
37
  # =====================================================
 
70
 
71
  auth = req.headers.get("Authorization")
72
 
73
+ # السماح بدون مفتاح للاختبار
74
  if not auth:
75
  return True
76
 
 
100
  return {
101
  "status": "online",
102
  "service": "Universal AI Gateway",
103
+ "version": "FINAL-5.1.0"
104
  }
105
 
106
  # =====================================================
 
111
  async def get_models():
112
 
113
  models_data = []
114
+ existing_models = set()
 
 
 
 
115
 
116
  try:
117
 
 
123
 
124
  model_name = str(model)
125
 
126
+ if model_name not in existing_models:
127
 
128
  models_data.append({
129
  "id": model_name,
 
132
  "owned_by": "g4f"
133
  })
134
 
135
+ existing_models.add(model_name)
136
 
137
  except Exception as e:
138
 
139
  logger.error(f"Models error: {e}")
140
 
141
  # =================================================
142
+ # IMPORTANT MODELS THAT ACTUALLY WORK
143
  # =================================================
144
 
145
+ extra_models = [
146
 
147
+ # Cohere
148
  "command-r",
149
  "command-r-plus",
150
  "command-r7b",
151
  "command-a",
152
 
 
 
 
153
  # DeepSeek
154
  "deepseek-chat",
155
  "deepseek-v3",
156
  "deepseek-r1",
157
 
158
+ # Kimi
159
+ "kimi-k2",
160
 
161
  # GPT
162
  "gpt-4",
163
  "gpt-4o",
164
+
165
+ # Gemini
166
+ "gemini-2.5-flash",
167
  ]
168
 
169
+ for model in extra_models:
170
 
171
+ if model not in existing_models:
172
 
173
  models_data.append({
174
  "id": model,
 
177
  "owned_by": "g4f"
178
  })
179
 
180
+ existing_models.add(model)
181
 
182
  return {
183
  "object": "list",
 
185
  "total": len(models_data)
186
  }
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  # =====================================================
189
  # CHAT COMPLETIONS
190
  # =====================================================
 
219
 
220
  try:
221
 
222
+ client = Client()
223
+
224
+ # IMPORTANT:
225
+ # لا timeout
226
+ # لا retry wrappers
227
+ # لا asyncio.to_thread
228
+ # لا watchdog
229
+ # نترك g4f يعمل بطبيعته
230
+
231
+ response = client.chat.completions.create(
232
  model=body.model,
233
  messages=messages,
234
  stream=True
 
242
 
243
  try:
244
 
 
 
245
  # تجاهل image models
246
  if hasattr(chunk, "images"):
247
  continue
248
 
249
+ content = ""
250
+
251
  if (
252
  hasattr(chunk, "choices")
253
  and chunk.choices
 
292
  # stream فارغ
293
  if not has_content:
294
 
295
+ logger.warning(
296
+ f"Empty stream | model={body.model}"
 
 
 
 
 
 
 
 
297
  )
298
 
299
  final_payload = {
 
349
 
350
  try:
351
 
352
+ client = Client()
353
+
354
+ response = await asyncio.to_thread(
355
+ client.chat.completions.create,
356
  model=body.model,
357
+ messages=messages
 
358
  )
359
 
360
  assistant_message = ""