arahrooh commited on
Commit
eed979f
·
1 Parent(s): e2ad839

Fix: Add assertions to ensure demo is always valid for Spaces

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -1014,19 +1014,25 @@ except Exception as e:
1014
  logger.info(f"Error demo created: {type(demo)}")
1015
 
1016
  # Final verification - ensure demo exists and is valid
 
1017
  if demo is None:
1018
- logger.error("CRITICAL: Demo variable is None!")
1019
- with gr.Blocks() as demo:
1020
  gr.Markdown("# Error: Demo was not created properly\n\nPlease check the logs for details.")
1021
  elif not isinstance(demo, (gr.Blocks, gr.Interface)):
1022
  logger.error(f"CRITICAL: Demo is not a valid Gradio object: {type(demo)}")
1023
- with gr.Blocks() as demo:
1024
  gr.Markdown(f"# Error: Invalid demo type\n\nDemo type: {type(demo)}\n\nPlease check the logs for details.")
1025
  else:
1026
  logger.info(f"✅ Final demo check passed: demo type={type(demo)}")
1027
  # Explicit print to ensure demo is accessible (Spaces might check this)
1028
  print(f"DEMO_VARIABLE_SET: {type(demo)}")
1029
 
 
 
 
 
 
1030
  # For local execution only (not on Spaces)
1031
  if __name__ == "__main__":
1032
  if not IS_SPACES:
 
1014
  logger.info(f"Error demo created: {type(demo)}")
1015
 
1016
  # Final verification - ensure demo exists and is valid
1017
+ # This is CRITICAL for Spaces - the demo variable MUST exist and be a valid Gradio object
1018
  if demo is None:
1019
+ logger.error("CRITICAL: Demo variable is None! Creating fallback demo.")
1020
+ with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
1021
  gr.Markdown("# Error: Demo was not created properly\n\nPlease check the logs for details.")
1022
  elif not isinstance(demo, (gr.Blocks, gr.Interface)):
1023
  logger.error(f"CRITICAL: Demo is not a valid Gradio object: {type(demo)}")
1024
+ with gr.Blocks(title="CGT-LLM-Beta RAG Chatbot") as demo:
1025
  gr.Markdown(f"# Error: Invalid demo type\n\nDemo type: {type(demo)}\n\nPlease check the logs for details.")
1026
  else:
1027
  logger.info(f"✅ Final demo check passed: demo type={type(demo)}")
1028
  # Explicit print to ensure demo is accessible (Spaces might check this)
1029
  print(f"DEMO_VARIABLE_SET: {type(demo)}")
1030
 
1031
+ # CRITICAL: Ensure demo is always set for Spaces
1032
+ # Spaces will look for a variable named 'demo' at module level
1033
+ assert demo is not None, "Demo must be set for Spaces"
1034
+ assert isinstance(demo, (gr.Blocks, gr.Interface)), f"Demo must be a Gradio object, got {type(demo)}"
1035
+
1036
  # For local execution only (not on Spaces)
1037
  if __name__ == "__main__":
1038
  if not IS_SPACES: