KYTHY commited on
Commit
440f8bb
·
verified ·
1 Parent(s): 7ad6667

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -42
app.py CHANGED
@@ -59,6 +59,19 @@ def analyze_text(text):
59
  score = (-1 * probs[0]) + (0 * probs[1]) + (1 * probs[2])
60
  return float(score)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def resolve_company_symbol(keyword: str):
63
  keyword = keyword.strip()
64
  ticker = None
@@ -85,7 +98,7 @@ def resolve_company_symbol(keyword: str):
85
  return name, ticker
86
 
87
  # --------------------------
88
- # ดึงข่าว 7 วัน (เพิ่ม progress bar)
89
  # --------------------------
90
  @st.cache_data(ttl=3600)
91
  def fetch_financial_news(keyword):
@@ -95,8 +108,6 @@ def fetch_financial_news(keyword):
95
  query_keyword = f"({company} OR {symbol}) finance stock"
96
  all_articles = []
97
  page = 1
98
- progress_bar = st.progress(0)
99
-
100
  while True:
101
  url = (
102
  f"https://newsapi.org/v2/everything?"
@@ -121,44 +132,11 @@ def fetch_financial_news(keyword):
121
  "source": a["source"]["name"],
122
  "url": a["url"]
123
  })
124
- progress_bar.progress(min(page * 10, 100)) # อัปเดต progress bar แบบหยาบ ๆ
125
  if len(articles) < 100:
126
  break
127
  page += 1
128
-
129
- progress_bar.progress(100)
130
  return pd.DataFrame(all_articles)
131
 
132
- # --------------------------
133
- # วิเคราะห์ Sentiment (เพิ่ม progress bar)
134
- # --------------------------
135
- def analyze_news_sentiment(news_df):
136
- sentiments = []
137
- progress_bar = st.progress(0)
138
- total = len(news_df)
139
- for i, text in enumerate(news_df["text"]):
140
- sentiments.append(analyze_text(text))
141
- progress_bar.progress(int((i + 1) / total * 100))
142
- progress_bar.progress(100)
143
- return sentiments
144
-
145
- # --------------------------
146
- # สรุปธีมข่าว (เพิ่ม progress bar)
147
- # --------------------------
148
- def summarize_themes(news_texts):
149
- themes = []
150
- progress_bar = st.progress(0)
151
- total = len(news_texts)
152
- for i, text in enumerate(news_texts):
153
- if not text.strip():
154
- themes.append("Unknown")
155
- else:
156
- result = theme_classifier(text, candidate_labels)
157
- themes.append(result["labels"][0])
158
- progress_bar.progress(int((i + 1) / total * 100))
159
- progress_bar.progress(100)
160
- return themes
161
-
162
  # --------------------------
163
  # ดึงราคาหุ้น
164
  # --------------------------
@@ -203,12 +181,7 @@ def main():
203
 
204
  # วิเคราะห์ Sentiment
205
  st.info("กำลังวิเคราะห์อารมณ์ของข่าว...")
206
- news_df["sentiment"] = analyze_news_sentiment(news_df)
207
-
208
- # สรุปธีมข่าว
209
- st.info("กำลังสรุปธีมข่าว...")
210
- news_df["theme"] = summarize_themes(news_df["text"].tolist())
211
-
212
  news_df["date"] = pd.to_datetime(news_df["date"])
213
 
214
  # Metrics
@@ -220,6 +193,12 @@ def main():
220
  col2.metric("ข่าวเชิงบวก", f"{pos_pct:.1f}%")
221
  col3.metric("ข่าวเชิงลบ", f"{neg_pct:.1f}%")
222
 
 
 
 
 
 
 
223
  # ส่วนกราฟ Sentiment & Price
224
  st.subheader("📈 แนวโน้มอารมณ์ของข่าว & ราคาหุ้น")
225
  news_df["date_day"] = pd.to_datetime(news_df["date"].dt.date)
 
59
  score = (-1 * probs[0]) + (0 * probs[1]) + (1 * probs[2])
60
  return float(score)
61
 
62
+ def summarize_themes(news_texts):
63
+ """สรุปธีมข่าวด้วย Zero-shot classification"""
64
+ themes = []
65
+ for text in news_texts:
66
+ if not text.strip():
67
+ continue
68
+ result = theme_classifier(text, candidate_labels)
69
+ themes.append(result["labels"][0])
70
+ return themes
71
+
72
+ # --------------------------
73
+ # แปลงชื่อ/ตัวย่อ → (Company Name, Symbol)
74
+ # --------------------------
75
  def resolve_company_symbol(keyword: str):
76
  keyword = keyword.strip()
77
  ticker = None
 
98
  return name, ticker
99
 
100
  # --------------------------
101
+ # ดึงข่าว 7 วัน
102
  # --------------------------
103
  @st.cache_data(ttl=3600)
104
  def fetch_financial_news(keyword):
 
108
  query_keyword = f"({company} OR {symbol}) finance stock"
109
  all_articles = []
110
  page = 1
 
 
111
  while True:
112
  url = (
113
  f"https://newsapi.org/v2/everything?"
 
132
  "source": a["source"]["name"],
133
  "url": a["url"]
134
  })
 
135
  if len(articles) < 100:
136
  break
137
  page += 1
 
 
138
  return pd.DataFrame(all_articles)
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # --------------------------
141
  # ดึงราคาหุ้น
142
  # --------------------------
 
181
 
182
  # วิเคราะห์ Sentiment
183
  st.info("กำลังวิเคราะห์อารมณ์ของข่าว...")
184
+ news_df["sentiment"] = news_df["text"].apply(analyze_text)
 
 
 
 
 
185
  news_df["date"] = pd.to_datetime(news_df["date"])
186
 
187
  # Metrics
 
193
  col2.metric("ข่าวเชิงบวก", f"{pos_pct:.1f}%")
194
  col3.metric("ข่าวเชิงลบ", f"{neg_pct:.1f}%")
195
 
196
+ # ธีมข่าวแทน Word Cloud
197
+ st.subheader("📰 ธีมข่าว (Top Theme per Article)")
198
+ news_df["theme"] = summarize_themes(news_df["text"].tolist())
199
+ theme_counts = news_df["theme"].value_counts()
200
+ st.bar_chart(theme_counts)
201
+
202
  # ส่วนกราฟ Sentiment & Price
203
  st.subheader("📈 แนวโน้มอารมณ์ของข่าว & ราคาหุ้น")
204
  news_df["date_day"] = pd.to_datetime(news_df["date"].dt.date)