Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +185 -115
streamlit_app.py
CHANGED
|
@@ -11,80 +11,62 @@ import re
|
|
| 11 |
|
| 12 |
# 1. ํ์ด์ง ์ค์
|
| 13 |
st.set_page_config(page_title="Pocket Quant AI", page_icon="๐ง ", layout="wide")
|
| 14 |
-
st.title("๐ง ์ง์ง AI ์ฃผ์ ๋น์")
|
| 15 |
-
st.caption("์ฌ์ฉ์์ ์๋๋ฅผ ๋จผ์ ํ์
ํ๊ณ -> ์ต์ ์ ๊ฒ์์ด๋ก ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ต๋๋ค.")
|
| 16 |
|
| 17 |
# 2. ํ ํฐ ํ์ธ
|
| 18 |
if "HF_TOKEN" in st.secrets:
|
| 19 |
client = InferenceClient(api_key=st.secrets["HF_TOKEN"])
|
| 20 |
else:
|
| 21 |
-
st.error("๐จ
|
| 22 |
st.stop()
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
| 25 |
def identify_target_with_ai(user_query):
|
| 26 |
"""
|
| 27 |
-
์ฌ์ฉ์์ ์
๋ ฅ(์ด๋ค ์ธ์ด๋ )์ ๋ฐ์์
|
| 28 |
-
1. ์ ํํ ๊ธฐ์
์๋ฌธ๋ช
|
| 29 |
-
2. ์ผํ/FDR ํฐ์ปค
|
| 30 |
-
3. ๋ด์ค ๊ฒ์์ฉ ์ต์ ํค์๋(์์ด)
|
| 31 |
-
๋ฅผ JSON์ผ๋ก ๋ฆฌํด๋ฐ์ต๋๋ค.
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
prompt = f"""
|
| 35 |
-
[ROLE]
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
[USER QUERY]
|
| 39 |
-
"{user_query}"
|
| 40 |
-
|
| 41 |
[TASK]
|
| 42 |
-
1. Identify the company
|
| 43 |
-
2. Find
|
| 44 |
-
- Vietnam:
|
| 45 |
-
|
| 46 |
-
- Korea: .KS or .KQ
|
| 47 |
-
- Japan: .T
|
| 48 |
-
- USA: Ticker only (e.g., AAPL)
|
| 49 |
-
3. Create a specific ENGLISH search keyword for news (to avoid unrelated results like NVIDIA).
|
| 50 |
|
| 51 |
-
[OUTPUT
|
| 52 |
-
Return ONLY a JSON object. No markdown. No explanations.
|
| 53 |
{{
|
| 54 |
-
"name": "
|
| 55 |
"ticker": "TICKER",
|
| 56 |
-
"search_keyword": "Company Name + 'stock
|
| 57 |
}}
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
messages = [{"role": "user", "content": prompt}]
|
| 61 |
response = client.chat.completions.create(
|
| 62 |
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=150
|
| 63 |
)
|
| 64 |
-
|
| 65 |
-
# JSON ํ์ฑ (AI๊ฐ ๊ฐ๋ ```json ๋ฑ์ ๋ถ์ผ ์ ์์ผ๋ฏ๋ก ์ ๊ฑฐ)
|
| 66 |
content = response.choices[0].message.content.strip()
|
| 67 |
content = re.sub(r"```json|```", "", content).strip()
|
| 68 |
return json.loads(content)
|
| 69 |
-
|
| 70 |
-
except Exception as e:
|
| 71 |
-
st.error(f"AI ํ๋จ ์คํจ: {e}")
|
| 72 |
-
return None
|
| 73 |
|
| 74 |
-
#
|
|
|
|
|
|
|
| 75 |
def get_market_data(ticker):
|
| 76 |
-
# 1. Yahoo
|
| 77 |
try:
|
| 78 |
stock = yf.Ticker(ticker)
|
| 79 |
hist = stock.history(period="3mo")
|
| 80 |
if not hist.empty: return hist, "Yahoo Finance"
|
| 81 |
except: pass
|
| 82 |
|
| 83 |
-
# 2. FDR
|
| 84 |
try:
|
| 85 |
-
# FDR์ฉ ์ฌ๋ณผ ๋ณํ
|
| 86 |
fdr_symbol = ticker
|
| 87 |
-
if ".VN" in ticker: fdr_symbol = ticker.split('.')[0]
|
| 88 |
elif ".JK" in ticker: fdr_symbol = f"IDX:{ticker.split('.')[0]}"
|
| 89 |
elif ".T" in ticker: fdr_symbol = f"TSE:{ticker.split('.')[0]}"
|
| 90 |
|
|
@@ -94,106 +76,194 @@ def get_market_data(ticker):
|
|
| 94 |
hist = hist.rename(columns={'Close':'Close', 'Open':'Open', 'High':'High', 'Low':'Low', 'Volume':'Volume'})
|
| 95 |
return hist, "FinanceDataReader"
|
| 96 |
except: pass
|
| 97 |
-
|
| 98 |
return pd.DataFrame(), "None"
|
| 99 |
|
| 100 |
-
# --- [ํต์ฌ ์์ง] 3๋จ๊ณ: ๋ด์ค ๊ฒ์ (AI๊ฐ ์ค ํค์๋๋ก ์ํ) ---
|
| 101 |
def get_news(keyword):
|
| 102 |
summary = ""
|
| 103 |
try:
|
| 104 |
with DDGS() as ddgs:
|
| 105 |
-
# AI๊ฐ ๋ง๋ค์ด์ค '์ต์ ์ ์์ด ํค์๋'๋ก ๊ฒ์ -> ์ ํ๋ ์์น
|
| 106 |
results = ddgs.news(keyword, timelimit="m", max_results=5)
|
| 107 |
for r in results:
|
| 108 |
summary += f"[{r.get('date','?')}] {r['title']}\n"
|
| 109 |
-
return summary if summary else "No recent news
|
| 110 |
-
except: return "Search Error"
|
| 111 |
|
| 112 |
def parse_stream(stream):
|
| 113 |
for chunk in stream:
|
| 114 |
if chunk.choices: yield chunk.choices[0].delta.content or ""
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
fig = go.Figure(data=[go.Candlestick(x=hist.index, open=hist['Open'], high=hist['High'], low=hist['Low'], close=hist['Close'], name="Price")])
|
| 119 |
-
fig.update_layout(title=f"{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return fig
|
| 121 |
|
| 122 |
# ==============================================================================
|
| 123 |
-
#
|
| 124 |
# ==============================================================================
|
| 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 |
else:
|
| 160 |
-
status.update(label="
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
if
|
| 170 |
-
|
| 171 |
-
else:
|
| 172 |
-
st.warning("์ฐจํธ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค์ง ๋ชปํ์ต๋๋ค.")
|
| 173 |
|
| 174 |
-
|
| 175 |
-
st.
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
1. Briefly explain what this company does (based on user query context).
|
| 188 |
-
2. Analyze the recent news sentiment.
|
| 189 |
-
3. Provide an investment verdict (Buy/Hold/Sell).
|
| 190 |
-
|
| 191 |
-
[LANGUAGE] Korean.
|
| 192 |
-
"""
|
| 193 |
-
messages = [{"role": "user", "content": prompt}]
|
| 194 |
-
stream = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=messages, stream=True)
|
| 195 |
-
st.write_stream(parse_stream(stream))
|
| 196 |
-
|
| 197 |
-
else:
|
| 198 |
-
status.update(label="โ ์ธ์ ์คํจ", state="error")
|
| 199 |
-
st.error("AI๊ฐ ์ด๋ค ๊ธฐ์
์ธ์ง ํ์
ํ์ง ๋ชปํ์ต๋๋ค. ์กฐ๊ธ ๋ ๊ตฌ์ฒด์ ์ผ๋ก ์
๋ ฅํด์ฃผ์ธ์.")
|
|
|
|
| 11 |
|
| 12 |
# 1. ํ์ด์ง ์ค์
|
| 13 |
st.set_page_config(page_title="Pocket Quant AI", page_icon="๐ง ", layout="wide")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# 2. ํ ํฐ ํ์ธ
|
| 16 |
if "HF_TOKEN" in st.secrets:
|
| 17 |
client = InferenceClient(api_key=st.secrets["HF_TOKEN"])
|
| 18 |
else:
|
| 19 |
+
st.error("๐จ ์ค์ ์ค๋ฅ: Secrets์ 'HF_TOKEN'์ด ํ์ํฉ๋๋ค.")
|
| 20 |
st.stop()
|
| 21 |
|
| 22 |
+
# ==============================================================================
|
| 23 |
+
# ๐ง [ํต์ฌ ์์ง 1] AI ์ง๋ฅํ ๊ฒ์ (์ฌ์ฉ์ ์๋ ํ์
)
|
| 24 |
+
# ==============================================================================
|
| 25 |
def identify_target_with_ai(user_query):
|
| 26 |
"""
|
| 27 |
+
์ฌ์ฉ์์ ์
๋ ฅ(์ด๋ค ์ธ์ด๋ )์ ๋ฐ์์ ๊ธฐ์
๋ช
, ํฐ์ปค, ๊ฒ์ ํค์๋๋ฅผ ์ถ๋ก
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
prompt = f"""
|
| 31 |
+
[ROLE] Financial Entity Resolver.
|
| 32 |
+
[QUERY] "{user_query}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
[TASK]
|
| 34 |
+
1. Identify the company (e.g., "๋น๋๋ฐํฌ" -> "Vinamilk").
|
| 35 |
+
2. Find best Ticker (Yahoo format preferred).
|
| 36 |
+
- Vietnam: .VN, Indonesia: .JK, US: Symbol only, Korea: .KS/.KQ
|
| 37 |
+
3. Create English search keyword for news.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
[OUTPUT JSON]
|
|
|
|
| 40 |
{{
|
| 41 |
+
"name": "English Company Name",
|
| 42 |
"ticker": "TICKER",
|
| 43 |
+
"search_keyword": "Company Name + 'stock news'"
|
| 44 |
}}
|
| 45 |
"""
|
|
|
|
| 46 |
messages = [{"role": "user", "content": prompt}]
|
| 47 |
response = client.chat.completions.create(
|
| 48 |
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=150
|
| 49 |
)
|
|
|
|
|
|
|
| 50 |
content = response.choices[0].message.content.strip()
|
| 51 |
content = re.sub(r"```json|```", "", content).strip()
|
| 52 |
return json.loads(content)
|
| 53 |
+
except: return None
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# ==============================================================================
|
| 56 |
+
# ๐ ๏ธ [ํต์ฌ ์์ง 2] ๋ฐ์ดํฐ ์์ง (Yahoo + FDR + News)
|
| 57 |
+
# ==============================================================================
|
| 58 |
def get_market_data(ticker):
|
| 59 |
+
# 1. Yahoo
|
| 60 |
try:
|
| 61 |
stock = yf.Ticker(ticker)
|
| 62 |
hist = stock.history(period="3mo")
|
| 63 |
if not hist.empty: return hist, "Yahoo Finance"
|
| 64 |
except: pass
|
| 65 |
|
| 66 |
+
# 2. FDR (Yahoo ์คํจ ์)
|
| 67 |
try:
|
|
|
|
| 68 |
fdr_symbol = ticker
|
| 69 |
+
if ".VN" in ticker: fdr_symbol = ticker.split('.')[0]
|
| 70 |
elif ".JK" in ticker: fdr_symbol = f"IDX:{ticker.split('.')[0]}"
|
| 71 |
elif ".T" in ticker: fdr_symbol = f"TSE:{ticker.split('.')[0]}"
|
| 72 |
|
|
|
|
| 76 |
hist = hist.rename(columns={'Close':'Close', 'Open':'Open', 'High':'High', 'Low':'Low', 'Volume':'Volume'})
|
| 77 |
return hist, "FinanceDataReader"
|
| 78 |
except: pass
|
|
|
|
| 79 |
return pd.DataFrame(), "None"
|
| 80 |
|
|
|
|
| 81 |
def get_news(keyword):
|
| 82 |
summary = ""
|
| 83 |
try:
|
| 84 |
with DDGS() as ddgs:
|
|
|
|
| 85 |
results = ddgs.news(keyword, timelimit="m", max_results=5)
|
| 86 |
for r in results:
|
| 87 |
summary += f"[{r.get('date','?')}] {r['title']}\n"
|
| 88 |
+
return summary if summary else "No recent news."
|
| 89 |
+
except: return "News Search Error"
|
| 90 |
|
| 91 |
def parse_stream(stream):
|
| 92 |
for chunk in stream:
|
| 93 |
if chunk.choices: yield chunk.choices[0].delta.content or ""
|
| 94 |
|
| 95 |
+
# ==============================================================================
|
| 96 |
+
# ๐ [์๊ฐํ] ์ฐจํธ ํจ์๋ค
|
| 97 |
+
# ==============================================================================
|
| 98 |
+
def plot_candle_chart(hist, title, source):
|
| 99 |
fig = go.Figure(data=[go.Candlestick(x=hist.index, open=hist['Open'], high=hist['High'], low=hist['Low'], close=hist['Close'], name="Price")])
|
| 100 |
+
fig.update_layout(title=f"{title} ({source})", height=350, margin=dict(l=10, r=10, t=30, b=10), template="plotly_dark", paper_bgcolor='rgba(0,0,0,0)')
|
| 101 |
+
return fig
|
| 102 |
+
|
| 103 |
+
def plot_bar_chart(df):
|
| 104 |
+
colors = ['#00FF00' if x > 0 else '#FF0000' for x in df['Change(%)']]
|
| 105 |
+
fig = go.Figure(go.Bar(x=df['Name'], y=df['Change(%)'], marker_color=colors, text=df['Change(%)'].apply(lambda x: f"{x:.2f}%")))
|
| 106 |
+
fig.update_layout(title="Market Heatmap", height=350, margin=dict(l=10, r=10, t=30, b=10), template="plotly_dark", paper_bgcolor='rgba(0,0,0,0)')
|
| 107 |
return fig
|
| 108 |
|
| 109 |
# ==============================================================================
|
| 110 |
+
# ๐พ [๋ฐ์ดํฐ] ์ค์บ๋์ฉ ๋ฆฌ์คํธ (๋ณต๊ตฌ๋จ!)
|
| 111 |
# ==============================================================================
|
| 112 |
+
TICKER_NAMES = {
|
| 113 |
+
"AAPL": "Apple", "NVDA": "NVIDIA", "TSLA": "Tesla", "AMZN": "Amazon", "MSFT": "Microsoft",
|
| 114 |
+
"005930.KS": "Samsung Elec", "000660.KS": "SK Hynix", "207940.KS": "Samsung Bio",
|
| 115 |
+
"VIC.VN": "Vingroup", "VHM.VN": "Vinhomes", "VNM.VN": "Vinamilk", "VCB.VN": "Vietcombank",
|
| 116 |
+
"BBCA.JK": "BCA", "BBRI.JK": "BRI", "GOTO.JK": "GoTo",
|
| 117 |
+
"7203.T": "Toyota", "6758.T": "Sony", "9984.T": "SoftBank",
|
| 118 |
+
"9988.HK": "Alibaba", "0700.HK": "Tencent", "2330.TW": "TSMC",
|
| 119 |
+
"RELIANCE.NS": "Reliance", "TCS.NS": "TCS"
|
| 120 |
+
}
|
| 121 |
|
| 122 |
+
MARKET_SAMPLES = {
|
| 123 |
+
"๐ป๐ณ ๋ฒ ํธ๋จ (Vietnam)": ["VIC.VN", "VHM.VN", "VCB.VN", "VNM.VN", "HPG.VN", "MSN.VN", "GAS.VN"],
|
| 124 |
+
"๐ฎ๐ฉ ์ธ๋๋ค์์": ["BBCA.JK", "BBRI.JK", "TLKM.JK", "BMRI.JK", "ASII.JK", "GOTO.JK"],
|
| 125 |
+
"๐ฐ๐ท ํ๊ตญ (Korea)": ["005930.KS", "000660.KS", "035420.KS", "035720.KS", "005380.KS", "207940.KS", "051910.KS"],
|
| 126 |
+
"๐บ๐ธ ๋ฏธ๊ตญ (USA)": ["AAPL", "NVDA", "TSLA", "AMZN", "MSFT", "GOOGL", "META", "AMD", "NFLX"],
|
| 127 |
+
"๐ฏ๐ต ์ผ๋ณธ (Japan)": ["7203.T", "6758.T", "9984.T", "8035.T", "6861.T", "7974.T", "9432.T"],
|
| 128 |
+
"๐จ๐ณ ์ค๊ตญ/ํ์ฝฉ": ["9988.HK", "0700.HK", "3690.HK", "1211.HK", "1810.HK", "0941.HK"],
|
| 129 |
+
"๐น๐ผ ๋๋ง (Taiwan)": ["2330.TW", "2454.TW", "2317.TW", "2308.TW", "2881.TW"],
|
| 130 |
+
"๐ฎ๐ณ ์ธ๋ (India)": ["RELIANCE.NS", "TCS.NS", "HDFCBANK.NS", "INFY.NS", "ICICIBANK.NS"],
|
| 131 |
+
"๐ธ๐ฌ ์ฑ๊ฐํฌ๋ฅด": ["D05.SI", "O39.SI", "U11.SI", "Z74.SI"],
|
| 132 |
+
"๐ฒ๐พ ๋ง๋ ์ด์์": ["1155.KL", "1023.KL", "1295.KL"],
|
| 133 |
+
"๐น๐ญ ํ๊ตญ": ["PTT.BK", "AOT.BK", "CPALL.BK"]
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
# ==============================================================================
|
| 137 |
+
# ๐ฅ๏ธ ๋ฉ์ธ UI & ์ฌ์ด๋๋ฐ
|
| 138 |
+
# ==============================================================================
|
| 139 |
+
with st.sidebar:
|
| 140 |
+
st.title("๐ง Pocket Quant AI")
|
| 141 |
+
st.divider()
|
| 142 |
+
# ๋ฉ๋ด ๋ถํ!
|
| 143 |
+
menu = st.radio("MENU", ["๐ AI ์ค๋งํธ ๊ฒ์", "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋"], index=0)
|
| 144 |
+
st.divider()
|
| 145 |
+
st.caption("Auto-Detect & Multi-Source")
|
| 146 |
+
|
| 147 |
+
# ------------------------------------------------------------------------------
|
| 148 |
+
# ๋ชจ๋ 1: AI ์ค๋งํธ ๊ฒ์ (๊ฐ๋ก๊ฐ์ด ๋งํด๋ ์ฐฐ๋ก๊ฐ์ด)
|
| 149 |
+
# ------------------------------------------------------------------------------
|
| 150 |
+
if menu == "๐ AI ์ค๋งํธ ๊ฒ์":
|
| 151 |
+
st.subheader("๐ง ๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์ (AI Smart Search)")
|
| 152 |
+
st.caption("์: '๋น๋๋ฐํฌ', '๋ฒ ํธ๋จ ๋น๊ทธ๋ฃน', '์ผ๋ณธ ๋์ํ' ๋ฑ ํธํ๊ฒ ์
๋ ฅํ์ธ์.")
|
| 153 |
+
|
| 154 |
+
col1, col2 = st.columns([3, 1])
|
| 155 |
+
with col1:
|
| 156 |
+
user_query = st.text_input("๊ฒ์์ด ์
๋ ฅ", "๋น๋๋ฐํฌ", label_visibility="collapsed")
|
| 157 |
+
with col2:
|
| 158 |
+
btn = st.button("๋ถ์ ์คํ", use_container_width=True)
|
| 159 |
+
|
| 160 |
+
if btn:
|
| 161 |
+
with st.status("๐ง AI๊ฐ ์ฌ์ฉ์์ ์๋๋ฅผ ํด์ํ๊ณ ์์ต๋๋ค...", expanded=True) as status:
|
| 162 |
|
| 163 |
+
# 1. AI ์๋ ํ์
|
| 164 |
+
target_info = identify_target_with_ai(user_query)
|
| 165 |
|
| 166 |
+
if target_info:
|
| 167 |
+
name = target_info.get('name', 'Unknown')
|
| 168 |
+
ticker = target_info.get('ticker', '')
|
| 169 |
+
keyword = target_info.get('search_keyword', '')
|
| 170 |
+
|
| 171 |
+
st.info(f"๐ก ๋ถ์ ๋์: **{name} ({ticker})** | ๊ฒ์์ด: '{keyword}'")
|
| 172 |
+
|
| 173 |
+
# 2. ๋ฐ์ดํฐ ์์ง
|
| 174 |
+
st.write("๐ ์์ฅ ๋ฐ์ดํฐ ์์ง ์ค...")
|
| 175 |
+
hist, src = get_market_data(ticker)
|
| 176 |
+
|
| 177 |
+
st.write("๐ฐ ๊ธ๋ก๋ฒ ๋ด์ค ์์ง ์ค...")
|
| 178 |
+
news_data = get_news(keyword)
|
| 179 |
+
|
| 180 |
+
# ๋ฐ์ดํฐ ์ ๋ฆฌ
|
| 181 |
+
curr, pct = "N/A", 0
|
| 182 |
+
if not hist.empty:
|
| 183 |
+
curr = hist['Close'].iloc[-1]
|
| 184 |
+
prev = hist['Close'].iloc[-2]
|
| 185 |
+
pct = ((curr - prev)/prev)*100
|
| 186 |
+
status.update(label="โ
๋ถ์ ์๋ฃ!", state="complete", expanded=False)
|
| 187 |
+
else:
|
| 188 |
+
status.update(label="โ ๏ธ ์ฐจํธ ๋ฐ์ดํฐ ์์ (๋ด์ค๋ง ๋ถ์)", state="complete", expanded=False)
|
| 189 |
+
|
| 190 |
+
st.divider()
|
| 191 |
+
|
| 192 |
+
# ์๊ฐํ
|
| 193 |
+
m1, m2 = st.columns(2)
|
| 194 |
+
m1.metric("Current Price", f"{curr:,.0f}" if isinstance(curr, float) else curr, f"{pct:.2f}%")
|
| 195 |
+
m2.metric("Source", src)
|
| 196 |
+
|
| 197 |
+
if not hist.empty:
|
| 198 |
+
st.plotly_chart(plot_candle_chart(hist, name, src), use_container_width=True)
|
| 199 |
+
else:
|
| 200 |
+
st.warning("์ฐจํธ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.")
|
| 201 |
+
|
| 202 |
+
with st.expander("๐ฐ ๋ด์ค ์๋ฌธ ๋ณด๊ธฐ"):
|
| 203 |
+
st.text(news_data)
|
| 204 |
+
|
| 205 |
+
# 3. ๋ฆฌํฌํธ ์์ฑ
|
| 206 |
+
st.subheader(f"๐ง AI Insight: {name}")
|
| 207 |
+
prompt = f"""
|
| 208 |
+
[ROLE] Global Analyst.
|
| 209 |
+
[TARGET] {name} ({ticker})
|
| 210 |
+
[QUERY] "{user_query}"
|
| 211 |
+
[DATA] Price: {curr}, Trend: {pct:.2f}%
|
| 212 |
+
[NEWS] {news_data}
|
| 213 |
+
[TASK] Analyze sentiment and provide investment verdict.
|
| 214 |
+
[LANG] Korean.
|
| 215 |
+
"""
|
| 216 |
+
messages = [{"role": "user", "content": prompt}]
|
| 217 |
+
stream = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=messages, stream=True)
|
| 218 |
+
st.write_stream(parse_stream(stream))
|
| 219 |
+
|
| 220 |
else:
|
| 221 |
+
status.update(label="โ ์ธ์ ์คํจ", state="error")
|
| 222 |
+
st.error("AI๊ฐ ๊ธฐ์
์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.")
|
| 223 |
+
|
| 224 |
+
# ------------------------------------------------------------------------------
|
| 225 |
+
# ๋ชจ๋ 2: ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋ (Top 10 ๋ฑ๋ฝ๋ฅ ) - ๋ณต๊ตฌ ์๋ฃ!
|
| 226 |
+
# ------------------------------------------------------------------------------
|
| 227 |
+
elif menu == "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋":
|
| 228 |
+
st.subheader("๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ ๊ดํ (Global Scanner)")
|
| 229 |
+
|
| 230 |
+
c1, c2 = st.columns([3, 1])
|
| 231 |
+
with c1:
|
| 232 |
+
target_market = st.selectbox("๊ตญ๊ฐ ์ ํ", list(MARKET_SAMPLES.keys()), label_visibility="collapsed")
|
| 233 |
+
with c2:
|
| 234 |
+
scan_btn = st.button("์ค์บ ์์", use_container_width=True)
|
| 235 |
+
|
| 236 |
+
if scan_btn:
|
| 237 |
+
tickers = MARKET_SAMPLES[target_market]
|
| 238 |
+
with st.spinner(f"{target_market} ์ฃผ์ ์ข
๋ชฉ ์ค์บ ์ค..."):
|
| 239 |
+
results = []
|
| 240 |
+
progress = st.progress(0)
|
| 241 |
|
| 242 |
+
for i, t in enumerate(tickers):
|
| 243 |
+
# Robust ๋ชจ๋๋ก ๋ฐ์ดํฐ ์์ง (์ผํ ์คํจ์ FDR ์๋ ์ฌ์ฉ)
|
| 244 |
+
h, s = get_market_data(t)
|
| 245 |
+
if not h.empty:
|
| 246 |
+
last = h['Close'].iloc[-1]
|
| 247 |
+
prev = h['Close'].iloc[-2]
|
| 248 |
+
pct = ((last - prev)/prev)*100
|
| 249 |
+
# ์ด๋ฆํ ๋ถ์ด๊ธฐ
|
| 250 |
+
name = TICKER_NAMES.get(t, t)
|
| 251 |
+
results.append({'Name': name, 'Price': last, 'Change(%)': pct})
|
| 252 |
+
progress.progress((i+1)/len(tickers))
|
| 253 |
|
| 254 |
+
if results:
|
| 255 |
+
df = pd.DataFrame(results).sort_values('Change(%)', ascending=False)
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
# ํํธ๋งต
|
| 258 |
+
st.plotly_chart(plot_bar_chart(df), use_container_width=True)
|
| 259 |
|
| 260 |
+
# Top/Bottom ๋ฆฌ์คํธ
|
| 261 |
+
col_up, col_down = st.columns(2)
|
| 262 |
+
with col_up:
|
| 263 |
+
st.success("๐ฅ ๊ธ๋ฑ Top 10")
|
| 264 |
+
st.dataframe(df.head(10).style.format({"Price": "{:,.2f}", "Change(%)": "{:,.2f}%"}), use_container_width=True)
|
| 265 |
+
with col_down:
|
| 266 |
+
st.error("๐ง ๊ธ๋ฝ Top 10")
|
| 267 |
+
st.dataframe(df.tail(10).sort_values('Change(%)').style.format({"Price": "{:,.2f}", "Change(%)": "{:,.2f}%"}), use_container_width=True)
|
| 268 |
+
else:
|
| 269 |
+
st.warning("๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค์ง ๋ชปํ์ต๋๋ค.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|