jatingocodeo commited on
Commit
50eab48
Β·
verified Β·
1 Parent(s): bf2292c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -43
app.py CHANGED
@@ -7,6 +7,8 @@ import math
7
  import os
8
  import sys
9
  import transformers
 
 
10
 
11
  class RMSNorm(nn.Module):
12
  def __init__(self, hidden_size, eps=1e-5):
@@ -226,16 +228,22 @@ def load_model():
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")
@@ -259,37 +267,14 @@ def load_model():
259
  print(f"Γ— Error adding special tokens: {str(e)}")
260
  raise
261
 
262
- print("\n3. Creating model configuration...")
263
- try:
264
- config = SmolLM2Config(
265
- pad_token_id=tokenizer.pad_token_id,
266
- bos_token_id=tokenizer.bos_token_id,
267
- eos_token_id=tokenizer.eos_token_id
268
- )
269
- print("βœ“ Configuration created successfully")
270
- print(f"Config: {config}")
271
- except Exception as e:
272
- print(f"Γ— Error creating configuration: {str(e)}")
273
- raise
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)}")
@@ -297,7 +282,7 @@ def load_model():
297
  print(f"Γ— Error loading model: {str(e)}")
298
  raise
299
 
300
- print("\n5. Moving model to device...")
301
  try:
302
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
303
  print(f"Selected device: {device}")
@@ -307,16 +292,6 @@ def load_model():
307
  print(f"Γ— Error moving model to device: {str(e)}")
308
  raise
309
 
310
- print("\n6. Resizing token embeddings...")
311
- try:
312
- old_size = model.get_input_embeddings().weight.shape[0]
313
- model.resize_token_embeddings(len(tokenizer))
314
- new_size = model.get_input_embeddings().weight.shape[0]
315
- print(f"βœ“ Token embeddings resized from {old_size} to {new_size}")
316
- except Exception as e:
317
- print(f"Γ— Error resizing token embeddings: {str(e)}")
318
- raise
319
-
320
  print("\n=== Model loading completed successfully! ===")
321
  return model, tokenizer
322
 
 
7
  import os
8
  import sys
9
  import transformers
10
+ from dotenv import load_dotenv
11
+ from huggingface_hub import login
12
 
13
  class RMSNorm(nn.Module):
14
  def __init__(self, hidden_size, eps=1e-5):
 
228
  print("\n=== Starting model loading process ===")
229
  print(f"Model ID: {model_id}")
230
 
231
+ # Load environment variables
232
+ load_dotenv()
233
+
234
+ # Get HF token
235
+ hf_token = os.getenv('HF_TOKEN')
236
+ if not hf_token:
237
+ raise ValueError("HF_TOKEN not found in environment variables")
238
+
239
+ # Login to Hugging Face
240
+ login(hf_token)
241
 
242
  print("\n1. Loading tokenizer...")
243
  try:
244
  tokenizer = AutoTokenizer.from_pretrained(
245
  model_id,
246
+ use_auth_token=hf_token,
247
  trust_remote_code=True
248
  )
249
  print("βœ“ Tokenizer loaded successfully")
 
267
  print(f"Γ— Error adding special tokens: {str(e)}")
268
  raise
269
 
270
+ print("\n3. Loading model...")
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  try:
 
 
 
 
 
 
 
 
272
  model = AutoModelForCausalLM.from_pretrained(
273
  model_id,
274
+ use_auth_token=hf_token,
 
275
  trust_remote_code=True,
276
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
277
+ low_cpu_mem_usage=True
 
278
  )
279
  print("βœ“ Model loaded successfully")
280
  print(f"Model type: {type(model)}")
 
282
  print(f"Γ— Error loading model: {str(e)}")
283
  raise
284
 
285
+ print("\n4. Moving model to device...")
286
  try:
287
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
288
  print(f"Selected device: {device}")
 
292
  print(f"Γ— Error moving model to device: {str(e)}")
293
  raise
294
 
 
 
 
 
 
 
 
 
 
 
295
  print("\n=== Model loading completed successfully! ===")
296
  return model, tokenizer
297