Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
def drawTweet(tweet,i):
|
| 20 |
-
|
| 21 |
width, height = 1000, 200
|
| 22 |
|
| 23 |
-
|
| 24 |
image = Image.new('RGBA', (width, height), 'white')
|
| 25 |
|
| 26 |
-
|
| 27 |
draw = ImageDraw.Draw(image)
|
| 28 |
|
| 29 |
-
|
| 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 |
-
|
| 37 |
|
| 38 |
|
| 39 |
-
tweet_text = text
|
| 40 |
-
|
| 41 |
words = tweet_text.split()
|
| 42 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 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 |
-
|
| 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 |
|