Bryceeee commited on
Commit
f54ef0b
Β·
verified Β·
1 Parent(s): e0b680e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -65
app.py CHANGED
@@ -13,10 +13,6 @@ IS_SPACES = os.getenv("SPACE_ID") is not None or os.getenv("HF_SPACE") is not No
13
  # Add the current directory to Python path for Spaces environment
14
  sys.path.insert(0, str(Path(__file__).parent))
15
 
16
- # Global flag to prevent duplicate initialization
17
- _app_initialized = False
18
- _demo_created = False
19
-
20
  from openai import OpenAI
21
  from src.config import Config
22
  from src.vector_store import VectorStoreManager
@@ -241,68 +237,46 @@ def create_app():
241
 
242
  # Create the app for Hugging Face Spaces
243
  # Spaces will automatically detect Gradio and run this
244
- # Use module-level variable and flag to prevent duplicate initialization
245
-
246
- # Initialize demo variable to None
247
- demo = None
248
 
249
- # Check if demo was already created (prevent duplicate initialization)
250
- if _demo_created and 'demo' in globals() and demo is not None:
251
- print("⚠️ Demo already created, skipping duplicate initialization")
252
- print(f"βœ… Reusing existing demo: {type(demo)}")
253
- else:
254
- # First time initialization
255
- if not _app_initialized:
256
- _app_initialized = True
257
- try:
258
- print("πŸ”„ Starting app initialization (first time)...")
259
- demo = create_app()
260
- _demo_created = True
261
- print("βœ… App created successfully!")
262
- print(f"βœ… Demo type: {type(demo)}")
263
- except Exception as e:
264
- print(f"❌ Fatal error creating app: {e}")
265
- import traceback
266
- traceback.print_exc()
267
-
268
- # Create a minimal error interface
269
- import gradio as gr
270
-
271
- def show_error():
272
- return f"""
273
- # ❌ Application Error
274
-
275
- **Fatal Error:** {str(e)}
276
-
277
- Please check:
278
- 1. OPENAI_API_KEY is set in Spaces Secrets
279
- 2. All required files are uploaded
280
- 3. Check the logs for detailed error information
281
-
282
- **Traceback:**
283
- ```
284
- {traceback.format_exc()}
285
- ```
286
- """
287
-
288
- demo = gr.Interface(
289
- fn=show_error,
290
- inputs=None,
291
- outputs=gr.Markdown(),
292
- title="CSRC Car Manual RAG System - Error",
293
- description="An error occurred. Please check the logs."
294
- )
295
- _demo_created = True
296
- else:
297
- # App was initialized but demo not created (shouldn't happen)
298
- print("⚠️ App initialized but demo not created, creating fallback...")
299
- import gradio as gr
300
- demo = gr.Interface(
301
- fn=lambda: "App is initializing, please wait...",
302
- inputs=None,
303
- outputs="text",
304
- title="CSRC Car Manual RAG System"
305
- )
306
 
307
  # Note: Do NOT call demo.launch() here
308
  # Hugging Face Spaces will automatically detect and launch the demo
 
13
  # Add the current directory to Python path for Spaces environment
14
  sys.path.insert(0, str(Path(__file__).parent))
15
 
 
 
 
 
16
  from openai import OpenAI
17
  from src.config import Config
18
  from src.vector_store import VectorStoreManager
 
237
 
238
  # Create the app for Hugging Face Spaces
239
  # Spaces will automatically detect Gradio and run this
240
+ # The demo variable must be exposed at module level for Spaces to detect it
 
 
 
241
 
242
+ try:
243
+ print("πŸ”„ Starting app initialization...")
244
+ demo = create_app()
245
+ print("βœ… App created successfully!")
246
+ print(f"βœ… Demo type: {type(demo)}")
247
+ print(f"βœ… Demo object: {demo}")
248
+ except Exception as e:
249
+ print(f"❌ Fatal error creating app: {e}")
250
+ import traceback
251
+ traceback.print_exc()
252
+
253
+ # Create a minimal error interface
254
+ import gradio as gr
255
+
256
+ def show_error():
257
+ return f"""
258
+ # ❌ Application Error
259
+
260
+ **Fatal Error:** {str(e)}
261
+
262
+ Please check:
263
+ 1. OPENAI_API_KEY is set in Spaces Secrets
264
+ 2. All required files are uploaded
265
+ 3. Check the logs for detailed error information
266
+
267
+ **Traceback:**
268
+ ```
269
+ {traceback.format_exc()}
270
+ ```
271
+ """
272
+
273
+ demo = gr.Interface(
274
+ fn=show_error,
275
+ inputs=None,
276
+ outputs=gr.Markdown(),
277
+ title="CSRC Car Manual RAG System - Error",
278
+ description="An error occurred. Please check the logs."
279
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
 
281
  # Note: Do NOT call demo.launch() here
282
  # Hugging Face Spaces will automatically detect and launch the demo