Spaces:
Running
on
Zero
Running
on
Zero
improved token-acess-parsing
Browse files
app.py
CHANGED
|
@@ -313,6 +313,8 @@ def predict(request: gr.Request, text_input, sample_size_slider, reduce_sample_c
|
|
| 313 |
# Initialize variables used later across branches
|
| 314 |
urls = []
|
| 315 |
query_indices = []
|
|
|
|
|
|
|
| 316 |
|
| 317 |
# Helper function to generate error responses
|
| 318 |
def create_error_response(error_message):
|
|
@@ -327,17 +329,26 @@ def predict(request: gr.Request, text_input, sample_size_slider, reduce_sample_c
|
|
| 327 |
# Get the authentication token
|
| 328 |
if is_running_in_hf_space():
|
| 329 |
token = _get_token(request)
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
user_type = "anonymous"
|
| 337 |
-
elif '[pro]' in user:
|
| 338 |
-
user_type = "pro"
|
| 339 |
-
else:
|
| 340 |
-
user_type = "registered"
|
| 341 |
print(f"User type: {user_type}")
|
| 342 |
|
| 343 |
# Check if a file has been uploaded or if we need to use OpenAlex query
|
|
|
|
| 313 |
# Initialize variables used later across branches
|
| 314 |
urls = []
|
| 315 |
query_indices = []
|
| 316 |
+
# Default to anonymous unless we can positively identify the user via a valid token
|
| 317 |
+
user_type = "anonymous"
|
| 318 |
|
| 319 |
# Helper function to generate error responses
|
| 320 |
def create_error_response(error_message):
|
|
|
|
| 329 |
# Get the authentication token
|
| 330 |
if is_running_in_hf_space():
|
| 331 |
token = _get_token(request)
|
| 332 |
+
try:
|
| 333 |
+
if token and '.' in token:
|
| 334 |
+
payload_part = token.split('.')[1]
|
| 335 |
+
payload_part = f"{payload_part}{'=' * ((4 - len(payload_part) % 4) % 4)}"
|
| 336 |
+
payload = json.loads(base64.urlsafe_b64decode(payload_part).decode())
|
| 337 |
+
print(payload)
|
| 338 |
+
user = payload.get('user')
|
| 339 |
+
if user is None:
|
| 340 |
+
user_type = "anonymous"
|
| 341 |
+
elif '[pro]' in user:
|
| 342 |
+
user_type = "pro"
|
| 343 |
+
else:
|
| 344 |
+
user_type = "registered"
|
| 345 |
+
else:
|
| 346 |
+
# No token available or malformed; treat as anonymous
|
| 347 |
+
user_type = "anonymous"
|
| 348 |
+
except Exception as e:
|
| 349 |
+
# Any decoding/parsing error → anonymous fallback
|
| 350 |
+
print(f"Warning: failed to parse auth token: {e}")
|
| 351 |
user_type = "anonymous"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
print(f"User type: {user_type}")
|
| 353 |
|
| 354 |
# Check if a file has been uploaded or if we need to use OpenAlex query
|