MogensR commited on
Commit
6fd9fe7
·
1 Parent(s): b761b10
Files changed (2) hide show
  1. app.py +6 -16
  2. ui.py +9 -1
app.py CHANGED
@@ -223,17 +223,15 @@ def _post_launch_diag():
223
  logger.warning("Post-launch CUDA diag failed: %s", e)
224
 
225
  # ---------------------------------------------------------------------
226
- # UI factory (your existing UI builder)
227
  # ---------------------------------------------------------------------
228
  def build_ui() -> gr.Blocks:
229
- from ui import create_interface # your module
 
230
  return create_interface()
231
 
232
  # ---------------------------------------------------------------------
233
  # Optional: custom FastAPI mount mode
234
- # Why: if Gradio’s internal FastAPI route schema generation glitches
235
- # under certain dependency combos, we can run our own FastAPI app,
236
- # mount Gradio, and provide a known-good /config shim.
237
  # ---------------------------------------------------------------------
238
  def build_fastapi_with_gradio(demo: gr.Blocks):
239
  """
@@ -245,26 +243,22 @@ def build_fastapi_with_gradio(demo: gr.Blocks):
245
 
246
  app = FastAPI(title="VideoBackgroundReplacer2")
247
 
248
- # Simple health
249
  @app.get("/healthz")
250
  def _healthz():
251
  return {"ok": True, "ts": time.time()}
252
 
253
- # Config shim — this bypasses Gradio's internal /config route
254
- # and returns the same structure the frontend expects.
255
  @app.get("/config")
256
  def _config():
257
  try:
258
- cfg = demo.get_config_file() # Gradio builds the JSON-able config dict
259
  return JSONResponse(content=cfg)
260
  except Exception as e:
261
- # If something fails, return explicit JSON (so the frontend won't choke on HTML)
262
  return JSONResponse(
263
  status_code=500,
264
  content={"error": "config_generation_failed", "detail": str(e)},
265
  )
266
 
267
- # Mount Gradio UI at root; static assets & index served by Gradio
268
  app = gr.mount_gradio_app(app, demo, path="/")
269
  return app
270
 
@@ -280,13 +274,11 @@ def _config():
280
  _log_web_stack_versions_and_paths()
281
 
282
  demo = build_ui()
283
- # Good defaults for Spaces
284
  demo.queue(max_size=16, api_open=False)
285
 
286
  threading.Thread(target=_post_launch_diag, daemon=True).start()
287
 
288
  if mount_mode:
289
- # Our own FastAPI + /config shim
290
  try:
291
  from uvicorn import run as uvicorn_run
292
  except Exception:
@@ -294,15 +286,13 @@ def _config():
294
  raise
295
 
296
  app = build_fastapi_with_gradio(demo)
297
- # NOTE: In Docker Spaces, this process is PID1; we call uvicorn.run programmatically.
298
  uvicorn_run(app=app, host=host, port=port, log_level="info")
299
  else:
300
- # Standard Gradio server (uses internal FastAPI app & routes)
301
  demo.launch(
302
  server_name=host,
303
  server_port=port,
304
  share=False,
305
- show_api=False, # keep off in Spaces
306
  show_error=True,
307
  quiet=False,
308
  debug=True,
 
223
  logger.warning("Post-launch CUDA diag failed: %s", e)
224
 
225
  # ---------------------------------------------------------------------
226
+ # UI factory (uses your existing builder)
227
  # ---------------------------------------------------------------------
228
  def build_ui() -> gr.Blocks:
229
+ # FIX: import from ui_core_interface (not from ui)
230
+ from ui_core_interface import create_interface
231
  return create_interface()
232
 
233
  # ---------------------------------------------------------------------
234
  # Optional: custom FastAPI mount mode
 
 
 
235
  # ---------------------------------------------------------------------
236
  def build_fastapi_with_gradio(demo: gr.Blocks):
237
  """
 
243
 
244
  app = FastAPI(title="VideoBackgroundReplacer2")
245
 
 
246
  @app.get("/healthz")
247
  def _healthz():
248
  return {"ok": True, "ts": time.time()}
249
 
 
 
250
  @app.get("/config")
251
  def _config():
252
  try:
253
+ cfg = demo.get_config_file()
254
  return JSONResponse(content=cfg)
255
  except Exception as e:
 
256
  return JSONResponse(
257
  status_code=500,
258
  content={"error": "config_generation_failed", "detail": str(e)},
259
  )
260
 
261
+ # Mount Gradio UI at root; our /config route remains at parent level
262
  app = gr.mount_gradio_app(app, demo, path="/")
263
  return app
264
 
 
274
  _log_web_stack_versions_and_paths()
275
 
276
  demo = build_ui()
 
277
  demo.queue(max_size=16, api_open=False)
278
 
279
  threading.Thread(target=_post_launch_diag, daemon=True).start()
280
 
281
  if mount_mode:
 
282
  try:
283
  from uvicorn import run as uvicorn_run
284
  except Exception:
 
286
  raise
287
 
288
  app = build_fastapi_with_gradio(demo)
 
289
  uvicorn_run(app=app, host=host, port=port, log_level="info")
290
  else:
 
291
  demo.launch(
292
  server_name=host,
293
  server_port=port,
294
  share=False,
295
+ show_api=False,
296
  show_error=True,
297
  quiet=False,
298
  debug=True,
ui.py CHANGED
@@ -4,8 +4,16 @@
4
  Clean, focused main file that coordinates the application
5
  """
6
 
 
 
 
 
 
 
 
 
 
7
  # ==== Runtime hygiene & paths (very high in file) ====
8
- import os
9
  import sys
10
  import logging
11
  from pathlib import Path
 
4
  Clean, focused main file that coordinates the application
5
  """
6
 
7
+ # ============================================================
8
+ # Mount-mode handoff: delegate to app.py when enabled
9
+ # (So we can serve a safe /config JSON via our FastAPI shim)
10
+ # ============================================================
11
+ import os, runpy
12
+ if os.getenv("GRADIO_MOUNT_MODE") == "1":
13
+ runpy.run_module("app", run_name="__main__")
14
+ raise SystemExit
15
+
16
  # ==== Runtime hygiene & paths (very high in file) ====
 
17
  import sys
18
  import logging
19
  from pathlib import Path