hamxaameer commited on
Commit
f9855c7
·
verified ·
1 Parent(s): 46e654a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -14
app.py CHANGED
@@ -4,8 +4,9 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import os
5
 
6
  # Model configuration
7
- # Loading from current directory since model files are uploaded to Space root
8
- MODEL_NAME = "." # Current directory contains model.safetensors and tokenizer files
 
9
 
10
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
11
 
@@ -24,24 +25,53 @@ def load_model():
24
  print(f"Loading model from: {MODEL_NAME}")
25
  print(f"Using device: {DEVICE}")
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  try:
28
- # Load tokenizer
29
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Set pad token if not set
32
  if tokenizer.pad_token is None:
33
  tokenizer.pad_token = tokenizer.eos_token
34
 
35
- # Load model with appropriate settings
36
- model = AutoModelForCausalLM.from_pretrained(
37
- MODEL_NAME,
38
- torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
39
- device_map="auto" if DEVICE == "cuda" else None,
40
- trust_remote_code=True
41
- )
42
-
43
  if DEVICE == "cpu":
44
  model = model.to(DEVICE)
 
 
45
 
46
  print("✅ Model and tokenizer loaded successfully!")
47
 
@@ -54,9 +84,13 @@ def load_model():
54
  except Exception as e:
55
  print(f"❌ Error loading model: {e}")
56
  print("\n🔧 Troubleshooting:")
57
- print("1. If using HF repository: Make sure MODEL_NAME is 'username/model-name'")
58
- print("2. If using local files: Make sure model files are in the correct folder")
59
- print("3. For private repos: Add authentication token")
 
 
 
 
60
  raise e
61
 
62
  # Initialize model and tokenizer
 
4
  import os
5
 
6
  # Model configuration
7
+ # Alternative: Load base model and then load your safetensors weights
8
+ MODEL_NAME = "gpt2" # Use base GPT-2 model
9
+ CUSTOM_WEIGHTS_PATH = "./model.safetensors" # Your custom weights
10
 
11
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
12
 
 
25
  print(f"Loading model from: {MODEL_NAME}")
26
  print(f"Using device: {DEVICE}")
27
 
28
+ # Check if required files exist
29
+ import os
30
+ required_files = [
31
+ "model.safetensors",
32
+ "tokenizer_config.json"
33
+ ]
34
+
35
+ missing_files = []
36
+ for file in required_files:
37
+ file_path = os.path.join(MODEL_NAME if MODEL_NAME != "." else "", file)
38
+ if not os.path.exists(file_path):
39
+ missing_files.append(file)
40
+
41
+ if missing_files:
42
+ print(f"❌ Missing required files: {missing_files}")
43
+ print("Available files in directory:")
44
+ try:
45
+ files = os.listdir(MODEL_NAME if MODEL_NAME != "." else ".")
46
+ for f in files:
47
+ print(f" - {f}")
48
+ except:
49
+ print(" Could not list directory contents")
50
+ raise FileNotFoundError(f"Missing required files: {missing_files}")
51
+
52
  try:
53
+ # Load base model and tokenizer
54
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
55
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
56
+
57
+ # Try to load your custom weights if they exist
58
+ if os.path.exists(CUSTOM_WEIGHTS_PATH):
59
+ print(f"Loading custom weights from: {CUSTOM_WEIGHTS_PATH}")
60
+ from safetensors.torch import load_file
61
+ custom_weights = load_file(CUSTOM_WEIGHTS_PATH)
62
+ model.load_state_dict(custom_weights, strict=False)
63
+ print("✅ Custom weights loaded!")
64
+ else:
65
+ print(f"⚠️ Custom weights not found at {CUSTOM_WEIGHTS_PATH}, using base model")
66
 
67
  # Set pad token if not set
68
  if tokenizer.pad_token is None:
69
  tokenizer.pad_token = tokenizer.eos_token
70
 
 
 
 
 
 
 
 
 
71
  if DEVICE == "cpu":
72
  model = model.to(DEVICE)
73
+ elif DEVICE == "cuda":
74
+ model = model.to(DEVICE)
75
 
76
  print("✅ Model and tokenizer loaded successfully!")
77
 
 
84
  except Exception as e:
85
  print(f"❌ Error loading model: {e}")
86
  print("\n🔧 Troubleshooting:")
87
+ print("1. Make sure you have uploaded ALL required files:")
88
+ print(" - model.safetensors (✅ you have this)")
89
+ print(" - config.json (❓ might be missing)")
90
+ print(" - tokenizer.json or vocab.json + merges.txt (❓ might be missing)")
91
+ print(" - tokenizer_config.json (✅ you have this)")
92
+ print("2. Files should be in the Space root directory")
93
+ print("3. Check if the model was saved correctly from your notebook")
94
  raise e
95
 
96
  # Initialize model and tokenizer