VictorM-Coder commited on
Commit
4323dd6
·
verified ·
1 Parent(s): cb2c9a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
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."
@@ -117,12 +116,12 @@ def analyze(text):
117
 
118
  if i in prob_map:
119
  score = prob_map[i]
120
- if score < 0.60:
121
- color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
122
- elif score < 0.80:
123
- color, bg = "#b8860b", "rgba(184, 134, 11, 0.15)"
124
- else:
125
- color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)"
126
 
127
  highlighted_html += (
128
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
@@ -133,15 +132,14 @@ def analyze(text):
133
  highlighted_html += block
134
  highlighted_html += "</div>"
135
 
136
- # --- LOGIC PER REQUEST (40% THRESHOLD) ---
137
- if weighted_avg > 0.40:
138
  label = f"{weighted_avg:.0%} AI Content Detected"
139
  display_score = f"{weighted_avg:.1%}"
140
  else:
141
  label = "0 or * AI Content Detected"
142
  display_score = "*"
143
 
144
- # Always return the dataframe if analysis was successful
145
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
146
  return label, display_score, highlighted_html, df
147
 
@@ -150,11 +148,11 @@ def analyze(text):
150
  # -----------------------------
151
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
152
  gr.Markdown("## 🕵️ AI Detector Pro")
153
- gr.Markdown("Sentence-level analysis. **Min 300 words required.** Score masked (*) if ≤ 40%.")
154
 
155
  with gr.Row():
156
  with gr.Column(scale=3):
157
- text_input = gr.Textbox(label="Paste Text", lines=12, placeholder="Minimum 300 words required for analysis...")
158
  run_btn = gr.Button("Analyze", variant="primary")
159
  with gr.Column(scale=1):
160
  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."
 
116
 
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:
140
  label = "0 or * AI Content Detected"
141
  display_score = "*"
142
 
 
143
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
144
  return label, display_score, highlighted_html, df
145
 
 
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")