Spaces:
Runtime error
Runtime error
Updated the code
Browse files
app.py
CHANGED
@@ -1,38 +1,74 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
|
5 |
-
# import os
|
6 |
from utils import run_sentiment_analysis, preprocess
|
7 |
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification
|
8 |
import os
|
9 |
import time
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
config = AutoConfig.from_pretrained(model_path)
|
15 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
16 |
-
|
17 |
-
# dark_theme = set_theme()
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
|
|
20 |
st.set_page_config(
|
21 |
page_title="Tweet Analyzer",
|
22 |
page_icon="π€",
|
23 |
initial_sidebar_state="expanded",
|
24 |
menu_items={
|
25 |
-
'About': "# This is a
|
26 |
}
|
27 |
)
|
28 |
|
|
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# st.sidebar.selectbox('Menu', ['About', 'Model'])
|
34 |
with my_expander:
|
35 |
-
|
36 |
st.markdown("""
|
37 |
<style>
|
38 |
h1 {
|
@@ -40,15 +76,16 @@ with my_expander:
|
|
40 |
}
|
41 |
</style>
|
42 |
""", unsafe_allow_html=True)
|
|
|
|
|
43 |
st.title(':green[Covid-19 Vaccines Tweets Analyzer]')
|
44 |
-
st.sidebar.markdown("""
|
45 |
-
## Demo App
|
46 |
|
47 |
-
This app analyzes your tweets on covid vaccines and classifies them us Neutral, Negative or Positive
|
48 |
-
""")
|
49 |
-
# my_expander.write('Container')
|
50 |
-
# create a three column layout
|
51 |
|
|
|
|
|
|
|
|
|
|
|
52 |
col1, col2, col3 = st.columns((1.6, 1,0.3))
|
53 |
# col2.markdown("""
|
54 |
# <p style= font-color:red>
|
@@ -62,24 +99,41 @@ with my_expander:
|
|
62 |
}
|
63 |
</style>
|
64 |
""", unsafe_allow_html=True)
|
|
|
|
|
65 |
tweet = col1.text_area('Tweets to analyze',height=200, max_chars=520, placeholder='Write your Tweets here')
|
|
|
|
|
66 |
colA, colb, colc, cold = st.columns(4)
|
67 |
clear_button = colA.button(label='Clear', type='secondary', use_container_width=True)
|
|
|
|
|
68 |
submit_button = colb.button(label='Submit', type='primary', use_container_width=True)
|
69 |
-
|
|
|
|
|
70 |
empty_container.text("Results from Analyzer")
|
71 |
-
|
|
|
72 |
empty_container2.text('Scores')
|
73 |
text = preprocess(tweet)
|
|
|
|
|
74 |
results = run_sentiment_analysis(text=text, model=model, tokenizer=tokenizer)
|
|
|
|
|
75 |
if submit_button:
|
|
|
76 |
success_message = st.success('Success', icon="β
")
|
|
|
|
|
77 |
|
|
|
78 |
with empty_container:
|
79 |
-
|
80 |
neutral = st.progress(value=results['Neutral'], text='Neutral',)
|
81 |
negative = st.progress(value=results['Negative'], text='Negative')
|
82 |
positive = st.progress(value=results['Positive'], text='Positive')
|
|
|
83 |
with empty_container2:
|
84 |
st.markdown(
|
85 |
"""
|
@@ -87,18 +141,28 @@ with my_expander:
|
|
87 |
[data-testid="stMetricValue"] {
|
88 |
font-size: 20px;
|
89 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
</style>
|
91 |
""",
|
92 |
unsafe_allow_html=True,
|
93 |
)
|
|
|
|
|
|
|
94 |
neutral_score = st.metric(label='Score', value=round(results['Neutral'], 4), label_visibility='collapsed')
|
95 |
negative_score = st.metric(label='Score', value=round(results['Negative'], 4), label_visibility='collapsed')
|
96 |
positive_score = st.metric(label='Score', value=round(results['Positive'], 4), label_visibility='collapsed')
|
97 |
-
|
98 |
-
|
99 |
-
# interpret_button = col2.button(label='Interpret',type='secondary', use_container_width=True)
|
100 |
|
101 |
|
102 |
-
# st.help()
|
103 |
-
# create a date input to receive date
|
104 |
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
|
|
5 |
from utils import run_sentiment_analysis, preprocess
|
6 |
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification
|
7 |
import os
|
8 |
import time
|
9 |
|
10 |
+
# the two model trained
|
11 |
+
dstbt_model_path = "bright1/fine-tuned-distilbert-base-uncased" # distilbert model
|
12 |
+
rbta_model_path = "bright1/fine-tuned-twitter-Roberta-base-sentiment" # roberta model
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# function to load model
|
15 |
+
def load_model_components(model_path):
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
17 |
+
config = AutoConfig.from_pretrained(model_path)
|
18 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
19 |
+
return model, tokenizer, config
|
20 |
|
21 |
+
# configure page
|
22 |
st.set_page_config(
|
23 |
page_title="Tweet Analyzer",
|
24 |
page_icon="π€",
|
25 |
initial_sidebar_state="expanded",
|
26 |
menu_items={
|
27 |
+
'About': "# This is a Sentiment Analysis App. Call it the Covid Vaccine tweet Analyzer!"
|
28 |
}
|
29 |
)
|
30 |
|
31 |
+
# Define custom CSS style
|
32 |
|
33 |
+
# Apply custom CSS
|
34 |
+
# st.markdown("""<style>
|
35 |
+
# [data-testid="stAppViewContainer"] {
|
36 |
+
# background-image: url("app\download.png");
|
37 |
+
# background-attachment: fixed;
|
38 |
+
# background-size: cover
|
39 |
+
# }
|
40 |
+
# </style>""", unsafe_allow_html=True)
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
# create a sidebar and contents
|
45 |
+
st.sidebar.markdown("""
|
46 |
+
## Demo App
|
47 |
+
|
48 |
+
This app analyzes your tweets on covid vaccines and classifies them us Neutral, Negative or Positive
|
49 |
+
""")
|
50 |
+
|
51 |
+
# create a three column layout
|
52 |
+
model_type = st.sidebar.selectbox(label=':red[Select your model]', options=('distilbert', 'roberta'))
|
53 |
+
st.markdown("""<style>
|
54 |
+
[data-testid="stMarkdownContainer"] {
|
55 |
+
font-size: 30px;
|
56 |
+
font-weight: 800;
|
57 |
+
}
|
58 |
+
</style>""", unsafe_allow_html=True)
|
59 |
|
60 |
+
# set a default model path
|
61 |
+
model_path = dstbt_model_path
|
62 |
+
if model_type == 'roberta':
|
63 |
+
model_path = rbta_model_path
|
64 |
+
|
65 |
+
|
66 |
+
# create app interface
|
67 |
+
my_expander = st.container()
|
68 |
|
69 |
# st.sidebar.selectbox('Menu', ['About', 'Model'])
|
70 |
with my_expander:
|
71 |
+
# center text in the container
|
72 |
st.markdown("""
|
73 |
<style>
|
74 |
h1 {
|
|
|
76 |
}
|
77 |
</style>
|
78 |
""", unsafe_allow_html=True)
|
79 |
+
|
80 |
+
#set title for the app
|
81 |
st.title(':green[Covid-19 Vaccines Tweets Analyzer]')
|
|
|
|
|
82 |
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# load model components
|
85 |
+
model, tokenizer, config = load_model_components(model_path)
|
86 |
+
|
87 |
+
|
88 |
+
# size columns
|
89 |
col1, col2, col3 = st.columns((1.6, 1,0.3))
|
90 |
# col2.markdown("""
|
91 |
# <p style= font-color:red>
|
|
|
99 |
}
|
100 |
</style>
|
101 |
""", unsafe_allow_html=True)
|
102 |
+
|
103 |
+
# set textarea to receive tweet
|
104 |
tweet = col1.text_area('Tweets to analyze',height=200, max_chars=520, placeholder='Write your Tweets here')
|
105 |
+
|
106 |
+
# divide container into columns
|
107 |
colA, colb, colc, cold = st.columns(4)
|
108 |
clear_button = colA.button(label='Clear', type='secondary', use_container_width=True)
|
109 |
+
|
110 |
+
# create a submit button
|
111 |
submit_button = colb.button(label='Submit', type='primary', use_container_width=True)
|
112 |
+
|
113 |
+
# set an empty container for the results
|
114 |
+
empty_container = col2.container() # for progress bars
|
115 |
empty_container.text("Results from Analyzer")
|
116 |
+
|
117 |
+
empty_container2 = col3.container() # for scores
|
118 |
empty_container2.text('Scores')
|
119 |
text = preprocess(tweet)
|
120 |
+
|
121 |
+
# run the analysis on the tweet
|
122 |
results = run_sentiment_analysis(text=text, model=model, tokenizer=tokenizer)
|
123 |
+
|
124 |
+
# when the tweet is submitted
|
125 |
if submit_button:
|
126 |
+
# print a success message
|
127 |
success_message = st.success('Success', icon="β
")
|
128 |
+
time.sleep(3)
|
129 |
+
success_message.empty()
|
130 |
|
131 |
+
# create am expander to contain the results
|
132 |
with empty_container:
|
|
|
133 |
neutral = st.progress(value=results['Neutral'], text='Neutral',)
|
134 |
negative = st.progress(value=results['Negative'], text='Negative')
|
135 |
positive = st.progress(value=results['Positive'], text='Positive')
|
136 |
+
|
137 |
with empty_container2:
|
138 |
st.markdown(
|
139 |
"""
|
|
|
141 |
[data-testid="stMetricValue"] {
|
142 |
font-size: 20px;
|
143 |
}
|
144 |
+
.st-ed {
|
145 |
+
background-color: #FF4B4B;
|
146 |
+
|
147 |
+
}
|
148 |
+
.st-ee {
|
149 |
+
background-color: #1B9C85;
|
150 |
+
}
|
151 |
+
.st-eb {
|
152 |
+
background-color: #FFD95A;
|
153 |
+
}
|
154 |
</style>
|
155 |
""",
|
156 |
unsafe_allow_html=True,
|
157 |
)
|
158 |
+
|
159 |
+
# class=""
|
160 |
+
# dispay the scores with metric widget
|
161 |
neutral_score = st.metric(label='Score', value=round(results['Neutral'], 4), label_visibility='collapsed')
|
162 |
negative_score = st.metric(label='Score', value=round(results['Negative'], 4), label_visibility='collapsed')
|
163 |
positive_score = st.metric(label='Score', value=round(results['Positive'], 4), label_visibility='collapsed')
|
164 |
+
|
165 |
+
# interpret_button = col2.button(label='Interpret',type='secondary', use_container_width=True)
|
|
|
166 |
|
167 |
|
|
|
|
|
168 |
|
utils.py
CHANGED
@@ -4,12 +4,6 @@ from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassific
|
|
4 |
from scipy.special import softmax
|
5 |
import os
|
6 |
|
7 |
-
# Requirements
|
8 |
-
# model_path = "bright1/fine-tuned-distilbert-base-uncased"
|
9 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_path)
|
10 |
-
# config = AutoConfig.from_pretrained(model_path)
|
11 |
-
# model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
12 |
-
|
13 |
|
14 |
|
15 |
def check_csv(csv_file, data):
|
@@ -27,7 +21,6 @@ def preprocess(text):
|
|
27 |
t = "http" if t.startswith("http") else t
|
28 |
print(t)
|
29 |
new_text.append(t)
|
30 |
-
print(new_text)
|
31 |
|
32 |
return " ".join(new_text)
|
33 |
|
|
|
4 |
from scipy.special import softmax
|
5 |
import os
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
def check_csv(csv_file, data):
|
|
|
21 |
t = "http" if t.startswith("http") else t
|
22 |
print(t)
|
23 |
new_text.append(t)
|
|
|
24 |
|
25 |
return " ".join(new_text)
|
26 |
|