jatingocodeo commited on
Commit
bf2292c
·
verified ·
1 Parent(s): ae00973

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -9
app.py CHANGED
@@ -191,14 +191,53 @@ AutoModelForCausalLM.register(SmolLM2Config, SmolLM2ForCausalLM)
191
  # Load model and tokenizer
192
  model_id = "jatingocodeo/SmolLM2"
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  def load_model():
195
  try:
196
  print("\n=== Starting model loading process ===")
197
  print(f"Model ID: {model_id}")
198
 
 
 
 
 
 
199
  print("\n1. Loading tokenizer...")
200
  try:
201
- tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
 
 
202
  print("✓ Tokenizer loaded successfully")
203
  print(f"Tokenizer type: {type(tokenizer)}")
204
  print(f"Vocabulary size: {len(tokenizer)}")
@@ -235,25 +274,27 @@ def load_model():
235
 
236
  print("\n4. Loading model from Hub...")
237
  try:
 
 
 
 
 
 
 
 
238
  model = AutoModelForCausalLM.from_pretrained(
239
  model_id,
240
  config=config,
241
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
242
  trust_remote_code=True,
 
243
  low_cpu_mem_usage=True,
244
- local_files_only=False # Force download from Hub
245
  )
246
  print("✓ Model loaded successfully")
247
  print(f"Model type: {type(model)}")
248
  except Exception as e:
249
  print(f"× Error loading model: {str(e)}")
250
- print("Attempting to print model files in Hub repo...")
251
- from huggingface_hub import list_repo_files
252
- try:
253
- files = list_repo_files(model_id)
254
- print(f"Files in repo: {files}")
255
- except Exception as hub_e:
256
- print(f"Error listing repo files: {str(hub_e)}")
257
  raise
258
 
259
  print("\n5. Moving model to device...")
@@ -293,6 +334,8 @@ def load_model():
293
  print(f"CUDA available: {torch.cuda.is_available()}")
294
  if torch.cuda.is_available():
295
  print(f"CUDA version: {torch.version.cuda}")
 
 
296
  raise
297
 
298
  def generate_text(prompt, max_length=100, temperature=0.7, top_k=50):
 
191
  # Load model and tokenizer
192
  model_id = "jatingocodeo/SmolLM2"
193
 
194
+ def check_huggingface_access():
195
+ try:
196
+ from huggingface_hub import HfApi
197
+ api = HfApi()
198
+
199
+ # Check if token exists
200
+ try:
201
+ token = os.getenv("HF_TOKEN")
202
+ if not token:
203
+ print("× Warning: HF_TOKEN environment variable not found")
204
+ print(" Please ensure you have set your HuggingFace token")
205
+ return False
206
+ except Exception as e:
207
+ print(f"× Error checking HF_TOKEN: {str(e)}")
208
+ return False
209
+
210
+ # Check repository access
211
+ try:
212
+ print(f"Checking access to repository: {model_id}")
213
+ repo_info = api.repo_info(model_id, token=token)
214
+ print("✓ Repository access confirmed")
215
+ print(f"Repository info: {repo_info}")
216
+ return True
217
+ except Exception as e:
218
+ print(f"× Error accessing repository: {str(e)}")
219
+ return False
220
+ except Exception as e:
221
+ print(f"× Error in HuggingFace access check: {str(e)}")
222
+ return False
223
+
224
  def load_model():
225
  try:
226
  print("\n=== Starting model loading process ===")
227
  print(f"Model ID: {model_id}")
228
 
229
+ # Check HuggingFace access first
230
+ print("\nChecking HuggingFace access...")
231
+ if not check_huggingface_access():
232
+ raise Exception("Unable to access HuggingFace repository. Please check your token and repository permissions.")
233
+
234
  print("\n1. Loading tokenizer...")
235
  try:
236
+ tokenizer = AutoTokenizer.from_pretrained(
237
+ model_id,
238
+ use_auth_token=os.getenv("HF_TOKEN"),
239
+ trust_remote_code=True
240
+ )
241
  print("✓ Tokenizer loaded successfully")
242
  print(f"Tokenizer type: {type(tokenizer)}")
243
  print(f"Vocabulary size: {len(tokenizer)}")
 
274
 
275
  print("\n4. Loading model from Hub...")
276
  try:
277
+ # First try to list files in the repository
278
+ from huggingface_hub import list_repo_files
279
+ try:
280
+ files = list_repo_files(model_id, token=os.getenv("HF_TOKEN"))
281
+ print(f"Files in repository: {files}")
282
+ except Exception as hub_e:
283
+ print(f"Warning: Could not list repository files: {str(hub_e)}")
284
+
285
  model = AutoModelForCausalLM.from_pretrained(
286
  model_id,
287
  config=config,
288
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
289
  trust_remote_code=True,
290
+ use_auth_token=os.getenv("HF_TOKEN"),
291
  low_cpu_mem_usage=True,
292
+ local_files_only=False
293
  )
294
  print("✓ Model loaded successfully")
295
  print(f"Model type: {type(model)}")
296
  except Exception as e:
297
  print(f"× Error loading model: {str(e)}")
 
 
 
 
 
 
 
298
  raise
299
 
300
  print("\n5. Moving model to device...")
 
334
  print(f"CUDA available: {torch.cuda.is_available()}")
335
  if torch.cuda.is_available():
336
  print(f"CUDA version: {torch.version.cuda}")
337
+ print("\nEnvironment variables:")
338
+ print(f"HF_TOKEN set: {'HF_TOKEN' in os.environ}")
339
  raise
340
 
341
  def generate_text(prompt, max_length=100, temperature=0.7, top_k=50):