VictorM-Coder commited on
Commit
d5dcb35
·
verified ·
1 Parent(s): fdd45e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -127
app.py CHANGED
@@ -6,185 +6,158 @@ import pandas as pd
6
  import gradio as gr
7
 
8
  # -----------------------------
9
- # MODEL (Fakespot 2025)
10
  # -----------------------------
11
  MODEL_NAME = "fakespot-ai/roberta-base-ai-text-detection-v1"
12
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  dtype = torch.bfloat16 if (device.type == "cuda" and torch.cuda.is_bf16_supported()) else torch.float32
15
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, dtype=dtype).to(device).eval()
16
 
17
- THRESHOLD = 0.80
 
 
 
 
18
 
19
  # -----------------------------
20
- # ABBREVIATION PROTECTION
21
  # -----------------------------
22
- ABBR = [
23
- "e.g", "i.e", "mr", "mrs", "ms", "dr", "prof", "vs", "etc",
24
- "fig", "al", "jr", "sr", "st", "no", "vol", "pp", "mt",
25
- "inc", "ltd", "co", "u.s", "u.k", "a.m", "p.m"
26
- ]
27
-
28
  ABBR_REGEX = re.compile(r"\b(" + "|".join(map(re.escape, ABBR)) + r")\.", re.IGNORECASE)
29
 
30
-
31
  def _protect(text):
32
  text = text.replace("...", "⟨ELLIPSIS⟩")
33
  text = re.sub(r"(?<=\d)\.(?=\d)", "⟨DECIMAL⟩", text)
34
  text = ABBR_REGEX.sub(r"\1⟨ABBRDOT⟩", text)
35
  return text
36
 
37
-
38
  def _restore(text):
39
- return (
40
- text.replace("⟨ABBRDOT⟩", ".")
41
- .replace("⟨DECIMAL⟩", ".")
42
- .replace("⟨ELLIPSIS⟩", "...")
43
- )
44
-
45
 
46
- # -----------------------------
47
- # PERFECT PARAGRAPH-PRESERVING SPLITTER
48
- # -----------------------------
49
  def split_preserving_structure(text):
50
- blocks = re.split(r"(\n+)", text) # keep newline separators
51
  final_blocks = []
52
-
53
  for block in blocks:
54
  if block.startswith("\n"):
55
  final_blocks.append(block)
56
  else:
57
  protected = _protect(block)
 
58
  parts = re.split(r"([.?!])(\s+)", protected)
59
-
60
  for i in range(0, len(parts), 3):
61
  sentence = parts[i]
62
- punct = parts[i + 1] if i + 1 < len(parts) else ""
63
- space = parts[i + 2] if i + 2 < len(parts) else ""
64
-
65
- whole = sentence + punct
66
- if whole.strip():
67
- final_blocks.append(_restore(whole))
68
  if space:
69
  final_blocks.append(space)
70
-
71
  return final_blocks
72
 
73
-
74
- def extract_sentences_only(blocks):
75
- return [
76
- b for b in blocks
77
- if b.strip() != "" and not b.startswith("\n") and not b.isspace()
78
- ]
79
-
80
-
81
  # -----------------------------
82
- # GROUPING
83
  # -----------------------------
84
- def group_sentences(sents, size=3):
85
- return [" ".join(sents[i:i + size]) for i in range(0, len(sents), size)]
86
-
87
-
88
- # -----------------------------
89
- # ANALYSIS LOGIC
90
- # -----------------------------
91
- def analyze(text, max_len=512):
92
 
93
  blocks = split_preserving_structure(text)
94
- pure_sentences = extract_sentences_only(blocks)
95
-
96
- if not pure_sentences:
97
- return "—", "—", "<em>Paste text to analyze.</em>", None
98
-
99
- # Group into 3-sentence windows
100
- grouped = group_sentences(pure_sentences, 3)
101
- clean_grouped = [re.sub(r"\s+", " ", g).strip() for g in grouped]
102
-
103
- # Model forward pass
104
- inputs = tokenizer(clean_grouped, return_tensors="pt",
105
- padding=True, truncation=True,
106
- max_length=max_len).to(device)
107
-
108
- with torch.no_grad():
109
- logits = model(**inputs).logits
110
-
111
- chunk_probs = F.softmax(logits, dim=-1)[:, 1].cpu().tolist()
112
-
113
- # expand back
114
- ai_scores = []
115
- for idx, prob in enumerate(chunk_probs):
116
- start = idx * 3
117
- end = min(start + 3, len(pure_sentences))
118
- for _ in range(start, end):
119
- ai_scores.append(prob)
 
 
120
 
121
  # -----------------------------
122
- # RECONSTRUCTION WITH HIGHLIGHT
123
  # -----------------------------
124
- highlighted = ""
125
- sentence_index = 0
126
-
127
  for block in blocks:
128
  if block.startswith("\n"):
129
- highlighted += block
130
  continue
131
-
132
  if block.isspace():
133
- highlighted += block
134
  continue
135
-
136
- # safety
137
- if sentence_index >= len(ai_scores):
138
- ai_p = ai_scores[-1]
139
- else:
140
- ai_p = ai_scores[sentence_index]
141
- sentence_index += 1
142
-
143
- pct = f"{ai_p * 100:.1f}%"
144
-
145
- if ai_p < 0.30:
146
- color = "#11823b"
147
- elif ai_p < 0.70:
148
- color = "#b8860b"
149
  else:
150
- color = "#b80d0d"
 
151
 
152
- highlighted += (
153
- f"<span style='background:rgba(0,0,0,0.03); padding:3px 4px; "
154
- f"border-radius:4px;'><strong style='color:{color}'>[{pct}]</strong> "
155
- f"{block.strip()}</span> "
156
  )
 
157
 
158
- # -----------------------------
159
- # OVERALL SCORE
160
- # -----------------------------
161
- overall = sum(ai_scores) / len(ai_scores)
162
- overall_pct = f"{overall * 100:.1f}%"
163
- overall_label = "🤖 Likely AI Written" if overall >= THRESHOLD else "🧒 Likely Human Written"
164
-
165
- df = pd.DataFrame(
166
- [[i + 1, s, ai_scores[i]] for i, s in enumerate(pure_sentences)],
167
- columns=["#", "Sentence", "AI_Prob"]
168
- )
169
-
170
- return overall_label, overall_pct, highlighted, df
171
 
 
172
 
173
  # -----------------------------
174
- # UI
175
  # -----------------------------
176
- with gr.Blocks() as demo:
177
- gr.Markdown("### 🕵️ AI Sentence-Level Detector — Exact Structure Highlighting")
178
-
179
- text_input = gr.Textbox(label="Paste text", lines=14, placeholder="Your text…")
180
- btn = gr.Button("Analyze")
181
-
182
- verdict = gr.Label(label="Verdict (Overall)")
183
- score = gr.Label(label="AI Score")
184
- highlights = gr.HTML(label="Highlighted Text (Exact Structure)")
185
- table = gr.Dataframe(headers=["#", "Sentence", "AI_Prob"], wrap=True)
186
-
187
- btn.click(analyze, inputs=[text_input], outputs=[verdict, score, highlights, table])
 
 
 
 
 
 
 
188
 
189
  if __name__ == "__main__":
190
- demo.launch()
 
6
  import gradio as gr
7
 
8
  # -----------------------------
9
+ # MODEL INITIALIZATION
10
  # -----------------------------
11
  MODEL_NAME = "fakespot-ai/roberta-base-ai-text-detection-v1"
12
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
  dtype = torch.bfloat16 if (device.type == "cuda" and torch.cuda.is_bf16_supported()) else torch.float32
 
15
 
16
+ model = AutoModelForSequenceClassification.from_pretrained(
17
+ MODEL_NAME, torch_dtype=dtype
18
+ ).to(device).eval()
19
+
20
+ THRESHOLD = 0.75 # Balanced threshold for Fakespot 2025
21
 
22
  # -----------------------------
23
+ # STRUCTURAL PRESERVATION
24
  # -----------------------------
25
+ ABBR = ["e.g", "i.e", "mr", "mrs", "ms", "dr", "prof", "vs", "etc", "fig", "al", "jr", "sr", "st", "inc", "ltd", "u.s", "u.k"]
 
 
 
 
 
26
  ABBR_REGEX = re.compile(r"\b(" + "|".join(map(re.escape, ABBR)) + r")\.", re.IGNORECASE)
27
 
 
28
  def _protect(text):
29
  text = text.replace("...", "⟨ELLIPSIS⟩")
30
  text = re.sub(r"(?<=\d)\.(?=\d)", "⟨DECIMAL⟩", text)
31
  text = ABBR_REGEX.sub(r"\1⟨ABBRDOT⟩", text)
32
  return text
33
 
 
34
  def _restore(text):
35
+ return text.replace("⟨ABBRDOT⟩", ".").replace("⟨DECIMAL⟩", ".").replace("⟨ELLIPSIS⟩", "...")
 
 
 
 
 
36
 
 
 
 
37
  def split_preserving_structure(text):
38
+ blocks = re.split(r"(\n+)", text)
39
  final_blocks = []
 
40
  for block in blocks:
41
  if block.startswith("\n"):
42
  final_blocks.append(block)
43
  else:
44
  protected = _protect(block)
45
+ # Split by punctuation followed by space
46
  parts = re.split(r"([.?!])(\s+)", protected)
 
47
  for i in range(0, len(parts), 3):
48
  sentence = parts[i]
49
+ punct = parts[i+1] if i+1 < len(parts) else ""
50
+ space = parts[i+2] if i+2 < len(parts) else ""
51
+ if sentence.strip():
52
+ final_blocks.append(_restore(sentence + punct))
 
 
53
  if space:
54
  final_blocks.append(space)
 
55
  return final_blocks
56
 
 
 
 
 
 
 
 
 
57
  # -----------------------------
58
+ # ADVANCED DETECTION LOGIC
59
  # -----------------------------
60
+ @torch.inference_mode()
61
+ def analyze(text):
62
+ if not text.strip():
63
+ return "—", "—", "<em>Please enter text...</em>", None
 
 
 
 
64
 
65
  blocks = split_preserving_structure(text)
66
+ # Filter out whitespace/newlines for the model
67
+ pure_sents = [b for b in blocks if b.strip() and not b.startswith("\n")]
68
+
69
+ if not pure_sents:
70
+ return "—", "—", "<em>No sentences detected.</em>", None
71
+
72
+ # 1. Create Overlapping Context Windows (Size=3, Stride=1)
73
+ # This ensures every sentence is evaluated in multiple contexts
74
+ windows = []
75
+ for i in range(len(pure_sents)):
76
+ start = max(0, i - 1)
77
+ end = min(len(pure_sents), i + 2)
78
+ windows.append(" ".join(pure_sents[start:end]))
79
+
80
+ # 2. Batched Inference
81
+ inputs = tokenizer(windows, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
82
+ logits = model(**inputs).logits
83
+ probs = F.softmax(logits, dim=-1)[:, 1].cpu().float().numpy().tolist()
84
+
85
+ # 3. Map Scores (Average scores if a sentence appeared in multiple windows)
86
+ # In this logic, probs[i] corresponds directly to pure_sents[i] as the 'center'
87
+ ai_scores = probs
88
+
89
+ # 4. Weighted Overall Score (Length matters!)
90
+ # AI models are more confident on longer sequences.
91
+ lengths = [len(s.split()) for s in pure_sents]
92
+ total_words = sum(lengths)
93
+ weighted_avg = sum(p * l for p, l in zip(ai_scores, lengths)) / total_words if total_words > 0 else 0
94
 
95
  # -----------------------------
96
+ # HTML HIGHLIGHTING RECONSTRUCTION
97
  # -----------------------------
98
+ highlighted_html = "<div style='font-family: sans-serif; line-height: 1.6;'>"
99
+ sent_ptr = 0
100
+
101
  for block in blocks:
102
  if block.startswith("\n"):
103
+ highlighted_html += block.replace("\n", "<br>")
104
  continue
 
105
  if block.isspace():
106
+ highlighted_html += block
107
  continue
108
+
109
+ score = ai_scores[sent_ptr]
110
+ sent_ptr += 1
111
+
112
+ # Dynamic color scaling
113
+ if score < 0.30:
114
+ color = "#11823b" # Green
115
+ bg = "rgba(17, 130, 59, 0.1)"
116
+ elif score < 0.70:
117
+ color = "#b8860b" # Gold
118
+ bg = "rgba(184, 134, 11, 0.1)"
 
 
 
119
  else:
120
+ color = "#b80d0d" # Red
121
+ bg = "rgba(184, 13, 13, 0.1)"
122
 
123
+ highlighted_html += (
124
+ f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
125
+ f"title='AI Probability: {score:.1%}'>"
126
+ f"<small style='color:{color}; font-weight:bold;'>[{score:.0%}]</small> {block}</span>"
127
  )
128
+ highlighted_html += "</div>"
129
 
130
+ label = "🤖 Likely AI Written" if weighted_avg >= THRESHOLD else "🧒 Likely Human Written"
131
+
132
+ df = pd.DataFrame({
133
+ "Sentence": pure_sents,
134
+ "AI Score": [f"{p:.1%}" for p in ai_scores]
135
+ })
 
 
 
 
 
 
 
136
 
137
+ return label, f"{weighted_avg:.1%}", highlighted_html, df
138
 
139
  # -----------------------------
140
+ # GRADIO INTERFACE
141
  # -----------------------------
142
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
143
+ gr.Markdown("## 🕵️ AI Detector Pro")
144
+ gr.Markdown("Sentence-level heatmap with **Weighted Sliding Window** logic.")
145
+
146
+ with gr.Row():
147
+ with gr.Column(scale=3):
148
+ text_input = gr.Textbox(label="Paste Text", lines=12)
149
+ run_btn = gr.Button("Analyze Content", variant="primary")
150
+ with gr.Column(scale=1):
151
+ verdict_out = gr.Label(label="Verdict")
152
+ score_out = gr.Label(label="Weighted AI Score")
153
+
154
+ with gr.Tabs():
155
+ with gr.TabItem("Analysis Heatmap"):
156
+ html_out = gr.HTML()
157
+ with gr.TabItem("Detailed Data"):
158
+ table_out = gr.Dataframe(headers=["Sentence", "AI Score"], wrap=True)
159
+
160
+ run_btn.click(analyze, inputs=text_input, outputs=[verdict_out, score_out, html_out, table_out])
161
 
162
  if __name__ == "__main__":
163
+ demo.launch()