Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,140 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import plotly.express as px
|
| 3 |
-
import plotly.graph_objects as go
|
| 4 |
-
import streamlit as st
|
| 5 |
-
import tweepy
|
| 6 |
-
from plotly.subplots import make_subplots
|
| 7 |
-
from transformers import pipeline
|
| 8 |
-
|
| 9 |
-
consumer_key = "sHz78Xj5Dl41cqfzEHVoRcaKo"
|
| 10 |
-
consumer_secret = "3y5caZfu91nmB2MNH7mDSu5Cgf5qaVRpMfbDoCPW4dU7E46k03"
|
| 11 |
-
access_key = "1116912581434695680-x359MscPSdqEcJzoIlg4jMsCZRdyNX"
|
| 12 |
-
access_secret = "wEsALFUava2TnYXWnuacrzSK4eiYfJUFLBRWPqGuMRnTz"
|
| 13 |
-
|
| 14 |
-
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
|
| 15 |
-
auth.set_access_token(access_key,access_secret)
|
| 16 |
-
api = tweepy.API(auth)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def get_tweets(username, count):
|
| 20 |
-
tweets = tweepy.Cursor(
|
| 21 |
-
api.user_timeline,
|
| 22 |
-
screen_name=username,
|
| 23 |
-
tweet_mode="extended",
|
| 24 |
-
exclude_replies=True,
|
| 25 |
-
include_rts=False,
|
| 26 |
-
).items(count)
|
| 27 |
-
|
| 28 |
-
tweets = list(tweets)
|
| 29 |
-
response = {
|
| 30 |
-
"tweets": [tweet.full_text.replace("\n", "").lower() for tweet in tweets],
|
| 31 |
-
"timestamps": [str(tweet.created_at) for tweet in tweets],
|
| 32 |
-
"retweets": [tweet.retweet_count for tweet in tweets],
|
| 33 |
-
"likes": [tweet.favorite_count for tweet in tweets],
|
| 34 |
-
}
|
| 35 |
-
return response
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def get_sentiment(texts):
|
| 39 |
-
preds = pipe(texts)
|
| 40 |
-
|
| 41 |
-
response = dict()
|
| 42 |
-
response["labels"] = [pred["label"] for pred in preds]
|
| 43 |
-
response["scores"] = [pred["score"] for pred in preds]
|
| 44 |
-
return response
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def neutralise_sentiment(preds):
|
| 48 |
-
for i, (label, score) in enumerate(zip(preds["labels"], preds["scores"])):
|
| 49 |
-
if score < 0.5:
|
| 50 |
-
preds["labels"][i] = "neutral"
|
| 51 |
-
preds["scores"][i] = 1.0 - score
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def get_aggregation_period(df):
|
| 55 |
-
t_min, t_max = df["timestamps"].min(), df["timestamps"].max()
|
| 56 |
-
t_delta = t_max - t_min
|
| 57 |
-
if t_delta < pd.to_timedelta("30D"):
|
| 58 |
-
return "1D"
|
| 59 |
-
elif t_delta < pd.to_timedelta("365D"):
|
| 60 |
-
return "7D"
|
| 61 |
-
else:
|
| 62 |
-
return "30D"
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
@st.cache_data
|
| 66 |
-
def load_model():
|
| 67 |
-
pipe = pipeline(task="sentiment-analysis", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
| 68 |
-
return pipe
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"""
|
| 72 |
-
# Twitter Emotion Analyser
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
pipe = load_model()
|
| 77 |
-
twitter_handle = st.sidebar.text_input("Twitter handle:", "elonmusk")
|
| 78 |
-
twitter_count = st.sidebar.selectbox("Number of tweets:", (10, 30, 50, 100))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
if st.sidebar.button("Get tweets!"):
|
| 82 |
-
tweets = get_tweets(twitter_handle, twitter_count)
|
| 83 |
-
preds = get_sentiment(tweets["tweets"])
|
| 84 |
-
# neutralise_sentiment(preds)
|
| 85 |
-
tweets.update(preds)
|
| 86 |
-
# dataframe creation + preprocessing
|
| 87 |
-
df = pd.DataFrame(tweets)
|
| 88 |
-
df["timestamps"] = pd.to_datetime(df["timestamps"])
|
| 89 |
-
# plots
|
| 90 |
-
agg_period = get_aggregation_period(df)
|
| 91 |
-
ts_sentiment = (
|
| 92 |
-
df.groupby(["timestamps", "labels"])
|
| 93 |
-
.count()["likes"]
|
| 94 |
-
.unstack()
|
| 95 |
-
.resample(agg_period)
|
| 96 |
-
.count()
|
| 97 |
-
.stack()
|
| 98 |
-
.reset_index()
|
| 99 |
-
)
|
| 100 |
-
ts_sentiment.columns = ["timestamp", "label", "count"]
|
| 101 |
-
|
| 102 |
-
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.15)
|
| 103 |
-
|
| 104 |
-
# TODO: check that stacking makes sense!
|
| 105 |
-
for label in ts_sentiment["label"].unique():
|
| 106 |
-
fig.add_trace(
|
| 107 |
-
go.Scatter(
|
| 108 |
-
x=ts_sentiment.query("label == @label")["timestamp"],
|
| 109 |
-
y=ts_sentiment.query("label == @label")["count"],
|
| 110 |
-
mode="lines",
|
| 111 |
-
name=label,
|
| 112 |
-
stackgroup="one",
|
| 113 |
-
hoverinfo="x+y",
|
| 114 |
-
),
|
| 115 |
-
row=1,
|
| 116 |
-
col=1,
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
likes_per_label = df.groupby("labels")["likes"].mean().reset_index()
|
| 120 |
-
|
| 121 |
-
fig.add_trace(
|
| 122 |
-
go.Bar(
|
| 123 |
-
x=likes_per_label["labels"],
|
| 124 |
-
y=likes_per_label["likes"],
|
| 125 |
-
showlegend=False,
|
| 126 |
-
marker_color=px.colors.qualitative.Plotly,
|
| 127 |
-
opacity=0.6,
|
| 128 |
-
),
|
| 129 |
-
row=1,
|
| 130 |
-
col=2,
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
fig.update_yaxes(title_text="Number of Tweets", row=1, col=1)
|
| 134 |
-
fig.update_yaxes(title_text="Number of Likes", row=1, col=2)
|
| 135 |
-
fig.update_layout(height=350, width=750)
|
| 136 |
-
|
| 137 |
-
st.plotly_chart(fig)
|
| 138 |
-
|
| 139 |
-
# tweet sample
|
| 140 |
-
st.markdown(df.to_markdown())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|