Spaces:
Runtime error
Runtime error
Added app files
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
# from scipy.special import softmax
|
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 |
+
# Requirements
|
12 |
+
model_path = "bright1/fine-tuned-distilbert-base-uncased"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
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 header. This is an *extremely* cool app!"
|
26 |
+
}
|
27 |
+
)
|
28 |
+
|
29 |
+
|
30 |
+
my_expander = st.container()
|
31 |
+
|
32 |
+
|
33 |
+
# st.sidebar.selectbox('Menu', ['About', 'Model'])
|
34 |
+
with my_expander:
|
35 |
+
|
36 |
+
st.markdown("""
|
37 |
+
<style>
|
38 |
+
h1 {
|
39 |
+
text-align: center;
|
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>
|
55 |
+
# Results from Analyzer
|
56 |
+
# </p>
|
57 |
+
# """,unsafe_allow_html=True)
|
58 |
+
st.markdown("""
|
59 |
+
<style>
|
60 |
+
p {
|
61 |
+
font-color: blue;
|
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 |
+
empty_container = col2.container()
|
70 |
+
empty_container.text("Results from Analyzer")
|
71 |
+
empty_container2 = col3.container()
|
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 |
+
"""
|
86 |
+
<style>
|
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 |
+
time.sleep(5)
|
98 |
+
success_message.empty()
|
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 |
+
|
utils.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import AutoTokenizer, AutoConfig,AutoModelForSequenceClassification
|
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):
|
16 |
+
if os.path.isfile(csv_file):
|
17 |
+
data.to_csv(csv_file, mode='a', header=False, index=False, encoding='utf-8')
|
18 |
+
else:
|
19 |
+
history = data.copy()
|
20 |
+
history.to_csv(csv_file, index=False)
|
21 |
+
|
22 |
+
#Preprocess text
|
23 |
+
def preprocess(text):
|
24 |
+
new_text = []
|
25 |
+
for t in text.split(" "):
|
26 |
+
t = "@user" if t.startswith("@") and len(t) > 1 else t
|
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 |
+
|
34 |
+
#Process the input and return prediction
|
35 |
+
def run_sentiment_analysis(text, tokenizer, model):
|
36 |
+
# save_text = {'tweet': text}
|
37 |
+
encoded_input = tokenizer(text, return_tensors = "pt") # for PyTorch-based models
|
38 |
+
output = model(**encoded_input)
|
39 |
+
scores_ = output[0][0].detach().numpy()
|
40 |
+
scores_ = softmax(scores_)
|
41 |
+
|
42 |
+
# Format output dict of scores
|
43 |
+
labels = ["Negative", "Neutral", "Positive"]
|
44 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
45 |
+
# save_text.update(scores)
|
46 |
+
# user_data = {key: [value] for key,value in save_text.items()}
|
47 |
+
# data = pd.DataFrame(user_data,)
|
48 |
+
# check_csv('history.csv', data)
|
49 |
+
# hist_df = pd.read_csv('history.csv')
|
50 |
+
return scores
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|