Update app.py
Browse files
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
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
TOXICITY_THRESHOLD = 0.7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
df.columns = [col.replace("_", " ") for col in df.columns]
|
| 24 |
|
| 25 |
-
# Add
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
-
|
|
|
|
| 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()
|