Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ import logging
|
|
| 12 |
|
| 13 |
import g4f
|
| 14 |
from g4f.client import Client
|
| 15 |
-
from g4f import Provider
|
| 16 |
|
| 17 |
|
| 18 |
# =====================================================
|
|
@@ -32,31 +31,6 @@ API_KEY = "sk-your-secret-key"
|
|
| 32 |
REQUEST_TIMEOUT = 60
|
| 33 |
|
| 34 |
|
| 35 |
-
# =====================================================
|
| 36 |
-
# PROVIDERS ONLY
|
| 37 |
-
# لا نحدد النماذج نهائياً
|
| 38 |
-
# فقط نحدد المزودين الجيدين
|
| 39 |
-
# =====================================================
|
| 40 |
-
|
| 41 |
-
WORKING_PROVIDERS = [
|
| 42 |
-
|
| 43 |
-
# مجاني وسريع
|
| 44 |
-
Provider.PollinationsAI,
|
| 45 |
-
|
| 46 |
-
# ممتاز لكوهير / كيمي / GPT
|
| 47 |
-
Provider.Jmuz,
|
| 48 |
-
|
| 49 |
-
# جيد لبعض نماذج GPT
|
| 50 |
-
Provider.Liaobots,
|
| 51 |
-
|
| 52 |
-
# جيد لبعض النماذج المفتوحة
|
| 53 |
-
Provider.HuggingSpace,
|
| 54 |
-
|
| 55 |
-
# DuckDuckGo AI
|
| 56 |
-
Provider.DDGS,
|
| 57 |
-
]
|
| 58 |
-
|
| 59 |
-
|
| 60 |
# =====================================================
|
| 61 |
# FASTAPI
|
| 62 |
# =====================================================
|
|
@@ -141,7 +115,6 @@ async def root():
|
|
| 141 |
|
| 142 |
# =====================================================
|
| 143 |
# MODELS
|
| 144 |
-
# استرجاع النماذج الحقيقية من g4f
|
| 145 |
# =====================================================
|
| 146 |
|
| 147 |
@app.get("/v1/models")
|
|
@@ -176,65 +149,25 @@ async def get_models():
|
|
| 176 |
|
| 177 |
# =====================================================
|
| 178 |
# SAFE COMPLETION
|
| 179 |
-
# ي
|
| 180 |
-
#
|
| 181 |
# =====================================================
|
| 182 |
|
| 183 |
async def safe_completion(model, messages, stream=False):
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
for provider_cls in WORKING_PROVIDERS:
|
| 188 |
-
|
| 189 |
-
provider_name = getattr(
|
| 190 |
-
provider_cls,
|
| 191 |
-
"__name__",
|
| 192 |
-
str(provider_cls)
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
try:
|
| 196 |
-
|
| 197 |
-
logger.info(
|
| 198 |
-
f"Trying provider={provider_name} model={model}"
|
| 199 |
-
)
|
| 200 |
-
|
| 201 |
-
client = Client(
|
| 202 |
-
provider=provider_cls
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
response = await asyncio.wait_for(
|
| 206 |
-
asyncio.to_thread(
|
| 207 |
-
client.chat.completions.create,
|
| 208 |
-
model=model,
|
| 209 |
-
messages=messages,
|
| 210 |
-
stream=stream
|
| 211 |
-
),
|
| 212 |
-
timeout=REQUEST_TIMEOUT
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
logger.info(
|
| 216 |
-
f"Success provider={provider_name} model={model}"
|
| 217 |
-
)
|
| 218 |
-
|
| 219 |
-
return response
|
| 220 |
-
|
| 221 |
-
except asyncio.TimeoutError:
|
| 222 |
-
|
| 223 |
-
last_error = (
|
| 224 |
-
f"Timeout provider={provider_name}"
|
| 225 |
-
)
|
| 226 |
-
|
| 227 |
-
logger.warning(last_error)
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
|
| 239 |
|
| 240 |
# =====================================================
|
|
@@ -269,6 +202,8 @@ async def chat_completions(
|
|
| 269 |
|
| 270 |
async def generate_stream():
|
| 271 |
|
|
|
|
|
|
|
| 272 |
try:
|
| 273 |
|
| 274 |
response = await safe_completion(
|
|
@@ -277,10 +212,6 @@ async def chat_completions(
|
|
| 277 |
stream=True
|
| 278 |
)
|
| 279 |
|
| 280 |
-
chunk_id = (
|
| 281 |
-
f"chatcmpl-{uuid.uuid4().hex}"
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
has_content = False
|
| 285 |
|
| 286 |
for chunk in response:
|
|
@@ -340,14 +271,11 @@ async def chat_completions(
|
|
| 340 |
f"Chunk error: {chunk_error}"
|
| 341 |
)
|
| 342 |
|
| 343 |
-
# stream فارغ
|
| 344 |
if not has_content:
|
| 345 |
|
| 346 |
error_payload = {
|
| 347 |
"error": {
|
| 348 |
-
"message":
|
| 349 |
-
"Provider returned empty stream"
|
| 350 |
-
),
|
| 351 |
"type": "empty_stream"
|
| 352 |
}
|
| 353 |
}
|
|
@@ -358,7 +286,6 @@ async def chat_completions(
|
|
| 358 |
+ "\n\n"
|
| 359 |
)
|
| 360 |
|
| 361 |
-
# stop
|
| 362 |
final_payload = {
|
| 363 |
"id": chunk_id,
|
| 364 |
"object": "chat.completion.chunk",
|
|
@@ -381,6 +308,27 @@ async def chat_completions(
|
|
| 381 |
|
| 382 |
yield "data: [DONE]\n\n"
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
except Exception as e:
|
| 385 |
|
| 386 |
logger.error(
|
|
@@ -470,6 +418,13 @@ async def chat_completions(
|
|
| 470 |
|
| 471 |
})
|
| 472 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
except Exception as e:
|
| 474 |
|
| 475 |
logger.error(f"Chat error: {e}")
|
|
|
|
| 12 |
|
| 13 |
import g4f
|
| 14 |
from g4f.client import Client
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
# =====================================================
|
|
|
|
| 31 |
REQUEST_TIMEOUT = 60
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# =====================================================
|
| 35 |
# FASTAPI
|
| 36 |
# =====================================================
|
|
|
|
| 115 |
|
| 116 |
# =====================================================
|
| 117 |
# MODELS
|
|
|
|
| 118 |
# =====================================================
|
| 119 |
|
| 120 |
@app.get("/v1/models")
|
|
|
|
| 149 |
|
| 150 |
# =====================================================
|
| 151 |
# SAFE COMPLETION
|
| 152 |
+
# لا تقييد providers
|
| 153 |
+
# نترك g4f يختار تلقائياً
|
| 154 |
# =====================================================
|
| 155 |
|
| 156 |
async def safe_completion(model, messages, stream=False):
|
| 157 |
|
| 158 |
+
client = Client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
response = await asyncio.wait_for(
|
| 161 |
+
asyncio.to_thread(
|
| 162 |
+
client.chat.completions.create,
|
| 163 |
+
model=model,
|
| 164 |
+
messages=messages,
|
| 165 |
+
stream=stream
|
| 166 |
+
),
|
| 167 |
+
timeout=REQUEST_TIMEOUT
|
| 168 |
+
)
|
| 169 |
|
| 170 |
+
return response
|
| 171 |
|
| 172 |
|
| 173 |
# =====================================================
|
|
|
|
| 202 |
|
| 203 |
async def generate_stream():
|
| 204 |
|
| 205 |
+
chunk_id = f"chatcmpl-{uuid.uuid4().hex}"
|
| 206 |
+
|
| 207 |
try:
|
| 208 |
|
| 209 |
response = await safe_completion(
|
|
|
|
| 212 |
stream=True
|
| 213 |
)
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
has_content = False
|
| 216 |
|
| 217 |
for chunk in response:
|
|
|
|
| 271 |
f"Chunk error: {chunk_error}"
|
| 272 |
)
|
| 273 |
|
|
|
|
| 274 |
if not has_content:
|
| 275 |
|
| 276 |
error_payload = {
|
| 277 |
"error": {
|
| 278 |
+
"message": "Provider returned empty stream",
|
|
|
|
|
|
|
| 279 |
"type": "empty_stream"
|
| 280 |
}
|
| 281 |
}
|
|
|
|
| 286 |
+ "\n\n"
|
| 287 |
)
|
| 288 |
|
|
|
|
| 289 |
final_payload = {
|
| 290 |
"id": chunk_id,
|
| 291 |
"object": "chat.completion.chunk",
|
|
|
|
| 308 |
|
| 309 |
yield "data: [DONE]\n\n"
|
| 310 |
|
| 311 |
+
except asyncio.TimeoutError:
|
| 312 |
+
|
| 313 |
+
logger.error(
|
| 314 |
+
f"Timeout model={body.model}"
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
error_payload = {
|
| 318 |
+
"error": {
|
| 319 |
+
"message": "Request timeout",
|
| 320 |
+
"type": "timeout"
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
yield (
|
| 325 |
+
"data: "
|
| 326 |
+
+ json.dumps(error_payload)
|
| 327 |
+
+ "\n\n"
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
yield "data: [DONE]\n\n"
|
| 331 |
+
|
| 332 |
except Exception as e:
|
| 333 |
|
| 334 |
logger.error(
|
|
|
|
| 418 |
|
| 419 |
})
|
| 420 |
|
| 421 |
+
except asyncio.TimeoutError:
|
| 422 |
+
|
| 423 |
+
raise HTTPException(
|
| 424 |
+
status_code=408,
|
| 425 |
+
detail="Request timeout"
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
except Exception as e:
|
| 429 |
|
| 430 |
logger.error(f"Chat error: {e}")
|