Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from google import genai | |
| import os | |
| import time | |
| # Initialize Client | |
| client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"), http_options={'api_version': 'v1'}) | |
| # Priority list of models (Flash-Lite is the most 'generous' in 2026) | |
| MODEL_PRIORITY = ["gemini-2.0-flash", "gemini-2.5-flash-lite", "gemini-1.5-flash"] | |
| def bot(message, history): | |
| prompt_parts = [message["text"]] | |
| for file_path in message["files"]: | |
| with open(file_path, "rb") as f: | |
| prompt_parts.append(genai.types.Part.from_bytes(data=f.read(), mime_type=None)) | |
| # Loop through models if one is exhausted | |
| for model_id in MODEL_PRIORITY: | |
| try: | |
| response = client.models.generate_content_stream( | |
| model=model_id, | |
| contents=prompt_parts | |
| ) | |
| partial_message = "" | |
| for chunk in response: | |
| if chunk.text: | |
| partial_message += chunk.text | |
| yield partial_message | |
| return # Success! Exit the function. | |
| except Exception as e: | |
| if "429" in str(e): | |
| print(f"π {model_id} exhausted, trying next model...") | |
| continue | |
| else: | |
| raise gr.Error(f"API Error: {str(e)}") | |
| raise gr.Error("β All free models are currently exhausted. Please wait a few minutes.") | |
| demo = gr.ChatInterface(fn=bot, multimodal=True, title="Resilient Gemini Assistant") | |
| demo.launch() | |