Spaces:
Runtime error
Runtime error
Update app.py
Browse files58 unmodified lines59606162636420 unmodified lines858687888990913 unmodified lines959697989910010110218 unmodified lines12112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316438 unmodified lines2032042052062072082092102112122132145 unmodified lines22022122222322422558 unmodified linesscore = (-1 * probs[0]) + (0 * probs[1]) + (1 * probs[2])return float(score)def resolve_company_symbol(keyword: str):keyword = keyword.strip()ticker = None20 unmodified linesreturn name, ticker# --------------------------# ดึงข่าว 7 วัน (เพิ่ม progress bar)# --------------------------@st.cache_data(ttl=3600)def fetch_financial_news(keyword):3 unmodified linesquery_keyword = f"({company} OR {symbol}) finance stock"all_articles = []page = 1progress_bar = st.progress(0)while True:url = (f"https://newsapi.org/v2/everything?"18 unmodified lines"source": a["source"]["name"],"url": a["url"]})progress_bar.progress(min(page * 10, 100)) # อัปเดต progress bar แบบหยาบ ๆif len(articles) < 100:breakpage += 1progress_bar.progress(100)return pd.DataFrame(all_articles)# --------------------------# วิเคราะห์ Sentiment (เพิ่ม progress bar)# --------------------------def analyze_news_sentiment(news_df):sentiments = []progress_bar = st.progress(0)total = len(news_df)for i, text in enumerate(news_df["text"]):sentiments.append(analyze_text(text))progress_bar.progress(int((i + 1) / total * 100))progress_bar.progress(100)return sentiments# --------------------------# สรุปธีมข่าว (เพิ่ม progress bar)# --------------------------def summarize_themes(news_texts):themes = []progress_bar = st.progress(0)total = len(news_texts)for i, text in enumerate(news_texts):if not text.strip():themes.append("Unknown")else:result = theme_classifier(text, candidate_labels)themes.append(result["labels"][0])progress_bar.progress(int((i + 1) / total * 100))progress_bar.progress(100)return themes# --------------------------# ดึงราคาหุ้น# --------------------------38 unmodified lines# วิเคราะห์ Sentimentst.info("กำลังวิเคราะห์อารมณ์ของข่าว...")news_df["sentiment"] = analyze_news_sentiment(news_df)# สรุปธีมข่าวst.info("กำลังสรุปธีมข่าว...")news_df["theme"] = summarize_themes(news_df["text"].tolist())news_df["date"] = pd.to_datetime(news_df["date"])# Metrics5 unmodified linescol2.metric("ข่าวเชิงบวก", f"{pos_pct:.1f}%")col3.metric("ข่าวเชิงลบ", f"{neg_pct:.1f}%")# ส่วนกราฟ Sentiment & Pricest.subheader("📈 แนวโน้มอารมณ์ของข่าว & ราคาหุ้น")news_df["date_day"] = pd.to_datetime(news_df["date"].dt.date)58 unmodified lines5960616263646566676869707172737475767720 unmodified lines98991001011021031043 unmodified lines10810911011111211318 unmodified lines13213313413513613713813914014114238 unmodified lines1811821831841851861875 unmodified lines19319419519619719819920020120220320458 unmodified linesscore = (-1 * probs[0]) + (0 * probs[1]) + (1 * probs[2])return float(score)def summarize_themes(news_texts):"""สรุปธีมข่าวด้วย Zero-shot classification"""themes = []for text in news_texts:if not text.strip():continueresult = theme_classifier(text, candidate_labels)themes.append(result["labels"][0])return themes# --------------------------# แปลงชื่อ/ตัวย่อ → (Company Name, Symbol)# --------------------------def resolve_company_symbol(keyword: str):keyword = keyword.strip()ticker = None20 unmodified linesreturn name, ticker# --------------------------# ดึงข่าว 7 วัน# --------------------------@st.cache_data(ttl=3600)def fetch_financial_news(keyword):3 unmodified linesquery_keyword = f"({company} OR {symbol}) finance stock"all_articles = []page = 1while True:url = (f"https://newsapi.org/v2/everything?"18 unmodified lines"source": a["source"]["name"],"url": a["url"]})if len(articles) < 100:breakpage += 1return pd.DataFrame(all_articles)# --------------------------# ดึงราคาหุ้น# --------------------------38 unmodified lines# วิเคราะห์ Sentimentst.info("กำลังวิเคราะห์อารมณ์ของข่าว...")news_df["sentiment"] = news_df["text"].apply(analyze_text)news_df["date"] = pd.to_datetime(news_df["date"])# Metrics5 unmodified linescol2.metric("ข่าวเชิงบวก", f"{pos_pct:.1f}%")col3.metric("ข่าวเชิงลบ", f"{neg_pct:.1f}%")# ธีมข่าวแทน Word Cloudst.subheader("📰 ธีมข่าว (Top Theme per Article)")news_df["theme"] = summarize_themes(news_df["text"].tolist())theme_counts = news_df["theme"].value_counts()st.bar_chart(theme_counts)# ส่วนกราฟ Sentiment & Pricest.subheader("📈 แนวโน้มอารมณ์ของข่าว & ราคาหุ้น")news_df["date_day"] = pd.to_datetime(news_df["date"].dt.date)
app.py+21-42