arahrooh commited on
Commit
63dafc0
·
1 Parent(s): f3b3fd8

Add explicit demo validation and error handling for Spaces

Browse files
Files changed (1) hide show
  1. app.py +18 -8
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 - Spaces will use it, local execution will override in main()
884
- if IS_SPACES:
885
- logger.info("Initializing for Hugging Face Spaces...")
886
- demo = create_demo_for_spaces()
887
- logger.info(f"Demo created: {type(demo)}")
888
- else:
889
- # Local execution: demo will be created in main()
890
- demo = None
 
 
 
 
 
 
 
 
 
 
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__":