Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
import
|
2 |
-
import praw # Reddit's API
|
3 |
import pandas as pd
|
|
|
4 |
import re # Regular expression module
|
|
|
5 |
import time
|
6 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
7 |
|
@@ -83,7 +84,7 @@ if st.button("Scrape Reddit"):
|
|
83 |
|
84 |
# Convert Date to datetime, sort descending and reset index
|
85 |
df['Date'] = pd.to_datetime(df['Date'], unit='s')
|
86 |
-
df = df.sort_values(by="Date", ascending=
|
87 |
|
88 |
progress_text.text(f"Collected {len(df)} valid posts.")
|
89 |
st.session_state["df"] = df
|
@@ -156,21 +157,46 @@ if st.button("Sentiment Analysis"):
|
|
156 |
|
157 |
|
158 |
with st.spinner("Doing Sentiment Analysis..."):
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
169 |
|
170 |
df = df.drop(["title_sentiment", "detail_sentiment"], axis=1)
|
171 |
cols = ["Title", "Title_sentiment_label", "Title_sentiment_score", \
|
172 |
"Detail", "Detail_sentiment_label", "Detail_sentiment_score", "Date"]
|
173 |
df = df[cols]
|
174 |
|
175 |
-
st.
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
|
|
2 |
import pandas as pd
|
3 |
+
import praw # Reddit's API
|
4 |
import re # Regular expression module
|
5 |
+
import streamlit as st
|
6 |
import time
|
7 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
8 |
|
|
|
84 |
|
85 |
# Convert Date to datetime, sort descending and reset index
|
86 |
df['Date'] = pd.to_datetime(df['Date'], unit='s')
|
87 |
+
df = df.sort_values(by="Date", ascending=True).reset_index(drop=True)
|
88 |
|
89 |
progress_text.text(f"Collected {len(df)} valid posts.")
|
90 |
st.session_state["df"] = df
|
|
|
157 |
|
158 |
|
159 |
with st.spinner("Doing Sentiment Analysis..."):
|
160 |
+
with st.spinner("Title Sentiment..."):
|
161 |
+
# Apply sentiment analysis to Title directly (assuming Title is short)
|
162 |
+
df['title_sentiment'] = df['Title'].apply(lambda x: safe_sentiment(preprocess_text(x)) if x else None)
|
163 |
+
df["Title_sentiment_label"] = df["title_sentiment"].apply(lambda x: x["label"] if x else None)
|
164 |
+
df["Title_sentiment_score"] = df["title_sentiment"].apply(lambda x: x["score"] if x else None)
|
165 |
|
166 |
+
with st.spinner("Detail Sentiment..."):
|
167 |
+
# Apply sentiment analysis to Detail by splitting into token-limited chunks and accumulating scores
|
168 |
+
df['detail_sentiment'] = df['Detail'].apply(lambda x: analyze_detail(x, tokenizer, sentiment_pipeline, max_tokens) if x else None)
|
169 |
+
df["Detail_sentiment_label"] = df["detail_sentiment"].apply(lambda x: x["label"] if x else None)
|
170 |
+
df["Detail_sentiment_score"] = df["detail_sentiment"].apply(lambda x: x["score"] if x else None)
|
171 |
|
172 |
df = df.drop(["title_sentiment", "detail_sentiment"], axis=1)
|
173 |
cols = ["Title", "Title_sentiment_label", "Title_sentiment_score", \
|
174 |
"Detail", "Detail_sentiment_label", "Detail_sentiment_score", "Date"]
|
175 |
df = df[cols]
|
176 |
|
177 |
+
st.session_state["df"] = df
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
if st.button("Draw Graph"):
|
182 |
+
df = st.session_state.get("df")
|
183 |
+
|
184 |
+
# Plot Title's sentiment_score
|
185 |
+
fig1, ax1 = plt.subplots(figsize=(10, 5))
|
186 |
+
ax1.plot(df["Date"], df["Title_sentiment_score"], marker="o", label="Title Sentiment Score")
|
187 |
+
ax1.set_title("Title Sentiment Score Over Time")
|
188 |
+
ax1.set_xlabel("Time")
|
189 |
+
ax1.set_ylabel("Sentiment Score")
|
190 |
+
ax1.legend()
|
191 |
+
plt.xticks(rotation=45)
|
192 |
+
st.pyplot(fig1)
|
193 |
+
|
194 |
+
# Plot Detail's sentiment_score
|
195 |
+
fig2, ax2 = plt.subplots(figsize=(10, 5))
|
196 |
+
ax2.plot(df["Date"], df["Detail_sentiment_score"], marker="△", label="Detail Sentiment Score")
|
197 |
+
ax2.set_title("Detail Sentiment Score Over Time")
|
198 |
+
ax2.set_xlabel("Time")
|
199 |
+
ax2.set_ylabel("Sentiment Score")
|
200 |
+
ax2.legend()
|
201 |
+
plt.xticks(rotation=45)
|
202 |
+
st.pyplot(fig2)
|