VictorM-Coder commited on
Commit
f1ded77
·
verified ·
1 Parent(s): 4323dd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
app.py CHANGED
@@ -8,7 +8,7 @@ import gradio as gr
8
  # -----------------------------
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,21 +16,20 @@ 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
- # UPDATED THRESHOLD: Only 81% and above is flagged as AI
30
- THRESHOLD = 0.81
31
 
32
  # -----------------------------
33
- # PROTECT STRUCTURE
34
  # -----------------------------
35
  ABBR = ["e.g", "i.e", "mr", "mrs", "ms", "dr", "prof", "vs", "etc", "fig", "al", "jr", "sr", "st", "inc", "ltd", "u.s", "u.k"]
36
  ABBR_REGEX = re.compile(r"\b(" + "|".join(map(re.escape, ABBR)) + r")\.", re.IGNORECASE)
@@ -73,6 +72,7 @@ def analyze(text):
73
  return "—", "—", "<em>Please enter text...</em>", None
74
 
75
  word_count = len(text.split())
 
76
  if word_count < 300:
77
  warning_msg = f"⚠️ <b>Insufficient Text:</b> Your input has {word_count} words. Please enter at least 300 words for an accurate analysis."
78
  return "Too Short", "N/A", f"<div style='color: #b80d0d; padding: 20px; border: 1px solid #b80d0d; border-radius: 8px;'>{warning_msg}</div>", None
@@ -96,8 +96,8 @@ def analyze(text):
96
  windows.append(" ".join(pure_sents[start:end]))
97
 
98
  inputs = tok(windows, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
99
- logits = mod(**inputs).logits
100
- probs = F.softmax(logits.float(), dim=-1)[:, 1].cpu().numpy().tolist()
101
 
102
  lengths = [len(s.split()) for s in pure_sents]
103
  total_words = sum(lengths)
@@ -116,8 +116,7 @@ def analyze(text):
116
 
117
  if i in prob_map:
118
  score = prob_map[i]
119
-
120
- # Logic: Red for > 0.81, Green for everything else (<= 0.81)
121
  if score >= THRESHOLD:
122
  color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)" # RED
123
  else:
@@ -125,38 +124,33 @@ def analyze(text):
125
 
126
  highlighted_html += (
127
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
128
- f"title='AI Probability: {score:.1%}'>"
129
- f"<b style='color:{color}; font-size: 0.8em;'>[{score:.0%}]</b> {block}</span>"
130
  )
131
  else:
132
  highlighted_html += block
133
  highlighted_html += "</div>"
134
 
135
- # --- FINAL VERDICT ---
136
- if weighted_avg >= THRESHOLD:
137
- label = f"{weighted_avg:.0%} AI Content Detected"
138
- display_score = f"{weighted_avg:.1%}"
139
- else:
140
- label = "0 or * AI Content Detected"
141
- display_score = "*"
142
 
143
- df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
144
  return label, display_score, highlighted_html, df
145
 
146
  # -----------------------------
147
  # GRADIO INTERFACE
148
  # -----------------------------
149
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
150
- gr.Markdown("## 🕵️ AI Detector Pro")
151
- gr.Markdown(f"Strict Analysis. Threshold: **{THRESHOLD*100:.0f}%**. Everything below this is considered Human.")
152
 
153
  with gr.Row():
154
  with gr.Column(scale=3):
155
- text_input = gr.Textbox(label="Paste Text", lines=12, placeholder="Minimum 300 words...")
156
  run_btn = gr.Button("Analyze", variant="primary")
157
  with gr.Column(scale=1):
158
- verdict_out = gr.Label(label="Verdict")
159
- score_out = gr.Label(label="Weighted AI Score")
160
 
161
  with gr.Tabs():
162
  with gr.TabItem("Visual Heatmap"):
 
8
  # -----------------------------
9
  # MODEL INITIALIZATION
10
  # -----------------------------
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")
 
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, use_fast=False)
 
 
 
21
  model = AutoModelForSequenceClassification.from_pretrained(
22
+ MODEL_NAME,
23
+ num_labels=1,
24
+ ignore_mismatched_sizes=True
25
  ).to(device).eval()
26
  return tokenizer, model
27
 
28
+ # UPDATED: Threshold changed from 0.81 to 0.59
29
+ THRESHOLD = 0.59
30
 
31
  # -----------------------------
32
+ # UTILITIES
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)
 
72
  return "—", "—", "<em>Please enter text...</em>", None
73
 
74
  word_count = len(text.split())
75
+ # Word count remains at 300
76
  if word_count < 300:
77
  warning_msg = f"⚠️ <b>Insufficient Text:</b> Your input has {word_count} words. Please enter at least 300 words for an accurate analysis."
78
  return "Too Short", "N/A", f"<div style='color: #b80d0d; padding: 20px; border: 1px solid #b80d0d; border-radius: 8px;'>{warning_msg}</div>", None
 
96
  windows.append(" ".join(pure_sents[start:end]))
97
 
98
  inputs = tok(windows, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
99
+ output = mod(**inputs)
100
+ probs = torch.sigmoid(output.logits).cpu().numpy().flatten().tolist()
101
 
102
  lengths = [len(s.split()) for s in pure_sents]
103
  total_words = sum(lengths)
 
116
 
117
  if i in prob_map:
118
  score = prob_map[i]
119
+ # Logic now uses the 0.59 threshold
 
120
  if score >= THRESHOLD:
121
  color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)" # RED
122
  else:
 
124
 
125
  highlighted_html += (
126
  f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
127
+ f"title='Raw Model Score: {score:.4f}'>"
128
+ f"<b style='color:{color}; font-size: 0.8em;'>[{score:.1%}]</b> {block}</span>"
129
  )
130
  else:
131
  highlighted_html += block
132
  highlighted_html += "</div>"
133
 
134
+ label = f"{weighted_avg:.1%} AI Probability"
135
+ display_score = f"{weighted_avg:.2%}"
 
 
 
 
 
136
 
137
+ df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.2%}" for p in probs]})
138
  return label, display_score, highlighted_html, df
139
 
140
  # -----------------------------
141
  # GRADIO INTERFACE
142
  # -----------------------------
143
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
144
+ gr.Markdown("## 🕵️ AI Detector Pro: Raw Mode")
145
+ gr.Markdown(f"Direct model output from **{MODEL_NAME}**. Visual highlight now triggers at **{THRESHOLD*100:.0f}%**.")
146
 
147
  with gr.Row():
148
  with gr.Column(scale=3):
149
+ text_input = gr.Textbox(label="Paste Text", lines=12, placeholder="Min 300 words...")
150
  run_btn = gr.Button("Analyze", variant="primary")
151
  with gr.Column(scale=1):
152
+ verdict_out = gr.Label(label="Model Verdict (Raw)")
153
+ score_out = gr.Label(label="Exact Weighted Probability")
154
 
155
  with gr.Tabs():
156
  with gr.TabItem("Visual Heatmap"):