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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -43
app.py CHANGED
@@ -4,9 +4,9 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
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,55 +25,59 @@ def load_model():
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
 
78
  # Cache the loaded model and tokenizer
79
  _model = model
 
4
  import os
5
 
6
  # Model configuration
7
+ # Since you have all model files in Space root, try loading directly
8
+ MODEL_NAME = "." # Load from current directory with all your uploaded files
9
+ CUSTOM_WEIGHTS_PATH = "./model.safetensors" # Backup: 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
+ # List available files for debugging
29
  import os
30
+ try:
31
+ current_files = os.listdir(".")
32
+ print("Available files in current directory:")
33
+ for f in current_files:
34
+ print(f" - {f}")
35
+ except Exception as e:
36
+ print(f"Could not list directory: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  try:
39
+ # First try to load directly from your uploaded files
40
+ print("Attempting to load model directly from uploaded files...")
41
+ try:
42
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
43
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
44
+ print("✅ Successfully loaded model directly from your uploaded files!")
45
+ except Exception as direct_load_error:
46
+ print(f"Direct load failed: {direct_load_error}")
47
+ print("Falling back to base model + custom weights...")
48
+
49
+ # Fallback: Load base model and add custom weights
50
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
51
+ model = AutoModelForCausalLM.from_pretrained("gpt2")
52
+
53
+ # Try to load your custom weights
54
+ if os.path.exists(CUSTOM_WEIGHTS_PATH):
55
+ print(f"Loading custom weights from: {CUSTOM_WEIGHTS_PATH}")
56
+ try:
57
+ from safetensors.torch import load_file
58
+ custom_weights = load_file(CUSTOM_WEIGHTS_PATH)
59
+
60
+ # Load the weights into the model
61
+ missing_keys, unexpected_keys = model.load_state_dict(custom_weights, strict=False)
62
+
63
+ if missing_keys:
64
+ print(f"⚠️ Missing keys: {len(missing_keys)} (this might be normal for LoRA models)")
65
+ if unexpected_keys:
66
+ print(f"⚠️ Unexpected keys: {len(unexpected_keys)}")
67
+
68
+ print("✅ Custom weights loaded successfully!")
69
+ except Exception as e:
70
+ print(f"⚠️ Could not load custom weights: {e}")
71
+ print("Using base GPT-2 model instead")
72
 
73
  # Set pad token if not set
74
  if tokenizer.pad_token is None:
75
  tokenizer.pad_token = tokenizer.eos_token
76
 
77
+ # Move model to device
78
+ model = model.to(DEVICE)
 
 
79
 
80
+ print(f"✅ Model loaded successfully on {DEVICE}!")
81
 
82
  # Cache the loaded model and tokenizer
83
  _model = model