Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -585,6 +585,7 @@ def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, m
|
|
| 585 |
"""
|
| 586 |
Send prompt_text and optionally an uploaded file (or an image object/list) to the model using NEW SDK.
|
| 587 |
Automatically rotates through available API keys on RESOURCE_EXHAUSTED errors.
|
|
|
|
| 588 |
Returns textual response and prints progress.
|
| 589 |
"""
|
| 590 |
contents = [prompt_text]
|
|
@@ -617,6 +618,7 @@ def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, m
|
|
| 617 |
current_client = client_manager.get_current_client()
|
| 618 |
current_key_num = client_manager.current_key_index + 1
|
| 619 |
|
|
|
|
| 620 |
try:
|
| 621 |
print(f"π Using API key #{current_key_num} with model {model_name}")
|
| 622 |
response = current_client.models.generate_content(
|
|
@@ -632,35 +634,58 @@ def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, m
|
|
| 632 |
|
| 633 |
except Exception as e:
|
| 634 |
error_str = str(e)
|
| 635 |
-
print(f"β Generation failed with API key #{current_key_num}: {e}")
|
| 636 |
|
| 637 |
# Check if it's a RESOURCE_EXHAUSTED error
|
| 638 |
if "429" in error_str or "RESOURCE_EXHAUSTED" in error_str:
|
| 639 |
-
print(f"β οΈ Quota exhausted for API key #{current_key_num}")
|
| 640 |
|
| 641 |
-
# Try
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
else:
|
| 662 |
-
#
|
| 663 |
-
print(f"β‘ Trying fallback model
|
| 664 |
try:
|
| 665 |
response = current_client.models.generate_content(
|
| 666 |
model=fallback_model,
|
|
@@ -672,19 +697,24 @@ def gemini_generate_content(prompt_text, file_upload_obj=None, image_obj=None, m
|
|
| 672 |
return raw_text
|
| 673 |
except Exception as e2:
|
| 674 |
print(f"β Fallback also failed: {e2}")
|
| 675 |
-
|
| 676 |
-
|
|
|
|
| 677 |
client_manager.rotate_to_next_key()
|
| 678 |
attempt += 1
|
| 679 |
print(f"π Trying next API key (attempt {attempt + 1}/{max_attempts})...")
|
| 680 |
continue
|
| 681 |
else:
|
| 682 |
-
|
|
|
|
|
|
|
|
|
|
| 683 |
|
| 684 |
# If we exhausted all attempts
|
| 685 |
raise Exception(f"β All {max_attempts} API key(s) exhausted. Please check your quota or try again later.")
|
| 686 |
|
| 687 |
|
|
|
|
| 688 |
# ---------------- PARSERS ----------------
|
| 689 |
def extract_question_ids_from_qpms(text: str):
|
| 690 |
"""Extract question IDs from QP+MS transcript."""
|
|
|
|
| 585 |
"""
|
| 586 |
Send prompt_text and optionally an uploaded file (or an image object/list) to the model using NEW SDK.
|
| 587 |
Automatically rotates through available API keys on RESOURCE_EXHAUSTED errors.
|
| 588 |
+
Note: Files uploaded with one API key cannot be accessed by another API key.
|
| 589 |
Returns textual response and prints progress.
|
| 590 |
"""
|
| 591 |
contents = [prompt_text]
|
|
|
|
| 618 |
current_client = client_manager.get_current_client()
|
| 619 |
current_key_num = client_manager.current_key_index + 1
|
| 620 |
|
| 621 |
+
# Try primary model first
|
| 622 |
try:
|
| 623 |
print(f"π Using API key #{current_key_num} with model {model_name}")
|
| 624 |
response = current_client.models.generate_content(
|
|
|
|
| 634 |
|
| 635 |
except Exception as e:
|
| 636 |
error_str = str(e)
|
| 637 |
+
print(f"β Generation failed with API key #{current_key_num} and model {model_name}: {e}")
|
| 638 |
|
| 639 |
# Check if it's a RESOURCE_EXHAUSTED error
|
| 640 |
if "429" in error_str or "RESOURCE_EXHAUSTED" in error_str:
|
| 641 |
+
print(f"β οΈ Quota exhausted for API key #{current_key_num} with model {model_name}")
|
| 642 |
|
| 643 |
+
# Try fallback model with SAME API key before switching keys
|
| 644 |
+
print(f"β‘ Trying fallback model {fallback_model} with same API key #{current_key_num}")
|
| 645 |
+
try:
|
| 646 |
+
response = current_client.models.generate_content(
|
| 647 |
+
model=fallback_model,
|
| 648 |
+
contents=contents
|
| 649 |
+
)
|
| 650 |
+
raw_text = response.text
|
| 651 |
+
print(f"π₯ Received response (chars): {len(raw_text)}")
|
| 652 |
+
client_manager.reset_to_primary()
|
| 653 |
+
return raw_text
|
| 654 |
+
except Exception as e_fallback:
|
| 655 |
+
error_fallback_str = str(e_fallback)
|
| 656 |
+
print(f"β Fallback model {fallback_model} also failed: {e_fallback}")
|
| 657 |
+
|
| 658 |
+
# Check if fallback also exhausted
|
| 659 |
+
if "429" in error_fallback_str or "RESOURCE_EXHAUSTED" in error_fallback_str:
|
| 660 |
+
print(f"β οΈ Fallback model also exhausted for API key #{current_key_num}")
|
| 661 |
+
|
| 662 |
+
# Now try next API key if available
|
| 663 |
+
if attempt < max_attempts - 1:
|
| 664 |
+
# Check if we have file uploads - they won't work with different keys
|
| 665 |
+
if file_upload_obj:
|
| 666 |
+
print("β οΈ WARNING: File uploads cannot be shared across API keys!")
|
| 667 |
+
print(" You need to re-upload files with each new API key.")
|
| 668 |
+
print(" Stopping rotation to avoid PERMISSION_DENIED errors.")
|
| 669 |
+
raise Exception(f"All models exhausted for API key #{current_key_num}. Cannot rotate keys with file uploads.")
|
| 670 |
+
|
| 671 |
+
client_manager.rotate_to_next_key()
|
| 672 |
+
attempt += 1
|
| 673 |
+
print(f"π Trying next API key (attempt {attempt + 1}/{max_attempts})...")
|
| 674 |
+
continue
|
| 675 |
+
else:
|
| 676 |
+
raise Exception(f"All {max_attempts} API key(s) exhausted with both models.")
|
| 677 |
+
else:
|
| 678 |
+
# Fallback failed with different error
|
| 679 |
+
raise Exception(f"Fallback model failed: {e_fallback}")
|
| 680 |
+
|
| 681 |
+
elif "403" in error_str or "PERMISSION_DENIED" in error_str:
|
| 682 |
+
# This happens when trying to access a file uploaded with a different API key
|
| 683 |
+
print(f"β οΈ Permission denied - likely due to file uploaded with different API key")
|
| 684 |
+
raise Exception(f"File access denied. Files uploaded with one API key cannot be accessed by another. Error: {e}")
|
| 685 |
+
|
| 686 |
else:
|
| 687 |
+
# Other error - try fallback model with same key
|
| 688 |
+
print(f"β‘ Trying fallback model {fallback_model} with same API key #{current_key_num}")
|
| 689 |
try:
|
| 690 |
response = current_client.models.generate_content(
|
| 691 |
model=fallback_model,
|
|
|
|
| 697 |
return raw_text
|
| 698 |
except Exception as e2:
|
| 699 |
print(f"β Fallback also failed: {e2}")
|
| 700 |
+
|
| 701 |
+
# If we have more keys and no file uploads, try them
|
| 702 |
+
if attempt < max_attempts - 1 and not file_upload_obj:
|
| 703 |
client_manager.rotate_to_next_key()
|
| 704 |
attempt += 1
|
| 705 |
print(f"π Trying next API key (attempt {attempt + 1}/{max_attempts})...")
|
| 706 |
continue
|
| 707 |
else:
|
| 708 |
+
if file_upload_obj:
|
| 709 |
+
raise Exception(f"All models failed. Cannot rotate keys with file uploads. Last error: {e2}")
|
| 710 |
+
else:
|
| 711 |
+
raise Exception(f"All attempts failed. Last error: {e2}")
|
| 712 |
|
| 713 |
# If we exhausted all attempts
|
| 714 |
raise Exception(f"β All {max_attempts} API key(s) exhausted. Please check your quota or try again later.")
|
| 715 |
|
| 716 |
|
| 717 |
+
|
| 718 |
# ---------------- PARSERS ----------------
|
| 719 |
def extract_question_ids_from_qpms(text: str):
|
| 720 |
"""Extract question IDs from QP+MS transcript."""
|