Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,12 +61,17 @@ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
|
| 61 |
if GEMINI_API_KEY:
|
| 62 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 63 |
|
| 64 |
-
# Models tried in order.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
GEMINI_MODELS = [
|
|
|
|
|
|
|
| 66 |
"gemini-2.0-flash",
|
| 67 |
-
"gemini-2.0-flash-
|
| 68 |
-
"gemini-1.5-flash",
|
| 69 |
-
"gemini-1.5-flash-8b",
|
| 70 |
]
|
| 71 |
|
| 72 |
# Relaxed safety thresholds — medical/pathology images can be flagged otherwise.
|
|
@@ -466,22 +471,57 @@ def answer_question(image, question):
|
|
| 466 |
f"{type(e).__name__}: {str(e)[:200]}")
|
| 467 |
continue
|
| 468 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
err_list = "<br>• ".join(errors) if errors else "Unknown error"
|
| 470 |
return (
|
| 471 |
"<div class='result-card warning-card'>"
|
| 472 |
-
"<div class='result-
|
| 473 |
-
"<div
|
| 474 |
-
"
|
| 475 |
-
"<
|
| 476 |
-
"<
|
| 477 |
-
"
|
| 478 |
-
"<
|
| 479 |
-
"
|
| 480 |
-
"<li>مشكلة شبكة مؤقتة (جرّب بعد دقيقة)</li>"
|
| 481 |
-
"</ul></div>"
|
| 482 |
-
"<details style='text-align:left;direction:ltr;margin-top:12px;'>"
|
| 483 |
-
"<summary style='cursor:pointer;color:#7f8c8d;'>تفاصيل تقنية</summary>"
|
| 484 |
-
f"<pre style='font-size:0.8em;white-space:pre-wrap;'>• {err_list}</pre>"
|
| 485 |
"</details>"
|
| 486 |
"</div>"
|
| 487 |
)
|
|
|
|
| 61 |
if GEMINI_API_KEY:
|
| 62 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 63 |
|
| 64 |
+
# Models tried in order. Free-tier daily quotas (as of 2025/2026):
|
| 65 |
+
# gemini-2.5-flash-lite → 1000 RPD, 15 RPM (highest daily quota)
|
| 66 |
+
# gemini-2.5-flash → 250 RPD, 10 RPM (best quality)
|
| 67 |
+
# gemini-2.0-flash → 200 RPD, 15 RPM (stable fallback)
|
| 68 |
+
# gemini-2.0-flash-lite → 200 RPD, 30 RPM (fastest, lowest cost)
|
| 69 |
+
# Order: prioritize highest daily quota first to maximize free usage.
|
| 70 |
GEMINI_MODELS = [
|
| 71 |
+
"gemini-2.5-flash-lite",
|
| 72 |
+
"gemini-2.5-flash",
|
| 73 |
"gemini-2.0-flash",
|
| 74 |
+
"gemini-2.0-flash-lite",
|
|
|
|
|
|
|
| 75 |
]
|
| 76 |
|
| 77 |
# Relaxed safety thresholds — medical/pathology images can be flagged otherwise.
|
|
|
|
| 471 |
f"{type(e).__name__}: {str(e)[:200]}")
|
| 472 |
continue
|
| 473 |
|
| 474 |
+
# Detect specific failure type from collected errors
|
| 475 |
+
all_errors_text = " ".join(errors).lower()
|
| 476 |
+
is_rate_limited = ("429" in all_errors_text
|
| 477 |
+
or "resourceexhausted" in all_errors_text
|
| 478 |
+
or "quota" in all_errors_text)
|
| 479 |
+
is_not_found = "404" in all_errors_text or "not found" in all_errors_text
|
| 480 |
+
|
| 481 |
+
if is_rate_limited and not is_not_found:
|
| 482 |
+
# All models hit rate limit (probably per-minute limit, not daily)
|
| 483 |
+
title = "⏱️ تجاوز سرعة الطلبات (Rate Limit)"
|
| 484 |
+
body = (
|
| 485 |
+
"النموذج المجاني محدود بـ 10-30 طلب في الدقيقة. "
|
| 486 |
+
"<strong>انتظر ~60 ثانية ثم أعد المحاولة.</strong>"
|
| 487 |
+
"<br><br>"
|
| 488 |
+
"إذا كنت أرسلت أكثر من 1000 طلب اليوم، فقد تكون تجاوزت الحصة "
|
| 489 |
+
"اليومية المجانية، وستحتاج الانتظار حتى الغد أو ترقية الخطة "
|
| 490 |
+
"في <a href='https://aistudio.google.com/app/apikey' "
|
| 491 |
+
"target='_blank'>Google AI Studio</a>."
|
| 492 |
+
)
|
| 493 |
+
color = "#e67e22"
|
| 494 |
+
elif is_not_found:
|
| 495 |
+
title = "❓ النماذج غير متاحة"
|
| 496 |
+
body = (
|
| 497 |
+
"النماذج المُحدَّدة غير متاحة على API. هذا يحدث عند "
|
| 498 |
+
"تقديم نسخة جديدة من Gemini. تواصل مع مطوّر التطبيق "
|
| 499 |
+
"لتحديث قائمة النماذج."
|
| 500 |
+
)
|
| 501 |
+
color = "#c0392b"
|
| 502 |
+
else:
|
| 503 |
+
title = "❌ تعذّر الحصول على إجابة"
|
| 504 |
+
body = (
|
| 505 |
+
"الأسباب المحتملة:"
|
| 506 |
+
"<ul style='text-align:right;margin-top:10px;'>"
|
| 507 |
+
"<li>المفتاح <code>GEMINI_API_KEY</code> غير صالح أو منتهي</li>"
|
| 508 |
+
"<li>الصورة كبيرة جداً أو بصيغة غير مدعومة</li>"
|
| 509 |
+
"<li>مشكلة شبكة مؤقتة</li>"
|
| 510 |
+
"</ul>"
|
| 511 |
+
)
|
| 512 |
+
color = "#c0392b"
|
| 513 |
+
|
| 514 |
err_list = "<br>• ".join(errors) if errors else "Unknown error"
|
| 515 |
return (
|
| 516 |
"<div class='result-card warning-card'>"
|
| 517 |
+
f"<div class='result-title' style='color:{color};font-size:1.3em;'>"
|
| 518 |
+
f"{title}</div>"
|
| 519 |
+
f"<div class='result-sub'>{body}</div>"
|
| 520 |
+
"<details style='text-align:left;direction:ltr;margin-top:14px;'>"
|
| 521 |
+
"<summary style='cursor:pointer;color:#7f8c8d;'>"
|
| 522 |
+
"تفاصيل تقنية</summary>"
|
| 523 |
+
f"<pre style='font-size:0.8em;white-space:pre-wrap;line-height:1.4;'>"
|
| 524 |
+
f"• {err_list}</pre>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
"</details>"
|
| 526 |
"</div>"
|
| 527 |
)
|