furquan commited on
Commit
c4e4261
·
1 Parent(s): dd059da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -8,38 +8,47 @@ import seaborn as sns
8
  from matplotlib.backends.backend_agg import FigureCanvasAgg
9
  from io import BytesIO
10
 
11
- import twitter
 
12
 
13
- api = twitter.Api(consumer_key='IXpQTCB9vo9IGfOVAPBePE2Wi',
14
- consumer_secret='qD1m4zaAiM6h2T7swBuWboORTXY4cA9eNcgDHlfFAuqKfNTiT3',
15
- access_token_key='1529787212417605634-Io7LlY8AEdZEzOgiAYMb3hZyu9gsLL',
16
- access_token_secret='QGo3eOn7xgPWHusmuP2JDZxkTMPJ51wtgO9wV3PY1b8wm')
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
  def drawTweet(tweet,i):
20
- # Set the dimensions of the image
21
  width, height = 1000, 200
22
 
23
- # Create a blank image with a white background
24
  image = Image.new('RGBA', (width, height), 'white')
25
 
26
- # Get a drawing context
27
  draw = ImageDraw.Draw(image)
28
 
29
- # Set the font for the tweet text
30
  font = ImageFont.truetype('arial.ttf', size=36, encoding='utf-16')
31
 
32
  user = tweet.user
33
 
34
 
35
  user_tag = user.screen_name
36
- text = tweet.text
37
 
38
 
39
- tweet_text = text
40
-
41
  words = tweet_text.split()
42
- # Insert a newline character after every 10 words
43
  formatted_string = ''
44
  for i, word in enumerate(words):
45
  formatted_string += word+' '
@@ -68,13 +77,12 @@ def drawTweet(tweet,i):
68
 
69
 
70
  def collect_tweets(topic):
71
-
72
- # Search for tweets matching the query
73
- tweets = api.GetSearch(term=f"{topic} -filter:retweets", lang='en', result_type="recent", count=100)
74
-
75
- # Filter out retweets
76
 
77
- tweets.sort(key=lambda tweet: tweet.favorite_count + tweet.retweet_count, reverse=True)
 
 
 
 
78
 
79
  images = []
80
  i = 1
@@ -87,11 +95,12 @@ def collect_tweets(topic):
87
  return images,sentiment_plot
88
 
89
  def sentiment_analysis(tweets,topic):
90
-
 
91
  tweet_procs = []
92
  for tweet in tweets:
93
  tweet_words = []
94
- for word in tweet.text.split(' '):
95
  if word.startswith('@') and len(word) > 1:
96
  word = '@user'
97
  elif word.startswith('https'):
@@ -104,6 +113,8 @@ def sentiment_analysis(tweets,topic):
104
  API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment"
105
  headers = {"Authorization": "Bearer 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"}
106
 
 
 
107
  def query(payload):
108
  response = requests.post(API_URL, headers=headers, json=payload)
109
  return response.json()
@@ -152,10 +163,9 @@ def sentiment_analysis(tweets,topic):
152
 
153
 
154
 
155
- # Create the Gradio app
156
  app = gr.Interface(fn=collect_tweets, inputs=gr.Textbox(label="Enter a topic for tweets"), outputs=[gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="50"), gr.Image(label="Sentiment Analysis Result")])
157
 
158
- # Run the app
159
  app.launch()
160
 
161
 
 
8
  from matplotlib.backends.backend_agg import FigureCanvasAgg
9
  from io import BytesIO
10
 
11
+ import configparser
12
+ import tweepy
13
 
14
+ config = configparser.ConfigParser()
15
+ config.read('./config.ini')
16
+
17
+ api_key = config['twitter']['api_key']
18
+ api_key_secret = config['twitter']['api_key_secret']
19
+ access_token = config['twitter']['access_token']
20
+ access_token_secret = config['twitter']['access_token_secret']
21
+
22
+ # Authenticate with Twitter
23
+ auth = tweepy.OAuthHandler(api_key, api_key_secret)
24
+ auth.set_access_token(access_token, access_token_secret)
25
+ api = tweepy.API(auth)
26
+
27
+ tweets = []
28
 
29
 
30
  def drawTweet(tweet,i):
31
+
32
  width, height = 1000, 200
33
 
34
+
35
  image = Image.new('RGBA', (width, height), 'white')
36
 
37
+
38
  draw = ImageDraw.Draw(image)
39
 
40
+
41
  font = ImageFont.truetype('arial.ttf', size=36, encoding='utf-16')
42
 
43
  user = tweet.user
44
 
45
 
46
  user_tag = user.screen_name
47
+ tweet_text = tweet.full_text
48
 
49
 
 
 
50
  words = tweet_text.split()
51
+
52
  formatted_string = ''
53
  for i, word in enumerate(words):
54
  formatted_string += word+' '
 
77
 
78
 
79
  def collect_tweets(topic):
 
 
 
 
 
80
 
81
+
82
+ limit=20
83
+ tweets = tweepy.Cursor(api.search_tweets,q=f"{topic} -filter:retweets", lang="en", tweet_mode='extended', result_type = 'recent').items(limit)
84
+
85
+ tweets = [tweet for tweet in tweets]
86
 
87
  images = []
88
  i = 1
 
95
  return images,sentiment_plot
96
 
97
  def sentiment_analysis(tweets,topic):
98
+
99
+
100
  tweet_procs = []
101
  for tweet in tweets:
102
  tweet_words = []
103
+ for word in tweet.full_text.split(' '):
104
  if word.startswith('@') and len(word) > 1:
105
  word = '@user'
106
  elif word.startswith('https'):
 
113
  API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment"
114
  headers = {"Authorization": "Bearer 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"}
115
 
116
+ print(len(tweet_procs))
117
+
118
  def query(payload):
119
  response = requests.post(API_URL, headers=headers, json=payload)
120
  return response.json()
 
163
 
164
 
165
 
166
+
167
  app = gr.Interface(fn=collect_tweets, inputs=gr.Textbox(label="Enter a topic for tweets"), outputs=[gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="50"), gr.Image(label="Sentiment Analysis Result")])
168
 
 
169
  app.launch()
170
 
171