Improve request handling and error checking for OAuth token
Browse files
app.py
CHANGED
|
@@ -724,21 +724,21 @@ def create_interface(initial_bot: RAGBot) -> gr.Blocks:
|
|
| 724 |
token = None
|
| 725 |
|
| 726 |
# Try to get token from request (OAuth token from logged-in user)
|
| 727 |
-
|
| 728 |
-
|
| 729 |
# Check if request has client with hf_token attribute
|
| 730 |
-
if hasattr(request, 'client') and request.client:
|
| 731 |
if hasattr(request.client, 'hf_token') and request.client.hf_token:
|
| 732 |
token = request.client.hf_token
|
| 733 |
elif hasattr(request.client, 'token') and request.client.token:
|
| 734 |
token = request.client.token
|
| 735 |
# Also check request headers
|
| 736 |
-
if not token and hasattr(request, 'headers'):
|
| 737 |
auth_header = request.headers.get('authorization', '') or request.headers.get('Authorization', '')
|
| 738 |
-
if auth_header.startswith('Bearer '):
|
| 739 |
token = auth_header[7:]
|
| 740 |
-
|
| 741 |
-
|
| 742 |
|
| 743 |
# Fallback to environment variable if OAuth token not available
|
| 744 |
# This allows the app to work even without user login (for public models)
|
|
@@ -808,6 +808,7 @@ def create_interface(initial_bot: RAGBot) -> gr.Blocks:
|
|
| 808 |
return demo
|
| 809 |
|
| 810 |
logger.info("Gradio interface created successfully")
|
|
|
|
| 811 |
return demo
|
| 812 |
|
| 813 |
|
|
@@ -938,6 +939,12 @@ elif not isinstance(demo, (gr.Blocks, gr.Interface)):
|
|
| 938 |
gr.Markdown(f"# Error: Invalid demo type\n\nDemo type: {type(demo)}\n\nPlease check the logs for details.")
|
| 939 |
else:
|
| 940 |
logger.info(f"✅ Final demo check passed: demo type={type(demo)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 941 |
|
| 942 |
# For local execution only (not on Spaces)
|
| 943 |
if __name__ == "__main__":
|
|
|
|
| 724 |
token = None
|
| 725 |
|
| 726 |
# Try to get token from request (OAuth token from logged-in user)
|
| 727 |
+
try:
|
| 728 |
+
if request is not None:
|
| 729 |
# Check if request has client with hf_token attribute
|
| 730 |
+
if hasattr(request, 'client') and request.client is not None:
|
| 731 |
if hasattr(request.client, 'hf_token') and request.client.hf_token:
|
| 732 |
token = request.client.hf_token
|
| 733 |
elif hasattr(request.client, 'token') and request.client.token:
|
| 734 |
token = request.client.token
|
| 735 |
# Also check request headers
|
| 736 |
+
if not token and hasattr(request, 'headers') and request.headers:
|
| 737 |
auth_header = request.headers.get('authorization', '') or request.headers.get('Authorization', '')
|
| 738 |
+
if auth_header and auth_header.startswith('Bearer '):
|
| 739 |
token = auth_header[7:]
|
| 740 |
+
except Exception as e:
|
| 741 |
+
logger.debug(f"Could not get token from request: {e}")
|
| 742 |
|
| 743 |
# Fallback to environment variable if OAuth token not available
|
| 744 |
# This allows the app to work even without user login (for public models)
|
|
|
|
| 808 |
return demo
|
| 809 |
|
| 810 |
logger.info("Gradio interface created successfully")
|
| 811 |
+
logger.info(f"Demo type: {type(demo)}, Demo ID: {id(demo)}")
|
| 812 |
return demo
|
| 813 |
|
| 814 |
|
|
|
|
| 939 |
gr.Markdown(f"# Error: Invalid demo type\n\nDemo type: {type(demo)}\n\nPlease check the logs for details.")
|
| 940 |
else:
|
| 941 |
logger.info(f"✅ Final demo check passed: demo type={type(demo)}")
|
| 942 |
+
# Explicitly ensure demo is accessible for Spaces
|
| 943 |
+
if IS_SPACES:
|
| 944 |
+
logger.info(f"Spaces mode: Demo is ready and accessible")
|
| 945 |
+
# Print confirmation for debugging
|
| 946 |
+
print(f"DEMO_READY: {type(demo)}")
|
| 947 |
+
print(f"DEMO_VALID: {isinstance(demo, (gr.Blocks, gr.Interface))}")
|
| 948 |
|
| 949 |
# For local execution only (not on Spaces)
|
| 950 |
if __name__ == "__main__":
|