VictorM-Coder commited on
Commit
41bba56
·
verified ·
1 Parent(s): e229307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -4,6 +4,7 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
  import re
5
  import pandas as pd
6
  import gradio as gr
 
7
 
8
  # -----------------------------
9
  # MODEL INITIALIZATION
@@ -11,21 +12,30 @@ import gradio as gr
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, 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 set to 59% for visual triggers
29
  THRESHOLD = 0.59
30
 
31
  # -----------------------------
@@ -70,9 +80,8 @@ def analyze(text):
70
  text = text.strip()
71
  if not text:
72
  return "—", "—", "<em>Please enter text...</em>", None
73
-
74
  word_count = len(text.split())
75
- # Word count requirement restored to 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
@@ -104,7 +113,7 @@ def analyze(text):
104
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
105
 
106
  # -----------------------------
107
- # HTML RECONSTRUCTION (Highlights trigger at THRESHOLD)
108
  # -----------------------------
109
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
110
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
@@ -113,10 +122,9 @@ def analyze(text):
113
  if block.startswith("\n") or block.isspace():
114
  highlighted_html += block.replace("\n", "<br>")
115
  continue
116
-
117
  if i in prob_map:
118
  score = prob_map[i]
119
- # Color determined by the new 59% threshold
120
  if score >= THRESHOLD:
121
  color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)" # RED
122
  else:
@@ -129,8 +137,8 @@ def analyze(text):
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
 
@@ -142,7 +150,7 @@ def analyze(text):
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):
@@ -151,7 +159,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
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"):
157
  html_out = gr.HTML()
 
4
  import re
5
  import pandas as pd
6
  import gradio as gr
7
+ import os
8
 
9
  # -----------------------------
10
  # MODEL INITIALIZATION
 
12
  MODEL_NAME = "desklib/ai-text-detector-v1.01"
13
  tokenizer = None
14
  model = None
15
+
16
+ # Force CPU if CUDA is not properly initialized to prevent crash
17
+ if torch.cuda.is_available():
18
+ device = torch.device("cuda")
19
+ dtype = torch.float16 # Half precision for GPU speed/memory
20
+ else:
21
+ device = torch.device("cpu")
22
+ dtype = torch.float32 # Full precision for CPU stability
23
 
24
  def get_model():
25
  global tokenizer, model
26
  if model is None:
27
  print(f"Loading Specialized Model: {MODEL_NAME} on {device}")
28
+ # Added low_cpu_mem_usage to prevent Build Exit Code 1 (OOM)
29
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False)
30
  model = AutoModelForSequenceClassification.from_pretrained(
31
  MODEL_NAME,
32
  num_labels=1,
33
+ ignore_mismatched_sizes=True,
34
+ low_cpu_mem_usage=True,
35
+ torch_dtype=dtype
36
  ).to(device).eval()
37
  return tokenizer, model
38
 
 
39
  THRESHOLD = 0.59
40
 
41
  # -----------------------------
 
80
  text = text.strip()
81
  if not text:
82
  return "—", "—", "<em>Please enter text...</em>", None
83
+
84
  word_count = len(text.split())
 
85
  if word_count < 300:
86
  warning_msg = f"⚠️ <b>Insufficient Text:</b> Your input has {word_count} words. Please enter at least 300 words for an accurate analysis."
87
  return "Too Short", "N/A", f"<div style='color: #b80d0d; padding: 20px; border: 1px solid #b80d0d; border-radius: 8px;'>{warning_msg}</div>", None
 
113
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
114
 
115
  # -----------------------------
116
+ # HTML RECONSTRUCTION
117
  # -----------------------------
118
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
119
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
 
122
  if block.startswith("\n") or block.isspace():
123
  highlighted_html += block.replace("\n", "<br>")
124
  continue
125
+
126
  if i in prob_map:
127
  score = prob_map[i]
 
128
  if score >= THRESHOLD:
129
  color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)" # RED
130
  else:
 
137
  )
138
  else:
139
  highlighted_html += block
 
140
 
141
+ highlighted_html += "</div>"
142
  label = f"{weighted_avg:.1%} AI Probability"
143
  display_score = f"{weighted_avg:.2%}"
144
 
 
150
  # -----------------------------
151
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
152
  gr.Markdown("## 🕵️ AI Detector Pro: Raw Mode")
153
+ gr.Markdown(f"Visual highlight triggers at **{THRESHOLD*100:.0f}%**.")
154
 
155
  with gr.Row():
156
  with gr.Column(scale=3):
 
159
  with gr.Column(scale=1):
160
  verdict_out = gr.Label(label="Model Verdict (Raw)")
161
  score_out = gr.Label(label="Exact Weighted Probability")
162
+
163
  with gr.Tabs():
164
  with gr.TabItem("Visual Heatmap"):
165
  html_out = gr.HTML()