Spaces:
Sleeping
Sleeping
| import traceback | |
| from gradio_client import Client, exceptions | |
| def get_space_details(space_id: str): | |
| """ | |
| Connects to a Gradio Space and returns its structured API details. | |
| Returns a dict with 'success': True/False, and either 'data' or 'error'. | |
| """ | |
| try: | |
| hf_token = os.getenv("HF_TOKEN") | |
| client = Client(space_id, hf_token=hf_token, httpx_kwargs={"timeout": 1000}, verbose=True) | |
| if not hasattr(client, 'config') or not client.config: | |
| raise Exception("Could not retrieve client configuration. If this is a private or gated Space, please set your HF_TOKEN environment variable.") | |
| # ... rest of the existing code to parse endpoints ... | |
| # (keep the same, but return a dict with success=True and data) | |
| return { | |
| "success": True, | |
| "data": { | |
| "space_id": space_id, | |
| "api_url": client.api_url, | |
| "endpoints": endpoints_data | |
| } | |
| } | |
| except exceptions.AuthenticationError as e: | |
| # Specific to authentication issues | |
| error_msg = f"Authentication failed for space '{space_id}'. Please check your HF_TOKEN or if the space is public." | |
| log_backend_error(f"AuthenticationError: {error_msg}\n{traceback.format_exc()}") | |
| return { | |
| "success": False, | |
| "error": { | |
| "type": "authentication", | |
| "message": error_msg, | |
| "detail": str(e) | |
| } | |
| } | |
| except exceptions.BuildError as e: | |
| # The space might be building or failed to build | |
| error_msg = f"The space '{space_id}' is currently building or failed to build. Please try again later." | |
| log_backend_error(f"BuildError: {error_msg}\n{traceback.format_exc()}") | |
| return { | |
| "success": False, | |
| "error": { | |
| "type": "build", | |
| "message": error_msg, | |
| "detail": str(e) | |
| } | |
| } | |
| except exceptions.InternalServerError as e: | |
| # Gradio server returned 5xx | |
| error_msg = f"The space '{space_id}' returned a server error. It might be temporarily unavailable." | |
| log_backend_error(f"InternalServerError: {error_msg}\n{traceback.format_exc()}") | |
| return { | |
| "success": False, | |
| "error": { | |
| "type": "server", | |
| "message": error_msg, | |
| "detail": str(e) | |
| } | |
| } | |
| except Exception as e: | |
| # Catch-all for other errors | |
| error_msg = f"Unexpected error while loading space '{space_id}': {str(e)}" | |
| log_backend_error(f"UnexpectedError: {error_msg}\n{traceback.format_exc()}") | |
| return { | |
| "success": False, | |
| "error": { | |
| "type": "unknown", | |
| "message": error_msg, | |
| "detail": str(e), | |
| "traceback": traceback.format_exc() # only include in DEBUG mode | |
| } | |
| } | |