VictorM-Coder commited on
Commit
7e7e4c6
·
verified ·
1 Parent(s): 6a9422a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -26,7 +26,6 @@ def get_model():
26
  ).to(device).eval()
27
  return tokenizer, model
28
 
29
- # Threshold for Verdict Label (Updated to match your 40% logic)
30
  THRESHOLD = 0.41
31
 
32
  # -----------------------------
@@ -68,8 +67,17 @@ def split_preserving_structure(text):
68
  # -----------------------------
69
  @torch.inference_mode()
70
  def analyze(text):
71
- if not text.strip():
 
 
72
  return "—", "—", "<em>Please enter text...</em>", None
 
 
 
 
 
 
 
73
  try:
74
  tok, mod = get_model()
75
  except Exception as e:
@@ -109,7 +117,6 @@ def analyze(text):
109
 
110
  if i in prob_map:
111
  score = prob_map[i]
112
- # Heatmap logic: < 60% is Human (Green)
113
  if score < 0.60:
114
  color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
115
  elif score < 0.80:
@@ -126,7 +133,7 @@ def analyze(text):
126
  highlighted_html += block
127
  highlighted_html += "</div>"
128
 
129
- # --- UPDATED LOGIC PER REQUEST ---
130
  if weighted_avg > 0.40:
131
  label = f"{weighted_avg:.0%} AI Content Detected"
132
  display_score = f"{weighted_avg:.1%}"
@@ -134,6 +141,7 @@ def analyze(text):
134
  label = "0 or * AI Content Detected"
135
  display_score = "*"
136
 
 
137
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
138
  return label, display_score, highlighted_html, df
139
 
@@ -142,11 +150,11 @@ def analyze(text):
142
  # -----------------------------
143
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
144
  gr.Markdown("## 🕵️ AI Detector Pro")
145
- gr.Markdown("Sentence-level analysis. **Score is hidden (*) if at or below 40%.**")
146
 
147
  with gr.Row():
148
  with gr.Column(scale=3):
149
- text_input = gr.Textbox(label="Paste Text", lines=12)
150
  run_btn = gr.Button("Analyze", variant="primary")
151
  with gr.Column(scale=1):
152
  verdict_out = gr.Label(label="Verdict")
 
26
  ).to(device).eval()
27
  return tokenizer, model
28
 
 
29
  THRESHOLD = 0.41
30
 
31
  # -----------------------------
 
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."
79
+ return "Too Short", "N/A", f"<div style='color: #b80d0d; padding: 20px; border: 1px solid #b80d0d; border-radius: 8px;'>{warning_msg}</div>", None
80
+
81
  try:
82
  tok, mod = get_model()
83
  except Exception as e:
 
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:
 
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%}"
 
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
  # -----------------------------
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")