DecadeNugget22 commited on
Commit
c452afe
β€’
1 Parent(s): 8711bb8

Add new model

Browse files
Files changed (2) hide show
  1. .vscode/settings.json +3 -0
  2. 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
- '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('https://www.gannett-cdn.com/presto/2022/04/12/USAT/3a93e183-d87d-493a-97a9-cf75fb7b9d18-AP_Pennsylvania_Easter.jpg')
17
- tweet = st.text_area('Enter a tweet', value=default_tweet)
18
- model = st.selectbox('Select a model', models)
19
- button = st.button('Predict')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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('Predicting...'):
29
- classifier = pipeline(model=model)
30
- result = classifier(tweet)
 
 
 
 
 
31
 
32
- label = result[0]["label"]
33
- score = result[0]["score"]
34
-
35
- # Cleanup label
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('Please enter a tweet')
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