Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,9 @@ import pandas as pd
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
-
# STRONGEST MODEL
|
| 10 |
# -----------------------------
|
| 11 |
-
MODEL_NAME = "
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
# LOAD MODEL
|
|
@@ -21,7 +21,7 @@ model.to(device).eval()
|
|
| 21 |
|
| 22 |
|
| 23 |
# -----------------------------
|
| 24 |
-
# PARAGRAPH
|
| 25 |
# -----------------------------
|
| 26 |
def paragraph_split(text):
|
| 27 |
paragraphs = [p.strip() for p in text.split("\n") if p.strip()]
|
|
@@ -29,7 +29,7 @@ def paragraph_split(text):
|
|
| 29 |
|
| 30 |
|
| 31 |
# -----------------------------
|
| 32 |
-
#
|
| 33 |
# -----------------------------
|
| 34 |
def classify_text(text):
|
| 35 |
if not text.strip():
|
|
@@ -64,12 +64,13 @@ def classify_text(text):
|
|
| 64 |
pred_label = preds[i].item()
|
| 65 |
confidence = probs[i, pred_label].item()
|
| 66 |
|
| 67 |
-
label = "AI" if pred_label ==
|
|
|
|
|
|
|
| 68 |
conf_text = f"{confidence:.2f}"
|
| 69 |
|
| 70 |
results.append([p, label, conf_text])
|
| 71 |
|
| 72 |
-
# Highlighting
|
| 73 |
if label == "AI":
|
| 74 |
highlighted_paragraphs.append(
|
| 75 |
f"<p style='color:red; font-weight:bold; margin-bottom:10px'>{p}</p>"
|
|
@@ -83,7 +84,7 @@ def classify_text(text):
|
|
| 83 |
# DOCUMENT LEVEL SCORE
|
| 84 |
# -----------------------------
|
| 85 |
avg = torch.mean(probs, dim=0)
|
| 86 |
-
ai_likelihood = avg[
|
| 87 |
|
| 88 |
highlighted_html = "\n".join(highlighted_paragraphs)
|
| 89 |
df = pd.DataFrame(results, columns=["Paragraph", "Classification", "Confidence"])
|
|
@@ -92,7 +93,7 @@ def classify_text(text):
|
|
| 92 |
|
| 93 |
|
| 94 |
# -----------------------------
|
| 95 |
-
# GRADIO
|
| 96 |
# -----------------------------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("## 🧠 Writenix Advanced AI Detection (Paragraph-Level)")
|
|
@@ -112,6 +113,5 @@ with gr.Blocks() as demo:
|
|
| 112 |
|
| 113 |
classify_btn.click(classify_text, inputs=text_input, outputs=[ai_score, highlighted, table])
|
| 114 |
|
| 115 |
-
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch()
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
+
# STRONGEST AVAILABLE PUBLIC MODEL (WORKING)
|
| 10 |
# -----------------------------
|
| 11 |
+
MODEL_NAME = "openai-community/roberta-base-openai-detector"
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
# LOAD MODEL
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# -----------------------------
|
| 24 |
+
# PARAGRAPH SPLITTER
|
| 25 |
# -----------------------------
|
| 26 |
def paragraph_split(text):
|
| 27 |
paragraphs = [p.strip() for p in text.split("\n") if p.strip()]
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
# -----------------------------
|
| 32 |
+
# AI DETECTION FUNCTION
|
| 33 |
# -----------------------------
|
| 34 |
def classify_text(text):
|
| 35 |
if not text.strip():
|
|
|
|
| 64 |
pred_label = preds[i].item()
|
| 65 |
confidence = probs[i, pred_label].item()
|
| 66 |
|
| 67 |
+
label = "AI" if pred_label == 1 else "Human"
|
| 68 |
+
# NOTE: roberta-base-openai-detector → label 1 = Fake (AI), 0 = Real (Human)
|
| 69 |
+
|
| 70 |
conf_text = f"{confidence:.2f}"
|
| 71 |
|
| 72 |
results.append([p, label, conf_text])
|
| 73 |
|
|
|
|
| 74 |
if label == "AI":
|
| 75 |
highlighted_paragraphs.append(
|
| 76 |
f"<p style='color:red; font-weight:bold; margin-bottom:10px'>{p}</p>"
|
|
|
|
| 84 |
# DOCUMENT LEVEL SCORE
|
| 85 |
# -----------------------------
|
| 86 |
avg = torch.mean(probs, dim=0)
|
| 87 |
+
ai_likelihood = avg[1].item() * 100 # class 1 = AI
|
| 88 |
|
| 89 |
highlighted_html = "\n".join(highlighted_paragraphs)
|
| 90 |
df = pd.DataFrame(results, columns=["Paragraph", "Classification", "Confidence"])
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
# -----------------------------
|
| 96 |
+
# GRADIO UI
|
| 97 |
# -----------------------------
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
gr.Markdown("## 🧠 Writenix Advanced AI Detection (Paragraph-Level)")
|
|
|
|
| 113 |
|
| 114 |
classify_btn.click(classify_text, inputs=text_input, outputs=[ai_score, highlighted, table])
|
| 115 |
|
|
|
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch()
|