Edisoneh commited on
Commit
f264142
·
verified ·
1 Parent(s): 04be0ac

Initialize ZeroGPU for the session-protected mounted Gradio app

Browse files
Files changed (1) hide show
  1. app.py +13 -2
app.py CHANGED
@@ -278,6 +278,7 @@ def create_app(model, device='cuda:0', output_root=None, examples_manifest=None,
278
  return response
279
 
280
  access.middleware(app)
 
281
  return gr.mount_gradio_app(app, demo, path='/', allowed_paths=example_files, show_error=False,
282
  theme=theme, css=(Path(__file__).parent/'style.css').read_text(), footer_links=[])
283
 
@@ -296,5 +297,15 @@ if __name__ == '__main__':
296
  from zero_gpu import initialize, reconstruct_on_gpu
297
  initialize(args.model)
298
  inference_fn = reconstruct_on_gpu
299
- uvicorn.run(create_app(args.model, args.device, args.output_root, args.examples,
300
- inference_fn=inference_fn), host=args.host, port=args.port)
 
 
 
 
 
 
 
 
 
 
 
278
  return response
279
 
280
  access.middleware(app)
281
+ app.state.demo = demo
282
  return gr.mount_gradio_app(app, demo, path='/', allowed_paths=example_files, show_error=False,
283
  theme=theme, css=(Path(__file__).parent/'style.css').read_text(), footer_links=[])
284
 
 
297
  from zero_gpu import initialize, reconstruct_on_gpu
298
  initialize(args.model)
299
  inference_fn = reconstruct_on_gpu
300
+ app = create_app(args.model, args.device, args.output_root, args.examples,
301
+ inference_fn=inference_fn)
302
+ server = uvicorn.Server(uvicorn.Config(app, host=args.host, port=args.port))
303
+ app.state.demo.server = server
304
+ if inference_fn is not None:
305
+ # Mounted Gradio uses Uvicorn rather than Blocks.launch. Execute the
306
+ # same startup hook from pinned spaces 0.51.3 to pack emulated CUDA.
307
+ from spaces.config import Config
308
+ if Config.zero_gpu:
309
+ from spaces.zero import startup
310
+ startup()
311
+ server.run()