Bryceeee commited on
Commit
aa52203
Β·
verified Β·
1 Parent(s): 301b4c9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -260,11 +260,15 @@ def create_app():
260
 
261
  # Create the app for Hugging Face Spaces
262
  # Spaces will automatically detect Gradio and run this
263
- # Use global flag to prevent duplicate initialization
 
 
264
  if not _app_initialized:
265
  _app_initialized = True
266
  try:
267
- demo = create_app()
 
 
268
  print("βœ… App created successfully!")
269
  print(f"βœ… Demo type: {type(demo)}")
270
  except Exception as e:
@@ -292,21 +296,30 @@ if not _app_initialized:
292
  ```
293
  """
294
 
295
- demo = gr.Interface(
296
  fn=show_error,
297
  inputs=None,
298
  outputs=gr.Markdown(),
299
  title="CSRC Car Manual RAG System - Error",
300
  description="An error occurred. Please check the logs."
301
  )
 
302
  else:
303
- # If already initialized, skip to avoid duplicate initialization
304
- print("⚠️ App already initialized, skipping duplicate initialization")
305
- # demo should already be defined from first initialization
306
- if 'demo' not in globals():
 
 
307
  # Fallback: create a simple demo if somehow not defined
 
308
  import gradio as gr
309
- demo = gr.Interface(fn=lambda: "App is initializing...", inputs=None, outputs="text")
 
 
 
 
 
310
 
311
  # Note: Do NOT call demo.launch() here
312
  # Hugging Face Spaces will automatically detect and launch the demo
 
260
 
261
  # Create the app for Hugging Face Spaces
262
  # Spaces will automatically detect Gradio and run this
263
+ # Use global flag and module-level demo variable to prevent duplicate initialization
264
+ _demo_instance = None
265
+
266
  if not _app_initialized:
267
  _app_initialized = True
268
  try:
269
+ print("πŸ”„ Starting app initialization (first time)...")
270
+ _demo_instance = create_app()
271
+ demo = _demo_instance # Expose to module level for Spaces
272
  print("βœ… App created successfully!")
273
  print(f"βœ… Demo type: {type(demo)}")
274
  except Exception as e:
 
296
  ```
297
  """
298
 
299
+ _demo_instance = gr.Interface(
300
  fn=show_error,
301
  inputs=None,
302
  outputs=gr.Markdown(),
303
  title="CSRC Car Manual RAG System - Error",
304
  description="An error occurred. Please check the logs."
305
  )
306
+ demo = _demo_instance
307
  else:
308
+ # If already initialized, reuse the existing demo
309
+ print("⚠️ App already initialized, reusing existing demo instance")
310
+ if _demo_instance is not None:
311
+ demo = _demo_instance
312
+ print("βœ… Reusing previously created demo")
313
+ else:
314
  # Fallback: create a simple demo if somehow not defined
315
+ print("⚠️ No demo instance found, creating fallback...")
316
  import gradio as gr
317
+ demo = gr.Interface(
318
+ fn=lambda: "App is initializing, please wait...",
319
+ inputs=None,
320
+ outputs="text",
321
+ title="CSRC Car Manual RAG System"
322
+ )
323
 
324
  # Note: Do NOT call demo.launch() here
325
  # Hugging Face Spaces will automatically detect and launch the demo