Spaces:
Runtime error
Runtime error
Add explicit demo validation and error handling for Spaces
Browse files
app.py
CHANGED
|
@@ -880,14 +880,24 @@ def create_demo_for_spaces():
|
|
| 880 |
# Check if we're on Spaces
|
| 881 |
IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("SYSTEM") == "spaces"
|
| 882 |
|
| 883 |
-
# Create demo
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
else
|
| 889 |
-
|
| 890 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 891 |
|
| 892 |
# For local execution only (not on Spaces)
|
| 893 |
if __name__ == "__main__":
|
|
|
|
| 880 |
# Check if we're on Spaces
|
| 881 |
IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("SYSTEM") == "spaces"
|
| 882 |
|
| 883 |
+
# Create demo unconditionally at module level - Spaces needs this
|
| 884 |
+
# For local execution, main() will create its own demo
|
| 885 |
+
try:
|
| 886 |
+
if IS_SPACES:
|
| 887 |
+
logger.info("Initializing for Hugging Face Spaces...")
|
| 888 |
+
demo = create_demo_for_spaces() if IS_SPACES else None
|
| 889 |
+
if IS_SPACES and demo is not None:
|
| 890 |
+
logger.info(f"Demo created successfully: {type(demo)}")
|
| 891 |
+
# Verify demo is a valid Gradio object
|
| 892 |
+
if not isinstance(demo, (gr.Blocks, gr.Interface)):
|
| 893 |
+
logger.error(f"Demo is not a valid Gradio object: {type(demo)}")
|
| 894 |
+
raise TypeError(f"Expected gr.Blocks or gr.Interface, got {type(demo)}")
|
| 895 |
+
except Exception as e:
|
| 896 |
+
logger.error(f"Failed to create demo for Spaces: {e}", exc_info=True)
|
| 897 |
+
# Create a fallback error demo so Spaces doesn't show blank
|
| 898 |
+
with gr.Blocks() as error_demo:
|
| 899 |
+
gr.Markdown(f"# Error Initializing Chatbot\n\nAn error occurred while initializing the chatbot.\n\nError: {str(e)}\n\nPlease check the logs for details.")
|
| 900 |
+
demo = error_demo
|
| 901 |
|
| 902 |
# For local execution only (not on Spaces)
|
| 903 |
if __name__ == "__main__":
|