Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,39 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
MODEL = "
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 10 |
|
| 11 |
def get_color(ai_score):
|
|
|
|
| 12 |
red = int(ai_score * 255)
|
| 13 |
green = int((1 - ai_score) * 255)
|
| 14 |
return f"rgb({red},{green},0)"
|
| 15 |
|
| 16 |
def detect_ai(text):
|
| 17 |
-
|
|
|
|
| 18 |
results = []
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
if not
|
| 22 |
continue
|
| 23 |
-
inputs = tokenizer(
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model(**inputs)
|
| 26 |
probs = torch.softmax(outputs.logits, dim=1)
|
| 27 |
-
|
| 28 |
-
# HC3 model: [0] = human, [1] = ChatGPT
|
| 29 |
ai_score = float(probs[0][1])
|
| 30 |
-
results.append({"
|
| 31 |
|
| 32 |
-
#
|
| 33 |
highlighted = ""
|
| 34 |
for r in results:
|
| 35 |
color = get_color(r['ai_score'])
|
| 36 |
-
highlighted += f"<
|
| 37 |
|
| 38 |
-
#
|
| 39 |
if results:
|
| 40 |
avg_ai = sum(r['ai_score'] for r in results) / len(results)
|
| 41 |
total_percent = round(avg_ai * 100, 2)
|
|
@@ -43,16 +43,16 @@ def detect_ai(text):
|
|
| 43 |
else:
|
| 44 |
total_percent = 0.0
|
| 45 |
|
| 46 |
-
return highlighted, {"
|
| 47 |
-
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("## 🤖 AI Detector (
|
| 51 |
-
gr.Markdown("Paste text
|
| 52 |
-
input_text = gr.Textbox(lines=8, placeholder="Enter text here
|
| 53 |
output_html = gr.HTML()
|
| 54 |
output_json = gr.JSON()
|
| 55 |
run_btn = gr.Button("Detect AI")
|
|
|
|
| 56 |
run_btn.click(detect_ai, inputs=input_text, outputs=[output_html, output_json])
|
| 57 |
|
| 58 |
demo.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
+
MODEL = "roberta-base-openai-detector"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 10 |
|
| 11 |
def get_color(ai_score):
|
| 12 |
+
"""Convert AI score (0-1) into a smooth green-yellow-red gradient."""
|
| 13 |
red = int(ai_score * 255)
|
| 14 |
green = int((1 - ai_score) * 255)
|
| 15 |
return f"rgb({red},{green},0)"
|
| 16 |
|
| 17 |
def detect_ai(text):
|
| 18 |
+
# Split by paragraphs instead of sentences
|
| 19 |
+
paragraphs = re.split(r"\n\s*\n", text.strip())
|
| 20 |
results = []
|
| 21 |
|
| 22 |
+
for para in paragraphs:
|
| 23 |
+
if not para.strip():
|
| 24 |
continue
|
| 25 |
+
inputs = tokenizer(para, return_tensors="pt", truncation=True, max_length=512)
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model(**inputs)
|
| 28 |
probs = torch.softmax(outputs.logits, dim=1)
|
|
|
|
|
|
|
| 29 |
ai_score = float(probs[0][1])
|
| 30 |
+
results.append({"paragraph": para, "ai_score": ai_score})
|
| 31 |
|
| 32 |
+
# Build highlighted HTML
|
| 33 |
highlighted = ""
|
| 34 |
for r in results:
|
| 35 |
color = get_color(r['ai_score'])
|
| 36 |
+
highlighted += f"<div style='background-color:{color}; padding:6px; margin-bottom:4px'>{r['paragraph']}</div>"
|
| 37 |
|
| 38 |
+
# Compute total AI percentage (average across paragraphs)
|
| 39 |
if results:
|
| 40 |
avg_ai = sum(r['ai_score'] for r in results) / len(results)
|
| 41 |
total_percent = round(avg_ai * 100, 2)
|
|
|
|
| 43 |
else:
|
| 44 |
total_percent = 0.0
|
| 45 |
|
| 46 |
+
return highlighted, {"paragraphs": results, "total_ai_percent": total_percent}
|
|
|
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("## 🤖 AI Detector (Paragraph-level)")
|
| 50 |
+
gr.Markdown("Paste your text below. Green = human-like, Yellow = mixed, Red = AI-like.")
|
| 51 |
+
input_text = gr.Textbox(lines=8, placeholder="Enter text here with paragraphs separated by blank lines...")
|
| 52 |
output_html = gr.HTML()
|
| 53 |
output_json = gr.JSON()
|
| 54 |
run_btn = gr.Button("Detect AI")
|
| 55 |
+
|
| 56 |
run_btn.click(detect_ai, inputs=input_text, outputs=[output_html, output_json])
|
| 57 |
|
| 58 |
demo.launch()
|