dschandra commited on
Commit
b726d24
·
verified ·
1 Parent(s): e299ab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -351,23 +351,37 @@ app.mount("/static", StaticFiles(directory=tempfile.gettempdir()), name="static"
351
 
352
  # Mount Gradio's static assets to fix UI rendering issue
353
  gradio_base_path = gr.__path__[0]
354
- gradio_static_path = os.path.join(gradio_base_path, "templates", "frontend", "dist")
355
-
356
- # Fallback to alternative path if dist is not found
357
- if not os.path.exists(gradio_static_path):
358
- gradio_static_path = os.path.join(gradio_base_path, "templates", "frontend", "build")
359
- if not os.path.exists(gradio_static_path):
360
- logger.error(f"Gradio static assets directory not found at {gradio_static_path}. Attempted paths: templates/frontend/dist, templates/frontend/build.")
361
- logger.error("Please ensure Gradio's frontend assets are built by reinstalling Gradio or manually building the frontend with 'npm run build' in the Gradio frontend directory.")
362
- raise FileNotFoundError(f"Gradio static assets not found. Please rebuild Gradio's frontend assets.")
363
-
364
- app.mount("/_app", StaticFiles(directory=gradio_static_path), name="gradio_static")
365
- logger.info(f"Mounted Gradio static assets at {gradio_static_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
 
367
  # Health check endpoint to verify static asset availability
368
  @app.get("/health")
369
  async def health_check():
370
- if os.path.exists(gradio_static_path):
371
  return {"status": "healthy", "gradio_static": "available"}
372
  return {"status": "unhealthy", "gradio_static": "missing"}
373
 
 
351
 
352
  # Mount Gradio's static assets to fix UI rendering issue
353
  gradio_base_path = gr.__path__[0]
354
+ possible_static_paths = [
355
+ os.path.join(gradio_base_path, "templates", "frontend", "dist"),
356
+ os.path.join(gradio_base_path, "templates", "frontend", "build"),
357
+ os.path.join(gradio_base_path, "static"),
358
+ ]
359
+
360
+ gradio_static_path = None
361
+ for path in possible_static_paths:
362
+ if os.path.exists(path) and os.path.exists(os.path.join(path, "_app")):
363
+ gradio_static_path = path
364
+ break
365
+
366
+ if gradio_static_path:
367
+ app.mount("/_app", StaticFiles(directory=gradio_static_path), name="gradio_static")
368
+ logger.info(f"Mounted Gradio static assets at {gradio_static_path}")
369
+ else:
370
+ logger.warning(
371
+ "Gradio static assets not found in any expected paths: " +
372
+ ", ".join(possible_static_paths) +
373
+ ". Falling back to Gradio's built-in server for UI rendering."
374
+ )
375
+ # Run Gradio directly if assets cannot be found
376
+ if __name__ == "__main__":
377
+ logger.info("Starting Gradio's built-in server...")
378
+ demo.launch(server_name="0.0.0.0", server_port=7860)
379
+ exit(0)
380
 
381
  # Health check endpoint to verify static asset availability
382
  @app.get("/health")
383
  async def health_check():
384
+ if gradio_static_path and os.path.exists(gradio_static_path):
385
  return {"status": "healthy", "gradio_static": "available"}
386
  return {"status": "unhealthy", "gradio_static": "missing"}
387