VictorM-Coder commited on
Commit
5237e13
·
verified ·
1 Parent(s): 92ac75c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -17
app.py CHANGED
@@ -26,7 +26,8 @@ def get_model():
26
  ).to(device).eval()
27
  return tokenizer, model
28
 
29
- THRESHOLD = 0.41
 
30
 
31
  # -----------------------------
32
  # PROTECT STRUCTURE
@@ -67,12 +68,10 @@ def split_preserving_structure(text):
67
  # -----------------------------
68
  @torch.inference_mode()
69
  def analyze(text):
70
- # Basic cleanup
71
  text = text.strip()
72
  if not text:
73
  return "—", "—", "<em>Please enter text...</em>", None
74
 
75
- # --- WORD COUNT CHECK ---
76
  word_count = len(text.split())
77
  if word_count < 300:
78
  warning_msg = f"⚠️ <b>Insufficient Text:</b> Your input has {word_count} words. Please enter at least 300 words for an accurate analysis."
@@ -90,7 +89,6 @@ def analyze(text):
90
  if not pure_sents:
91
  return "—", "—", "<em>No sentences detected.</em>", None
92
 
93
- # Sliding window inference
94
  windows = []
95
  for i in range(len(pure_sents)):
96
  start = max(0, i - 1)
@@ -101,13 +99,12 @@ def analyze(text):
101
  logits = mod(**inputs).logits
102
  probs = F.softmax(logits.float(), dim=-1)[:, 1].cpu().numpy().tolist()
103
 
104
- # Calculate Weighted Average
105
  lengths = [len(s.split()) for s in pure_sents]
106
  total_words = sum(lengths)
107
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
108
 
109
  # -----------------------------
110
- # HTML RECONSTRUCTION (Red vs Green Only)
111
  # -----------------------------
112
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
113
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
@@ -120,13 +117,11 @@ def analyze(text):
120
  if i in prob_map:
121
  score = prob_map[i]
122
 
123
- # Use THRESHOLD for binary color logic
124
- if score > THRESHOLD:
125
- # RED: Above the threshold (AI detected)
126
- color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)"
127
  else:
128
- # GREEN: Below the threshold (Human-like)
129
- color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
130
 
131
  highlighted_html += (
132
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
@@ -137,8 +132,8 @@ def analyze(text):
137
  highlighted_html += block
138
  highlighted_html += "</div>"
139
 
140
- # --- FINAL VERDICT LOGIC ---
141
- if weighted_avg > THRESHOLD:
142
  label = f"{weighted_avg:.0%} AI Content Detected"
143
  display_score = f"{weighted_avg:.1%}"
144
  else:
@@ -152,12 +147,12 @@ def analyze(text):
152
  # GRADIO INTERFACE
153
  # -----------------------------
154
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
155
- gr.Markdown("## 🕵️ Detector Pro")
156
- gr.Markdown(f"Sentence-level analysis. **Min 300 words required.** Score masked (*) if ≤ {THRESHOLD*100:.0f}%.")
157
 
158
  with gr.Row():
159
  with gr.Column(scale=3):
160
- text_input = gr.Textbox(label="Paste Text", lines=12, placeholder="Minimum 300 words required for analysis...")
161
  run_btn = gr.Button("Analyze", variant="primary")
162
  with gr.Column(scale=1):
163
  verdict_out = gr.Label(label="Verdict")
 
26
  ).to(device).eval()
27
  return tokenizer, model
28
 
29
+ # UPDATED THRESHOLD: Only 81% and above is flagged as AI
30
+ THRESHOLD = 0.81
31
 
32
  # -----------------------------
33
  # PROTECT STRUCTURE
 
68
  # -----------------------------
69
  @torch.inference_mode()
70
  def analyze(text):
 
71
  text = text.strip()
72
  if not text:
73
  return "—", "—", "<em>Please enter text...</em>", None
74
 
 
75
  word_count = len(text.split())
76
  if word_count < 300:
77
  warning_msg = f"⚠️ <b>Insufficient Text:</b> Your input has {word_count} words. Please enter at least 300 words for an accurate analysis."
 
89
  if not pure_sents:
90
  return "—", "—", "<em>No sentences detected.</em>", None
91
 
 
92
  windows = []
93
  for i in range(len(pure_sents)):
94
  start = max(0, i - 1)
 
99
  logits = mod(**inputs).logits
100
  probs = F.softmax(logits.float(), dim=-1)[:, 1].cpu().numpy().tolist()
101
 
 
102
  lengths = [len(s.split()) for s in pure_sents]
103
  total_words = sum(lengths)
104
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
105
 
106
  # -----------------------------
107
+ # HTML RECONSTRUCTION
108
  # -----------------------------
109
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
110
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
 
117
  if i in prob_map:
118
  score = prob_map[i]
119
 
120
+ # Logic: Red for > 0.81, Green for everything else (<= 0.81)
121
+ if score >= THRESHOLD:
122
+ color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)" # RED
 
123
  else:
124
+ color, bg = "#11823b", "rgba(17, 130, 59, 0.15)" # GREEN
 
125
 
126
  highlighted_html += (
127
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
 
132
  highlighted_html += block
133
  highlighted_html += "</div>"
134
 
135
+ # --- FINAL VERDICT ---
136
+ if weighted_avg >= THRESHOLD:
137
  label = f"{weighted_avg:.0%} AI Content Detected"
138
  display_score = f"{weighted_avg:.1%}"
139
  else:
 
147
  # GRADIO INTERFACE
148
  # -----------------------------
149
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
150
+ gr.Markdown("## 🕵️ AI Detector Pro")
151
+ gr.Markdown(f"Strict Analysis. Threshold: **{THRESHOLD*100:.0f}%**. Everything below this is considered Human.")
152
 
153
  with gr.Row():
154
  with gr.Column(scale=3):
155
+ text_input = gr.Textbox(label="Paste Text", lines=12, placeholder="Minimum 300 words...")
156
  run_btn = gr.Button("Analyze", variant="primary")
157
  with gr.Column(scale=1):
158
  verdict_out = gr.Label(label="Verdict")