SolshineMisfit commited on
Commit
1bfef43
·
verified ·
1 Parent(s): 113e66a

Added extra model routes for if overloaded

Browse files
Files changed (1) hide show
  1. app.py +55 -1
app.py CHANGED
@@ -4,11 +4,11 @@ import requests
4
  import pytz
5
  import yaml
6
  import os
7
- import uuid
8
  from datasets import Dataset
9
  from huggingface_hub import HfApi
10
  from openai import OpenAI
11
  from tools.final_answer import FinalAnswerTool
 
12
 
13
  from Gradio_UI import GradioUI
14
 
@@ -318,6 +318,60 @@ model = HfApiModel(
318
  custom_role_conversions=None
319
  )
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  # Import tool from Hub
322
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
323
 
 
4
  import pytz
5
  import yaml
6
  import os
 
7
  from datasets import Dataset
8
  from huggingface_hub import HfApi
9
  from openai import OpenAI
10
  from tools.final_answer import FinalAnswerTool
11
+ from huggingface_hub import InferenceClient
12
 
13
  from Gradio_UI import GradioUI
14
 
 
318
  custom_role_conversions=None
319
  )
320
 
321
+ # Add fallback logic that only activates if the primary model fails
322
+ def try_model_call_with_fallbacks(prompt):
323
+ """Try to use the primary model first, fall back to alternatives if it fails."""
324
+ # First attempt with primary model
325
+ try:
326
+ return model(prompt)
327
+ except Exception as primary_error:
328
+ print(f"Primary model call failed: {str(primary_error)}")
329
+ print("Trying fallback models...")
330
+
331
+ # List of fallback models
332
+ fallbacks = [
333
+ {
334
+ "provider": "sambanova",
335
+ "model_name": "Qwen/Qwen2.5-Coder-32B-Instruct",
336
+ "display_name": "Qwen 2.5 Coder 32B"
337
+ },
338
+ {
339
+ "provider": "hf-inference",
340
+ "model_name": "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
341
+ "display_name": "DeepSeek R1 Distill Qwen 32B"
342
+ }
343
+ ]
344
+
345
+ # Get API key
346
+ api_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACE_API_KEY")
347
+ if not api_key:
348
+ raise ValueError("No Hugging Face API key found in environment variables")
349
+
350
+ # Try each fallback model in sequence
351
+ for fallback in fallbacks:
352
+ try:
353
+ print(f"Trying fallback model: {fallback['display_name']}")
354
+ client = InferenceClient(provider=fallback["provider"], api_key=api_key)
355
+ messages = [{"role": "user", "content": prompt}]
356
+ completion = client.chat.completions.create(
357
+ model=fallback["model_name"],
358
+ messages=messages,
359
+ max_tokens=2096,
360
+ temperature=0.5
361
+ )
362
+ print(f"Successfully used fallback model: {fallback['display_name']}")
363
+ return completion.choices[0].message.content
364
+ except Exception as e:
365
+ print(f"Fallback model {fallback['display_name']} failed: {str(e)}")
366
+ continue
367
+
368
+ # If all fallbacks fail, re-raise the original error
369
+ raise primary_error
370
+
371
+ # Monkey patch the model's __call__ method to use our fallback logic
372
+ original_call = model.__call__
373
+ model.__call__ = try_model_call_with_fallbacks
374
+
375
  # Import tool from Hub
376
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
377