Upload stream.py
Browse files
stream.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_resource(allow_output_mutation=True)
|
| 7 |
+
def load_model():
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained("your-huggingface-username/your-model-repo")
|
| 9 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 10 |
+
return model, tokenizer
|
| 11 |
+
|
| 12 |
+
model, tokenizer = load_model()
|
| 13 |
+
|
| 14 |
+
# Define Streamlit app layout
|
| 15 |
+
st.title("AI vs Human Text Classifier")
|
| 16 |
+
|
| 17 |
+
user_input = st.text_area("Enter the text to classify:")
|
| 18 |
+
|
| 19 |
+
if st.button("Classify"):
|
| 20 |
+
# Preprocess the input text
|
| 21 |
+
inputs = tokenizer(user_input, return_tensors="pt", max_length=256, padding="max_length", truncation=True)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
|
| 25 |
+
# Get prediction
|
| 26 |
+
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 27 |
+
|
| 28 |
+
# Convert prediction to human-readable label
|
| 29 |
+
label_mapping = {0: "Human-written", 1: "AI-generated"}
|
| 30 |
+
st.write(f"The text is classified as: {label_mapping[prediction]}")
|