serhany commited on
Commit
4d2f42d
Β·
verified Β·
1 Parent(s): fcf00e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -9
app.py CHANGED
@@ -35,15 +35,13 @@ def load_model_and_tokenizer(model_identifier: str, model_key: str, tokenizer_ke
35
  try:
36
  tokenizer = AutoTokenizer.from_pretrained(
37
  model_identifier,
38
- trust_remote_code=True,
39
- use_auth_token=False # Ensure we're not using auth for public models
40
  )
41
  model = AutoModelForCausalLM.from_pretrained(
42
  model_identifier,
43
  torch_dtype=torch.bfloat16,
44
  device_map="auto",
45
- trust_remote_code=True,
46
- use_auth_token=False # Ensure we're not using auth for public models
47
  )
48
  model.eval()
49
 
@@ -54,13 +52,35 @@ def load_model_and_tokenizer(model_identifier: str, model_key: str, tokenizer_ke
54
 
55
  _models_cache[model_key] = model
56
  _models_cache[tokenizer_key] = tokenizer
57
- print(f"Finished loading and cached {model_key} and {tokenizer_key}.")
58
  return model, tokenizer
59
  except Exception as e:
60
- print(f"ERROR loading {model_key} model ({model_identifier}): {e}")
61
- print(f"Error type: {type(e).__name__}")
62
- if "404" in str(e) or "not found" in str(e).lower():
63
- print(f"Model {model_identifier} not found. Please check the model ID.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  _models_cache[model_key] = "error"
65
  _models_cache[tokenizer_key] = "error"
66
  raise
@@ -189,6 +209,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="🎬 CineGuide Comparison") as dem
189
  Type your movie-related query below and see how fine-tuning improves movie recommendations!
190
 
191
  ⚠️ **Note:** Models are loaded on first use and may take 30-60 seconds initially.
 
192
  """
193
  )
194
 
 
35
  try:
36
  tokenizer = AutoTokenizer.from_pretrained(
37
  model_identifier,
38
+ trust_remote_code=True
 
39
  )
40
  model = AutoModelForCausalLM.from_pretrained(
41
  model_identifier,
42
  torch_dtype=torch.bfloat16,
43
  device_map="auto",
44
+ trust_remote_code=True
 
45
  )
46
  model.eval()
47
 
 
52
 
53
  _models_cache[model_key] = model
54
  _models_cache[tokenizer_key] = tokenizer
55
+ print(f"βœ… Successfully loaded {model_key} model!")
56
  return model, tokenizer
57
  except Exception as e:
58
+ print(f"❌ ERROR loading {model_key} model ({model_identifier}): {e}")
59
+
60
+ # FALLBACK: Use base model if fine-tuned model fails
61
+ if model_key == "finetuned" and model_identifier != BASE_MODEL_ID:
62
+ print(f"πŸ”„ FALLBACK: Loading base model instead for fine-tuned model...")
63
+ try:
64
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
65
+ model = AutoModelForCausalLM.from_pretrained(
66
+ BASE_MODEL_ID,
67
+ torch_dtype=torch.bfloat16,
68
+ device_map="auto",
69
+ trust_remote_code=True
70
+ )
71
+ model.eval()
72
+ if tokenizer.pad_token is None:
73
+ tokenizer.pad_token = tokenizer.eos_token
74
+ if hasattr(tokenizer, "pad_token_id") and tokenizer.pad_token_id is None and tokenizer.eos_token_id is not None:
75
+ tokenizer.pad_token_id = tokenizer.eos_token_id
76
+
77
+ _models_cache[model_key] = model
78
+ _models_cache[tokenizer_key] = tokenizer
79
+ print(f"βœ… FALLBACK successful! Using base model with CineGuide prompt.")
80
+ return model, tokenizer
81
+ except Exception as fallback_e:
82
+ print(f"❌ FALLBACK also failed: {fallback_e}")
83
+
84
  _models_cache[model_key] = "error"
85
  _models_cache[tokenizer_key] = "error"
86
  raise
 
209
  Type your movie-related query below and see how fine-tuning improves movie recommendations!
210
 
211
  ⚠️ **Note:** Models are loaded on first use and may take 30-60 seconds initially.
212
+ πŸ’‘ **Fallback:** If fine-tuned model fails, will use base model with specialized prompting.
213
  """
214
  )
215