VictorM-Coder commited on
Commit
c9da6e8
·
verified ·
1 Parent(s): 9dbe641

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -9,7 +9,6 @@ import gradio as gr
9
  # MODEL INITIALIZATION
10
  # -----------------------------
11
  MODEL_NAME = "fakespot-ai/roberta-base-ai-text-detection-v1"
12
-
13
  tokenizer = None
14
  model = None
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -17,21 +16,18 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
  def get_model():
18
  global tokenizer, model
19
  if model is None:
20
- # Loading messages to help debug in HF logs
21
  print(f"Loading model: {MODEL_NAME} on {device}")
22
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
23
-
24
- # Use float32 on CPU to prevent build-time precision hangs
25
  dtype = torch.float32
26
  if device.type == "cuda" and torch.cuda.is_bf16_supported():
27
  dtype = torch.bfloat16
28
-
29
  model = AutoModelForSequenceClassification.from_pretrained(
30
  MODEL_NAME, torch_dtype=dtype
31
  ).to(device).eval()
32
  return tokenizer, model
33
 
34
- THRESHOLD = 0.20
 
35
 
36
  # -----------------------------
37
  # PROTECT STRUCTURE
@@ -82,7 +78,7 @@ def analyze(text):
82
  blocks = split_preserving_structure(text)
83
  pure_sents_indices = [i for i, b in enumerate(blocks) if b.strip() and not b.startswith("\n")]
84
  pure_sents = [blocks[i] for i in pure_sents_indices]
85
-
86
  if not pure_sents:
87
  return "—", "—", "<em>No sentences detected.</em>", None
88
 
@@ -105,7 +101,7 @@ def analyze(text):
105
  # -----------------------------
106
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
107
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
108
-
109
  for i, block in enumerate(blocks):
110
  if block.startswith("\n") or block.isspace():
111
  highlighted_html += block.replace("\n", "<br>")
@@ -113,9 +109,13 @@ def analyze(text):
113
 
114
  if i in prob_map:
115
  score = prob_map[i]
116
- if score < 0.35: color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
117
- elif score < 0.70: color, bg = "#b8860b", "rgba(184, 134, 11, 0.15)"
118
- else: color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)"
 
 
 
 
119
 
120
  highlighted_html += (
121
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
@@ -124,22 +124,25 @@ def analyze(text):
124
  )
125
  else:
126
  highlighted_html += block
127
-
128
  highlighted_html += "</div>"
129
-
130
- # Updated Label Logic per your request
 
131
  label = "AI Content Detected" if weighted_avg >= THRESHOLD else "0 or * AI Content Detected"
132
 
 
 
 
133
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
134
- return label, f"{weighted_avg:.1%}", highlighted_html, df
135
 
136
  # -----------------------------
137
  # GRADIO INTERFACE
138
  # -----------------------------
139
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
140
  gr.Markdown("## 🕵️ AI Detector Pro")
141
- gr.Markdown("Sentence-level analysis with weighted context windows.")
142
-
143
  with gr.Row():
144
  with gr.Column(scale=3):
145
  text_input = gr.Textbox(label="Paste Text", lines=12)
@@ -147,7 +150,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
147
  with gr.Column(scale=1):
148
  verdict_out = gr.Label(label="Verdict")
149
  score_out = gr.Label(label="Weighted AI Score")
150
-
151
  with gr.Tabs():
152
  with gr.TabItem("Visual Heatmap"):
153
  html_out = gr.HTML()
 
9
  # MODEL INITIALIZATION
10
  # -----------------------------
11
  MODEL_NAME = "fakespot-ai/roberta-base-ai-text-detection-v1"
 
12
  tokenizer = None
13
  model = None
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
16
  def get_model():
17
  global tokenizer, model
18
  if model is None:
 
19
  print(f"Loading model: {MODEL_NAME} on {device}")
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
 
21
  dtype = torch.float32
22
  if device.type == "cuda" and torch.cuda.is_bf16_supported():
23
  dtype = torch.bfloat16
 
24
  model = AutoModelForSequenceClassification.from_pretrained(
25
  MODEL_NAME, torch_dtype=dtype
26
  ).to(device).eval()
27
  return tokenizer, model
28
 
29
+ # Threshold for Verdict Label
30
+ THRESHOLD = 0.60
31
 
32
  # -----------------------------
33
  # PROTECT STRUCTURE
 
78
  blocks = split_preserving_structure(text)
79
  pure_sents_indices = [i for i, b in enumerate(blocks) if b.strip() and not b.startswith("\n")]
80
  pure_sents = [blocks[i] for i in pure_sents_indices]
81
+
82
  if not pure_sents:
83
  return "—", "—", "<em>No sentences detected.</em>", None
84
 
 
101
  # -----------------------------
102
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
103
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
104
+
105
  for i, block in enumerate(blocks):
106
  if block.startswith("\n") or block.isspace():
107
  highlighted_html += block.replace("\n", "<br>")
 
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:
116
+ color, bg = "#b8860b", "rgba(184, 134, 11, 0.15)"
117
+ else:
118
+ color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)"
119
 
120
  highlighted_html += (
121
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
 
124
  )
125
  else:
126
  highlighted_html += block
 
127
  highlighted_html += "</div>"
128
+
129
+ # --- THE FIXES ---
130
+ # 1. Verdict Label Fix
131
  label = "AI Content Detected" if weighted_avg >= THRESHOLD else "0 or * AI Content Detected"
132
 
133
+ # 2. Weighted Score Fix: If below 50%, show '*'
134
+ display_score = f"{weighted_avg:.1%}" if weighted_avg >= 0.50 else "*"
135
+
136
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
137
+ return label, display_score, 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 analysis. **Green < 60% | Score masked if < 50%**")
145
+
146
  with gr.Row():
147
  with gr.Column(scale=3):
148
  text_input = gr.Textbox(label="Paste Text", lines=12)
 
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("Visual Heatmap"):
156
  html_out = gr.HTML()