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