VictorM-Coder commited on
Commit
8b36ef6
·
verified ·
1 Parent(s): 300e79a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -45
app.py CHANGED
@@ -11,23 +11,21 @@ import gradio as gr
11
  MODEL_NAME = "desklib/ai-text-detector-v1.01"
12
  tokenizer = None
13
  model = None
14
- # Check device once and keep it
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
17
  def get_model():
18
  global tokenizer, model
19
  if model is None:
20
- print(f"🚀 Loading Model: {MODEL_NAME} on {device}")
21
- # Added: local_files_only=False (checks cache first), low_cpu_mem_usage=True
22
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
23
  model = AutoModelForSequenceClassification.from_pretrained(
24
  MODEL_NAME,
25
- low_cpu_mem_usage=True,
26
  ignore_mismatched_sizes=True
27
  ).to(device).eval()
28
  return tokenizer, model
29
 
30
- THRESHOLD = 0.81
31
 
32
  # -----------------------------
33
  # UTILITIES
@@ -45,11 +43,9 @@ def _restore(text):
45
  return text.replace("⟨ABBRDOT⟩", ".").replace("⟨DECIMAL⟩", ".").replace("⟨ELLIPSIS⟩", "...")
46
 
47
  def split_preserving_structure(text):
48
- # Regex splitting can be slow on massive blocks; simplified slightly for speed
49
  blocks = re.split(r"(\n+)", text)
50
  final_blocks = []
51
  for block in blocks:
52
- if not block: continue
53
  if block.startswith("\n"):
54
  final_blocks.append(block)
55
  else:
@@ -57,11 +53,12 @@ def split_preserving_structure(text):
57
  parts = re.split(r"([.?!])(\s+)", protected)
58
  for i in range(0, len(parts), 3):
59
  sentence = parts[i]
60
- if not sentence.strip(): continue
61
  punct = parts[i+1] if i+1 < len(parts) else ""
62
  space = parts[i+2] if i+2 < len(parts) else ""
63
- final_blocks.append(_restore(sentence + punct))
64
- if space: final_blocks.append(space)
 
 
65
  return final_blocks
66
 
67
  # -----------------------------
@@ -69,76 +66,87 @@ def split_preserving_structure(text):
69
  # -----------------------------
70
  @torch.inference_mode()
71
  def analyze(text):
72
- if not text or not text.strip():
 
73
  return "—", "—", "<em>Please enter text...</em>", None
74
 
75
  word_count = len(text.split())
76
- if word_count < 100: # Lowered further for quick testing
77
- return "Too Short", "N/A", f"⚠️ Text too short ({word_count} words).", None
 
 
 
 
 
 
78
 
79
- tok, mod = get_model()
80
  blocks = split_preserving_structure(text)
81
  pure_sents_indices = [i for i, b in enumerate(blocks) if b.strip() and not b.startswith("\n")]
82
  pure_sents = [blocks[i] for i in pure_sents_indices]
83
 
84
  if not pure_sents:
85
- return "—", "—", "No sentences found.", None
86
 
87
- # Windows creation
88
  windows = []
89
  for i in range(len(pure_sents)):
90
  start = max(0, i - 1)
91
  end = min(len(pure_sents), i + 2)
92
  windows.append(" ".join(pure_sents[start:end]))
93
 
94
- # --- SPEED OPTIMIZATION: BATCHING & FP16 ---
95
- # Increased batch size to 16 for better throughput
96
- batch_size = 16
97
  probs = []
98
-
99
  for i in range(0, len(windows), batch_size):
100
  batch = windows[i : i + batch_size]
101
- # Added: truncation=True and max_length=512 is crucial for speed
102
  inputs = tok(batch, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
103
-
104
  output = mod(**inputs)
105
 
 
106
  if output.logits.shape[1] > 1:
107
- batch_probs = F.softmax(output.logits, dim=-1)[:, 1].cpu().tolist()
 
108
  else:
109
- batch_probs = torch.sigmoid(output.logits).cpu().flatten().tolist()
110
  probs.extend(batch_probs)
111
 
112
- # Weights
113
  lengths = [len(s.split()) for s in pure_sents]
114
  total_words = sum(lengths)
115
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
116
 
117
  # -----------------------------
118
- # UI RECONSTRUCTION (Optimized)
119
  # -----------------------------
120
- highlighted_html = "<div style='font-family: sans-serif; line-height: 1.6;'>"
121
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
122
 
123
  for i, block in enumerate(blocks):
124
- if block.startswith("\n"):
125
- highlighted_html += "<br>" * len(block)
126
  continue
127
 
128
  if i in prob_map:
129
  score = prob_map[i]
130
- color = "#d32f2f" if score >= THRESHOLD else "#2e7d32"
131
- bg = "rgba(211, 47, 47, 0.1)" if score >= THRESHOLD else "transparent"
 
 
 
 
 
132
 
133
  highlighted_html += (
134
- f"<span style='background:{bg}; border-bottom: 2px solid {color}55;' "
135
- f"title='{score:.1%} AI'>{block}</span>"
 
 
136
  )
137
  else:
138
  highlighted_html += block
139
  highlighted_html += "</div>"
140
 
141
- label = f"{weighted_avg:.1%} AI"
142
  display_score = f"{weighted_avg:.2%}"
143
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.2%}" for p in probs]})
144
 
@@ -147,23 +155,32 @@ def analyze(text):
147
  # -----------------------------
148
  # GRADIO INTERFACE
149
  # -----------------------------
150
- with gr.Blocks(theme=gr.themes.Default()) as demo:
151
- gr.Markdown("# 🚀 Fast AI Detector")
 
 
152
  with gr.Row():
153
- with gr.Column(scale=2):
154
- text_input = gr.Textbox(label="Input", lines=10)
155
- run_btn = gr.Button("Analyze", variant="primary")
 
 
 
156
  with gr.Column(scale=1):
157
- verdict_out = gr.Label(label="Verdict")
158
- score_out = gr.Label(label="Score")
 
 
 
159
 
160
  with gr.Tabs():
161
- with gr.TabItem("Heatmap"):
162
- html_out = gr.HTML()
163
- with gr.TabItem("Data"):
164
- table_out = gr.Dataframe()
165
 
166
  run_btn.click(analyze, inputs=text_input, outputs=[verdict_out, score_out, html_out, table_out])
 
167
 
168
  if __name__ == "__main__":
169
  demo.launch()
 
11
  MODEL_NAME = "desklib/ai-text-detector-v1.01"
12
  tokenizer = None
13
  model = None
 
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
 
16
  def get_model():
17
  global tokenizer, model
18
  if model is None:
19
+ print(f"Loading Specialized Model: {MODEL_NAME} on {device}")
 
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
21
+ # Load with default labels; if the model has 2 (Human/AI), we handle it in analyze()
22
  model = AutoModelForSequenceClassification.from_pretrained(
23
  MODEL_NAME,
 
24
  ignore_mismatched_sizes=True
25
  ).to(device).eval()
26
  return tokenizer, model
27
 
28
+ THRESHOLD = 0.59
29
 
30
  # -----------------------------
31
  # UTILITIES
 
43
  return text.replace("⟨ABBRDOT⟩", ".").replace("⟨DECIMAL⟩", ".").replace("⟨ELLIPSIS⟩", "...")
44
 
45
  def split_preserving_structure(text):
 
46
  blocks = re.split(r"(\n+)", text)
47
  final_blocks = []
48
  for block in blocks:
 
49
  if block.startswith("\n"):
50
  final_blocks.append(block)
51
  else:
 
53
  parts = re.split(r"([.?!])(\s+)", protected)
54
  for i in range(0, len(parts), 3):
55
  sentence = parts[i]
 
56
  punct = parts[i+1] if i+1 < len(parts) else ""
57
  space = parts[i+2] if i+2 < len(parts) else ""
58
+ if sentence.strip():
59
+ final_blocks.append(_restore(sentence + punct))
60
+ if space:
61
+ final_blocks.append(space)
62
  return final_blocks
63
 
64
  # -----------------------------
 
66
  # -----------------------------
67
  @torch.inference_mode()
68
  def analyze(text):
69
+ text = text.strip()
70
+ if not text:
71
  return "—", "—", "<em>Please enter text...</em>", None
72
 
73
  word_count = len(text.split())
74
+ if word_count < 300: # Slightly lowered for testing flexibility
75
+ warning_msg = f"⚠️ <b>Insufficient Text:</b> Your input has {word_count} words. Please enter at least 250-300 words for accurate results."
76
+ return "Too Short", "N/A", f"<div style='color: #b80d0d; padding: 20px; border: 1px solid #b80d0d; border-radius: 8px;'>{warning_msg}</div>", None
77
+
78
+ try:
79
+ tok, mod = get_model()
80
+ except Exception as e:
81
+ return "ERROR", "0%", f"Failed to load model: {str(e)}", None
82
 
 
83
  blocks = split_preserving_structure(text)
84
  pure_sents_indices = [i for i, b in enumerate(blocks) if b.strip() and not b.startswith("\n")]
85
  pure_sents = [blocks[i] for i in pure_sents_indices]
86
 
87
  if not pure_sents:
88
+ return "—", "—", "<em>No sentences detected.</em>", None
89
 
90
+ # Sliding Window Generation (Context of 3 sentences)
91
  windows = []
92
  for i in range(len(pure_sents)):
93
  start = max(0, i - 1)
94
  end = min(len(pure_sents), i + 2)
95
  windows.append(" ".join(pure_sents[start:end]))
96
 
97
+ # --- BATCHED INFERENCE (Prevents OOM) ---
98
+ batch_size = 8
 
99
  probs = []
 
100
  for i in range(0, len(windows), batch_size):
101
  batch = windows[i : i + batch_size]
 
102
  inputs = tok(batch, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
 
103
  output = mod(**inputs)
104
 
105
+ # Check if model is binary classification (2 labels) or regression (1 label)
106
  if output.logits.shape[1] > 1:
107
+ # Assumes Label 1 is 'AI'
108
+ batch_probs = F.softmax(output.logits, dim=-1)[:, 1].cpu().numpy().tolist()
109
  else:
110
+ batch_probs = torch.sigmoid(output.logits).cpu().numpy().flatten().tolist()
111
  probs.extend(batch_probs)
112
 
113
+ # Calculation for Final Score
114
  lengths = [len(s.split()) for s in pure_sents]
115
  total_words = sum(lengths)
116
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
117
 
118
  # -----------------------------
119
+ # HTML RECONSTRUCTION
120
  # -----------------------------
121
+ highlighted_html = "<div style='font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif; line-height: 1.8;'>"
122
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
123
 
124
  for i, block in enumerate(blocks):
125
+ if block.startswith("\n") or block.isspace():
126
+ highlighted_html += block.replace("\n", "<br>")
127
  continue
128
 
129
  if i in prob_map:
130
  score = prob_map[i]
131
+ # Color logic based on Threshold
132
+ if score >= THRESHOLD:
133
+ color, bg = "#d32f2f", "rgba(211, 47, 47, 0.12)" # Soft Red
134
+ border = "2px solid #d32f2f"
135
+ else:
136
+ color, bg = "#2e7d32", "rgba(46, 125, 50, 0.08)" # Soft Green
137
+ border = "1px solid transparent"
138
 
139
  highlighted_html += (
140
+ f"<span style='background:{bg}; padding:1px 2px; border-radius:3px; border-bottom: {border}; cursor: help;' "
141
+ f"title='AI Confidence: {score:.2%}'>"
142
+ f"<span style='color:{color}; font-weight: bold; font-size: 0.75em; vertical-align: super; margin-right: 2px;'>{score:.0%}</span>"
143
+ f"{block}</span>"
144
  )
145
  else:
146
  highlighted_html += block
147
  highlighted_html += "</div>"
148
 
149
+ label = f"{weighted_avg:.1%} AI Written"
150
  display_score = f"{weighted_avg:.2%}"
151
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.2%}" for p in probs]})
152
 
 
155
  # -----------------------------
156
  # GRADIO INTERFACE
157
  # -----------------------------
158
+ with gr.Blocks(theme=gr.themes.Soft(), title="AI Detector Pro") as demo:
159
+ gr.Markdown("# 🕵️ AI Detector Pro")
160
+ gr.Markdown(f"Utilizing **{MODEL_NAME}**. Values above **{THRESHOLD*100:.0f}%** are flagged as highly likely AI.")
161
+
162
  with gr.Row():
163
+ with gr.Column(scale=3):
164
+ text_input = gr.Textbox(label="Input Text", lines=15, placeholder="Paste your essay here (minimum 250 words for accuracy)...")
165
+ with gr.Row():
166
+ clear_btn = gr.Button("Clear")
167
+ run_btn = gr.Button("Analyze Text", variant="primary")
168
+
169
  with gr.Column(scale=1):
170
+ verdict_out = gr.Label(label="Global Verdict")
171
+ score_out = gr.Label(label="Weighted Probability")
172
+ gr.Markdown("---")
173
+ gr.Markdown("### How to read:")
174
+ gr.Markdown("- **Red Highlight:** High AI probability\n- **Green Highlight:** Likely Human\n- **Super-script:** Exact sentence-level AI score")
175
 
176
  with gr.Tabs():
177
+ with gr.TabItem("Visual Heatmap"):
178
+ html_out = gr.HTML(label="Heatmap")
179
+ with gr.TabItem("Data Breakdown"):
180
+ table_out = gr.Dataframe(headers=["Sentence", "AI Confidence"], wrap=True)
181
 
182
  run_btn.click(analyze, inputs=text_input, outputs=[verdict_out, score_out, html_out, table_out])
183
+ clear_btn.click(lambda: ["", "", "", "", None], outputs=[text_input, verdict_out, score_out, html_out, table_out])
184
 
185
  if __name__ == "__main__":
186
  demo.launch()