Spaces:
Runtime error
Runtime error
Commit
Β·
c452afe
1
Parent(s):
8711bb8
Add new model
Browse files- .vscode/settings.json +3 -0
- app.py +58 -30
.vscode/settings.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"python.formatting.provider": "black"
|
3 |
+
}
|
app.py
CHANGED
@@ -1,51 +1,79 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
st.title('Toxic Tweets')
|
6 |
|
7 |
models = [
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
11 |
]
|
12 |
|
13 |
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 ππ.
|
14 |
"""
|
15 |
|
16 |
-
st.image(
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
labels = {
|
22 |
-
"LABEL_0": "NEGATIVE",
|
23 |
-
"LABEL_1": "NEUTRAL",
|
24 |
-
"LABEL_2": "POSITIVE",
|
25 |
-
}
|
26 |
|
27 |
def predict(tweet, model):
|
28 |
-
with st.spinner(
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
if label != 'POSITIVE' and label != 'NEGATIVE':
|
37 |
-
label = labels[label]
|
38 |
-
|
39 |
-
if label == 'POSITIVE':
|
40 |
-
st.balloons()
|
41 |
-
|
42 |
-
st.info(f"Label: {label} \n\n Score: {score}")
|
43 |
|
|
|
|
|
|
|
|
|
44 |
|
45 |
|
46 |
if button:
|
47 |
if not tweet:
|
48 |
-
st.warning(
|
49 |
else:
|
50 |
predict(tweet, model)
|
51 |
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline, DistilBertTokenizerFast
|
3 |
|
4 |
+
st.title("Toxic Tweets")
|
|
|
5 |
|
6 |
models = [
|
7 |
+
"notbhu/toxic-tweet-classifier",
|
8 |
+
"distilbert-base-uncased-finetuned-sst-2-english",
|
9 |
+
"cardiffnlp/twitter-roberta-base-sentiment",
|
10 |
+
"Seethal/sentiment_analysis_generic_dataset",
|
11 |
]
|
12 |
|
13 |
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 ππ.
|
14 |
"""
|
15 |
|
16 |
+
st.image(
|
17 |
+
"https://www.gannett-cdn.com/presto/2022/04/12/USAT/3a93e183-d87d-493a-97a9-cf75fb7b9d18-AP_Pennsylvania_Easter.jpg"
|
18 |
+
)
|
19 |
+
|
20 |
+
tweet = st.text_area("Enter a tweet", value=default_tweet)
|
21 |
+
model = st.selectbox("Select a model", models)
|
22 |
+
button = st.button("Predict")
|
23 |
+
|
24 |
+
|
25 |
+
def getLabel(label, model):
|
26 |
+
labels = {
|
27 |
+
"notbhu/toxic-tweet-classifier": {
|
28 |
+
"LABEL_0": "toxic",
|
29 |
+
"LABEL_1": "severe_toxic",
|
30 |
+
"LABEL_2": "obscene",
|
31 |
+
"LABEL_3": "threat",
|
32 |
+
"LABEL_4": "insult",
|
33 |
+
"LABEL_5": "identity_hate",
|
34 |
+
},
|
35 |
+
"distilbert-base-uncased-finetuned-sst-2-english": {
|
36 |
+
"POSITIVE": "POSITIVE",
|
37 |
+
"NEGATIVE": "NEGATIVE",
|
38 |
+
},
|
39 |
+
"cardiffnlp/twitter-roberta-base-sentiment": {
|
40 |
+
"LABEL_0": "NEGATIVE",
|
41 |
+
"LABEL_1": "NEUTRAL",
|
42 |
+
"LABEL_2": "POSITIVE",
|
43 |
+
},
|
44 |
+
"Seethal/sentiment_analysis_generic_dataset": {
|
45 |
+
"LABEL_0": "NEGATIVE",
|
46 |
+
"LABEL_1": "POSITIVE",
|
47 |
+
},
|
48 |
+
}
|
49 |
+
|
50 |
+
return labels[model][label]
|
51 |
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
def predict(tweet, model):
|
54 |
+
with st.spinner("Predicting..."):
|
55 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased")
|
56 |
+
classifier = pipeline(model=model, tokenizer=tokenizer)
|
57 |
+
|
58 |
+
try:
|
59 |
+
result = classifier(tweet)
|
60 |
+
label = result[0]["label"]
|
61 |
+
score = result[0]["score"]
|
62 |
|
63 |
+
label = getLabel(label, model)
|
64 |
+
|
65 |
+
if label == "POSITIVE":
|
66 |
+
st.balloons()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
st.info(f"Label: {label} \n\n Score: {score}")
|
69 |
+
except Exception as e:
|
70 |
+
st.error("Something went wrong")
|
71 |
+
st.error(e)
|
72 |
|
73 |
|
74 |
if button:
|
75 |
if not tweet:
|
76 |
+
st.warning("Please enter a tweet")
|
77 |
else:
|
78 |
predict(tweet, model)
|
79 |
|