File size: 3,729 Bytes
4ca63a2
 
 
 
 
 
bee2fdd
4ca63a2
 
 
 
 
 
 
c6612b9
4ca63a2
 
 
 
 
 
 
 
 
 
 
2e9f562
4ca63a2
 
 
 
 
 
bee2fdd
 
4ca63a2
 
4a19c17
4ca63a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce86bc2
4ca63a2
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import tweepy
import time
import pandas as pd
from transformers import pipeline
import matplotlib.pyplot as plt
import gradio as gr
import os

def twitter_auth(consumerkey,consumersecret):
  consumer_key = consumerkey
  consumer_secret = consumersecret

  auth = tweepy.AppAuthHandler(consumer_key,consumer_secret)

  api = tweepy.API(auth,wait_on_rate_limit= True)
  return api

"""## Helper function for handling ratelimit and pagination"""

def limit_handled(cursor):
  """
  Function takes the cursor and returns tweets
  """
  while True:
    try:
      yield cursor.next()
    except tweepy.errors.TweepyException:
      print('reached rate limit, sleeping for > 15 mins')
      time.sleep(15*61)
    except StopIteration:
      break

def tweets_collector(query,count):
  consumerkey = os.environ.get('consumerkey')
  consumersecret = os.environ.get('consumersecret')
  api = twitter_auth(consumerkey,consumersecret)
  query = query +' -filter:retweets'
  search = limit_handled(tweepy.Cursor(api.search_tweets,q = query,tweet_mode = 'extended',lang ='en',result_type ='recent').items(count))
  sentiment_analysis = pipeline(model = "finiteautomata/bertweet-base-sentiment-analysis")
  tweets = []

  for tweet in search:
    try:
      content = tweet.full_text
      sentiment = sentiment_analysis(content)
      tweets.append({'tweet' : content ,'sentiment': sentiment[0]['label']})
    except:
      pass
  return tweets

"""## Run sentiment Analysis"""

#tweets = tweets_collector(query,count)
#df = pd.DataFrame(tweets)

import pandas as pd

pd.set_option('max_colwidth',None)
pd.set_option('display.width',3000)

#import matplotlib.pyplot as plt

#sentiment_counts = df.groupby(['sentiment']).size()

#fig = plt.figure(figsize = (6,6),dpi = 100)
#ax = plt.subplot(111)
#sentiment_counts.plot.pie(ax = ax,autopct = '%1.f%%',startangle = 270,fontsize = 12,label = "")

def complaint_analysis(query,count):
  tweets = tweets_collector(query,count)
  df = pd.DataFrame(tweets)
  from wordcloud import WordCloud
  from wordcloud import STOPWORDS
  sentiment_counts = df.groupby(['sentiment']).size()
  fig = plt.figure(figsize = (6,6),dpi = 100)
  ax = plt.subplot(111)
  sentiment_counts.plot.pie(ax = ax,autopct = '%1.f%%',startangle = 270,fontsize = 12,label = "")
  plt.savefig('Overall_satisfaction.png')

  positive_tweets = df['tweet'][df['sentiment'] == 'POS']
  stop_words = ["https","co","RT","ola_supports","ola_cabs","customer"] + list(STOPWORDS)
  positive_wordcloud = WordCloud(max_font_size=50,max_words = 30,background_color="white",stopwords=stop_words).generate(str(positive_tweets))
  plt.figure()
  plt.title("Positive Tweets - Wordcloud")
  plt.imshow(positive_wordcloud,interpolation="bilinear")
  plt.axis("off")
  #plt.show()
  plt.savefig('positive_tweet.png')
  negative_tweets = df['tweet'][df['sentiment'] == 'NEG']
  stop_words = ["https","co","RT","ola_supports","ola_cabs","customer"] + list(STOPWORDS)
  negative_wordcloud = WordCloud(max_font_size=50,max_words = 30,background_color="white",stopwords=stop_words).generate(str(negative_tweets))
  plt.figure()
  plt.title("Negative Tweets - Wordcloud")
  plt.imshow(negative_wordcloud,interpolation="bilinear")
  plt.axis("off")
  #plt.show()
  plt.savefig('negative_tweet.png')
  return ['Overall_satisfaction.png','positive_tweet.png','negative_tweet.png']

gr.Interface(fn=complaint_analysis, 
             inputs=[
            gr.inputs.Textbox(
                placeholder="Tweet handle please", label="Company support Twitter Handle", lines=5), gr.Slider(100, 1000) ],
             outputs= [gr.outputs.Image(type="pil"),gr.outputs.Image(type="pil"),gr.outputs.Image(type="pil")],
             examples=[]).launch(debug= True)