Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,13 +18,18 @@ def get_model():
|
|
| 18 |
global tokenizer, model
|
| 19 |
if model is None:
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 23 |
MODEL_NAME, torch_dtype=dtype
|
| 24 |
).to(device).eval()
|
| 25 |
return tokenizer, model
|
| 26 |
|
| 27 |
-
#
|
| 28 |
THRESHOLD = 0.20
|
| 29 |
|
| 30 |
# -----------------------------
|
|
@@ -101,21 +106,16 @@ def analyze(text):
|
|
| 101 |
prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
|
| 102 |
|
| 103 |
for i, block in enumerate(blocks):
|
| 104 |
-
if block.startswith("\n"):
|
| 105 |
highlighted_html += block.replace("\n", "<br>")
|
| 106 |
continue
|
| 107 |
-
if block.isspace():
|
| 108 |
-
highlighted_html += block
|
| 109 |
-
continue
|
| 110 |
|
| 111 |
if i in prob_map:
|
| 112 |
score = prob_map[i]
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
elif score < 0.70:
|
| 116 |
-
|
| 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};' "
|
|
@@ -127,7 +127,7 @@ def analyze(text):
|
|
| 127 |
|
| 128 |
highlighted_html += "</div>"
|
| 129 |
|
| 130 |
-
#
|
| 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]})
|
|
@@ -157,4 +157,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 157 |
run_btn.click(analyze, inputs=text_input, outputs=[verdict_out, score_out, html_out, table_out])
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
-
demo.launch()
|
|
|
|
| 18 |
global tokenizer, model
|
| 19 |
if model is None:
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 21 |
+
|
| 22 |
+
# Hugging Face Build Fix: Force float32 unless a capable GPU is detected
|
| 23 |
+
dtype = torch.float32
|
| 24 |
+
if device.type == "cuda" and torch.cuda.is_bf16_supported():
|
| 25 |
+
dtype = torch.bfloat16
|
| 26 |
+
|
| 27 |
model = AutoModelForSequenceClassification.from_pretrained(
|
| 28 |
MODEL_NAME, torch_dtype=dtype
|
| 29 |
).to(device).eval()
|
| 30 |
return tokenizer, model
|
| 31 |
|
| 32 |
+
# Only affects the content label as requested
|
| 33 |
THRESHOLD = 0.20
|
| 34 |
|
| 35 |
# -----------------------------
|
|
|
|
| 106 |
prob_map = {idx: probs[i] for i, idx in enumerate(pure_sents_indices)}
|
| 107 |
|
| 108 |
for i, block in enumerate(blocks):
|
| 109 |
+
if block.startswith("\n") or block.isspace():
|
| 110 |
highlighted_html += block.replace("\n", "<br>")
|
| 111 |
continue
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
if i in prob_map:
|
| 114 |
score = prob_map[i]
|
| 115 |
+
# Standard coloring thresholds (Independent of the final label threshold)
|
| 116 |
+
if score < 0.35: color, bg = "#11823b", "rgba(17, 130, 59, 0.15)"
|
| 117 |
+
elif score < 0.70: color, bg = "#b8860b", "rgba(184, 134, 11, 0.15)"
|
| 118 |
+
else: 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};' "
|
|
|
|
| 127 |
|
| 128 |
highlighted_html += "</div>"
|
| 129 |
|
| 130 |
+
# CONTENT LABEL LOGIC
|
| 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]})
|
|
|
|
| 157 |
run_btn.click(analyze, inputs=text_input, outputs=[verdict_out, score_out, html_out, table_out])
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
+
demo.launch()
|