VictorM-Coder commited on
Commit
63b8d06
·
verified ·
1 Parent(s): a35760f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -11
app.py CHANGED
@@ -10,7 +10,6 @@ import gradio as gr
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")
@@ -19,14 +18,14 @@ 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
@@ -75,14 +74,12 @@ def analyze(text):
75
  return "ERROR", "0%", f"Failed to load model: {str(e)}", None
76
 
77
  blocks = split_preserving_structure(text)
78
- # Track indices of actual sentences to align probabilities correctly
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
 
85
- # Sliding Window (Size 3, Stride 1)
86
  windows = []
87
  for i in range(len(pure_sents)):
88
  start = max(0, i - 1)
@@ -91,10 +88,8 @@ def analyze(text):
91
 
92
  inputs = tok(windows, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
93
  logits = mod(**inputs).logits
94
- # Ensure stable math with .float() before softmax
95
  probs = F.softmax(logits.float(), dim=-1)[:, 1].cpu().numpy().tolist()
96
 
97
- # Weighted Scoring
98
  lengths = [len(s.split()) for s in pure_sents]
99
  total_words = sum(lengths)
100
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
@@ -103,8 +98,6 @@ def analyze(text):
103
  # HTML RECONSTRUCTION
104
  # -----------------------------
105
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
106
-
107
- # Map probabilities to the original block indices
108
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
109
 
110
  for i, block in enumerate(blocks):
@@ -115,7 +108,6 @@ def analyze(text):
115
  highlighted_html += block
116
  continue
117
 
118
- # Only highlight if the block was actually a sentence we analyzed
119
  if i in prob_map:
120
  score = prob_map[i]
121
  if score < 0.35:
@@ -135,7 +127,7 @@ def analyze(text):
135
 
136
  highlighted_html += "</div>"
137
 
138
- # Restored original labels exactly as requested
139
  label = "AI Content Detected" if weighted_avg >= THRESHOLD else "0 or * AI Content Detected"
140
 
141
  df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
 
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")
 
18
  global tokenizer, model
19
  if model is None:
20
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
21
  dtype = torch.bfloat16 if (device.type == "cuda" and torch.cuda.is_bf16_supported()) else torch.float32
22
  model = AutoModelForSequenceClassification.from_pretrained(
23
  MODEL_NAME, torch_dtype=dtype
24
  ).to(device).eval()
25
  return tokenizer, model
26
 
27
+ # Threshold set to 20% per your request
28
+ THRESHOLD = 0.20
29
 
30
  # -----------------------------
31
  # PROTECT STRUCTURE
 
74
  return "ERROR", "0%", f"Failed to load model: {str(e)}", None
75
 
76
  blocks = split_preserving_structure(text)
 
77
  pure_sents_indices = [i for i, b in enumerate(blocks) if b.strip() and not b.startswith("\n")]
78
  pure_sents = [blocks[i] for i in pure_sents_indices]
79
 
80
  if not pure_sents:
81
  return "—", "—", "<em>No sentences detected.</em>", None
82
 
 
83
  windows = []
84
  for i in range(len(pure_sents)):
85
  start = max(0, i - 1)
 
88
 
89
  inputs = tok(windows, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
90
  logits = mod(**inputs).logits
 
91
  probs = F.softmax(logits.float(), dim=-1)[:, 1].cpu().numpy().tolist()
92
 
 
93
  lengths = [len(s.split()) for s in pure_sents]
94
  total_words = sum(lengths)
95
  weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
 
98
  # HTML RECONSTRUCTION
99
  # -----------------------------
100
  highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
 
 
101
  prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
102
 
103
  for i, block in enumerate(blocks):
 
108
  highlighted_html += block
109
  continue
110
 
 
111
  if i in prob_map:
112
  score = prob_map[i]
113
  if score < 0.35:
 
127
 
128
  highlighted_html += "</div>"
129
 
130
+ # Logic Updated: > 20% triggers detection label
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]})