Spaces:
Runtime error
Runtime error
MaryanneMuchai
commited on
Commit
•
41632d5
1
Parent(s):
43ab1b3
Upload 2 files
Browse files- app.py +143 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# pip install streamlit,
|
2 |
+
# pip install pyngrok==4.1.1,
|
3 |
+
# pip install vaderSentiment,
|
4 |
+
# pip install transformers
|
5 |
+
|
6 |
+
import os
|
7 |
+
|
8 |
+
os.system('pip install --upgrade pip')
|
9 |
+
os.system('pip install textblob')
|
10 |
+
os.system('pip install vaderSentiment')
|
11 |
+
os.system('pip install transformers')
|
12 |
+
os.system('pip install numpy')
|
13 |
+
os.system('pip install scipy')
|
14 |
+
os.system('pip install streamlit')
|
15 |
+
os.system('pip install pandas')
|
16 |
+
os.system('pip install altair')
|
17 |
+
os.system('pip install vaderSentiment')
|
18 |
+
os.system('pip install torch')
|
19 |
+
os.system('pip install pyngrok')
|
20 |
+
os.system('pip install streamlit --upgrade')
|
21 |
+
|
22 |
+
import torch
|
23 |
+
import streamlit as st
|
24 |
+
from textblob import TextBlob
|
25 |
+
import pandas as pd
|
26 |
+
import altair as alt
|
27 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
28 |
+
|
29 |
+
from transformers import AutoModelForSequenceClassification
|
30 |
+
from transformers import TFAutoModelForSequenceClassification
|
31 |
+
from transformers import AutoTokenizer, AutoConfig
|
32 |
+
import numpy as np
|
33 |
+
from scipy.special import softmax
|
34 |
+
import streamlit as st
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
# Requirements
|
39 |
+
model_path = f"MaryanneMuchai/twitter-finetuned-model"
|
40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
41 |
+
config = AutoConfig.from_pretrained(model_path)
|
42 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
43 |
+
|
44 |
+
# Preprocess text (username and link placeholders)
|
45 |
+
def preprocess(text):
|
46 |
+
new_text = []
|
47 |
+
for t in text.split(" "):
|
48 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
49 |
+
t = 'http' if t.startswith('http') else t
|
50 |
+
new_text.append(t)
|
51 |
+
return " ".join(new_text)
|
52 |
+
|
53 |
+
|
54 |
+
def sentiment_analysis(text):
|
55 |
+
text = preprocess(text)
|
56 |
+
|
57 |
+
# PyTorch-based models
|
58 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
59 |
+
output = model(**encoded_input)
|
60 |
+
scores_ = output[0][0].detach().numpy()
|
61 |
+
scores_ = softmax(scores_)
|
62 |
+
|
63 |
+
# Format output dict of scores
|
64 |
+
labels = ['Negative', 'Neutral', 'Positive']
|
65 |
+
scores = {l:float(s) for (l,s) in zip(labels, scores_) }
|
66 |
+
|
67 |
+
return scores
|
68 |
+
def convert_to_df(sentiment):
|
69 |
+
sentiment_dict = {'polarity':sentiment.polarity,'subjectivity':sentiment.subjectivity}
|
70 |
+
sentiment_df = pd.DataFrame(sentiment_dict.items(),columns=['metric','value'])
|
71 |
+
return sentiment_df
|
72 |
+
|
73 |
+
def main():
|
74 |
+
st.title("Sentiment Analysis NLP App")
|
75 |
+
st.subheader("Streamlit Projects")
|
76 |
+
|
77 |
+
menu = ["Home","About"]
|
78 |
+
choice = st.sidebar.selectbox("Menu",menu)
|
79 |
+
|
80 |
+
if choice == "Home":
|
81 |
+
st.subheader("Home")
|
82 |
+
with st.form(key='nlpForm'):
|
83 |
+
raw_text = st.text_area("Enter Text Here")
|
84 |
+
submit_button = st.form_submit_button(label='Analyze')
|
85 |
+
|
86 |
+
# layout
|
87 |
+
col1,col2 = st.columns(2)
|
88 |
+
if submit_button:
|
89 |
+
|
90 |
+
with col1:
|
91 |
+
st.info("Results")
|
92 |
+
sentiment = TextBlob(raw_text).sentiment
|
93 |
+
st.write(sentiment)
|
94 |
+
|
95 |
+
# Emoji
|
96 |
+
if sentiment.polarity > 0:
|
97 |
+
st.markdown("Sentiment:: Positive :smiley: ")
|
98 |
+
elif sentiment.polarity < 0:
|
99 |
+
st.markdown("Sentiment:: Negative :angry: ")
|
100 |
+
else:
|
101 |
+
st.markdown("Sentiment:: Neutral ?? ")
|
102 |
+
|
103 |
+
# Dataframe
|
104 |
+
result_df = convert_to_df(sentiment)
|
105 |
+
st.dataframe(result_df)
|
106 |
+
|
107 |
+
# Visualization
|
108 |
+
c = alt.Chart(result_df).mark_bar().encode(
|
109 |
+
x='metric',
|
110 |
+
y='value',
|
111 |
+
color='metric')
|
112 |
+
st.altair_chart(c,use_container_width=True)
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
with col2:
|
117 |
+
st.info("Token Sentiment")
|
118 |
+
|
119 |
+
token_sentiments = sentiment_analysis(raw_text)
|
120 |
+
st.write(token_sentiments)
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
else:
|
128 |
+
st.subheader("About")
|
129 |
+
|
130 |
+
|
131 |
+
if __name__ == '__main__':
|
132 |
+
main()
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
# Expose the app publicly using ngrok
|
137 |
+
# from pyngrok import ngrok
|
138 |
+
# public_url = ngrok.connect(port='8501')
|
139 |
+
# public_url
|
140 |
+
|
141 |
+
# !streamlit run --server.port 8501 Sentiment_Analysis.py
|
142 |
+
|
143 |
+
# !pip freeze > requirements.txt
|
requirements.txt
ADDED
Binary file (2.23 kB). View file
|
|