Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,22 +8,9 @@ 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 green-yellow-red gradient."""
|
| 13 |
-
# Green (0,255,0) -> Yellow (255,255,0) -> Red (255,0,0)
|
| 14 |
-
if ai_score < 0.5: # Green to Yellow
|
| 15 |
-
red = int(ai_score * 510)
|
| 16 |
-
green = 255
|
| 17 |
-
else: # Yellow to Red
|
| 18 |
-
red = 255
|
| 19 |
-
green = int(255 - (ai_score - 0.5) * 510)
|
| 20 |
-
return f"rgb({red},{green},0)"
|
| 21 |
-
|
| 22 |
def detect_ai(text):
|
| 23 |
-
# Split into paragraphs (
|
| 24 |
paragraphs = re.split(r"\n\s*\n", text.strip())
|
| 25 |
-
if len(paragraphs) == 1: # no clear paragraphs
|
| 26 |
-
paragraphs = re.split(r'(?<=[.!?]) +', text)
|
| 27 |
|
| 28 |
results = []
|
| 29 |
for para in paragraphs:
|
|
@@ -33,41 +20,43 @@ def detect_ai(text):
|
|
| 33 |
with torch.no_grad():
|
| 34 |
outputs = model(**inputs)
|
| 35 |
probs = torch.softmax(outputs.logits, dim=1)
|
| 36 |
-
ai_score = float(probs[0][1])
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
-
label = "
|
|
|
|
| 45 |
|
| 46 |
-
results.append({"
|
| 47 |
|
| 48 |
# Build highlighted HTML
|
| 49 |
highlighted = ""
|
| 50 |
for r in results:
|
| 51 |
-
color = get_color(r['ai_score'])
|
| 52 |
highlighted += (
|
| 53 |
-
f"<div style='background-color:{color}; padding:
|
| 54 |
-
f"<b>{r['label']}
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# Compute
|
| 58 |
if results:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
highlighted += f"<p><b>βοΈ Overall
|
| 62 |
else:
|
| 63 |
-
|
| 64 |
|
| 65 |
-
return highlighted, {"paragraphs": results, "
|
| 66 |
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("## π€ AI Detector (Paragraph-level)")
|
| 69 |
-
gr.Markdown("
|
| 70 |
-
input_text = gr.Textbox(lines=
|
| 71 |
output_html = gr.HTML()
|
| 72 |
output_json = gr.JSON()
|
| 73 |
run_btn = gr.Button("Detect AI")
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 9 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def detect_ai(text):
|
| 12 |
+
# Split into paragraphs (double newlines)
|
| 13 |
paragraphs = re.split(r"\n\s*\n", text.strip())
|
|
|
|
|
|
|
| 14 |
|
| 15 |
results = []
|
| 16 |
for para in paragraphs:
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = model(**inputs)
|
| 22 |
probs = torch.softmax(outputs.logits, dim=1)
|
|
|
|
| 23 |
|
| 24 |
+
ai_score = float(probs[0][1]) # AI likelihood
|
| 25 |
+
human_score = 1 - ai_score # Human likelihood
|
| 26 |
+
|
| 27 |
+
# Decide label: if <90% human β AI
|
| 28 |
+
if human_score < 0.9:
|
| 29 |
+
label = "π΄ AI"
|
| 30 |
+
color = "rgb(255,120,120)" # red
|
| 31 |
else:
|
| 32 |
+
label = "π’ Human"
|
| 33 |
+
color = "rgb(120,255,120)" # green
|
| 34 |
|
| 35 |
+
results.append({"paragraph": para, "ai_score": ai_score, "human_score": human_score, "label": label, "color": color})
|
| 36 |
|
| 37 |
# Build highlighted HTML
|
| 38 |
highlighted = ""
|
| 39 |
for r in results:
|
|
|
|
| 40 |
highlighted += (
|
| 41 |
+
f"<div style='background-color:{r['color']}; padding:8px; margin-bottom:6px; border-radius:6px'>"
|
| 42 |
+
f"<b>{r['label']} β Human {round(r['human_score']*100,1)}% | AI {round(r['ai_score']*100,1)}%</b><br>"
|
| 43 |
+
f"{r['paragraph']}</div>"
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Compute overall human %
|
| 47 |
if results:
|
| 48 |
+
avg_human = sum(r['human_score'] for r in results) / len(results)
|
| 49 |
+
total_human = round(avg_human * 100, 2)
|
| 50 |
+
highlighted += f"<p><b>βοΈ Overall Human Probability: {total_human}%</b></p>"
|
| 51 |
else:
|
| 52 |
+
total_human = 0.0
|
| 53 |
|
| 54 |
+
return highlighted, {"paragraphs": results, "overall_human_percent": total_human}
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("## π€ AI Detector (Paragraph-level)")
|
| 58 |
+
gr.Markdown("Paragraphs with <90% human probability are flagged as **AI**.")
|
| 59 |
+
input_text = gr.Textbox(lines=12, placeholder="Paste your essay or report here...")
|
| 60 |
output_html = gr.HTML()
|
| 61 |
output_json = gr.JSON()
|
| 62 |
run_btn = gr.Button("Detect AI")
|