Spaces:
Sleeping
Sleeping
Clear
Browse files
app.py
CHANGED
|
@@ -1,38 +1,73 @@
|
|
| 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 +75,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 +98,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 +140,27 @@ 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 |
-
|
| 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 |
+
This app analyzes your tweets on covid vaccines and classifies them us Neutral, Negative or Positive
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
# create a three column layout
|
| 51 |
+
model_type = st.sidebar.selectbox(label=':red[Select your model]', options=('distilbert', 'roberta'))
|
| 52 |
+
st.markdown("""<style>
|
| 53 |
+
[data-testid="stMarkdownContainer"] {
|
| 54 |
+
font-size: 30px;
|
| 55 |
+
font-weight: 800;
|
| 56 |
+
}
|
| 57 |
+
</style>""", unsafe_allow_html=True)
|
| 58 |
+
|
| 59 |
+
# set a default model path
|
| 60 |
+
model_path = dstbt_model_path
|
| 61 |
+
if model_type == 'roberta':
|
| 62 |
+
model_path = rbta_model_path
|
| 63 |
|
| 64 |
|
| 65 |
+
# create app interface
|
| 66 |
+
my_expander = st.container()
|
| 67 |
+
|
| 68 |
# st.sidebar.selectbox('Menu', ['About', 'Model'])
|
| 69 |
with my_expander:
|
| 70 |
+
# center text in the container
|
| 71 |
st.markdown("""
|
| 72 |
<style>
|
| 73 |
h1 {
|
|
|
|
| 75 |
}
|
| 76 |
</style>
|
| 77 |
""", unsafe_allow_html=True)
|
| 78 |
+
|
| 79 |
+
#set title for the app
|
| 80 |
st.title(':green[Covid-19 Vaccines Tweets Analyzer]')
|
|
|
|
|
|
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# load model components
|
| 84 |
+
model, tokenizer, config = load_model_components(model_path)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# size columns
|
| 88 |
col1, col2, col3 = st.columns((1.6, 1,0.3))
|
| 89 |
# col2.markdown("""
|
| 90 |
# <p style= font-color:red>
|
|
|
|
| 98 |
}
|
| 99 |
</style>
|
| 100 |
""", unsafe_allow_html=True)
|
| 101 |
+
|
| 102 |
+
# set textarea to receive tweet
|
| 103 |
tweet = col1.text_area('Tweets to analyze',height=200, max_chars=520, placeholder='Write your Tweets here')
|
| 104 |
+
|
| 105 |
+
# divide container into columns
|
| 106 |
colA, colb, colc, cold = st.columns(4)
|
| 107 |
clear_button = colA.button(label='Clear', type='secondary', use_container_width=True)
|
| 108 |
+
|
| 109 |
+
# create a submit button
|
| 110 |
submit_button = colb.button(label='Submit', type='primary', use_container_width=True)
|
| 111 |
+
|
| 112 |
+
# set an empty container for the results
|
| 113 |
+
empty_container = col2.container() # for progress bars
|
| 114 |
empty_container.text("Results from Analyzer")
|
| 115 |
+
|
| 116 |
+
empty_container2 = col3.container() # for scores
|
| 117 |
empty_container2.text('Scores')
|
| 118 |
text = preprocess(tweet)
|
| 119 |
+
|
| 120 |
+
# run the analysis on the tweet
|
| 121 |
results = run_sentiment_analysis(text=text, model=model, tokenizer=tokenizer)
|
| 122 |
+
|
| 123 |
+
# when the tweet is submitted
|
| 124 |
if submit_button:
|
| 125 |
+
# print a success message
|
| 126 |
success_message = st.success('Success', icon="✅")
|
| 127 |
+
time.sleep(3)
|
| 128 |
+
success_message.empty()
|
| 129 |
|
| 130 |
+
# create am expander to contain the results
|
| 131 |
with empty_container:
|
|
|
|
| 132 |
neutral = st.progress(value=results['Neutral'], text='Neutral',)
|
| 133 |
negative = st.progress(value=results['Negative'], text='Negative')
|
| 134 |
positive = st.progress(value=results['Positive'], text='Positive')
|
| 135 |
+
|
| 136 |
with empty_container2:
|
| 137 |
st.markdown(
|
| 138 |
"""
|
|
|
|
| 140 |
[data-testid="stMetricValue"] {
|
| 141 |
font-size: 20px;
|
| 142 |
}
|
| 143 |
+
.st-ed {
|
| 144 |
+
background-color: #FF4B4B;
|
| 145 |
+
|
| 146 |
+
}
|
| 147 |
+
.st-ee {
|
| 148 |
+
background-color: #1B9C85;
|
| 149 |
+
}
|
| 150 |
+
.st-eb {
|
| 151 |
+
background-color: #FFD95A;
|
| 152 |
+
}
|
| 153 |
</style>
|
| 154 |
""",
|
| 155 |
unsafe_allow_html=True,
|
| 156 |
)
|
| 157 |
+
|
| 158 |
+
# class=""
|
| 159 |
+
# dispay the scores with metric widget
|
| 160 |
neutral_score = st.metric(label='Score', value=round(results['Neutral'], 4), label_visibility='collapsed')
|
| 161 |
negative_score = st.metric(label='Score', value=round(results['Negative'], 4), label_visibility='collapsed')
|
| 162 |
positive_score = st.metric(label='Score', value=round(results['Positive'], 4), label_visibility='collapsed')
|
| 163 |
+
|
| 164 |
+
# interpret_button = col2.button(label='Interpret',type='secondary', use_container_width=True)
|
| 165 |
+
if clear_button:
|
| 166 |
+
tweet = ""
|
|
|
|
|
|
|
|
|
|
|
|