VictorM-Coder commited on
Commit
e7db512
Β·
verified Β·
1 Parent(s): 8df1574

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -28
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 (double newlines)
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
- 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()
 
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()