Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,79 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
import re
|
| 6 |
-
import logging
|
| 7 |
import pandas as pd
|
| 8 |
-
|
| 9 |
-
st.set_page_config(
|
| 10 |
-
page_title="AI Article Detection by Writenix",
|
| 11 |
-
page_icon="🧠",
|
| 12 |
-
layout="wide"
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
st.logo(
|
| 16 |
-
image="https://dejan.ai/wp-content/uploads/2024/02/dejan-300x103.png",
|
| 17 |
-
link="https://dejan.ai/",
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
# --- Logging & Streamlit setup ---
|
| 21 |
-
logging.basicConfig(level=logging.INFO)
|
| 22 |
-
logger = logging.getLogger(__name__)
|
| 23 |
-
|
| 24 |
-
st.markdown("""
|
| 25 |
-
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
|
| 26 |
-
<style>
|
| 27 |
-
html, body, [class*="css"] {
|
| 28 |
-
font-family: 'Roboto', sans-serif;
|
| 29 |
-
}
|
| 30 |
-
</style>
|
| 31 |
-
""", unsafe_allow_html=True)
|
| 32 |
-
|
| 33 |
-
@st.cache_resource
|
| 34 |
-
def load_model_and_tokenizer(model_name):
|
| 35 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 36 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
-
dtype = torch.bfloat16 if (device.type=="cuda" and torch.cuda.is_bf16_supported()) else torch.float32
|
| 38 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name, torch_dtype=dtype)
|
| 39 |
-
model.to(device).eval()
|
| 40 |
-
return tokenizer, model, device
|
| 41 |
|
| 42 |
MODEL_NAME = "dejanseo/ai-cop"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
def sent_tokenize(text):
|
| 51 |
return [s for s in re.split(r'(?<=[\.!?])\s+', text.strip()) if s]
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
text = st.text_area("Enter text to classify", height=200, placeholder="Paste your text here…")
|
| 56 |
-
|
| 57 |
-
if st.button("Classify", type="primary"):
|
| 58 |
if not text.strip():
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
# Highlighted text output
|
| 118 |
-
st.markdown("### 🔍 Highlighted Text")
|
| 119 |
-
st.markdown(" ".join(highlighted_sentences), unsafe_allow_html=True)
|
| 120 |
-
|
| 121 |
-
# Overall score (just model avg)
|
| 122 |
-
avg = torch.mean(probs, dim=0)
|
| 123 |
-
model_ai = avg[0].item()
|
| 124 |
-
|
| 125 |
-
st.subheader(f"⚖️ AI Likelihood: {model_ai*100:.1f}%")
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn.functional as F
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
import re
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
MODEL_NAME = "dejanseo/ai-cop"
|
| 9 |
+
|
| 10 |
+
# --- Load model ---
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
+
dtype = torch.bfloat16 if (device.type=="cuda" and torch.cuda.is_bf16_supported()) else torch.float32
|
| 14 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, torch_dtype=dtype)
|
| 15 |
+
model.to(device).eval()
|
| 16 |
|
| 17 |
def sent_tokenize(text):
|
| 18 |
return [s for s in re.split(r'(?<=[\.!?])\s+', text.strip()) if s]
|
| 19 |
|
| 20 |
+
# --- Inference function ---
|
| 21 |
+
def classify_text(text):
|
|
|
|
|
|
|
|
|
|
| 22 |
if not text.strip():
|
| 23 |
+
return "⚠️ Please enter some text.", None, None
|
| 24 |
+
|
| 25 |
+
sentences = sent_tokenize(text)
|
| 26 |
+
if not sentences:
|
| 27 |
+
return "⚠️ No sentences detected.", None, None
|
| 28 |
+
|
| 29 |
+
inputs = tokenizer(
|
| 30 |
+
sentences,
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
padding=True,
|
| 33 |
+
truncation=True,
|
| 34 |
+
max_length=model.config.max_position_embeddings
|
| 35 |
+
).to(device)
|
| 36 |
+
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
logits = model(**inputs).logits
|
| 39 |
+
probs = F.softmax(logits, dim=-1).cpu()
|
| 40 |
+
preds = torch.argmax(probs, dim=-1).cpu()
|
| 41 |
+
|
| 42 |
+
results = []
|
| 43 |
+
highlighted_sentences = []
|
| 44 |
+
for i, s in enumerate(sentences):
|
| 45 |
+
p = preds[i].item()
|
| 46 |
+
conf = probs[i, p].item()
|
| 47 |
+
label = "AI" if p == 0 else "Human"
|
| 48 |
+
|
| 49 |
+
results.append([s, label, f"{conf:.2f}"])
|
| 50 |
+
if label == "AI":
|
| 51 |
+
highlighted_sentences.append(f"<span style='color:red; font-weight:bold'>{s}</span>")
|
| 52 |
+
else:
|
| 53 |
+
highlighted_sentences.append(f"<span style='color:green; font-weight:bold'>{s}</span>")
|
| 54 |
+
|
| 55 |
+
# Overall AI likelihood
|
| 56 |
+
avg = torch.mean(probs, dim=0)
|
| 57 |
+
model_ai = avg[0].item() * 100
|
| 58 |
+
|
| 59 |
+
highlighted_text = " ".join(highlighted_sentences)
|
| 60 |
+
df = pd.DataFrame(results, columns=["Sentence", "Classification", "Confidence"])
|
| 61 |
+
return f"⚖️ AI Likelihood: {model_ai:.1f}%", highlighted_text, df
|
| 62 |
+
|
| 63 |
+
# --- Gradio Interface ---
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("## 🧠 AI Article Detection by Writenix")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
text_input = gr.Textbox(label="Enter text", lines=10, placeholder="Paste your text here…")
|
| 69 |
+
|
| 70 |
+
classify_btn = gr.Button("Classify")
|
| 71 |
+
|
| 72 |
+
ai_score = gr.Label(label="Overall AI Likelihood")
|
| 73 |
+
highlighted = gr.HTML()
|
| 74 |
+
table = gr.Dataframe(headers=["Sentence", "Classification", "Confidence"], wrap=True)
|
| 75 |
+
|
| 76 |
+
classify_btn.click(classify_text, inputs=text_input, outputs=[ai_score, highlighted, table])
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|