ashishkblink commited on
Commit
a8bac12
·
verified ·
1 Parent(s): a7d6c9f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +38 -4
app.py CHANGED
@@ -108,10 +108,23 @@ def load_model():
108
  try:
109
  # Download model files from Hugging Face
110
  print("Downloading model files from Hugging Face...")
 
 
 
 
 
 
 
 
 
 
 
 
111
  model_dir = snapshot_download(
112
  repo_id=MODEL_REPO_ID,
113
  cache_dir=None,
114
- local_files_only=False
 
115
  )
116
 
117
  # Find checkpoint and vocab files
@@ -169,11 +182,32 @@ def load_model():
169
  return "✅ Model loaded successfully!"
170
 
171
  except Exception as e:
172
- error_msg = f"❌ Error loading model: {str(e)}"
173
- print(error_msg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  import traceback
175
  traceback.print_exc()
176
- return error_msg
177
 
178
  return "✅ Model already loaded!"
179
 
 
108
  try:
109
  # Download model files from Hugging Face
110
  print("Downloading model files from Hugging Face...")
111
+ print(f"Repository: {MODEL_REPO_ID}")
112
+
113
+ # Try to download with authentication
114
+ try:
115
+ from huggingface_hub import login
116
+ # Check if token is available via environment variable
117
+ token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
118
+ if token:
119
+ login(token=token, add_to_git_credential=False)
120
+ except:
121
+ pass # Token might not be set, that's okay if repo is public
122
+
123
  model_dir = snapshot_download(
124
  repo_id=MODEL_REPO_ID,
125
  cache_dir=None,
126
+ local_files_only=False,
127
+ token=os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
128
  )
129
 
130
  # Find checkpoint and vocab files
 
182
  return "✅ Model loaded successfully!"
183
 
184
  except Exception as e:
185
+ error_msg = str(e)
186
+ error_details = f"❌ Error loading model: {error_msg}"
187
+ print(error_details)
188
+
189
+ # Check if it's an authentication error
190
+ if "401" in error_msg or "Repository Not Found" in error_msg or "Invalid username or password" in error_msg:
191
+ detailed_error = (
192
+ f"❌ Authentication Error: The model repository '{MODEL_REPO_ID}' is private or requires authentication.\n\n"
193
+ f"**Solutions:**\n"
194
+ f"1. **Make repository public** (Recommended for playground):\n"
195
+ f" - Go to: https://huggingface.co/{MODEL_REPO_ID}/settings\n"
196
+ f" - Change visibility to 'Public'\n\n"
197
+ f"2. **Add authentication token to Space** (if keeping private):\n"
198
+ f" - Go to Space Settings → Repository secrets\n"
199
+ f" - Add secret: HF_TOKEN with your Hugging Face token\n"
200
+ f" - Get token from: https://huggingface.co/settings/tokens\n\n"
201
+ f"3. **Upload model files directly to Space** (alternative):\n"
202
+ f" - Upload model checkpoint and vocab.txt to the Space repository\n"
203
+ f" - The app will use local files instead"
204
+ )
205
+ print(detailed_error)
206
+ return detailed_error
207
+
208
  import traceback
209
  traceback.print_exc()
210
+ return error_details
211
 
212
  return "✅ Model already loaded!"
213