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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -33
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 (by double newlines), fallback to sentences if no 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
- # Add label for clarity
39
- if ai_score < 0.3:
40
- label = "🟒 Likely Human"
41
- elif ai_score < 0.7:
42
- label = "🟑 Mixed"
 
 
43
  else:
44
- label = "πŸ”΄ Likely AI"
 
45
 
46
- results.append({"text": para, "ai_score": ai_score, "label": label})
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:6px; margin-bottom:6px; border-radius:6px'>"
54
- f"<b>{r['label']} ({round(r['ai_score']*100,1)}%)</b><br>{r['text']}</div>"
 
55
  )
56
 
57
- # Compute total AI probability (average)
58
  if results:
59
- avg_ai = sum(r['ai_score'] for r in results) / len(results)
60
- total_percent = round(avg_ai * 100, 2)
61
- highlighted += f"<p><b>βš–οΈ Overall AI Probability: {total_percent}%</b></p>"
62
  else:
63
- total_percent = 0.0
64
 
65
- return highlighted, {"paragraphs": results, "total_ai_percent": total_percent}
66
 
67
  with gr.Blocks() as demo:
68
  gr.Markdown("## πŸ€– AI Detector (Paragraph-level)")
69
- gr.Markdown("Paste your text below. Green = Human-like, Yellow = Mixed, Red = AI-like.")
70
- input_text = gr.Textbox(lines=10, placeholder="Enter text here...")
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")