MoraxCheng commited on
Commit
6c6d326
·
1 Parent(s): 01b4983

Enhance model loading in transformers with improved error handling and fallback mechanisms; implement compatibility checks for trust_remote_code parameter

Browse files
Files changed (1) hide show
  1. app.py +62 -24
app.py CHANGED
@@ -290,15 +290,25 @@ def load_model_direct(model_type):
290
 
291
  try:
292
  # Load config from temp file
293
- config = AutoConfig.from_pretrained(config_path, trust_remote_code=True)
 
 
 
 
294
 
295
  # Load model with manual config
296
- model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
297
- f"PascalNotin/Tranception_{model_type}",
298
- config=config,
299
- trust_remote_code=True,
300
- ignore_mismatched_sizes=True
301
- )
 
 
 
 
 
 
302
 
303
  os.unlink(config_path) # Clean up temp file
304
  return model
@@ -337,13 +347,28 @@ def load_model_cached(model_type):
337
  os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
338
  os.environ["HF_HUB_DISABLE_EXPERIMENTAL_WARNING"] = "1"
339
 
340
- model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
341
- model_path,
342
- cache_dir=cache_dir,
343
- force_download=True,
344
- trust_remote_code=True,
345
- resume_download=False
346
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  MODEL_CACHE[model_type] = model
348
  print(f"{model_type} model loaded and cached")
349
  return model
@@ -360,10 +385,17 @@ def load_model_cached(model_type):
360
  try:
361
  # Use full URL to bypass any path resolution issues
362
  full_url = f"https://huggingface.co/PascalNotin/Tranception_{model_type}"
363
- model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
364
- full_url,
365
- cache_dir=cache_dir
366
- )
 
 
 
 
 
 
 
367
  MODEL_CACHE[model_type] = model
368
  print(f"{model_type} model loaded successfully with full URL")
369
  return model
@@ -400,12 +432,18 @@ def load_model_cached(model_type):
400
  print("Medium model failed, trying Small model...")
401
  # Try Small model as last resort
402
  try:
403
- model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
404
- "PascalNotin/Tranception_Small",
405
- trust_remote_code=True,
406
- force_download=True,
407
- cache_dir="/tmp/huggingface/small"
408
- )
 
 
 
 
 
 
409
  MODEL_CACHE["Small"] = model
410
  print("Small model loaded as fallback")
411
  return model
 
290
 
291
  try:
292
  # Load config from temp file
293
+ try:
294
+ config = AutoConfig.from_pretrained(config_path)
295
+ except Exception:
296
+ # Try without trust_remote_code
297
+ config = AutoConfig.from_pretrained(config_path)
298
 
299
  # Load model with manual config
300
+ try:
301
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
302
+ f"PascalNotin/Tranception_{model_type}",
303
+ config=config,
304
+ ignore_mismatched_sizes=True
305
+ )
306
+ except TypeError:
307
+ # Fallback without newer parameters
308
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
309
+ f"PascalNotin/Tranception_{model_type}",
310
+ config=config
311
+ )
312
 
313
  os.unlink(config_path) # Clean up temp file
314
  return model
 
347
  os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "1"
348
  os.environ["HF_HUB_DISABLE_EXPERIMENTAL_WARNING"] = "1"
349
 
350
+ # Try loading without trust_remote_code first (compatibility issue)
351
+ try:
352
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
353
+ model_path,
354
+ cache_dir=cache_dir,
355
+ force_download=True,
356
+ resume_download=False
357
+ )
358
+ except Exception as e1:
359
+ print(f"Loading without trust_remote_code failed: {e1}")
360
+ # Fallback: try with trust_remote_code for older transformers versions
361
+ try:
362
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
363
+ model_path,
364
+ cache_dir=cache_dir,
365
+ force_download=True,
366
+ trust_remote_code=True,
367
+ resume_download=False
368
+ )
369
+ except Exception as e2:
370
+ print(f"Loading with trust_remote_code also failed: {e2}")
371
+ raise e1 # Raise the original exception
372
  MODEL_CACHE[model_type] = model
373
  print(f"{model_type} model loaded and cached")
374
  return model
 
385
  try:
386
  # Use full URL to bypass any path resolution issues
387
  full_url = f"https://huggingface.co/PascalNotin/Tranception_{model_type}"
388
+ try:
389
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
390
+ full_url,
391
+ cache_dir=cache_dir
392
+ )
393
+ except TypeError:
394
+ # Try without trust_remote_code if it's not supported
395
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
396
+ full_url,
397
+ cache_dir=cache_dir
398
+ )
399
  MODEL_CACHE[model_type] = model
400
  print(f"{model_type} model loaded successfully with full URL")
401
  return model
 
432
  print("Medium model failed, trying Small model...")
433
  # Try Small model as last resort
434
  try:
435
+ try:
436
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
437
+ "PascalNotin/Tranception_Small",
438
+ force_download=True,
439
+ cache_dir="/tmp/huggingface/small"
440
+ )
441
+ except TypeError:
442
+ model = tranception.model_pytorch.TranceptionLMHeadModel.from_pretrained(
443
+ "PascalNotin/Tranception_Small",
444
+ force_download=True,
445
+ cache_dir="/tmp/huggingface/small"
446
+ )
447
  MODEL_CACHE["Small"] = model
448
  print("Small model loaded as fallback")
449
  return model