Spaces:
Runtime error
Runtime error
File size: 2,847 Bytes
8711bb8 c452afe 8711bb8 c452afe 8711bb8 c452afe 8711bb8 c452afe 8711bb8 c452afe 8711bb8 c452afe 8711bb8 c452afe 8711bb8 c452afe 8711bb8 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import streamlit as st
from transformers import pipeline, DistilBertTokenizerFast
st.title("Toxic Tweets")
models = [
"notbhu/toxic-tweet-classifier",
"distilbert-base-uncased-finetuned-sst-2-english",
"cardiffnlp/twitter-roberta-base-sentiment",
"Seethal/sentiment_analysis_generic_dataset",
]
default_tweet = """π°πΈπ£ Happy Easter πΈπ°π£! It's time to crack open some eggs π₯ and celebrate with the Easter Bunny π°π. Hop π on over to church βͺοΈ and get down on your knees π§ββοΈπ for some Easter blessings π°βοΈπ·. Did you know that Jesus ππ died and rose again πππ
? It's a time for rejoicing π and enjoying the company of loved ones π¨βπ©βπ§βπ¦. So put on your Sunday best π and get ready to hunt π΅οΈββοΈ for some Easter treats π«π₯π. Happy Easter, bunnies π°π―ββοΈ! Don't forget to spread the love β€οΈ and send this message to your favorite bunnies ππ.
"""
st.image(
"https://www.gannett-cdn.com/presto/2022/04/12/USAT/3a93e183-d87d-493a-97a9-cf75fb7b9d18-AP_Pennsylvania_Easter.jpg"
)
tweet = st.text_area("Enter a tweet", value=default_tweet)
model = st.selectbox("Select a model", models)
button = st.button("Predict")
def getLabel(label, model):
labels = {
"notbhu/toxic-tweet-classifier": {
"LABEL_0": "toxic",
"LABEL_1": "severe_toxic",
"LABEL_2": "obscene",
"LABEL_3": "threat",
"LABEL_4": "insult",
"LABEL_5": "identity_hate",
},
"distilbert-base-uncased-finetuned-sst-2-english": {
"POSITIVE": "POSITIVE",
"NEGATIVE": "NEGATIVE",
},
"cardiffnlp/twitter-roberta-base-sentiment": {
"LABEL_0": "NEGATIVE",
"LABEL_1": "NEUTRAL",
"LABEL_2": "POSITIVE",
},
"Seethal/sentiment_analysis_generic_dataset": {
"LABEL_0": "NEGATIVE",
"LABEL_1": "POSITIVE",
},
}
return labels[model][label]
def predict(tweet, model):
with st.spinner("Predicting..."):
tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased")
classifier = pipeline(model=model, tokenizer=tokenizer)
try:
result = classifier(tweet)
label = result[0]["label"]
score = result[0]["score"]
label = getLabel(label, model)
if label == "POSITIVE":
st.balloons()
st.info(f"Label: {label} \n\n Score: {score}")
except Exception as e:
st.error("Something went wrong")
st.error(e)
if button:
if not tweet:
st.warning("Please enter a tweet")
else:
predict(tweet, model)
elif tweet:
predict(tweet, model) |