Spaces:
Sleeping
Sleeping
File size: 1,087 Bytes
cb3a151 afd840f cb3a151 |
1 2 3 4 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 41 |
import streamlit as st
import numpy as np
from transformers import BertTokenizer, BertForSequenceClassification
import torch
# Add title
st.title("Toxic Comment Classification")
@st.cache_resource
def get_model():
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('tournas/FineTuneBert')
return tokenizer, model
tokenizer, model = get_model()
user_input = st.text_area('Enter Text to Analyze')
button = st.button('Analyze')
d = {
1: 'Toxic',
0: 'Non Toxic'
}
if user_input and button:
st.write('Analyzing...please wait.')
# Tokenization with padding and truncation
test_sample = tokenizer([user_input], padding=True, truncation=True, max_length=512, return_tensors='pt')
# Get output from the model
output = model(**test_sample)
# Softmax for probabilities
probs = torch.softmax(output.logits, dim=-1).detach().numpy()
# Prediction: Find the class with the highest probability
y_pred = np.argmax(probs, axis=1)
st.write('prediction: ',d[y_pred[0]])
|