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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,29 +1,31 @@
 
1
  import gradio as gr
2
- import spaces
3
  from fastapi import FastAPI
4
  from fastapi.middleware.cors import CORSMiddleware
5
- from app.main import app as moderation_app # your existing FastAPI app with /moderate
6
 
7
- # Dummy function to pass ZeroGPU check
 
 
 
 
8
  @spaces.GPU
9
  def _satisfy_zerogpu_startup_check():
10
  return "GPU available"
11
 
12
- # The UI function
13
  async def moderate_ui(text):
14
  if not text.strip():
15
  return "Please enter some text."
16
  try:
17
- from app.main import moderate_text, TextRequest
18
- request = TextRequest(text=text)
19
- response = await moderate_text(request)
20
 
21
  result = f"🚨 Flagged: {response.is_flagged}\n\n"
22
  result += f"πŸ“ Original: {response.original_text}\n\n"
23
  if response.is_flagged and response.rewritten_text:
24
  result += "βœ… Suggested Proper Sentences:\n"
25
- for i, suggestion in enumerate(response.rewritten_text, 1):
26
- result += f"{i}. {suggestion}\n"
27
  elif response.is_flagged:
28
  result += "βœ… (Model could not generate a rewrite)"
29
  else:
@@ -32,7 +34,7 @@ async def moderate_ui(text):
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
35
- # Create Gradio UI
36
  demo = gr.Interface(
37
  fn=moderate_ui,
38
  inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
@@ -40,25 +42,22 @@ demo = gr.Interface(
40
  title="Multilingual Moderation API",
41
  )
42
 
43
- # Create the main FastAPI app
44
- app = FastAPI(title="Multilingual Moderation API")
45
 
46
- # Add CORS middleware – allow all origins (or restrict as needed)
47
- app.add_middleware(
48
  CORSMiddleware,
49
- allow_origins=["*"], # replace "*" with your Angular app's origin for production
50
- allow_credentials=True,
51
  allow_methods=["*"],
52
  allow_headers=["*"],
53
  )
54
 
55
- # Mount the Gradio UI at the root path (or "/ui")
56
- app = gr.mount_gradio_app(app, demo, path="/")
57
 
58
- # Mount the moderation API at "/api" – keeps your /api/moderate endpoint
59
- app.mount("/api", moderation_app)
60
 
61
- # If you run this file directly:
62
  if __name__ == "__main__":
63
- import uvicorn
64
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ import spaces # must be first
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
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):
18
  if not text.strip():
19
  return "Please enter some text."
20
  try:
21
+ response = await moderate_text(TextRequest(text=text))
 
 
22
 
23
  result = f"🚨 Flagged: {response.is_flagged}\n\n"
24
  result += f"πŸ“ Original: {response.original_text}\n\n"
25
  if response.is_flagged and response.rewritten_text:
26
  result += "βœ… Suggested Proper Sentences:\n"
27
+ for i, s in enumerate(response.rewritten_text, 1):
28
+ result += f"{i}. {s}\n"
29
  elif response.is_flagged:
30
  result += "βœ… (Model could not generate a rewrite)"
31
  else:
 
34
  except Exception as e:
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..."),
 
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__":
 
63
  uvicorn.run(app, host="0.0.0.0", port=7860)