arahrooh commited on
Commit
55b42f4
·
1 Parent(s): 9c35d2f

Add error handling back to create_demo_for_spaces

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -834,47 +834,57 @@ def main():
834
  # Spaces will import this module and look for a 'demo' variable
835
  def create_demo_for_spaces():
836
  """Create demo for Hugging Face Spaces - called at module level"""
837
- # Initialize with default args for Spaces
838
- parser = argparse.ArgumentParser()
839
- parser.add_argument('--model', type=str, default='meta-llama/Llama-3.2-3B-Instruct')
840
- parser.add_argument('--vector-db-dir', default='./chroma_db')
841
- parser.add_argument('--data-dir', default='./Data Resources')
842
- parser.add_argument('--max-new-tokens', type=int, default=1024)
843
- parser.add_argument('--temperature', type=float, default=0.2)
844
- parser.add_argument('--top-p', type=float, default=0.9)
845
- parser.add_argument('--repetition-penalty', type=float, default=1.1)
846
- parser.add_argument('--k', type=int, default=5)
847
- parser.add_argument('--skip-indexing', action='store_true', default=True)
848
- parser.add_argument('--verbose', action='store_true', default=False)
849
- parser.add_argument('--share', action='store_true', default=False)
850
- parser.add_argument('--server-name', type=str, default='0.0.0.0')
851
- parser.add_argument('--server-port', type=int, default=7860)
852
- parser.add_argument('--seed', type=int, default=42)
853
-
854
- args = parser.parse_args([]) # Empty args for Spaces
855
- args.skip_model_loading = True # Skip model loading, use Inference API
856
-
857
- # Create bot (will skip model loading)
858
- bot = RAGBot(args)
859
-
860
- if bot.vector_retriever is None:
861
- raise Exception("Vector database not available")
862
-
863
- # Create and return the demo interface
864
- return create_interface(bot, use_inference_api=True)
 
 
 
 
 
 
 
 
 
865
 
866
  # For Hugging Face Spaces: create demo at module level
867
  # Spaces will import this module and look for a 'demo' variable
868
  # Always create demo - Spaces will use it, local execution will override in main()
869
 
870
- # Check if we're on Spaces
871
- IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("SYSTEM") == "spaces"
872
-
873
  # For Hugging Face Spaces: create demo at module level
874
  # Spaces will import this module and look for a 'demo' variable
875
  # Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
876
  # DO NOT call demo.launch() - Spaces handles that automatically
877
 
 
 
 
 
878
  if IS_SPACES:
879
  logger.info("Initializing for Hugging Face Spaces...")
880
  demo = create_demo_for_spaces()
 
834
  # Spaces will import this module and look for a 'demo' variable
835
  def create_demo_for_spaces():
836
  """Create demo for Hugging Face Spaces - called at module level"""
837
+ try:
838
+ # Initialize with default args for Spaces
839
+ parser = argparse.ArgumentParser()
840
+ parser.add_argument('--model', type=str, default='meta-llama/Llama-3.2-3B-Instruct')
841
+ parser.add_argument('--vector-db-dir', default='./chroma_db')
842
+ parser.add_argument('--data-dir', default='./Data Resources')
843
+ parser.add_argument('--max-new-tokens', type=int, default=1024)
844
+ parser.add_argument('--temperature', type=float, default=0.2)
845
+ parser.add_argument('--top-p', type=float, default=0.9)
846
+ parser.add_argument('--repetition-penalty', type=float, default=1.1)
847
+ parser.add_argument('--k', type=int, default=5)
848
+ parser.add_argument('--skip-indexing', action='store_true', default=True)
849
+ parser.add_argument('--verbose', action='store_true', default=False)
850
+ parser.add_argument('--share', action='store_true', default=False)
851
+ parser.add_argument('--server-name', type=str, default='0.0.0.0')
852
+ parser.add_argument('--server-port', type=int, default=7860)
853
+ parser.add_argument('--seed', type=int, default=42)
854
+
855
+ args = parser.parse_args([]) # Empty args for Spaces
856
+ args.skip_model_loading = True # Skip model loading, use Inference API
857
+
858
+ # Create bot (will skip model loading)
859
+ bot = RAGBot(args)
860
+
861
+ if bot.vector_retriever is None:
862
+ raise Exception("Vector database not available")
863
+
864
+ # Create and return the demo interface
865
+ demo_obj = create_interface(bot, use_inference_api=True)
866
+ logger.info("Demo interface created successfully")
867
+ return demo_obj
868
+ except Exception as e:
869
+ logger.error(f"Error in create_demo_for_spaces: {e}", exc_info=True)
870
+ # Return a simple error demo so Spaces doesn't crash
871
+ with gr.Blocks() as error_demo:
872
+ gr.Markdown(f"# Error\n\n{str(e)}")
873
+ return error_demo
874
 
875
  # For Hugging Face Spaces: create demo at module level
876
  # Spaces will import this module and look for a 'demo' variable
877
  # Always create demo - Spaces will use it, local execution will override in main()
878
 
 
 
 
879
  # For Hugging Face Spaces: create demo at module level
880
  # Spaces will import this module and look for a 'demo' variable
881
  # Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
882
  # DO NOT call demo.launch() - Spaces handles that automatically
883
 
884
+ # Check if we're on Spaces
885
+ IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("SYSTEM") == "spaces"
886
+
887
+ # Create demo - Spaces will use it, local execution will override in main()
888
  if IS_SPACES:
889
  logger.info("Initializing for Hugging Face Spaces...")
890
  demo = create_demo_for_spaces()