Fix runtime error: prevent multiple demo initializations on Spaces
Browse files- Add guard flag to prevent multiple calls to _create_demo()
- Add check to only create demo if it doesn't exist or is invalid
- Explicitly set demo in module namespace for Spaces compatibility
- Fixes issue where demo was being created twice causing runtime errors
app.py
CHANGED
|
@@ -819,11 +819,18 @@ IS_SPACES = (
|
|
| 819 |
os.getenv("HF_SPACE_ID") is not None
|
| 820 |
)
|
| 821 |
|
| 822 |
-
# Initialize demo variable
|
| 823 |
demo = None
|
|
|
|
| 824 |
|
| 825 |
def _create_demo():
|
| 826 |
"""Create the demo - separated into function for better error handling"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 827 |
try:
|
| 828 |
logger.info("=" * 80)
|
| 829 |
logger.info("Starting demo creation...")
|
|
@@ -862,9 +869,9 @@ def _create_demo():
|
|
| 862 |
logger.warning("Vector database is empty. The chatbot may not find relevant documents.")
|
| 863 |
|
| 864 |
logger.info("Creating interface...")
|
| 865 |
-
|
| 866 |
-
logger.info(f"Demo created successfully: {type(
|
| 867 |
-
return
|
| 868 |
|
| 869 |
except Exception as bot_error:
|
| 870 |
logger.error(f"Error initializing: {bot_error}", exc_info=True)
|
|
@@ -893,40 +900,44 @@ def _create_demo():
|
|
| 893 |
logger.info(f"Error demo created: {type(error_demo)}")
|
| 894 |
return error_demo
|
| 895 |
|
| 896 |
-
# Create demo at module level
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
if demo is None or not isinstance(demo, (gr.Blocks, gr.Interface)):
|
| 906 |
-
raise ValueError(f"Demo creation returned invalid result: {type(demo)}")
|
| 907 |
-
|
| 908 |
-
logger.info("Demo creation completed successfully")
|
| 909 |
-
except Exception as e:
|
| 910 |
-
logger.error(f"CRITICAL: Error creating demo: {e}", exc_info=True)
|
| 911 |
-
import traceback
|
| 912 |
-
error_trace = traceback.format_exc()
|
| 913 |
-
logger.error(f"Full traceback: {error_trace}")
|
| 914 |
-
with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
|
| 915 |
-
gr.Markdown(f"""
|
| 916 |
-
# Error Initializing Chatbot
|
| 917 |
-
|
| 918 |
-
A critical error occurred while initializing the chatbot.
|
| 919 |
|
| 920 |
-
|
| 921 |
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
{error_trace[:1500]}...
|
| 925 |
-
```
|
| 926 |
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 930 |
|
| 931 |
# Final verification
|
| 932 |
if demo is None:
|
|
@@ -945,12 +956,12 @@ else:
|
|
| 945 |
# Print confirmation for debugging
|
| 946 |
print(f"DEMO_READY: {type(demo)}")
|
| 947 |
print(f"DEMO_VALID: {isinstance(demo, (gr.Blocks, gr.Interface))}")
|
| 948 |
-
# Ensure demo is in
|
| 949 |
import sys
|
| 950 |
current_module = sys.modules[__name__]
|
| 951 |
-
|
| 952 |
-
|
| 953 |
-
|
| 954 |
|
| 955 |
# For local execution only (not on Spaces)
|
| 956 |
if __name__ == "__main__":
|
|
|
|
| 819 |
os.getenv("HF_SPACE_ID") is not None
|
| 820 |
)
|
| 821 |
|
| 822 |
+
# Initialize demo variable and creation flag
|
| 823 |
demo = None
|
| 824 |
+
_demo_created = False
|
| 825 |
|
| 826 |
def _create_demo():
|
| 827 |
"""Create the demo - separated into function for better error handling"""
|
| 828 |
+
global _demo_created, demo
|
| 829 |
+
if _demo_created and demo is not None and isinstance(demo, (gr.Blocks, gr.Interface)):
|
| 830 |
+
logger.warning("Demo already created, skipping...")
|
| 831 |
+
return demo
|
| 832 |
+
|
| 833 |
+
_demo_created = True
|
| 834 |
try:
|
| 835 |
logger.info("=" * 80)
|
| 836 |
logger.info("Starting demo creation...")
|
|
|
|
| 869 |
logger.warning("Vector database is empty. The chatbot may not find relevant documents.")
|
| 870 |
|
| 871 |
logger.info("Creating interface...")
|
| 872 |
+
created_demo = create_interface(bot)
|
| 873 |
+
logger.info(f"Demo created successfully: {type(created_demo)}")
|
| 874 |
+
return created_demo
|
| 875 |
|
| 876 |
except Exception as bot_error:
|
| 877 |
logger.error(f"Error initializing: {bot_error}", exc_info=True)
|
|
|
|
| 900 |
logger.info(f"Error demo created: {type(error_demo)}")
|
| 901 |
return error_demo
|
| 902 |
|
| 903 |
+
# Create demo at module level - only once
|
| 904 |
+
# Guard against multiple initializations (can happen on Spaces)
|
| 905 |
+
if demo is None or not isinstance(demo, (gr.Blocks, gr.Interface)):
|
| 906 |
+
try:
|
| 907 |
+
if IS_SPACES:
|
| 908 |
+
logger.info("Creating demo directly at module level for Spaces...")
|
| 909 |
+
else:
|
| 910 |
+
logger.info("Creating demo for local execution...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 911 |
|
| 912 |
+
demo = _create_demo()
|
| 913 |
|
| 914 |
+
if demo is None or not isinstance(demo, (gr.Blocks, gr.Interface)):
|
| 915 |
+
raise ValueError(f"Demo creation returned invalid result: {type(demo)}")
|
|
|
|
|
|
|
| 916 |
|
| 917 |
+
logger.info("Demo creation completed successfully")
|
| 918 |
+
except Exception as e:
|
| 919 |
+
logger.error(f"CRITICAL: Error creating demo: {e}", exc_info=True)
|
| 920 |
+
import traceback
|
| 921 |
+
error_trace = traceback.format_exc()
|
| 922 |
+
logger.error(f"Full traceback: {error_trace}")
|
| 923 |
+
with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
|
| 924 |
+
gr.Markdown(f"""
|
| 925 |
+
# Error Initializing Chatbot
|
| 926 |
+
|
| 927 |
+
A critical error occurred while initializing the chatbot.
|
| 928 |
+
|
| 929 |
+
**Error:** {str(e)}
|
| 930 |
+
|
| 931 |
+
**Traceback:**
|
| 932 |
+
```
|
| 933 |
+
{error_trace[:1500]}...
|
| 934 |
+
```
|
| 935 |
+
|
| 936 |
+
Please check the logs for more details.
|
| 937 |
+
""")
|
| 938 |
+
logger.info(f"Fallback error demo created: {type(demo)}")
|
| 939 |
+
else:
|
| 940 |
+
logger.info("Demo already exists, skipping creation")
|
| 941 |
|
| 942 |
# Final verification
|
| 943 |
if demo is None:
|
|
|
|
| 956 |
# Print confirmation for debugging
|
| 957 |
print(f"DEMO_READY: {type(demo)}")
|
| 958 |
print(f"DEMO_VALID: {isinstance(demo, (gr.Blocks, gr.Interface))}")
|
| 959 |
+
# Ensure demo is in module namespace for Spaces to find it
|
| 960 |
import sys
|
| 961 |
current_module = sys.modules[__name__]
|
| 962 |
+
current_module.demo = demo
|
| 963 |
+
current_module.__dict__['demo'] = demo
|
| 964 |
+
logger.info("Demo explicitly set in module namespace")
|
| 965 |
|
| 966 |
# For local execution only (not on Spaces)
|
| 967 |
if __name__ == "__main__":
|