hmrizal commited on
Commit
516ac46
·
verified ·
1 Parent(s): af47d46

move get_fallback_model and fallback_model to the beginning

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -234,16 +234,25 @@ def initialize_model_once(model_key):
234
 
235
  return MODEL_CACHE["tokenizer"], MODEL_CACHE["model"], MODEL_CACHE.get("is_gguf", False)
236
 
 
 
 
 
 
 
 
 
 
237
  def create_llm_pipeline(model_key):
238
  """Create a new pipeline using the specified model with better error handling"""
239
  try:
240
  print(f"Creating pipeline for model: {model_key}")
 
241
  tokenizer, model, is_gguf = initialize_model_once(model_key)
242
 
243
  # Additional check to ensure model was properly loaded
244
  if model is None:
245
  print(f"Model is None for {model_key}, falling back to alternate model")
246
- fallback_model = get_fallback_model(model_key)
247
  if fallback_model != model_key:
248
  print(f"Attempting to use fallback model: {fallback_model}")
249
  tokenizer, model, is_gguf = initialize_model_once(fallback_model)
@@ -303,15 +312,6 @@ def create_llm_pipeline(model_key):
303
  print(traceback.format_exc())
304
  raise RuntimeError(f"Failed to create pipeline: {str(e)}")
305
 
306
- def get_fallback_model(current_model):
307
- """Get appropriate fallback model for problematic models"""
308
- fallback_map = {
309
- "Phi-4 Mini Instruct": "TinyLlama Chat",
310
- "DeepSeek Lite Chat": "DeepSeek Coder Instruct",
311
- "Flan T5 Small": "Llama 2 Chat"
312
- }
313
- return fallback_map.get(current_model, "TinyLlama Chat")
314
-
315
  # Modified handle_model_loading_error function
316
  def handle_model_loading_error(model_key, session_id):
317
  """Handle model loading errors by providing alternative model suggestions or fallbacks"""
 
234
 
235
  return MODEL_CACHE["tokenizer"], MODEL_CACHE["model"], MODEL_CACHE.get("is_gguf", False)
236
 
237
+ def get_fallback_model(current_model):
238
+ """Get appropriate fallback model for problematic models"""
239
+ fallback_map = {
240
+ "Phi-4 Mini Instruct": "TinyLlama Chat",
241
+ "DeepSeek Lite Chat": "DeepSeek Coder Instruct",
242
+ "Flan T5 Small": "Llama 2 Chat"
243
+ }
244
+ return fallback_map.get(current_model, "Llama 2 Chat")
245
+
246
  def create_llm_pipeline(model_key):
247
  """Create a new pipeline using the specified model with better error handling"""
248
  try:
249
  print(f"Creating pipeline for model: {model_key}")
250
+ fallback_model = get_fallback_model(model_key) # Define fallback_model at the beginning
251
  tokenizer, model, is_gguf = initialize_model_once(model_key)
252
 
253
  # Additional check to ensure model was properly loaded
254
  if model is None:
255
  print(f"Model is None for {model_key}, falling back to alternate model")
 
256
  if fallback_model != model_key:
257
  print(f"Attempting to use fallback model: {fallback_model}")
258
  tokenizer, model, is_gguf = initialize_model_once(fallback_model)
 
312
  print(traceback.format_exc())
313
  raise RuntimeError(f"Failed to create pipeline: {str(e)}")
314
 
 
 
 
 
 
 
 
 
 
315
  # Modified handle_model_loading_error function
316
  def handle_model_loading_error(model_key, session_id):
317
  """Handle model loading errors by providing alternative model suggestions or fallbacks"""