khadija3818 commited on
Commit
fea44c9
·
1 Parent(s): 3548aff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -38
app.py CHANGED
@@ -1,44 +1,83 @@
1
- from deepsparse import Pipeline
2
- import time
3
- import gradio as gr
 
 
 
4
 
5
- task = "zero_shot_text_classification"
6
- sparse_classification_pipeline = Pipeline.create(
7
- task=task,
8
- model_path="zoo:nlp/text_classification/distilbert-none/pytorch/huggingface/mnli/pruned80_quant-none-vnni",
9
- model_scheme="mnli",
10
- model_config={"hypothesis_template": "This text is related to {}"},
11
- )
12
 
13
- def run_pipeline(text):
14
- sparse_start = time.perf_counter()
15
- sparse_output = sparse_classification_pipeline(sequences=text, labels=['politics', 'public health', 'Europe'])
16
- sparse_result = dict(sparse_output)
17
- sparse_end = time.perf_counter()
18
- sparse_duration = (sparse_end - sparse_start) * 1000.0
19
- dict_r = {
20
- sparse_result['labels'][0]: sparse_result['scores'][0],
21
- sparse_result['labels'][1]: sparse_result['scores'][1],
22
- sparse_result['labels'][2]: sparse_result['scores'][2]
23
- }
24
- return dict_r, sparse_duration
25
 
26
- with gr.Blocks() as demo:
27
- with gr.Row():
28
- with gr.Column():
29
- text = gr.Textbox(placeholder="Enter text here...", label="Text", lines=5, width=500)
30
- btn = gr.Button("Submit", style="info", size="lg", block=True)
31
 
32
- with gr.Column():
33
-
34
- sparse_answers = gr.Label(label="Sparse Model Answers", num_top_classes=3, style="info")
35
- sparse_duration = gr.Number(label="Sparse Latency (ms)", style="success", size="lg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- btn.click(
38
- run_pipeline,
39
- inputs=[text],
40
- outputs=[sparse_answers, sparse_duration],
41
- )
 
 
 
 
42
 
43
- if __name__ == "__main__":
44
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import plotly.express as px
5
+ from wordcloud import WordCloud, STOPWORDS
6
+ import matplotlib.pyplot as plt
7
 
8
+ st.set_option('deprecation.showPyplotGlobalUse', False)
 
 
 
 
 
 
9
 
10
+ DATA_ = pd.read_csv("Tweets.csv")
11
+ st.title("Sentiment Analysis of Tweets about US Airlines")
12
+ st.sidebar.title("Sentiment Analysis of Tweets about US Airlines")
13
+ st.markdown("This application is a streamlit dashboard to analyze the sentiment of Tweets")
14
+ st.sidebar.markdown("This application is a streamlit dashboard to analyze the sentiment of Tweets")
 
 
 
 
 
 
 
15
 
 
 
 
 
 
16
 
17
+ def run():
18
+
19
+ @st.cache(persist=True)
20
+ def load_data():
21
+ DATA_['tweet_created'] = pd.to_datetime(DATA_['tweet_created'])
22
+ return DATA_
23
+ data = load_data()
24
+
25
+ st.sidebar.subheader("Show random tweet")
26
+ random_tweet = st.sidebar.radio('Sentiment', ('positive', 'neutral', 'negative'))
27
+ st.sidebar.markdown(data.query('airline_sentiment == @random_tweet')[["text"]].sample(n=1).iat[0,0])
28
+
29
+ st.sidebar.markdown("### Number of tweets by sentiment")
30
+ select = st.sidebar.selectbox('Visualization type', ['Histogram', 'Pie chart'])
31
+ sentiment_count = data['airline_sentiment'].value_counts()
32
+ sentiment_count = pd.DataFrame({'Sentiment':sentiment_count.index, 'Tweets':sentiment_count.values})
33
+
34
+ if not st.sidebar.checkbox("Hide", True):
35
+ st.markdown("### Number of tweets by sentiment")
36
+ if select == "Histogram":
37
+ fig = px.bar(sentiment_count, x='Sentiment', y='Tweets', color='Tweets', height=500)
38
+ st.plotly_chart(fig)
39
+ else:
40
+ fig = px.pie(sentiment_count, values='Tweets', names='Sentiment')
41
+ st.plotly_chart(fig)
42
+
43
+
44
+ st.sidebar.subheader("When and Where are users tweeting from?")
45
+ hour = st.sidebar.slider("Hour of day", 0,23)
46
+ modified_data = data[data['tweet_created'].dt.hour == hour]
47
+ if not st.sidebar.checkbox("Close", True, key='1'):
48
+ st.markdown("### Tweets locations based on the time of date")
49
+ st.markdown("%i tweets between %i:00 and %i:00" % (len(modified_data), hour, (hour+1)%24))
50
+ st.map(modified_data)
51
+ if st.sidebar.checkbox("Show Raw Data", False):
52
+ st.write(modified_data)
53
+ st.sidebar.subheader("Breakdown airline tweets by sentiment")
54
+ choice = st.sidebar.multiselect('Pick airline', ('US Airways', 'United', 'American', 'Southwest', 'Delta', 'Virgin America'), key='0')
55
 
56
+ if len(choice) > 0:
57
+ choice_data = data[data.airline.isin(choice)]
58
+ fig_choice = px.histogram(choice_data, x='airline',
59
+ y='airline_sentiment',
60
+ histfunc = 'count', color = 'airline_sentiment',
61
+ facet_col='airline_sentiment',
62
+ labels={'airline_sentiment':'tweets'}, height=600, width=800)
63
+ st.plotly_chart(fig_choice)
64
+
65
 
66
+ st.sidebar.header("Word Cloud")
67
+ word_sentiment = st.sidebar.radio('Display word cloud for what sentiment?',('positive', 'neutral','negative'))
68
+
69
+ if not st.sidebar.checkbox("Close", True, key='3'):
70
+ st.header('Word cloud for %s sentiment' % (word_sentiment))
71
+ df = data[data['airline_sentiment']==word_sentiment]
72
+ words = ' '.join(df['text'])
73
+ processed_words = ' '.join([word for word in words.split() if 'http' not in word and not word.startswith('@') and word !='RT'])
74
+ wordcloud = WordCloud(stopwords=STOPWORDS,
75
+ background_color='white', height=640, width=800).generate(processed_words)
76
+ plt.imshow(wordcloud)
77
+ plt.xticks([])
78
+ plt.yticks([])
79
+ st.pyplot()
80
+
81
+
82
+ if __name__ == '__main__':
83
+ run()