KavinduHansaka commited on
Commit
e00c4c7
·
verified ·
1 Parent(s): ef2b3bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -2,24 +2,26 @@ import gradio as gr
2
  from detoxify import Detoxify
3
  import pandas as pd
4
 
5
- # Load model once at startup
6
  model = Detoxify('original')
7
 
8
- def classify_toxicity(text):
9
- if not text.strip():
10
- return "Please enter a valid comment."
 
 
11
 
12
- results = model.predict(text)
13
- df = pd.DataFrame([results], index=["Score"]).T
14
- df = df.round(4)
15
  return df
16
 
17
  iface = gr.Interface(
18
- fn=classify_toxicity,
19
- inputs=gr.Textbox(label="Enter a Comment", lines=3, placeholder="Example: You are so annoying..."),
20
- outputs=gr.Dataframe(label="Toxicity Classification"),
21
- title="💬 Toxic Comment Classifier",
22
- description="Classifies input comments as toxic, severe toxic, obscene, threat, insult, and identity hate using Detoxify (unitary/toxic-bert)."
23
  )
24
 
25
  if __name__ == "__main__":
 
2
  from detoxify import Detoxify
3
  import pandas as pd
4
 
5
+ # Load model once
6
  model = Detoxify('original')
7
 
8
+ def classify_multiple(comments):
9
+ # Split input by newlines and clean
10
+ comment_list = [c.strip() for c in comments.split('\n') if c.strip()]
11
+ if not comment_list:
12
+ return "Please enter at least one valid comment."
13
 
14
+ results = model.predict(comment_list) # Returns a dict of lists
15
+
16
+ df = pd.DataFrame(results, index=comment_list).round(4)
17
  return df
18
 
19
  iface = gr.Interface(
20
+ fn=classify_multiple,
21
+ inputs=gr.Textbox(lines=8, placeholder="Enter one or more comments, each on a new line..."),
22
+ outputs=gr.Dataframe(label="Toxicity Predictions"),
23
+ title="💬 Toxic Comment Classifier (Multi-Comment)",
24
+ description="Paste one or more comments. Each will be scored for toxicity, severe toxicity, insult, threat, obscene, and identity hate using Detoxify."
25
  )
26
 
27
  if __name__ == "__main__":