Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +61 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,62 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# ====================== APP CONFIG ======================
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="AI Text Detector",
|
| 8 |
+
page_icon="🤖",
|
| 9 |
+
layout="centered"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
st.title("🧠 AI Text Detector (DeBERTa-v3-large)")
|
| 13 |
+
st.markdown("""
|
| 14 |
+
This tool detects whether the given text is **Human-written** or **AI-generated**
|
| 15 |
+
using a fine-tuned `microsoft/deberta-v3-large` model.
|
| 16 |
+
""")
|
| 17 |
+
|
| 18 |
+
# ====================== LOAD MODEL ======================
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_model():
|
| 21 |
+
model_name = "your-username/ai-text-detector-deberta-v3-large" # Replace with your HF model repo
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 23 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 24 |
+
return tokenizer, model
|
| 25 |
+
|
| 26 |
+
tokenizer, model = load_model()
|
| 27 |
+
|
| 28 |
+
# ====================== TEXT INPUT ======================
|
| 29 |
+
user_text = st.text_area(
|
| 30 |
+
"Enter text to analyze:",
|
| 31 |
+
placeholder="Paste or write any text here...",
|
| 32 |
+
height=200
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if st.button("🔍 Analyze Text", type="primary"):
|
| 36 |
+
if not user_text.strip():
|
| 37 |
+
st.warning("⚠️ Please enter some text.")
|
| 38 |
+
else:
|
| 39 |
+
with st.spinner("Analyzing..."):
|
| 40 |
+
inputs = tokenizer(user_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
outputs = model(**inputs)
|
| 43 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
|
| 44 |
+
confidence, prediction = torch.max(probs, dim=0)
|
| 45 |
+
|
| 46 |
+
label = "🤖 AI-generated" if prediction.item() == 1 else "🧍 Human-written"
|
| 47 |
+
confidence_percent = confidence.item() * 100
|
| 48 |
+
|
| 49 |
+
st.success(f"**Prediction:** {label}")
|
| 50 |
+
st.progress(confidence.item())
|
| 51 |
+
st.write(f"**Confidence:** {confidence_percent:.2f}%")
|
| 52 |
+
|
| 53 |
+
# Detailed Probabilities
|
| 54 |
+
st.markdown("### 📊 Detailed Probabilities")
|
| 55 |
+
st.write({
|
| 56 |
+
"Human (0)": f"{probs[0].item() * 100:.2f}%",
|
| 57 |
+
"AI (1)": f"{probs[1].item() * 100:.2f}%"
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
# ====================== FOOTER ======================
|
| 61 |
+
st.markdown("---")
|
| 62 |
+
st.caption("Built with ❤️ using [Streamlit](https://streamlit.io) and [Hugging Face Transformers](https://huggingface.co/transformers).")
|