AsyncCoder-10 commited on
Commit
2b57455
·
verified ·
1 Parent(s): 2004048

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -13
app.py CHANGED
@@ -1,32 +1,28 @@
1
  import gradio as gr
2
  import spaces
3
 
4
- # 1. Import the logic and models from your FastAPI app
5
  from app.main import moderate_text, TextRequest
6
 
7
- # 2. ZeroGPU Startup Check
8
  @spaces.GPU
9
  def _satisfy_zerogpu_startup_check():
10
  return "GPU available"
11
 
12
- # 3. The Real UI Function
13
  async def moderate_ui(text):
14
  if not text.strip():
15
  return "Please enter some text."
16
 
17
  try:
18
- # Create the request object expected by your FastAPI route
19
  request = TextRequest(text=text)
20
-
21
- # Call your actual FastAPI function directly
22
  response = await moderate_text(request)
23
 
24
- # Format the result nicely for the UI
25
  result = f"🚨 Flagged: {response.is_flagged}\n\n"
26
  result += f"📝 Original: {response.original_text}\n\n"
27
 
28
  if response.is_flagged and response.rewritten_text:
29
- result += f"✅ Suggested Proper Sentence:\n\"{response.rewritten_text}\""
 
 
 
30
  elif response.is_flagged:
31
  result += "✅ Suggested Proper Sentence: (Model could not generate a rewrite)"
32
  else:
@@ -36,15 +32,13 @@ async def moderate_ui(text):
36
  except Exception as e:
37
  return f"Error: {str(e)}"
38
 
39
- # 4. Build the Interface
40
  demo = gr.Interface(
41
  fn=moderate_ui,
42
- inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here (e.g., 'I will kill you')..."),
43
- outputs=gr.Textbox(lines=6, label="Moderation Result"),
44
  title="Multilingual Moderation API",
45
- description="Enter text to detect abuse/threats and get a suggested proper sentence."
46
  )
47
 
48
- # 5. Launch
49
  if __name__ == "__main__":
50
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  import spaces
3
 
 
4
  from app.main import moderate_text, TextRequest
5
 
 
6
  @spaces.GPU
7
  def _satisfy_zerogpu_startup_check():
8
  return "GPU available"
9
 
 
10
  async def moderate_ui(text):
11
  if not text.strip():
12
  return "Please enter some text."
13
 
14
  try:
 
15
  request = TextRequest(text=text)
 
 
16
  response = await moderate_text(request)
17
 
 
18
  result = f"🚨 Flagged: {response.is_flagged}\n\n"
19
  result += f"📝 Original: {response.original_text}\n\n"
20
 
21
  if response.is_flagged and response.rewritten_text:
22
+ result += "✅ Suggested Proper Sentences:\n"
23
+ # Loop through the 5 suggestions and format them
24
+ for i, suggestion in enumerate(response.rewritten_text, 1):
25
+ result += f"{i}. {suggestion}\n"
26
  elif response.is_flagged:
27
  result += "✅ Suggested Proper Sentence: (Model could not generate a rewrite)"
28
  else:
 
32
  except Exception as e:
33
  return f"Error: {str(e)}"
34
 
 
35
  demo = gr.Interface(
36
  fn=moderate_ui,
37
+ inputs=gr.Textbox(lines=3, placeholder="Type a toxic sentence here..."),
38
+ outputs=gr.Textbox(lines=12, label="Moderation Result"), # Increased lines for output
39
  title="Multilingual Moderation API",
40
+ description="Enter text to detect abuse/threats and get suggested proper sentences."
41
  )
42
 
 
43
  if __name__ == "__main__":
44
  demo.launch(server_name="0.0.0.0", server_port=7860)