zaiffi commited on
Commit
ffbd65c
·
1 Parent(s): 7a745a0

Update app to load models from the model repo

Browse files
Files changed (2) hide show
  1. app.py +18 -14
  2. requirements.txt +1 -0
app.py CHANGED
@@ -8,6 +8,8 @@ import time
8
  import asyncio
9
  import sys
10
 
 
 
11
  os.environ["STREAMLIT_WATCHED_FILES"] = ""
12
  if sys.platform.startswith("win"):
13
  asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
@@ -373,31 +375,33 @@ class BiLSTMLanguageModel(nn.Module):
373
  # -------------------------------
374
  @st.cache_resource
375
  def load_models():
376
- # Load SentencePiece Model
377
- sp_model_path = "urdu_sp.model"
378
- sp = spm.SentencePieceProcessor()
379
-
380
- if not os.path.exists(sp_model_path):
381
- st.error("SentencePiece model not found! Make sure urdu_sp.model is present.")
 
 
 
 
382
  return None, None
383
 
384
- sp.load(sp_model_path)
385
-
386
  # Initialize & Load the Trained Model
387
  vocab_size = sp.get_piece_size()
388
  model = BiLSTMLanguageModel(vocab_size, embed_dim=512, hidden_dim=768, num_layers=3, dropout=0.2)
389
 
390
- weights_path = "model_weights.pth"
391
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
392
  model.to(device)
393
 
394
- if not os.path.exists(weights_path):
395
- st.error("Trained model weights not found! Make sure model_weights.pth is present.")
 
 
 
 
396
  return sp, None
397
 
398
- model.load_state_dict(torch.load(weights_path, map_location=device))
399
- model.eval()
400
-
401
  return sp, model
402
 
403
  def generate_poetry_nucleus(model, sp, start_word, num_words=12, temperature=1.2, top_p=0.85):
 
8
  import asyncio
9
  import sys
10
 
11
+ from huggingface_hub import hf_hub_download
12
+
13
  os.environ["STREAMLIT_WATCHED_FILES"] = ""
14
  if sys.platform.startswith("win"):
15
  asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
 
375
  # -------------------------------
376
  @st.cache_resource
377
  def load_models():
378
+ # Define your model repository
379
+ repo_id = "zaiffi/Mehfil-e-Sukhan" # Replace with your actual username/model-name
380
+
381
+ # Download SentencePiece Model
382
+ try:
383
+ sp_model_path = hf_hub_download(repo_id=repo_id, filename="urdu_sp.model")
384
+ sp = spm.SentencePieceProcessor()
385
+ sp.load(sp_model_path)
386
+ except Exception as e:
387
+ st.error(f"Error loading SentencePiece model: {e}")
388
  return None, None
389
 
 
 
390
  # Initialize & Load the Trained Model
391
  vocab_size = sp.get_piece_size()
392
  model = BiLSTMLanguageModel(vocab_size, embed_dim=512, hidden_dim=768, num_layers=3, dropout=0.2)
393
 
 
394
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
395
  model.to(device)
396
 
397
+ try:
398
+ weights_path = hf_hub_download(repo_id=repo_id, filename="model_weights.pth")
399
+ model.load_state_dict(torch.load(weights_path, map_location=device))
400
+ model.eval()
401
+ except Exception as e:
402
+ st.error(f"Error loading model weights: {e}")
403
  return sp, None
404
 
 
 
 
405
  return sp, model
406
 
407
  def generate_poetry_nucleus(model, sp, start_word, num_words=12, temperature=1.2, top_p=0.85):
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  torch==2.6.0
2
  sentencepiece==0.2.0
 
3
  streamlit==1.43.0
 
1
  torch==2.6.0
2
  sentencepiece==0.2.0
3
+ huggingface-hub==0.29.3
4
  streamlit==1.43.0