Spaces:
Runtime error
Runtime error
Commit
·
4ca63a2
1
Parent(s):
0e5f2ec
initial commit
Browse files- complaintbox_appV1.py +111 -0
complaintbox_appV1.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import tweepy
|
4 |
+
import time
|
5 |
+
import pandas as pd
|
6 |
+
from transformers import pipeline
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
def twitter_auth(consumerkey,consumersecret):
|
11 |
+
consumer_key = consumerkey
|
12 |
+
consumer_secret = consumersecret
|
13 |
+
|
14 |
+
auth = tweepy.AppAuthHandler(consumer_key,consumer_secret)
|
15 |
+
|
16 |
+
api = tweepy.API(auth,wait_on_rate_limit= True,wait_on_rate_limit_notify=True)
|
17 |
+
return api
|
18 |
+
|
19 |
+
"""## Helper function for handling ratelimit and pagination"""
|
20 |
+
|
21 |
+
def limit_handled(cursor):
|
22 |
+
"""
|
23 |
+
Function takes the cursor and returns tweets
|
24 |
+
"""
|
25 |
+
while True:
|
26 |
+
try:
|
27 |
+
yield cursor.next()
|
28 |
+
except tweepy.RateLimitError:
|
29 |
+
print('reached rate limit, sleeping for > 15 mins')
|
30 |
+
time.sleep(15*61)
|
31 |
+
except StopIteration:
|
32 |
+
break
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
def tweets_collector(query,count):
|
37 |
+
api = twitter_auth(consumerkey,consumersecret)
|
38 |
+
query = query +' -filter:retweets'
|
39 |
+
search = limit_handled(tweepy.Cursor(api.search,q = query,tweet_mode = 'extended',lang ='en',result_type ='recent').items(count))
|
40 |
+
sentiment_analysis = pipeline(model = "finiteautomata/bertweet-base-sentiment-analysis")
|
41 |
+
tweets = []
|
42 |
+
|
43 |
+
for tweet in search:
|
44 |
+
try:
|
45 |
+
content = tweet.full_text
|
46 |
+
sentiment = sentiment_analysis(content)
|
47 |
+
tweets.append({'tweet' : content ,'sentiment': sentiment[0]['label']})
|
48 |
+
except:
|
49 |
+
pass
|
50 |
+
return tweets
|
51 |
+
|
52 |
+
"""## Run sentiment Analysis"""
|
53 |
+
|
54 |
+
#tweets = tweets_collector(query,count)
|
55 |
+
#df = pd.DataFrame(tweets)
|
56 |
+
|
57 |
+
import pandas as pd
|
58 |
+
|
59 |
+
pd.set_option('max_colwidth',None)
|
60 |
+
pd.set_option('display.width',3000)
|
61 |
+
|
62 |
+
#import matplotlib.pyplot as plt
|
63 |
+
|
64 |
+
#sentiment_counts = df.groupby(['sentiment']).size()
|
65 |
+
|
66 |
+
#fig = plt.figure(figsize = (6,6),dpi = 100)
|
67 |
+
#ax = plt.subplot(111)
|
68 |
+
#sentiment_counts.plot.pie(ax = ax,autopct = '%1.f%%',startangle = 270,fontsize = 12,label = "")
|
69 |
+
|
70 |
+
def complaint_analysis(query,count):
|
71 |
+
tweets = tweets_collector(query,count)
|
72 |
+
df = pd.DataFrame(tweets)
|
73 |
+
from wordcloud import WordCloud
|
74 |
+
from wordcloud import STOPWORDS
|
75 |
+
sentiment_counts = df.groupby(['sentiment']).size()
|
76 |
+
fig = plt.figure(figsize = (6,6),dpi = 100)
|
77 |
+
ax = plt.subplot(111)
|
78 |
+
sentiment_counts.plot.pie(ax = ax,autopct = '%1.f%%',startangle = 270,fontsize = 12,label = "")
|
79 |
+
plt.savefig('Overall_satisfaction.png')
|
80 |
+
|
81 |
+
positive_tweets = df['tweet'][df['sentiment'] == 'POS']
|
82 |
+
stop_words = ["https","co","RT","ola_supports","ola_cabs","customer"] + list(STOPWORDS)
|
83 |
+
positive_wordcloud = WordCloud(max_font_size=50,max_words = 30,background_color="white",stopwords=stop_words).generate(str(positive_tweets))
|
84 |
+
plt.figure()
|
85 |
+
plt.title("Positive Tweets - Wordcloud")
|
86 |
+
plt.imshow(positive_wordcloud,interpolation="bilinear")
|
87 |
+
plt.axis("off")
|
88 |
+
#plt.show()
|
89 |
+
plt.savefig('positive_tweet.png')
|
90 |
+
negative_tweets = df['tweet'][df['sentiment'] == 'NEG']
|
91 |
+
stop_words = ["https","co","RT","ola_supports","ola_cabs","customer"] + list(STOPWORDS)
|
92 |
+
negative_wordcloud = WordCloud(max_font_size=50,max_words = 30,background_color="white",stopwords=stop_words).generate(str(negative_tweets))
|
93 |
+
plt.figure()
|
94 |
+
plt.title("Negative Tweets - Wordcloud")
|
95 |
+
plt.imshow(negative_wordcloud,interpolation="bilinear")
|
96 |
+
plt.axis("off")
|
97 |
+
#plt.show()
|
98 |
+
plt.savefig('negative_tweet.png')
|
99 |
+
return ['Overall_satisfaction.png','positive_tweet.png','negative_tweet.png']
|
100 |
+
|
101 |
+
gr.Interface(fn=complaint_analysis,
|
102 |
+
inputs=[
|
103 |
+
gr.inputs.Textbox(
|
104 |
+
placeholder="Tweet handle ples", label="Company support Twitter Handle", lines=5), gr.Slider(100, 1000) ],
|
105 |
+
outputs= [gr.outputs.Image(type="pil"),gr.outputs.Image(type="pil"),gr.outputs.Image(type="pil")],
|
106 |
+
examples=[]).launch(debug= True)
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
|