Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Twitter API keys (should be stored securely, e.g., in environment variables)
|
10 |
+
consumer_key = "your_consumer_key"
|
11 |
+
consumer_secret = "your_consumer_secret"
|
12 |
+
access_key = "your_access_key"
|
13 |
+
access_secret = "your_access_secret"
|
14 |
+
|
15 |
+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
|
16 |
+
auth.set_access_token(access_key, access_secret)
|
17 |
+
api = tweepy.API(auth)
|
18 |
+
|
19 |
+
def get_tweets(username, count):
|
20 |
+
tweets = tweepy.Cursor(api.user_timeline, screen_name=username, tweet_mode="extended", exclude_replies=True, include_rts=False).items(count)
|
21 |
+
tweets = list(tweets)
|
22 |
+
response = {
|
23 |
+
"tweets": [tweet.full_text.replace("\n", "").lower() for tweet in tweets],
|
24 |
+
"timestamps": [str(tweet.created_at) for tweet in tweets],
|
25 |
+
"retweets": [tweet.retweet_count for tweet in tweets],
|
26 |
+
"likes": [tweet.favorite_count for tweet in tweets],
|
27 |
+
}
|
28 |
+
return response
|
29 |
+
|
30 |
+
def get_sentiment(texts):
|
31 |
+
preds = pipe(texts)
|
32 |
+
response = {
|
33 |
+
"labels": [pred["label"] for pred in preds],
|
34 |
+
"scores": [pred["score"] for pred in preds],
|
35 |
+
}
|
36 |
+
return response
|
37 |
+
|
38 |
+
def get_aggregation_period(df):
|
39 |
+
t_min, t_max = df["timestamps"].min(), df["timestamps"].max()
|
40 |
+
t_delta = t_max - t_min
|
41 |
+
if t_delta < pd.to_timedelta("30D"):
|
42 |
+
return "1D"
|
43 |
+
elif t_delta < pd.to_timedelta("365D"):
|
44 |
+
return "7D"
|
45 |
+
else:
|
46 |
+
return "30D"
|
47 |
+
|
48 |
+
@st.cache_data
|
49 |
+
def load_model():
|
50 |
+
pipe = pipeline(task="sentiment-analysis", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
51 |
+
return pipe
|
52 |
+
|
53 |
+
# Streamlit app
|
54 |
+
st.title("Twitter Emotion Analyser")
|
55 |
+
|
56 |
+
pipe = load_model()
|
57 |
+
twitter_handle = st.sidebar.text_input("Twitter handle:", "elonmusk")
|
58 |
+
twitter_count = st.sidebar.selectbox("Number of tweets:", (10, 30, 50, 100))
|
59 |
+
|
60 |
+
if st.sidebar.button("Get tweets!"):
|
61 |
+
tweets = get_tweets(twitter_handle, twitter_count)
|
62 |
+
preds = get_sentiment(tweets["tweets"])
|
63 |
+
tweets.update(preds)
|
64 |
+
|
65 |
+
df = pd.DataFrame(tweets)
|
66 |
+
df["timestamps"] = pd.to_datetime(df["timestamps"])
|
67 |
+
|
68 |
+
agg_period = get_aggregation_period(df)
|
69 |
+
ts_sentiment = df.groupby(["timestamps", "labels"]).count()["likes"].unstack().resample(agg_period).count().stack().reset_index()
|
70 |
+
ts_sentiment.columns = ["timestamp", "label", "count"]
|
71 |
+
|
72 |
+
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.15)
|
73 |
+
|
74 |
+
for label in ts_sentiment["label"].unique():
|
75 |
+
fig.add_trace(go.Scatter(x=ts_sentiment.query("label == @label")["timestamp"], y=ts_sentiment.query("label == @label")["count"], mode="lines", name=label, stackgroup="one", hoverinfo="x+y"), row=1, col=1)
|
76 |
+
|
77 |
+
likes_per_label = df.groupby("labels")["likes"].mean().reset_index()
|
78 |
+
|
79 |
+
fig.add_trace(go.Bar(x=likes_per_label["labels"], y=likes_per_label["likes"], showlegend=False, marker_color=px.colors.qualitative.Plotly, opacity=0.6), row=1, col=2)
|
80 |
+
|
81 |
+
fig.update_yaxes(title_text="Number of Tweets", row=1, col=1)
|
82 |
+
fig.update_yaxes(title_text="Number of Likes", row=1, col=2)
|
83 |
+
fig.update_layout(height=350, width=750)
|
84 |
+
|
85 |
+
st.plotly_chart(fig)
|
86 |
+
st.markdown(df.to_markdown())
|