Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,40 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
| 5 |
import yfinance as yf
|
| 6 |
-
from
|
|
|
|
| 7 |
from textblob import TextBlob
|
| 8 |
-
from
|
| 9 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# -------------------------------
|
| 12 |
-
# 🔧
|
| 13 |
# -------------------------------
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
st.caption("ใช้ NewsAPI + yfinance เพื่อค้นหาข่าวหุ้น และพยากรณ์อารมณ์ข่าว 7 วันข้างหน้า")
|
| 20 |
|
| 21 |
# -------------------------------
|
| 22 |
-
# 🧠
|
| 23 |
# -------------------------------
|
|
|
|
| 24 |
def resolve_company_symbol(keyword: str):
|
| 25 |
keyword = keyword.strip()
|
| 26 |
ticker = None
|
|
@@ -28,132 +42,179 @@ def resolve_company_symbol(keyword: str):
|
|
| 28 |
try:
|
| 29 |
data = yf.Ticker(keyword)
|
| 30 |
info = data.info
|
| 31 |
-
if "symbol" in info and info["symbol"]:
|
| 32 |
ticker = info["symbol"]
|
| 33 |
name = info.get("longName", info.get("shortName", keyword))
|
| 34 |
else:
|
| 35 |
-
# ถ้าไม่ใช่ ticker → ค้นจากชื่อบริษัท
|
| 36 |
url = f"https://query2.finance.yahoo.com/v1/finance/search?q={keyword}"
|
| 37 |
res = requests.get(url).json()
|
| 38 |
if "quotes" in res and len(res["quotes"]) > 0:
|
| 39 |
q = res["quotes"][0]
|
| 40 |
-
ticker = q.get("symbol"
|
| 41 |
-
name = q.get("longname", q.get("shortname", keyword
|
| 42 |
-
except Exception:
|
|
|
|
|
|
|
|
|
|
| 43 |
ticker = keyword.upper()
|
|
|
|
| 44 |
name = keyword.capitalize()
|
| 45 |
return name, ticker
|
| 46 |
|
| 47 |
# -------------------------------
|
| 48 |
-
# 📰
|
| 49 |
# -------------------------------
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
)
|
| 66 |
-
response = requests.get(url).json()
|
| 67 |
-
if response.get("status") != "ok" or not response.get("articles"):
|
| 68 |
-
break
|
| 69 |
-
all_articles.extend(response["articles"])
|
| 70 |
-
|
| 71 |
-
if not all_articles:
|
| 72 |
return pd.DataFrame()
|
| 73 |
|
| 74 |
-
|
| 75 |
-
df
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
df["company"] = company
|
| 83 |
df["symbol"] = symbol
|
| 84 |
return df
|
| 85 |
|
| 86 |
# -------------------------------
|
| 87 |
-
#
|
| 88 |
# -------------------------------
|
| 89 |
-
def analyze_sentiment(text):
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return 0
|
| 92 |
-
analysis = TextBlob(text)
|
| 93 |
-
return analysis.sentiment.polarity # ค่า -1 (ลบ) → +1 (บวก)
|
| 94 |
|
| 95 |
# -------------------------------
|
| 96 |
-
#
|
| 97 |
# -------------------------------
|
| 98 |
def forecast_sentiment_trend(df):
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
forecast_df = pd.DataFrame({"date": future_dates, "predicted_sentiment": future_preds})
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
# -------------------------------
|
| 115 |
-
#
|
| 116 |
# -------------------------------
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
| 122 |
|
|
|
|
| 123 |
if news_df.empty:
|
| 124 |
-
st.warning("ไม่พบข่าวในช่วง 7 วันที่ผ่านมา
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
import yfinance as yf
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 7 |
from textblob import TextBlob
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
import plotly.graph_objects as go
|
| 10 |
+
import nltk
|
| 11 |
+
import numpy as np
|
| 12 |
+
from sklearn.linear_model import Ridge
|
| 13 |
+
from sklearn.preprocessing import PolynomialFeatures
|
| 14 |
+
from sklearn.pipeline import make_pipeline
|
| 15 |
+
import os
|
| 16 |
|
| 17 |
# -------------------------------
|
| 18 |
+
# 🔧 CONFIG
|
| 19 |
# -------------------------------
|
| 20 |
+
st.set_page_config(page_title="📈 News Sentiment & Stock Tracker", layout="wide")
|
| 21 |
+
API_KEY = "88bc396d4eab4be494a4b86ec842db47"
|
| 22 |
|
| 23 |
+
# -------------------------------
|
| 24 |
+
# 📦 โหลดโมเดล
|
| 25 |
+
# -------------------------------
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def load_models():
|
| 28 |
+
bert = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 29 |
+
vader = SentimentIntensityAnalyzer()
|
| 30 |
+
return bert, vader
|
| 31 |
|
| 32 |
+
bert_model, vader_analyzer = load_models()
|
|
|
|
| 33 |
|
| 34 |
# -------------------------------
|
| 35 |
+
# 🧠 ฟังก์ชันแปลงชื่อบริษัท <-> ตัวย่อหุ้น
|
| 36 |
# -------------------------------
|
| 37 |
+
@st.cache_data(ttl=86400)
|
| 38 |
def resolve_company_symbol(keyword: str):
|
| 39 |
keyword = keyword.strip()
|
| 40 |
ticker = None
|
|
|
|
| 42 |
try:
|
| 43 |
data = yf.Ticker(keyword)
|
| 44 |
info = data.info
|
| 45 |
+
if "symbol" in info and info["symbol"]:
|
| 46 |
ticker = info["symbol"]
|
| 47 |
name = info.get("longName", info.get("shortName", keyword))
|
| 48 |
else:
|
|
|
|
| 49 |
url = f"https://query2.finance.yahoo.com/v1/finance/search?q={keyword}"
|
| 50 |
res = requests.get(url).json()
|
| 51 |
if "quotes" in res and len(res["quotes"]) > 0:
|
| 52 |
q = res["quotes"][0]
|
| 53 |
+
ticker = q.get("symbol")
|
| 54 |
+
name = q.get("longname", q.get("shortname", keyword))
|
| 55 |
+
except Exception as e:
|
| 56 |
+
st.warning(f"⚠️ ไม่สามารถค้นหาข้อมูลบริษัทได้: {e}")
|
| 57 |
+
|
| 58 |
+
if not ticker:
|
| 59 |
ticker = keyword.upper()
|
| 60 |
+
if not name:
|
| 61 |
name = keyword.capitalize()
|
| 62 |
return name, ticker
|
| 63 |
|
| 64 |
# -------------------------------
|
| 65 |
+
# 📰 ดึงข่าวย้อนหลัง 7 วัน
|
| 66 |
# -------------------------------
|
| 67 |
+
@st.cache_data(ttl=3600)
|
| 68 |
+
def fetch_news(company, symbol):
|
| 69 |
+
to_date = datetime.utcnow()
|
| 70 |
+
from_date = to_date - timedelta(days=7)
|
| 71 |
+
query = f"({company} OR {symbol}) finance stock"
|
| 72 |
+
|
| 73 |
+
url = (
|
| 74 |
+
f"https://newsapi.org/v2/everything?"
|
| 75 |
+
f"q={query}&from={from_date.date()}&to={to_date.isoformat()}&"
|
| 76 |
+
f"language=en&sortBy=publishedAt&pageSize=100&apiKey={API_KEY}"
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
res = requests.get(url)
|
| 80 |
+
data = res.json()
|
| 81 |
+
if data.get("status") != "ok":
|
| 82 |
+
st.error("❌ ดึงข้อมูลข่าวไม่สำเร็จ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return pd.DataFrame()
|
| 84 |
|
| 85 |
+
articles = data.get("articles", [])
|
| 86 |
+
df = pd.DataFrame([{
|
| 87 |
+
"date": datetime.fromisoformat(a["publishedAt"].replace("Z", "+00:00")),
|
| 88 |
+
"title": a["title"],
|
| 89 |
+
"description": a["description"],
|
| 90 |
+
"source": a["source"]["name"],
|
| 91 |
+
"url": a["url"],
|
| 92 |
+
} for a in articles])
|
| 93 |
+
|
| 94 |
+
df["text"] = df["title"].fillna('') + " " + df["description"].fillna('')
|
| 95 |
df["company"] = company
|
| 96 |
df["symbol"] = symbol
|
| 97 |
return df
|
| 98 |
|
| 99 |
# -------------------------------
|
| 100 |
+
# 💬 วิเคราะห์อารมณ์ข่าว
|
| 101 |
# -------------------------------
|
| 102 |
+
def analyze_sentiment(text, models):
|
| 103 |
+
bert, vader = models
|
| 104 |
+
if not text.strip():
|
| 105 |
+
return 0
|
| 106 |
+
try:
|
| 107 |
+
vader_score = vader.polarity_scores(text)["compound"]
|
| 108 |
+
tb_score = TextBlob(text).sentiment.polarity
|
| 109 |
+
bert_res = bert(text[:512])[0]
|
| 110 |
+
label_map = {
|
| 111 |
+
"1 star": -1, "2 stars": -0.5, "3 stars": 0,
|
| 112 |
+
"4 stars": 0.5, "5 stars": 1
|
| 113 |
+
}
|
| 114 |
+
bert_score = label_map.get(bert_res["label"], 0)
|
| 115 |
+
return np.mean([vader_score, tb_score, bert_score])
|
| 116 |
+
except Exception:
|
| 117 |
return 0
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# -------------------------------
|
| 120 |
+
# 📈 สร้างโมเดลพยากรณ์
|
| 121 |
# -------------------------------
|
| 122 |
def forecast_sentiment_trend(df):
|
| 123 |
+
# ensure datetime format
|
| 124 |
+
df["date"] = pd.to_datetime(df["date"], errors="coerce")
|
| 125 |
+
df = df.dropna(subset=["date"])
|
| 126 |
+
df_daily = df.groupby(df["date"].dt.date)["sentiment"].mean().reset_index()
|
| 127 |
+
df_daily["date"] = pd.to_datetime(df_daily["date"])
|
| 128 |
+
df_daily["days"] = (df_daily["date"] - df_daily["date"].min()).dt.days
|
| 129 |
+
|
| 130 |
+
X = df_daily["days"].values.reshape(-1, 1)
|
| 131 |
+
y = df_daily["sentiment"].values
|
| 132 |
+
model = make_pipeline(PolynomialFeatures(2), Ridge(alpha=1.0))
|
| 133 |
+
model.fit(X, y)
|
| 134 |
+
|
| 135 |
+
last_day = df_daily["days"].max()
|
| 136 |
+
future_days = np.arange(last_day + 1, last_day + 8).reshape(-1, 1)
|
| 137 |
+
future_preds = model.predict(future_days)
|
| 138 |
+
future_dates = [df_daily["date"].max() + timedelta(days=i) for i in range(1, 8)]
|
| 139 |
forecast_df = pd.DataFrame({"date": future_dates, "predicted_sentiment": future_preds})
|
| 140 |
+
|
| 141 |
+
return df_daily, forecast_df
|
| 142 |
|
| 143 |
# -------------------------------
|
| 144 |
+
# 📊 ส่วนแสดงผลหลัก
|
| 145 |
# -------------------------------
|
| 146 |
+
st.title("📈 News Sentiment & Stock Tracker")
|
| 147 |
|
| 148 |
+
keyword = st.text_input("🔍 ค้นหาบริษัทหรือตัวย่อหุ้น (เช่น Apple หรือ AAPL):", "AAPL")
|
| 149 |
+
if st.button("Analyze"):
|
| 150 |
+
company, symbol = resolve_company_symbol(keyword)
|
| 151 |
+
st.info(f"📊 กำลังวิเคราะห์ข่าวของ **{company} ({symbol})**...")
|
| 152 |
|
| 153 |
+
news_df = fetch_news(company, symbol)
|
| 154 |
if news_df.empty:
|
| 155 |
+
st.warning("ไม่พบข่าวในช่วง 7 วันที่ผ่านมา")
|
| 156 |
+
st.stop()
|
| 157 |
+
|
| 158 |
+
news_df["sentiment"] = news_df["text"].apply(lambda x: analyze_sentiment(x, (bert_model, vader_analyzer)))
|
| 159 |
+
|
| 160 |
+
avg_sent = news_df["sentiment"].mean()
|
| 161 |
+
st.metric("📈 ค่าเฉลี่ยอารมณ์ข่าว (7 วัน)", f"{avg_sent:.2f}",
|
| 162 |
+
"Positive" if avg_sent > 0 else "Negative" if avg_sent < 0 else "Neutral")
|
| 163 |
+
|
| 164 |
+
# -------------------------------
|
| 165 |
+
# 📈 แนวโน้มอารมณ์ + ราคาหุ้น
|
| 166 |
+
# -------------------------------
|
| 167 |
+
st.subheader("📊 แนวโน้มอารมณ์ข่าว & ราคาหุ้น")
|
| 168 |
+
|
| 169 |
+
df_actual, df_forecast = forecast_sentiment_trend(news_df)
|
| 170 |
+
|
| 171 |
+
# ดึงราคาหุ้นจาก yfinance
|
| 172 |
+
price_df = yf.download(symbol, period="14d", interval="1d")
|
| 173 |
+
price_df = price_df.reset_index()[["Date", "Close"]]
|
| 174 |
+
price_df.rename(columns={"Date": "date"}, inplace=True)
|
| 175 |
+
price_df["date"] = pd.to_datetime(price_df["date"]).dt.date
|
| 176 |
+
df_actual["date"] = pd.to_datetime(df_actual["date"]).dt.date
|
| 177 |
+
df_forecast["date"] = pd.to_datetime(df_forecast["date"]).dt.date
|
| 178 |
+
|
| 179 |
+
# สร้างกราฟรวม
|
| 180 |
+
fig = go.Figure()
|
| 181 |
+
fig.add_trace(go.Scatter(
|
| 182 |
+
x=df_actual["date"], y=df_actual["sentiment"],
|
| 183 |
+
mode="lines+markers", name="Actual Sentiment", line=dict(color="blue")
|
| 184 |
+
))
|
| 185 |
+
fig.add_trace(go.Scatter(
|
| 186 |
+
x=df_forecast["date"], y=df_forecast["predicted_sentiment"],
|
| 187 |
+
mode="lines+markers", name="Predicted Sentiment (Next 7 Days)",
|
| 188 |
+
line=dict(color="orange", dash="dash")
|
| 189 |
+
))
|
| 190 |
+
fig.add_trace(go.Scatter(
|
| 191 |
+
x=price_df["date"], y=price_df["Close"],
|
| 192 |
+
mode="lines+markers", name=f"{symbol} Stock Price",
|
| 193 |
+
line=dict(color="green"), yaxis="y2"
|
| 194 |
+
))
|
| 195 |
+
|
| 196 |
+
fig.update_layout(
|
| 197 |
+
title=f"📈 แนวโน้มอารมณ์ข่าว & ราคาหุ้น ({symbol})",
|
| 198 |
+
xaxis=dict(title="วันที่"),
|
| 199 |
+
yaxis=dict(title="Sentiment", side="left", range=[-1, 1]),
|
| 200 |
+
yaxis2=dict(title="Stock Price (USD)", overlaying="y", side="right", showgrid=False),
|
| 201 |
+
legend=dict(x=0, y=1.1, orientation="h"),
|
| 202 |
+
hovermode="x unified",
|
| 203 |
+
template="plotly_white"
|
| 204 |
+
)
|
| 205 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 206 |
+
|
| 207 |
+
# -------------------------------
|
| 208 |
+
# 📰 แสดงข่าวที่ใช้วิเคราะห์
|
| 209 |
+
# -------------------------------
|
| 210 |
+
st.subheader("📰 ข่าวที่ใช้วิเคราะห์")
|
| 211 |
+
st.dataframe(news_df[["date", "source", "title", "sentiment"]])
|
| 212 |
+
|
| 213 |
+
# -------------------------------
|
| 214 |
+
# 📚 โหลด NLTK
|
| 215 |
+
# -------------------------------
|
| 216 |
+
try:
|
| 217 |
+
nltk.download("punkt", quiet=True)
|
| 218 |
+
nltk.download("stopwords", quiet=True)
|
| 219 |
+
except:
|
| 220 |
+
pass
|