Spaces:
Runtime error
Runtime error
Refactor demo creation to match HF Spaces documentation pattern - create directly at module level
Browse files
app.py
CHANGED
|
@@ -831,10 +831,19 @@ def main():
|
|
| 831 |
|
| 832 |
|
| 833 |
# For Hugging Face Spaces: create demo at module level
|
| 834 |
-
# Spaces
|
| 835 |
-
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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')
|
|
@@ -861,35 +870,14 @@ def create_demo_for_spaces():
|
|
| 861 |
if bot.vector_retriever is None:
|
| 862 |
raise Exception("Vector database not available")
|
| 863 |
|
| 864 |
-
# Create
|
| 865 |
-
|
| 866 |
-
logger.info("Demo
|
| 867 |
-
return demo_obj
|
| 868 |
except Exception as e:
|
| 869 |
-
logger.error(f"Error
|
| 870 |
-
#
|
| 871 |
-
with gr.Blocks() as
|
| 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 |
-
# Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
|
| 878 |
-
# DO NOT call demo.launch() - Spaces handles that automatically
|
| 879 |
-
|
| 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 |
-
if IS_SPACES:
|
| 886 |
-
logger.info("Initializing for Hugging Face Spaces...")
|
| 887 |
-
demo = create_demo_for_spaces()
|
| 888 |
-
logger.info(f"Demo created successfully: {type(demo)}")
|
| 889 |
-
# Verify demo is a valid Gradio object
|
| 890 |
-
if not isinstance(demo, (gr.Blocks, gr.Interface)):
|
| 891 |
-
logger.error(f"Demo is not a valid Gradio object: {type(demo)}")
|
| 892 |
-
raise TypeError(f"Expected gr.Blocks or gr.Interface, got {type(demo)}")
|
| 893 |
else:
|
| 894 |
# Local execution: demo will be created in main()
|
| 895 |
demo = None
|
|
|
|
| 831 |
|
| 832 |
|
| 833 |
# For Hugging Face Spaces: create demo at module level
|
| 834 |
+
# Following the HF Spaces pattern: create the Gradio app directly at module level
|
| 835 |
+
# Spaces will import this module and look for a Gradio Blocks/Interface object
|
| 836 |
+
# Pattern: demo = gr.Interface(...) or demo = gr.Blocks(...)
|
| 837 |
+
# DO NOT call demo.launch() - Spaces handles that automatically
|
| 838 |
+
|
| 839 |
+
# Check if we're on Spaces
|
| 840 |
+
IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("SYSTEM") == "spaces"
|
| 841 |
+
|
| 842 |
+
# Create demo at module level for Spaces (similar to HF docs example)
|
| 843 |
+
# For local execution, main() will create its own demo
|
| 844 |
+
if IS_SPACES:
|
| 845 |
try:
|
| 846 |
+
logger.info("Initializing for Hugging Face Spaces...")
|
| 847 |
# Initialize with default args for Spaces
|
| 848 |
parser = argparse.ArgumentParser()
|
| 849 |
parser.add_argument('--model', type=str, default='meta-llama/Llama-3.2-3B-Instruct')
|
|
|
|
| 870 |
if bot.vector_retriever is None:
|
| 871 |
raise Exception("Vector database not available")
|
| 872 |
|
| 873 |
+
# Create the demo interface directly at module level (like HF docs example)
|
| 874 |
+
demo = create_interface(bot, use_inference_api=True)
|
| 875 |
+
logger.info(f"Demo created successfully: {type(demo)}")
|
|
|
|
| 876 |
except Exception as e:
|
| 877 |
+
logger.error(f"Error creating demo for Spaces: {e}", exc_info=True)
|
| 878 |
+
# Create a fallback error demo so Spaces doesn't show blank
|
| 879 |
+
with gr.Blocks() as demo:
|
| 880 |
+
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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
else:
|
| 882 |
# Local execution: demo will be created in main()
|
| 883 |
demo = None
|