VictorM-Coder commited on
Commit
0a84024
·
verified ·
1 Parent(s): 5237e13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -8,7 +8,8 @@ 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 +17,24 @@ 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)
@@ -89,6 +93,7 @@ def analyze(text):
89
  if not pure_sents:
90
  return "—", "—", "<em>No sentences detected.</em>", None
91
 
 
92
  windows = []
93
  for i in range(len(pure_sents)):
94
  start = max(0, i - 1)
@@ -104,7 +109,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
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)}
@@ -117,7 +122,7 @@ def analyze(text):
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:
@@ -132,7 +137,7 @@ def analyze(text):
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%}"
@@ -147,8 +152,8 @@ def analyze(text):
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):
 
8
  # -----------------------------
9
  # MODEL INITIALIZATION
10
  # -----------------------------
11
+ # This is a DeBERTa-v3-Large model fine-tuned on the DAIGT (Student Writing vs AI) dataset.
12
+ MODEL_NAME = "Hamidreza/DeBERTa-v3-large-AI-Detector-v2"
13
  tokenizer = None
14
  model = None
15
  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
+ print(f"Loading High-Performance Model: {MODEL_NAME} on {device}")
21
+ # DeBERTa-v3 requires use_fast=False for stable SentencePiece tokenization
22
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False)
23
+
24
  dtype = torch.float32
25
  if device.type == "cuda" and torch.cuda.is_bf16_supported():
26
  dtype = torch.bfloat16
27
+
28
  model = AutoModelForSequenceClassification.from_pretrained(
29
  MODEL_NAME, torch_dtype=dtype
30
  ).to(device).eval()
31
  return tokenizer, model
32
 
33
+ # Only 81% and above is flagged as AI
34
  THRESHOLD = 0.81
35
 
36
  # -----------------------------
37
+ # PROTECT STRUCTURE (Regex)
38
  # -----------------------------
39
  ABBR = ["e.g", "i.e", "mr", "mrs", "ms", "dr", "prof", "vs", "etc", "fig", "al", "jr", "sr", "st", "inc", "ltd", "u.s", "u.k"]
40
  ABBR_REGEX = re.compile(r"\b(" + "|".join(map(re.escape, ABBR)) + r")\.", re.IGNORECASE)
 
93
  if not pure_sents:
94
  return "—", "—", "<em>No sentences detected.</em>", None
95
 
96
+ # Sliding window inference (Contextual)
97
  windows = []
98
  for i in range(len(pure_sents)):
99
  start = max(0, i - 1)
 
109
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
110
 
111
  # -----------------------------
112
+ # HTML RECONSTRUCTION (Strict Binary)
113
  # -----------------------------
114
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
115
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
 
122
  if i in prob_map:
123
  score = prob_map[i]
124
 
125
+ # Binary logic: Threshold applied to color
126
  if score >= THRESHOLD:
127
  color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)" # RED
128
  else:
 
137
  highlighted_html += block
138
  highlighted_html += "</div>"
139
 
140
+ # --- FINAL VERDICT (Masking below 81%) ---
141
  if weighted_avg >= THRESHOLD:
142
  label = f"{weighted_avg:.0%} AI Content Detected"
143
  display_score = f"{weighted_avg:.1%}"
 
152
  # GRADIO INTERFACE
153
  # -----------------------------
154
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
155
+ gr.Markdown("## 🕵️ AI Detector Pro (DeBERTa Edition)")
156
+ gr.Markdown(f"Advanced Academic Analysis. Threshold: **{THRESHOLD*100:.0f}%**. Everything below is categorized as Human.")
157
 
158
  with gr.Row():
159
  with gr.Column(scale=3):