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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -11
app.py CHANGED
@@ -9,16 +9,23 @@ 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
@@ -27,19 +34,31 @@ def detect_ai(text):
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)
42
- highlighted += f"<p><b>Total AI Probability: {total_percent}%</b></p>"
43
  else:
44
  total_percent = 0.0
45
 
@@ -47,8 +66,8 @@ def detect_ai(text):
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")
 
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:
30
  if not para.strip():
31
  continue
 
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
 
 
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")