AsyncCoder-10 commited on
Commit
b56fc39
·
verified ·
1 Parent(s): e6b065a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import spaces # must be first, before torch/transformers
2
  import gradio as gr
3
- import uvicorn
4
- from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
 
6
 
7
  from app.main import app as moderation_api, moderate_text, TextRequest, generate_suggestions
8
 
@@ -35,24 +34,28 @@ with gr.Blocks(title="Multilingual Moderation API") as demo:
35
  out = gr.Textbox(lines=12, label="Moderation Result")
36
  btn.click(moderate_ui, inp, out, api_name="moderate_ui")
37
 
38
- # Hidden event so ZeroGPU detects the @spaces.GPU function at startup
39
  with gr.Row(visible=False):
40
  h_in = gr.Textbox()
41
  h_out = gr.JSON()
42
  h_btn = gr.Button()
43
  h_btn.click(generate_suggestions, h_in, h_out, api_name="rewrite")
44
 
45
-
46
- root = FastAPI()
47
- root.add_middleware(
48
  CORSMiddleware,
49
  allow_origins=["*"], # restrict to your Angular domain in production
50
  allow_methods=["*"],
51
  allow_headers=["*"],
52
  )
53
 
54
- root.mount("/api", moderation_api) # -> /api/moderate and /api/health
55
- app = gr.mount_gradio_app(root, demo, path="/")
56
-
57
  if __name__ == "__main__":
58
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
1
  import spaces # must be first, before torch/transformers
2
  import gradio as gr
 
 
3
  from fastapi.middleware.cors import CORSMiddleware
4
+ from starlette.routing import Mount
5
 
6
  from app.main import app as moderation_api, moderate_text, TextRequest, generate_suggestions
7
 
 
34
  out = gr.Textbox(lines=12, label="Moderation Result")
35
  btn.click(moderate_ui, inp, out, api_name="moderate_ui")
36
 
37
+ # Hidden event so the @spaces.GPU function is registered with Gradio
38
  with gr.Row(visible=False):
39
  h_in = gr.Textbox()
40
  h_out = gr.JSON()
41
  h_btn = gr.Button()
42
  h_btn.click(generate_suggestions, h_in, h_out, api_name="rewrite")
43
 
44
+ # CORS goes on the sub-app (middleware can't be added to Gradio's app after launch)
45
+ moderation_api.add_middleware(
 
46
  CORSMiddleware,
47
  allow_origins=["*"], # restrict to your Angular domain in production
48
  allow_methods=["*"],
49
  allow_headers=["*"],
50
  )
51
 
 
 
 
52
  if __name__ == "__main__":
53
+ # launch() is what triggers ZeroGPU's startup detection
54
+ gradio_app, _, _ = demo.launch(
55
+ server_name="0.0.0.0",
56
+ server_port=7860,
57
+ prevent_thread_lock=True,
58
+ )
59
+ # Put /api first so it wins over any Gradio route
60
+ gradio_app.router.routes.insert(0, Mount("/api", app=moderation_api))
61
+ demo.block_thread()