Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -143,7 +143,7 @@ def fetch_stock_price(symbol, start_date, end_date):
|
|
| 143 |
|
| 144 |
|
| 145 |
# --------------------------
|
| 146 |
-
# MAIN APP
|
| 147 |
# --------------------------
|
| 148 |
def main():
|
| 149 |
st.title("📰 SentimentSync NewsAI")
|
|
@@ -219,7 +219,6 @@ def main():
|
|
| 219 |
# 2. เทรนโมเดล Prediction โดยใช้ข้อมูล "รายวัน"
|
| 220 |
df_sorted = daily_data.sort_values("date_day").copy()
|
| 221 |
|
| 222 |
-
# ป้องกัน Error ถ้ามีข้อมูลน้อยกว่า 2 วัน
|
| 223 |
if len(df_sorted) < 2:
|
| 224 |
st.warning("มีข้อมูลข่าวไม่เพียงพอที่จะสร้างแนวโน้ม (น้อยกว่า 2 วัน)")
|
| 225 |
return
|
|
@@ -242,14 +241,19 @@ def main():
|
|
| 242 |
st.info(f"กำลังดึงราคาหุ้น {symbol} ระหว่างวันที่ {min_date.strftime('%Y-%m-%d')} ถึง {max_date.strftime('%Y-%m-%d')}...")
|
| 243 |
stock_df = fetch_stock_price(symbol, min_date, max_date)
|
| 244 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
# 4. สร้างกราฟ (Plot) ด้วย Subplots
|
| 246 |
fig = make_subplots(rows=2, cols=1, specs=[[{"secondary_y": True}], [{}]],
|
| 247 |
row_heights=[0.7, 0.3], vertical_spacing=0.1,
|
| 248 |
shared_xaxes=True)
|
| 249 |
|
| 250 |
# --- กราฟส่วนบน (ราคา, Sentiment, Prediction) ---
|
| 251 |
-
|
| 252 |
-
# Add stock price (Y-axis 1, สีเขียว)
|
| 253 |
if not stock_df.empty:
|
| 254 |
fig.add_trace(
|
| 255 |
go.Scatter(
|
|
@@ -260,7 +264,6 @@ def main():
|
|
| 260 |
row=1, col=1, secondary_y=False
|
| 261 |
)
|
| 262 |
|
| 263 |
-
# Add daily sentiment score (Y-axis 2, สีน้ำเงิน)
|
| 264 |
fig.add_trace(
|
| 265 |
go.Scatter(
|
| 266 |
x=df_sorted["date_day"], y=df_sorted["avg_sentiment"],
|
|
@@ -271,17 +274,14 @@ def main():
|
|
| 271 |
row=1, col=1, secondary_y=True
|
| 272 |
)
|
| 273 |
|
| 274 |
-
# (ใหม่) Add Predicted sentiment (Y-axis 2, สีส้ม)
|
| 275 |
-
# ----- (นี่คือส่วนที่แก้ไข) -----
|
| 276 |
fig.add_trace(go.Scatter(
|
| 277 |
x=future_dates, y=future_preds,
|
| 278 |
mode="lines+markers", name="Predicted Sentiment (7-day Forecast)",
|
| 279 |
line=dict(color="orange", dash="dash")
|
| 280 |
),
|
| 281 |
-
row=1, col=1,
|
| 282 |
-
secondary_y=True
|
| 283 |
)
|
| 284 |
-
# ------------------------------
|
| 285 |
|
| 286 |
# --- กราฟส่วนล่าง (จำนวนข่าว) ---
|
| 287 |
fig.add_trace(go.Bar(x=df_sorted["date_day"], y=df_sorted["neutral"], name="Neutral", marker_color='rgba(128, 128, 128, 0.7)'), row=2, col=1)
|
|
@@ -306,7 +306,7 @@ def main():
|
|
| 306 |
|
| 307 |
st.plotly_chart(fig, use_container_width=True)
|
| 308 |
|
| 309 |
-
# แสดงข่าว
|
| 310 |
st.subheader("📰 รายการข่าว")
|
| 311 |
st.dataframe(news_df[["date", "source", "text", "sentiment", "url"]], use_container_width=True)
|
| 312 |
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
# --------------------------
|
| 146 |
+
# MAIN APP (เพิ่ม Code Debug)
|
| 147 |
# --------------------------
|
| 148 |
def main():
|
| 149 |
st.title("📰 SentimentSync NewsAI")
|
|
|
|
| 219 |
# 2. เทรนโมเดล Prediction โดยใช้ข้อมูล "รายวัน"
|
| 220 |
df_sorted = daily_data.sort_values("date_day").copy()
|
| 221 |
|
|
|
|
| 222 |
if len(df_sorted) < 2:
|
| 223 |
st.warning("มีข้อมูลข่าวไม่เพียงพอที่จะสร้างแนวโน้ม (น้อยกว่า 2 วัน)")
|
| 224 |
return
|
|
|
|
| 241 |
st.info(f"กำลังดึงราคาหุ้น {symbol} ระหว่างวันที่ {min_date.strftime('%Y-%m-%d')} ถึง {max_date.strftime('%Y-%m-%d')}...")
|
| 242 |
stock_df = fetch_stock_price(symbol, min_date, max_date)
|
| 243 |
|
| 244 |
+
# ---------------------------------
|
| 245 |
+
# ----- (เพิ่ม DEBUGGING) -----
|
| 246 |
+
st.subheader("🕵️ DEBUGGING INFO: ข้อมูลราคาหุ้นที่ดึงได้")
|
| 247 |
+
st.write(f"Found {len(stock_df)} stock price data points:")
|
| 248 |
+
st.dataframe(stock_df)
|
| 249 |
+
# ---------------------------------
|
| 250 |
+
|
| 251 |
# 4. สร้างกราฟ (Plot) ด้วย Subplots
|
| 252 |
fig = make_subplots(rows=2, cols=1, specs=[[{"secondary_y": True}], [{}]],
|
| 253 |
row_heights=[0.7, 0.3], vertical_spacing=0.1,
|
| 254 |
shared_xaxes=True)
|
| 255 |
|
| 256 |
# --- กราฟส่วนบน (ราคา, Sentiment, Prediction) ---
|
|
|
|
|
|
|
| 257 |
if not stock_df.empty:
|
| 258 |
fig.add_trace(
|
| 259 |
go.Scatter(
|
|
|
|
| 264 |
row=1, col=1, secondary_y=False
|
| 265 |
)
|
| 266 |
|
|
|
|
| 267 |
fig.add_trace(
|
| 268 |
go.Scatter(
|
| 269 |
x=df_sorted["date_day"], y=df_sorted["avg_sentiment"],
|
|
|
|
| 274 |
row=1, col=1, secondary_y=True
|
| 275 |
)
|
| 276 |
|
|
|
|
|
|
|
| 277 |
fig.add_trace(go.Scatter(
|
| 278 |
x=future_dates, y=future_preds,
|
| 279 |
mode="lines+markers", name="Predicted Sentiment (7-day Forecast)",
|
| 280 |
line=dict(color="orange", dash="dash")
|
| 281 |
),
|
| 282 |
+
row=1, col=1,
|
| 283 |
+
secondary_y=True
|
| 284 |
)
|
|
|
|
| 285 |
|
| 286 |
# --- กราฟส่วนล่าง (จำนวนข่าว) ---
|
| 287 |
fig.add_trace(go.Bar(x=df_sorted["date_day"], y=df_sorted["neutral"], name="Neutral", marker_color='rgba(128, 128, 128, 0.7)'), row=2, col=1)
|
|
|
|
| 306 |
|
| 307 |
st.plotly_chart(fig, use_container_width=True)
|
| 308 |
|
| 309 |
+
# แสดงข่าว
|
| 310 |
st.subheader("📰 รายการข่าว")
|
| 311 |
st.dataframe(news_df[["date", "source", "text", "sentiment", "url"]], use_container_width=True)
|
| 312 |
|