APJ23's picture
Update app.py
8ff07d7
raw
history blame
1.73 kB
import streamlit as st
import pandas as pd
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import csv
import random as r
with open('train.csv','r') as f:
read = csv.reader(f)
data = [row for row in read]
df = pd.DataFrame(data[1:],columns=data[0])
tweet = df['comment_text'][r.randint(0,1000)]
tokenizer = AutoTokenizer.from_pretrained("APJ23/MultiHeaded_Sentiment_Analysis_Model")
model = AutoModelForSequenceClassification.from_pretrained("APJ23/MultiHeaded_Sentiment_Analysis_Model")
# Define the classes and their corresponding labels
classes = {
0: 'Non-Toxic',
1: 'Toxic',
2: 'Severely Toxic',
3: 'Obscene',
4: 'Threat',
5: 'Insult',
6: 'Identity Hate'
}
# Create a function to generate the toxicity predictions
@st.cache(allow_output_mutation=True)
# Create a table to display the toxicity predictions
def create_table(predictions):
data = {'Tweet': [], 'Highest Toxicity Class': [], 'Probability': []}
for tweet, prediction in predictions.items():
data['Tweet'].append(tweet)
data['Highest Toxicity Class'].append(prediction[0])
data['Probability'].append(prediction[1])
df = pd.DataFrame(data)
return df
st.title('Toxicity Prediction App')
tweet_input = st.text_input('Enter a tweet:')
if st.button('Predict'):
predicted_class_label, predicted_prob = predict_toxicity(tweet_input, model, tokenizer)
prediction_text = f'Prediction: {predicted_class_label} ({predicted_prob:.2f})'
st.write(prediction_text)
# Display the toxicity predictions in a table
predictions = {tweet_input: (predicted_class_label, predicted_prob)}
table = create_table(predictions)
st.table(table)