AsyncCoder-10 commited on
Commit
d5da69a
·
verified ·
1 Parent(s): 3e876c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -4,14 +4,9 @@ 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
8
- # If main.py sits next to app.py (no app/ folder), use:
9
- # from main import app as moderation_api, moderate_text, TextRequest
10
-
11
-
12
- @spaces.GPU
13
- def _satisfy_zerogpu_startup_check():
14
- return "GPU available"
15
 
16
 
17
  async def moderate_ui(text):
@@ -35,28 +30,30 @@ async def moderate_ui(text):
35
  return f"Error: {str(e)}"
36
 
37
 
38
- demo = gr.Interface(
39
- fn=moderate_ui,
40
- inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
41
- outputs=gr.Textbox(lines=12, label="Moderation Result"),
42
- title="Multilingual Moderation API",
43
- )
44
 
45
- # Root FastAPI app
46
- root = FastAPI()
 
 
 
 
47
 
48
- # CORS so your Angular app can call it from the browser
 
49
  root.add_middleware(
50
  CORSMiddleware,
51
- allow_origins=["*"], # replace with your Angular domain(s) in production
52
  allow_methods=["*"],
53
  allow_headers=["*"],
54
  )
55
 
56
- # API first -> /api/moderate and /api/health
57
- root.mount("/api", moderation_api)
58
-
59
- # Gradio UI at "/"
60
  app = gr.mount_gradio_app(root, demo, path="/")
61
 
62
  if __name__ == "__main__":
 
4
  from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
6
 
7
+ # main.py next to app.py:
8
+ from main import app as moderation_api, moderate_text, TextRequest, generate_suggestions
9
+ # If main.py is inside an app/ folder, use: from app.main import ...
 
 
 
 
 
10
 
11
 
12
  async def moderate_ui(text):
 
30
  return f"Error: {str(e)}"
31
 
32
 
33
+ with gr.Blocks(title="Multilingual Moderation API") as demo:
34
+ gr.Markdown("# Multilingual Moderation API")
35
+ inp = gr.Textbox(lines=3, placeholder="Type a toxic sentence here...", label="Text")
36
+ btn = gr.Button("Moderate")
37
+ out = gr.Textbox(lines=12, label="Moderation Result")
38
+ btn.click(moderate_ui, inp, out, api_name="moderate_ui")
39
 
40
+ # Hidden event: registers the @spaces.GPU function so ZeroGPU detects it
41
+ with gr.Row(visible=False):
42
+ h_in = gr.Textbox()
43
+ h_out = gr.JSON()
44
+ h_btn = gr.Button()
45
+ h_btn.click(generate_suggestions, h_in, h_out, api_name="rewrite")
46
 
47
+
48
+ root = FastAPI()
49
  root.add_middleware(
50
  CORSMiddleware,
51
+ allow_origins=["*"], # restrict to your Angular domain in production
52
  allow_methods=["*"],
53
  allow_headers=["*"],
54
  )
55
 
56
+ root.mount("/api", moderation_api) # -> /api/moderate, /api/health
 
 
 
57
  app = gr.mount_gradio_app(root, demo, path="/")
58
 
59
  if __name__ == "__main__":