VictorM-Coder commited on
Commit
6f4d839
Β·
verified Β·
1 Parent(s): 237d929

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -47
app.py CHANGED
@@ -9,18 +9,27 @@ import gradio as gr
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)
@@ -42,7 +51,6 @@ def split_preserving_structure(text):
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]
@@ -55,49 +63,46 @@ def split_preserving_structure(text):
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>")
@@ -106,33 +111,26 @@ def analyze(text):
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
 
@@ -141,21 +139,21 @@ def analyze(text):
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
 
 
9
  # MODEL INITIALIZATION
10
  # -----------------------------
11
  MODEL_NAME = "fakespot-ai/roberta-base-ai-text-detection-v1"
12
+
13
+ # Global placeholders for lazy loading (prevents startup crashes)
14
+ tokenizer = None
15
+ model = None
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
17
 
18
+ def get_model():
19
+ global tokenizer, model
20
+ if model is None:
21
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
22
+ # 2026 Check: Use bfloat16 for modern GPU efficiency
23
+ dtype = torch.bfloat16 if (device.type == "cuda" and torch.cuda.is_bf16_supported()) else torch.float32
24
+ model = AutoModelForSequenceClassification.from_pretrained(
25
+ MODEL_NAME, torch_dtype=dtype
26
+ ).to(device).eval()
27
+ return tokenizer, model
28
 
29
+ THRESHOLD = 0.75
30
 
31
  # -----------------------------
32
+ # PROTECT STRUCTURE
33
  # -----------------------------
34
  ABBR = ["e.g", "i.e", "mr", "mrs", "ms", "dr", "prof", "vs", "etc", "fig", "al", "jr", "sr", "st", "inc", "ltd", "u.s", "u.k"]
35
  ABBR_REGEX = re.compile(r"\b(" + "|".join(map(re.escape, ABBR)) + r")\.", re.IGNORECASE)
 
51
  final_blocks.append(block)
52
  else:
53
  protected = _protect(block)
 
54
  parts = re.split(r"([.?!])(\s+)", protected)
55
  for i in range(0, len(parts), 3):
56
  sentence = parts[i]
 
63
  return final_blocks
64
 
65
  # -----------------------------
66
+ # IMPROVED DETECTION LOGIC
67
  # -----------------------------
68
  @torch.inference_mode()
69
  def analyze(text):
70
  if not text.strip():
71
  return "β€”", "β€”", "<em>Please enter text...</em>", None
72
 
73
+ try:
74
+ tok, mod = get_model()
75
+ except Exception as e:
76
+ return "ERROR", "0%", f"Failed to load model: {str(e)}", None
77
+
78
  blocks = split_preserving_structure(text)
 
79
  pure_sents = [b for b in blocks if b.strip() and not b.startswith("\n")]
80
 
81
  if not pure_sents:
82
  return "β€”", "β€”", "<em>No sentences detected.</em>", None
83
 
84
+ # Sliding Window (Size 3, Stride 1) - Much more accurate than hard chunks
 
85
  windows = []
86
  for i in range(len(pure_sents)):
87
  start = max(0, i - 1)
88
  end = min(len(pure_sents), i + 2)
89
  windows.append(" ".join(pure_sents[start:end]))
90
 
91
+ inputs = tok(windows, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
92
+ logits = mod(**inputs).logits
93
+ # Ensure stable math with .float() before softmax
94
+ probs = F.softmax(logits.float(), dim=-1)[:, 1].cpu().numpy().tolist()
 
 
 
 
95
 
96
+ # Weighted Scoring: Long sentences have more impact than "Hello."
 
97
  lengths = [len(s.split()) for s in pure_sents]
98
  total_words = sum(lengths)
99
+ weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
100
 
101
  # -----------------------------
102
+ # HTML RECONSTRUCTION
103
  # -----------------------------
104
+ highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
105
  sent_ptr = 0
 
106
  for block in blocks:
107
  if block.startswith("\n"):
108
  highlighted_html += block.replace("\n", "<br>")
 
111
  highlighted_html += block
112
  continue
113
 
114
+ score = probs[sent_ptr]
115
  sent_ptr += 1
116
 
117
+ # Define colors based on score
118
+ if score < 0.35:
119
+ color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
 
120
  elif score < 0.70:
121
+ color, bg = "#b8860b", "rgba(184, 134, 11, 0.15)"
 
122
  else:
123
+ color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)"
 
124
 
125
  highlighted_html += (
126
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
127
  f"title='AI Probability: {score:.1%}'>"
128
+ f"<b style='color:{color}; font-size: 0.8em;'>[{score:.0%}]</b> {block}</span>"
129
  )
130
  highlighted_html += "</div>"
131
 
132
+ label = "πŸ€– Likely AI" if weighted_avg >= THRESHOLD else "πŸ§’ Likely Human"
133
+ df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
 
 
 
 
134
 
135
  return label, f"{weighted_avg:.1%}", highlighted_html, df
136
 
 
139
  # -----------------------------
140
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
141
  gr.Markdown("## πŸ•΅οΈ AI Detector Pro")
142
+ gr.Markdown("Sentence-level analysis with weighted context windows.")
143
 
144
  with gr.Row():
145
  with gr.Column(scale=3):
146
  text_input = gr.Textbox(label="Paste Text", lines=12)
147
+ run_btn = gr.Button("Analyze", variant="primary")
148
  with gr.Column(scale=1):
149
  verdict_out = gr.Label(label="Verdict")
150
  score_out = gr.Label(label="Weighted AI Score")
151
 
152
  with gr.Tabs():
153
+ with gr.TabItem("Visual Heatmap"):
154
  html_out = gr.HTML()
155
+ with gr.TabItem("Raw Data Breakdown"):
156
+ table_out = gr.Dataframe(headers=["Sentence", "AI Confidence"], wrap=True)
157
 
158
  run_btn.click(analyze, inputs=text_input, outputs=[verdict_out, score_out, html_out, table_out])
159