Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ import logging
|
|
| 13 |
import g4f
|
| 14 |
from g4f.client import Client
|
| 15 |
|
| 16 |
-
|
| 17 |
# =====================================================
|
| 18 |
# LOGGING
|
| 19 |
# =====================================================
|
|
@@ -21,33 +20,27 @@ from g4f.client import Client
|
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
-
|
| 25 |
# =====================================================
|
| 26 |
# CONFIG
|
| 27 |
# =====================================================
|
| 28 |
|
| 29 |
API_KEY = "sk-your-secret-key"
|
| 30 |
|
| 31 |
-
# timeout عا
|
| 32 |
REQUEST_TIMEOUT = 45
|
| 33 |
|
| 34 |
-
# timeout للـ stream نفسه
|
| 35 |
-
STREAM_CHUNK_TIMEOUT = 20
|
| 36 |
-
|
| 37 |
# retry خفيف
|
| 38 |
MAX_RETRIES = 2
|
| 39 |
|
| 40 |
-
|
| 41 |
# =====================================================
|
| 42 |
# FASTAPI
|
| 43 |
# =====================================================
|
| 44 |
|
| 45 |
app = FastAPI(
|
| 46 |
title="Universal AI Gateway",
|
| 47 |
-
version="
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
# =====================================================
|
| 52 |
# CORS
|
| 53 |
# =====================================================
|
|
@@ -60,7 +53,6 @@ app.add_middleware(
|
|
| 60 |
allow_headers=["*"],
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
# =====================================================
|
| 65 |
# MODELS
|
| 66 |
# =====================================================
|
|
@@ -77,7 +69,6 @@ class ChatRequest(BaseModel):
|
|
| 77 |
temperature: Optional[float] = 0.7
|
| 78 |
max_tokens: Optional[int] = 4096
|
| 79 |
|
| 80 |
-
|
| 81 |
# =====================================================
|
| 82 |
# AUTH
|
| 83 |
# =====================================================
|
|
@@ -106,7 +97,6 @@ def verify_api_key(req: Request):
|
|
| 106 |
|
| 107 |
return True
|
| 108 |
|
| 109 |
-
|
| 110 |
# =====================================================
|
| 111 |
# ROOT
|
| 112 |
# =====================================================
|
|
@@ -117,10 +107,9 @@ async def root():
|
|
| 117 |
return {
|
| 118 |
"status": "online",
|
| 119 |
"service": "Universal AI Gateway",
|
| 120 |
-
"version": "
|
| 121 |
}
|
| 122 |
|
| 123 |
-
|
| 124 |
# =====================================================
|
| 125 |
# MODELS
|
| 126 |
# =====================================================
|
|
@@ -129,82 +118,19 @@ async def root():
|
|
| 129 |
async def get_models():
|
| 130 |
|
| 131 |
models_data = []
|
|
|
|
| 132 |
|
| 133 |
# =================================================
|
| 134 |
-
#
|
| 135 |
# =================================================
|
| 136 |
|
| 137 |
-
fallback_models = [
|
| 138 |
-
|
| 139 |
-
# GPT
|
| 140 |
-
"gpt-4o-mini",
|
| 141 |
-
"gpt-4o",
|
| 142 |
-
"gpt-4",
|
| 143 |
-
"gpt-3.5-turbo",
|
| 144 |
-
|
| 145 |
-
# Claude
|
| 146 |
-
"claude-3-haiku",
|
| 147 |
-
"claude-3-sonnet",
|
| 148 |
-
|
| 149 |
-
# Llama
|
| 150 |
-
"llama-3.1-70b",
|
| 151 |
-
"llama-3.1-8b",
|
| 152 |
-
|
| 153 |
-
# Qwen
|
| 154 |
-
"qwen-2-72b",
|
| 155 |
-
"qwen-2.5-72b",
|
| 156 |
-
|
| 157 |
-
# DeepSeek
|
| 158 |
-
"deepseek-chat",
|
| 159 |
-
"deepseek-v3",
|
| 160 |
-
"deepseek-r1-distill-qwen-14b",
|
| 161 |
-
|
| 162 |
-
# Kimi
|
| 163 |
-
"kimi-k2",
|
| 164 |
-
|
| 165 |
-
# Mixtral
|
| 166 |
-
"mixtral-8x7b",
|
| 167 |
-
|
| 168 |
-
# Aria
|
| 169 |
-
"aria",
|
| 170 |
-
|
| 171 |
-
# Sonar
|
| 172 |
-
"sonar-reasoning",
|
| 173 |
-
"sonar-reasoning-pro",
|
| 174 |
-
|
| 175 |
-
# Nemotron
|
| 176 |
-
"nemotron-70b",
|
| 177 |
-
|
| 178 |
-
# =================================================
|
| 179 |
-
# COHERE FAMILY
|
| 180 |
-
# =================================================
|
| 181 |
-
|
| 182 |
-
"command-r",
|
| 183 |
-
"command-r-plus",
|
| 184 |
-
"command-r7b",
|
| 185 |
-
"command",
|
| 186 |
-
"command-light",
|
| 187 |
-
"command-light-nightly",
|
| 188 |
-
"command-nightly",
|
| 189 |
-
|
| 190 |
-
# Cohere advanced
|
| 191 |
-
"cohere-command-r",
|
| 192 |
-
"cohere-command-r-plus",
|
| 193 |
-
|
| 194 |
-
# Additional aliases
|
| 195 |
-
"c4ai-command-r-plus",
|
| 196 |
-
"c4ai-command-r-v01",
|
| 197 |
-
]
|
| 198 |
-
|
| 199 |
-
added_models = set()
|
| 200 |
-
|
| 201 |
try:
|
| 202 |
|
| 203 |
if hasattr(g4f.models, "_all_models"):
|
| 204 |
|
| 205 |
all_models = list(g4f.models._all_models)
|
| 206 |
|
| 207 |
-
for model in all_models[:
|
| 208 |
|
| 209 |
model_name = str(model)
|
| 210 |
|
|
@@ -224,10 +150,34 @@ async def get_models():
|
|
| 224 |
logger.error(f"Models error: {e}")
|
| 225 |
|
| 226 |
# =================================================
|
| 227 |
-
#
|
| 228 |
# =================================================
|
| 229 |
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
if model not in added_models:
|
| 233 |
|
|
@@ -246,7 +196,6 @@ async def get_models():
|
|
| 246 |
"total": len(models_data)
|
| 247 |
}
|
| 248 |
|
| 249 |
-
|
| 250 |
# =====================================================
|
| 251 |
# SAFE COMPLETION
|
| 252 |
# =====================================================
|
|
@@ -305,16 +254,6 @@ async def safe_completion(
|
|
| 305 |
|
| 306 |
raise Exception(last_error)
|
| 307 |
|
| 308 |
-
|
| 309 |
-
# =====================================================
|
| 310 |
-
# STREAM WATCHDOG
|
| 311 |
-
# =====================================================
|
| 312 |
-
|
| 313 |
-
async def next_chunk(iterator):
|
| 314 |
-
|
| 315 |
-
return await asyncio.to_thread(next, iterator)
|
| 316 |
-
|
| 317 |
-
|
| 318 |
# =====================================================
|
| 319 |
# CHAT COMPLETIONS
|
| 320 |
# =====================================================
|
|
@@ -359,50 +298,14 @@ async def chat_completions(
|
|
| 359 |
|
| 360 |
has_content = False
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
try:
|
| 365 |
-
|
| 366 |
-
# watchdog للـ stream
|
| 367 |
-
chunk = await asyncio.wait_for(
|
| 368 |
-
next_chunk(response),
|
| 369 |
-
timeout=STREAM_CHUNK_TIMEOUT
|
| 370 |
-
)
|
| 371 |
-
|
| 372 |
-
except StopIteration:
|
| 373 |
-
break
|
| 374 |
-
|
| 375 |
-
except asyncio.TimeoutError:
|
| 376 |
-
|
| 377 |
-
logger.warning(
|
| 378 |
-
f"Stream stalled | model={body.model}"
|
| 379 |
-
)
|
| 380 |
-
|
| 381 |
-
error_payload = {
|
| 382 |
-
"error": {
|
| 383 |
-
"message": "Stream timeout",
|
| 384 |
-
"type": "stream_timeout"
|
| 385 |
-
}
|
| 386 |
-
}
|
| 387 |
-
|
| 388 |
-
yield (
|
| 389 |
-
f"data: "
|
| 390 |
-
f"{json.dumps(error_payload)}\n\n"
|
| 391 |
-
)
|
| 392 |
-
|
| 393 |
-
break
|
| 394 |
|
| 395 |
try:
|
| 396 |
|
| 397 |
content = ""
|
| 398 |
|
| 399 |
-
#
|
| 400 |
if hasattr(chunk, "images"):
|
| 401 |
-
|
| 402 |
-
logger.warning(
|
| 403 |
-
f"Image model detected | model={body.model}"
|
| 404 |
-
)
|
| 405 |
-
|
| 406 |
continue
|
| 407 |
|
| 408 |
if (
|
|
@@ -451,7 +354,7 @@ async def chat_completions(
|
|
| 451 |
|
| 452 |
error_payload = {
|
| 453 |
"error": {
|
| 454 |
-
"message": "
|
| 455 |
"type": "empty_stream"
|
| 456 |
}
|
| 457 |
}
|
|
@@ -563,7 +466,6 @@ async def chat_completions(
|
|
| 563 |
detail=str(e)
|
| 564 |
)
|
| 565 |
|
| 566 |
-
|
| 567 |
# =====================================================
|
| 568 |
# RUN
|
| 569 |
# =====================================================
|
|
|
|
| 13 |
import g4f
|
| 14 |
from g4f.client import Client
|
| 15 |
|
|
|
|
| 16 |
# =====================================================
|
| 17 |
# LOGGING
|
| 18 |
# =====================================================
|
|
|
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
|
|
|
|
| 23 |
# =====================================================
|
| 24 |
# CONFIG
|
| 25 |
# =====================================================
|
| 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 |
# =====================================================
|
| 45 |
# CORS
|
| 46 |
# =====================================================
|
|
|
|
| 53 |
allow_headers=["*"],
|
| 54 |
)
|
| 55 |
|
|
|
|
| 56 |
# =====================================================
|
| 57 |
# MODELS
|
| 58 |
# =====================================================
|
|
|
|
| 69 |
temperature: Optional[float] = 0.7
|
| 70 |
max_tokens: Optional[int] = 4096
|
| 71 |
|
|
|
|
| 72 |
# =====================================================
|
| 73 |
# AUTH
|
| 74 |
# =====================================================
|
|
|
|
| 97 |
|
| 98 |
return True
|
| 99 |
|
|
|
|
| 100 |
# =====================================================
|
| 101 |
# ROOT
|
| 102 |
# =====================================================
|
|
|
|
| 107 |
return {
|
| 108 |
"status": "online",
|
| 109 |
"service": "Universal AI Gateway",
|
| 110 |
+
"version": "5.0.0"
|
| 111 |
}
|
| 112 |
|
|
|
|
| 113 |
# =====================================================
|
| 114 |
# MODELS
|
| 115 |
# =====================================================
|
|
|
|
| 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 |
|
| 129 |
if hasattr(g4f.models, "_all_models"):
|
| 130 |
|
| 131 |
all_models = list(g4f.models._all_models)
|
| 132 |
|
| 133 |
+
for model in all_models[:200]:
|
| 134 |
|
| 135 |
model_name = str(model)
|
| 136 |
|
|
|
|
| 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 |
|
|
|
|
| 196 |
"total": len(models_data)
|
| 197 |
}
|
| 198 |
|
|
|
|
| 199 |
# =====================================================
|
| 200 |
# SAFE COMPLETION
|
| 201 |
# =====================================================
|
|
|
|
| 254 |
|
| 255 |
raise Exception(last_error)
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
# =====================================================
|
| 258 |
# CHAT COMPLETIONS
|
| 259 |
# =====================================================
|
|
|
|
| 298 |
|
| 299 |
has_content = False
|
| 300 |
|
| 301 |
+
for chunk in response:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
try:
|
| 304 |
|
| 305 |
content = ""
|
| 306 |
|
| 307 |
+
# تجاهل image models
|
| 308 |
if hasattr(chunk, "images"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
continue
|
| 310 |
|
| 311 |
if (
|
|
|
|
| 354 |
|
| 355 |
error_payload = {
|
| 356 |
"error": {
|
| 357 |
+
"message": "Empty stream",
|
| 358 |
"type": "empty_stream"
|
| 359 |
}
|
| 360 |
}
|
|
|
|
| 466 |
detail=str(e)
|
| 467 |
)
|
| 468 |
|
|
|
|
| 469 |
# =====================================================
|
| 470 |
# RUN
|
| 471 |
# =====================================================
|