Spaces:
Sleeping
Sleeping
Update app.py
Browse files5 unmodified lines678910111213141516171819202122232411 unmodified lines3637383940414280 unmodified lines1231241251261271281291301316 unmodified lines13813914014114214314481 unmodified lines22622722822923023123229 unmodified lines2622632642652662672685 unmodified linesfrom textblob import TextBlobimport nltkfrom wordcloud import WordCloudimport base66from io import BytesIOimport numpy as npfrom sklearn.linear_model import LinearRegressionimport plotly.graph_objects as gofrom plotly.subplots import make_subplots # Import สิ่งนี้import yfinance as yf# --------------------------# CONFIG# --------------------------st.set_page_config(page_title="📰 SentimentSync NewsAI", layout="wide")API_KEY = "88bc396d4eab4be494a4b86ec842db47" # (แนะนำให้เปลี่ยนเป็นคีย์ของคุณ)# --------------------------# UTILITIES11 unmodified lineswordcloud = WordCloud(width=800, height=400, background_color="white", stopwords=stopwords).generate(text)buf = BytesIO()wordcloud.to_image().save(buf, format="PNG")return base66.b64encode(buf.getvalue()).decode()# --------------------------80 unmodified lines# --------------------------@st.cache_data(ttl=3600)def fetch_stock_price(symbol, start_date, end_date):"""ดึงราคาหุ้นตามช่วงเวลาของข่าวที่พบ (บวก/ลบ 1-2 วัน เผื่อวันหยุด)"""try:start_str = (start_date - timedelta(days=2)).strftime('%Y-%m-%d')end_str = (end_date + timedelta(days=1)).strftime('%Y-%m-%d')6 unmodified linesdf = df.reset_index()[["Date", "Close"]]df.rename(columns={"Date": "date", "Close": "price"}, inplace=True)# Normalize วันที่ให้เป็น .dt.datedf["date"] = pd.to_datetime(df["date"].dt.date)return dfexcept Exception as e:81 unmodified lines# ป้องกัน Error ถ้ามีข้อมูลน้อยกว่า 2 วันif len(df_sorted) < 2:st.warning("มีข้อมูลข่าวไม่เพียงพอที่จะสร้างแนวโน้ม (น้อยกว่า 2 วัน)")# แสดงตารางข่าวต่อไป แม้จะพล็อตไม่ได้st.subheader("📰 รายการข่าว")st.dataframe(news_df[["date", "source", "text", "sentiment", "url"]], use_column_width=True)return29 unmodified linesgo.Scatter(x=stock_df["date"], y=stock_df["price"],name=f"{symbol} Stock Price",mode="lines+markers", # <-- นี่คือจุดที่แก้ไขล่าสุดline=dict(color="green", width=2)),row=1, col=1, secondary_y=False5 unmodified lines678910111213141516171819202122232411 unmodified lines3637383940414280 unmodified lines1231241251261271286 unmodified lines13513613713813914081 unmodified lines22222322422522622729 unmodified lines2572582592602612622635 unmodified linesfrom textblob import TextBlobimport nltkfrom wordcloud import WordCloudimport base64 # <--- (แก้ไขจาก base66)from io import BytesIOimport numpy as npfrom sklearn.linear_model import LinearRegressionimport plotly.graph_objects as gofrom plotly.subplots import make_subplotsimport yfinance as yf# --------------------------# CONFIG# --------------------------st.set_page_config(page_title="📰 SentimentSync NewsAI", layout="wide")API_KEY = "88bc396d4eab4be494a4b86ec842db47"# --------------------------# UTILITIES11 unmodified lineswordcloud = WordCloud(width=800, height=400, background_color="white", stopwords=stopwords).generate(text)buf = BytesIO()wordcloud.to_image().save(buf, format="PNG")return base64.b64encode(buf.getvalue()).decode() # <--- (แก้ไขจาก base66)# --------------------------80 unmodified lines# --------------------------@st.cache_data(ttl=3600)def fetch_stock_price(symbol, start_date, end_date):try:start_str = (start_date - timedelta(days=2)).strftime('%Y-%m-%d')end_str = (end_date + timedelta(days=1)).strftime('%Y-%m-%d')6 unmodified linesdf = df.reset_index()[["Date", "Close"]]df.rename(columns={"Date": "date", "Close": "price"}, inplace=True)df["date"] = pd.to_datetime(df["date"].dt.date)return dfexcept Exception as e:81 unmodified lines# ป้องกัน Error ถ้ามีข้อมูลน้อยกว่า 2 วันif len(df_sorted) < 2:st.warning("มีข้อมูลข่าวไม่เพียงพอที่จะสร้างแนวโน้ม (น้อยกว่า 2 วัน)")st.subheader("📰 รายการข่าว")st.dataframe(news_df[["date", "source", "text", "sentiment", "url"]], use_column_width=True)return29 unmodified linesgo.Scatter(x=stock_df["date"], y=stock_df["price"],name=f"{symbol} Stock Price",mode="lines+markers",line=dict(color="green", width=2)),row=1, col=1, secondary_y=False
app.py+5-10