Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,61 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
if len(input_text.split()) < 3: # Skip very short text
|
| 17 |
-
return "Text too short to classify accurately."
|
| 18 |
-
|
| 19 |
-
result = classifier(input_text)[0]
|
| 20 |
-
label = result['label']
|
| 21 |
-
score = result['score'] * 100
|
| 22 |
-
return f"Prediction: {label}\nConfidence: {score:.2f}%"
|
| 23 |
-
|
| 24 |
-
# Optional: show a sample from MAGE dataset
|
| 25 |
-
def sample_text(index):
|
| 26 |
-
s = dataset['train'][index]
|
| 27 |
-
return f"Text: {s['text']}\nLabel: {s['label']}"
|
| 28 |
-
|
| 29 |
-
# Gradio UI
|
| 30 |
-
demo = gr.Blocks()
|
| 31 |
-
|
| 32 |
-
with demo:
|
| 33 |
-
gr.Markdown("# AI vs Human Text Detector")
|
| 34 |
-
with gr.Tab("Detect Text"):
|
| 35 |
-
text_input = gr.Textbox(label="Enter Text Here", lines=5)
|
| 36 |
-
output = gr.Textbox(label="Prediction")
|
| 37 |
-
detect_btn = gr.Button("Detect")
|
| 38 |
-
detect_btn.click(detect_text, inputs=text_input, outputs=output)
|
| 39 |
-
|
| 40 |
-
with gr.Tab("Sample Text from MAGE"):
|
| 41 |
-
index_input = gr.Number(label="Sample Index", value=0)
|
| 42 |
-
sample_output = gr.Textbox(label="Sample")
|
| 43 |
-
index_input.change(sample_text, inputs=index_input, outputs=sample_output)
|
| 44 |
|
| 45 |
demo.launch()
|
|
|
|
| 1 |
+
!pip install gradio transformers torch
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, GPT2LMHeadModel
|
| 5 |
+
import torch
|
| 6 |
+
import math
|
| 7 |
+
|
| 8 |
+
# Load a free AI detector model (RoBERTa)
|
| 9 |
+
detector_name = "Hello-SimpleAI/AI-Text-Detector-RoBERTa"
|
| 10 |
+
detector_tokenizer = AutoTokenizer.from_pretrained(detector_name)
|
| 11 |
+
detector_model = AutoModelForSequenceClassification.from_pretrained(detector_name)
|
| 12 |
+
|
| 13 |
+
# Load GPT-2 for perplexity scoring
|
| 14 |
+
gpt2_name = "gpt2"
|
| 15 |
+
gpt2_tokenizer = AutoTokenizer.from_pretrained(gpt2_name)
|
| 16 |
+
gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_name)
|
| 17 |
+
|
| 18 |
+
def compute_perplexity(text):
|
| 19 |
+
enc = gpt2_tokenizer(text, return_tensors="pt")
|
| 20 |
+
input_ids = enc.input_ids
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
loss = gpt2_model(input_ids, labels=input_ids).loss
|
| 23 |
+
return math.exp(loss.item())
|
| 24 |
+
|
| 25 |
+
def analyze_text(user_text):
|
| 26 |
+
# Model 1: RoBERTa detector
|
| 27 |
+
inputs = detector_tokenizer(user_text, return_tensors="pt", truncation=True, max_length=512)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
logits = detector_model(**inputs).logits
|
| 30 |
+
probs = torch.softmax(logits, dim=1).tolist()[0]
|
| 31 |
+
human_prob, ai_prob = probs[0], probs[1]
|
| 32 |
+
|
| 33 |
+
# Model 2: GPT-2 Perplexity
|
| 34 |
+
ppl = compute_perplexity(user_text)
|
| 35 |
+
# Heuristic: low perplexity → AI
|
| 36 |
+
ppl_score = max(0, min(1, 100/ppl)) # normalize to 0..1
|
| 37 |
+
|
| 38 |
+
# Aggregate
|
| 39 |
+
final_ai = (ai_prob + ppl_score) / 2
|
| 40 |
+
final_human = 1 - final_ai
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
"RoBERTa AI Probability": round(ai_prob*100, 2),
|
| 44 |
+
"Perplexity-based AI Probability": round(ppl_score*100, 2),
|
| 45 |
+
"Final AI Probability (avg)": round(final_ai*100, 2),
|
| 46 |
+
"Final Human Probability (avg)": round(final_human*100, 2),
|
| 47 |
+
}
|
| 48 |
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# 🔍 Free AI vs Human Text Detector (Demo)")
|
| 51 |
+
user_input = gr.Textbox(
|
| 52 |
+
label="✍️ Enter Text",
|
| 53 |
+
placeholder="Paste text here...",
|
| 54 |
+
lines=12,
|
| 55 |
+
type="text"
|
| 56 |
+
)
|
| 57 |
+
output = gr.JSON(label="Results")
|
| 58 |
+
run_btn = gr.Button("Run Detection")
|
| 59 |
+
run_btn.click(analyze_text, inputs=user_input, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
demo.launch()
|