KavinduHansaka commited on
Commit
9e56761
·
verified ·
1 Parent(s): 4ad87c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -28
app.py CHANGED
@@ -1,43 +1,71 @@
1
  import gradio as gr
2
- from detoxify import Detoxify
3
  import pandas as pd
 
 
 
 
 
4
 
5
- # Load model once
6
- model = Detoxify('original')
7
 
8
- # Threshold for flagging a comment as risky
 
 
 
 
9
  TOXICITY_THRESHOLD = 0.7
 
 
 
 
 
 
 
 
10
 
11
- def classify_multiple(comments):
12
- # Split input into lines
13
- comment_list = [c.strip() for c in comments.split('\n') if c.strip()]
14
- if not comment_list:
15
- return "Please enter at least one valid comment."
 
 
 
16
 
17
- # Predict toxicity scores
18
- results = model.predict(comment_list)
19
- df = pd.DataFrame(results, index=comment_list).round(4)
20
 
21
- # Capitalize headers
22
- df.columns = [col.replace("_", " ").title().replace(" ", "_") for col in df.columns]
23
- df.columns = [col.replace("_", " ") for col in df.columns]
24
 
25
- # Add warning column
26
- def check_warning(row):
27
- return "⚠️ High Risk" if any(score > TOXICITY_THRESHOLD for score in row) else " Safe"
28
 
29
- df["⚠️ Warning"] = df.apply(check_warning, axis=1)
 
 
 
 
30
 
31
- return df
 
 
 
32
 
33
- # UI setup
34
- iface = gr.Interface(
35
- fn=classify_multiple,
36
- inputs=gr.Textbox(lines=8, placeholder="Enter one or more comments, each on a new line..."),
37
- outputs=gr.Dataframe(label="Toxicity Predictions with Warnings"),
38
- title="💬 Toxic Comment Classifier (Multi-Comment, with Warnings)",
39
- description="Paste one or more comments. Each comment is scored for toxicity, and flagged as ⚠️ if any label exceeds 0.7."
 
 
 
40
  )
41
 
42
  if __name__ == "__main__":
43
- iface.launch()
 
1
  import gradio as gr
 
2
  import pandas as pd
3
+ from detoxify import Detoxify
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+ import torch
6
+ import numpy as np
7
+ import io
8
 
9
+ # Load Detoxify multilingual model
10
+ tox_model = Detoxify('multilingual')
11
 
12
+ # Load AI detector model
13
+ ai_tokenizer = AutoTokenizer.from_pretrained("openai-community/roberta-base-openai-detector")
14
+ ai_model = AutoModelForSequenceClassification.from_pretrained("openai-community/roberta-base-openai-detector")
15
+
16
+ # Thresholds
17
  TOXICITY_THRESHOLD = 0.7
18
+ AI_THRESHOLD = 0.5 # If >0.5, it's likely AI-generated
19
+
20
+ def detect_ai_generated(text):
21
+ with torch.no_grad():
22
+ inputs = ai_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
23
+ logits = ai_model(**inputs).logits
24
+ probs = torch.sigmoid(logits).squeeze().item()
25
+ return round(probs, 4)
26
 
27
+ def process_input(file):
28
+ df = pd.read_csv(file.name)
29
+ if 'comment' not in df.columns:
30
+ return "CSV must contain a 'comment' column."
31
+
32
+ comments = df['comment'].astype(str).tolist()
33
+ tox_results = tox_model.predict(comments)
34
+ tox_df = pd.DataFrame(tox_results, index=comments).round(4)
35
 
36
+ # Format columns
37
+ tox_df.columns = [col.replace("_", " ").title().replace(" ", "_") for col in tox_df.columns]
38
+ tox_df.columns = [col.replace("_", " ") for col in tox_df.columns]
39
 
40
+ # Add warnings
41
+ tox_df["⚠️ Warning"] = tox_df.apply(lambda row: "⚠️ High Risk" if any(score > TOXICITY_THRESHOLD for score in row) else "✅ Safe", axis=1)
 
42
 
43
+ # Add AI detection
44
+ tox_df["🧪 AI Probability"] = [detect_ai_generated(c) for c in tox_df.index]
45
+ tox_df["🧪 AI Detection"] = tox_df["🧪 AI Probability"].apply(lambda x: "🤖 Likely AI" if x > AI_THRESHOLD else "🧍 Human")
46
 
47
+ # Store downloadable CSV
48
+ csv_data = tox_df.copy()
49
+ csv_data.insert(0, "Comment", tox_df.index)
50
+ csv_bytes = csv_data.to_csv(index=False).encode()
51
+ return tox_df, ("toxicity_report.csv", csv_bytes)
52
 
53
+ # Gradio UI
54
+ upload = gr.File(label="📥 Upload .CSV (Must contain 'comment' column)")
55
+ output_table = gr.Dataframe(label="📊 Predictions (Multilingual + AI Detection)")
56
+ download = gr.File(label="📤 Download Predictions")
57
 
58
+ app = gr.Interface(
59
+ fn=process_input,
60
+ inputs=upload,
61
+ outputs=[output_table, download],
62
+ title="🌍 Toxic Comment Classifier + AI Text Detector",
63
+ description="""
64
+ 📥 Upload a .csv file with a 'comment' column.
65
+ 🔍 Each comment will be scored for toxicity (Multilingual model) and AI-generation probability (RoBERTa-based).
66
+ 📤 Download the full report as .csv.
67
+ """
68
  )
69
 
70
  if __name__ == "__main__":
71
+ app.launch()