Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ def get_model():
|
|
| 26 |
).to(device).eval()
|
| 27 |
return tokenizer, model
|
| 28 |
|
| 29 |
-
#
|
| 30 |
THRESHOLD = 0.60
|
| 31 |
|
| 32 |
# -----------------------------
|
|
@@ -82,7 +82,6 @@ def analyze(text):
|
|
| 82 |
if not pure_sents:
|
| 83 |
return "—", "—", "<em>No sentences detected.</em>", None
|
| 84 |
|
| 85 |
-
# Context window analysis
|
| 86 |
windows = []
|
| 87 |
for i in range(len(pure_sents)):
|
| 88 |
start = max(0, i - 1)
|
|
@@ -98,7 +97,7 @@ def analyze(text):
|
|
| 98 |
weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
|
| 99 |
|
| 100 |
# -----------------------------
|
| 101 |
-
# HTML RECONSTRUCTION
|
| 102 |
# -----------------------------
|
| 103 |
highlighted_html = "<div style='font-family: sans-serif; line-height: 1.8;'>"
|
| 104 |
prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
|
|
@@ -110,14 +109,13 @@ def analyze(text):
|
|
| 110 |
|
| 111 |
if i in prob_map:
|
| 112 |
score = prob_map[i]
|
| 113 |
-
|
| 114 |
-
# Revised coloring logic: < 60% is Human (Green)
|
| 115 |
if score < 0.60:
|
| 116 |
-
color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
|
| 117 |
elif score < 0.80:
|
| 118 |
-
color, bg = "#b8860b", "rgba(184, 134, 11, 0.15)"
|
| 119 |
else:
|
| 120 |
-
color, bg = "#b80d0d", "rgba(184, 13, 13, 0.15)"
|
| 121 |
|
| 122 |
highlighted_html += (
|
| 123 |
f"<span style='background:{bg}; padding:2px 4px; border-radius:4px; border-bottom: 2px solid {color};' "
|
|
@@ -128,18 +126,22 @@ def analyze(text):
|
|
| 128 |
highlighted_html += block
|
| 129 |
highlighted_html += "</div>"
|
| 130 |
|
| 131 |
-
#
|
|
|
|
| 132 |
label = "AI Content Detected" if weighted_avg >= THRESHOLD else "0 or * AI Content Detected"
|
| 133 |
|
|
|
|
|
|
|
|
|
|
| 134 |
df = pd.DataFrame({"Sentence": pure_sents, "AI Confidence": [f"{p:.1%}" for p in probs]})
|
| 135 |
-
return label,
|
| 136 |
|
| 137 |
# -----------------------------
|
| 138 |
# GRADIO INTERFACE
|
| 139 |
# -----------------------------
|
| 140 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 141 |
gr.Markdown("## 🕵️ AI Detector Pro")
|
| 142 |
-
gr.Markdown("Sentence-level analysis
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column(scale=3):
|
|
|
|
| 26 |
).to(device).eval()
|
| 27 |
return tokenizer, model
|
| 28 |
|
| 29 |
+
# Threshold for Verdict Label
|
| 30 |
THRESHOLD = 0.60
|
| 31 |
|
| 32 |
# -----------------------------
|
|
|
|
| 82 |
if not pure_sents:
|
| 83 |
return "—", "—", "<em>No sentences detected.</em>", None
|
| 84 |
|
|
|
|
| 85 |
windows = []
|
| 86 |
for i in range(len(pure_sents)):
|
| 87 |
start = max(0, i - 1)
|
|
|
|
| 97 |
weighted_avg = sum(p * l for p, l in zip(probs, lengths)) / total_words if total_words > 0 else 0
|
| 98 |
|
| 99 |
# -----------------------------
|
| 100 |
+
# HTML RECONSTRUCTION
|
| 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)}
|
|
|
|
| 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};' "
|
|
|
|
| 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):
|