Spaces:
Sleeping
Sleeping
Debopam Dey
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer from Hugging Face
|
| 6 |
+
@st.cache(allow_output_mutation=True)
|
| 7 |
+
def load_model():
|
| 8 |
+
model_name = "pritam2014/BERTAIDetector"
|
| 9 |
+
model = BertForSequenceClassification.from_pretrained(model_name)
|
| 10 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
| 11 |
+
return model, tokenizer
|
| 12 |
+
|
| 13 |
+
# Load the model and tokenizer
|
| 14 |
+
model, tokenizer = load_model()
|
| 15 |
+
|
| 16 |
+
# Create a Streamlit app
|
| 17 |
+
st.title("AI Text Detector")
|
| 18 |
+
|
| 19 |
+
# Get user input
|
| 20 |
+
user_input = st.text_area("Enter your text:", height=200)
|
| 21 |
+
|
| 22 |
+
# Make predictions
|
| 23 |
+
if st.button("Detect"):
|
| 24 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
logits = model(**inputs).logits
|
| 27 |
+
predicted_class = torch.argmax(logits).item()
|
| 28 |
+
|
| 29 |
+
if predicted_class == 0:
|
| 30 |
+
st.write("This text is likely **human-written**.")
|
| 31 |
+
else:
|
| 32 |
+
st.write("This text is likely **AI-generated**.")
|