$P@D$3RV£R commited on
Commit
a7532e0
·
1 Parent(s): 4342d7e

Add HuggingFace token authentication for API requests

Browse files
Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -135,10 +135,27 @@ def get_hf_space_visits(space_id="0001AMA/auto_object_annotator_0.0.4"):
135
  """Try to get HuggingFace Space visit count from the Space page or metrics API"""
136
  import re
137
 
138
- # Method 1: Try the metrics API endpoint (may require auth, but worth trying)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  try:
140
  metrics_url = f"https://huggingface.co/api/spaces/{space_id}/metrics"
141
- response = requests.get(metrics_url, timeout=2) # Short timeout to avoid blocking
142
  if response.status_code == 200:
143
  data = response.json()
144
  # Look for visit count in the response
@@ -147,13 +164,18 @@ def get_hf_space_visits(space_id="0001AMA/auto_object_annotator_0.0.4"):
147
  for key in ['views', 'visits', 'total_views', 'total_visits', 'viewCount', 'visitCount']:
148
  if key in data:
149
  return int(data[key])
 
 
 
 
150
  except Exception as e:
 
151
  pass # Silently fail - will try scraping
152
 
153
  # Method 2: Scrape from the Space page HTML
154
  try:
155
  space_url = f"https://huggingface.co/spaces/{space_id}"
156
- response = requests.get(space_url, timeout=2, headers={'User-Agent': 'Mozilla/5.0'}) # Short timeout
157
  if response.status_code == 200:
158
  html = response.text
159
 
 
135
  """Try to get HuggingFace Space visit count from the Space page or metrics API"""
136
  import re
137
 
138
+ # Get HuggingFace token from environment (automatically provided in Spaces)
139
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")
140
+
141
+ # If no token in env, try to get it from huggingface_hub
142
+ if not hf_token:
143
+ try:
144
+ from huggingface_hub import HfApi
145
+ api = HfApi()
146
+ hf_token = api.token
147
+ except:
148
+ pass
149
+
150
+ # Prepare headers with authentication if token is available
151
+ headers = {'User-Agent': 'Mozilla/5.0'}
152
+ if hf_token:
153
+ headers['Authorization'] = f'Bearer {hf_token}'
154
+
155
+ # Method 1: Try the metrics API endpoint with authentication
156
  try:
157
  metrics_url = f"https://huggingface.co/api/spaces/{space_id}/metrics"
158
+ response = requests.get(metrics_url, timeout=2, headers=headers) # Short timeout to avoid blocking
159
  if response.status_code == 200:
160
  data = response.json()
161
  # Look for visit count in the response
 
164
  for key in ['views', 'visits', 'total_views', 'total_visits', 'viewCount', 'visitCount']:
165
  if key in data:
166
  return int(data[key])
167
+ elif response.status_code == 401:
168
+ print("HF API: Authentication required but token may be invalid")
169
+ elif response.status_code == 403:
170
+ print("HF API: Access forbidden - may need owner permissions")
171
  except Exception as e:
172
+ print(f"HF API request failed: {e}") # Debug logging
173
  pass # Silently fail - will try scraping
174
 
175
  # Method 2: Scrape from the Space page HTML
176
  try:
177
  space_url = f"https://huggingface.co/spaces/{space_id}"
178
+ response = requests.get(space_url, timeout=2, headers=headers) # Use same headers (with auth if available)
179
  if response.status_code == 200:
180
  html = response.text
181