Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +102 -112
streamlit_app.py
CHANGED
|
@@ -8,6 +8,8 @@ from duckduckgo_search import DDGS
|
|
| 8 |
import json
|
| 9 |
import datetime
|
| 10 |
import re
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# 1. ํ์ด์ง ์ค์
|
| 13 |
st.set_page_config(page_title="Pocket Quant AI", page_icon="๐ง ", layout="wide")
|
|
@@ -20,81 +22,74 @@ else:
|
|
| 20 |
st.stop()
|
| 21 |
|
| 22 |
# ==============================================================================
|
| 23 |
-
#
|
| 24 |
# ==============================================================================
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
[ROLE] Financial Entity Resolver & Polyglot Linguist.
|
| 32 |
-
[QUERY] "{user_query}"
|
| 33 |
-
|
| 34 |
-
[TASK]
|
| 35 |
-
1. Identify the company.
|
| 36 |
-
2. Determine the best Ticker (Yahoo/FDR format).
|
| 37 |
-
3. Generate TWO search keywords:
|
| 38 |
-
- 'english_keyword': Company Name + "stock news"
|
| 39 |
-
- 'native_keyword': Company Name + "news" in the COMPANY'S LOCAL LANGUAGE.
|
| 40 |
-
(e.g., Vietnam -> "Vinamilk tin tแปฉc", Indonesia -> "BCA berita saham", Japan -> "ใใจใฟ ๆ ชไพก ใใฅใผใน")
|
| 41 |
-
|
| 42 |
-
[OUTPUT JSON]
|
| 43 |
-
{{
|
| 44 |
-
"name": "English Name",
|
| 45 |
-
"ticker": "TICKER",
|
| 46 |
-
"english_keyword": "...",
|
| 47 |
-
"native_keyword": "..."
|
| 48 |
-
}}
|
| 49 |
-
"""
|
| 50 |
-
messages = [{"role": "user", "content": prompt}]
|
| 51 |
-
response = client.chat.completions.create(
|
| 52 |
-
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=200
|
| 53 |
-
)
|
| 54 |
-
content = response.choices[0].message.content.strip()
|
| 55 |
-
content = re.sub(r"```json|```", "", content).strip()
|
| 56 |
-
return json.loads(content)
|
| 57 |
-
except: return None
|
| 58 |
|
| 59 |
# ==============================================================================
|
| 60 |
-
#
|
| 61 |
# ==============================================================================
|
| 62 |
-
def
|
| 63 |
-
"""
|
| 64 |
-
์์ด ๊ฒ์๊ฒฐ๊ณผ + ํ์ง์ด ๊ฒ์๊ฒฐ๊ณผ๋ฅผ ํฉ์ณ์ ๋ฆฌํด
|
| 65 |
-
"""
|
| 66 |
summary = ""
|
| 67 |
seen_urls = set()
|
| 68 |
-
|
| 69 |
try:
|
| 70 |
with DDGS() as ddgs:
|
| 71 |
-
# 1
|
| 72 |
-
|
| 73 |
-
summary += f"=== Global News (English) ===\n"
|
| 74 |
-
for r in results_eng:
|
| 75 |
-
if r['url'] not in seen_urls:
|
| 76 |
-
summary += f"[{r.get('date','?')}] {r['title']}\n"
|
| 77 |
-
seen_urls.add(r['url'])
|
| 78 |
|
| 79 |
-
# 2
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# ==============================================================================
|
| 94 |
-
# ๐ ๏ธ [์์ง
|
| 95 |
# ==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def get_price_data_robust(ticker, name):
|
| 97 |
-
# 1. FDR
|
| 98 |
try:
|
| 99 |
fdr_symbol = ticker
|
| 100 |
if ".VN" in ticker: fdr_symbol = ticker.split('.')[0]
|
|
@@ -110,7 +105,7 @@ def get_price_data_robust(ticker, name):
|
|
| 110 |
return curr, pct, "FDR"
|
| 111 |
except: pass
|
| 112 |
|
| 113 |
-
# 2. Yahoo
|
| 114 |
try:
|
| 115 |
stock = yf.Ticker(ticker)
|
| 116 |
price = stock.fast_info.last_price
|
|
@@ -120,21 +115,38 @@ def get_price_data_robust(ticker, name):
|
|
| 120 |
return price, pct, "Yahoo"
|
| 121 |
except: pass
|
| 122 |
|
| 123 |
-
# 3. Web Search (
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# ์ฃผ๊ฐ ๊ฒ์์ ์์ด๋ก ํ๋๊ฒ ๊ฐ์ฅ ํ์ฑํ๊ธฐ ์ฌ์
|
| 127 |
-
q = f"{name} {ticker} stock price quote"
|
| 128 |
-
results = ddgs.text(q, max_results=2)
|
| 129 |
-
blob = " ".join([r['body'] for r in results])
|
| 130 |
-
match = re.search(r'(\d{1,3}(,\d{3})*(\.\d+)?)', blob)
|
| 131 |
-
if match:
|
| 132 |
-
price = float(match.group(0).replace(",", ""))
|
| 133 |
-
return price, 0.0, "Web Search"
|
| 134 |
-
except: pass
|
| 135 |
|
| 136 |
return None, None, "Fail"
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# ์ฐจํธ ๋ฐ์ดํฐ
|
| 139 |
def get_chart_data(ticker):
|
| 140 |
try:
|
|
@@ -143,6 +155,8 @@ def get_chart_data(ticker):
|
|
| 143 |
elif ".JK" in ticker: fdr_symbol = f"IDX:{ticker.split('.')[0]}"
|
| 144 |
elif ".T" in ticker: fdr_symbol = f"TSE:{ticker.split('.')[0]}"
|
| 145 |
start = (datetime.datetime.now() - datetime.timedelta(days=90)).strftime('%Y-%m-%d')
|
|
|
|
|
|
|
| 146 |
hist = fdr.DataReader(fdr_symbol, start)
|
| 147 |
if not hist.empty: return hist.rename(columns={'Close':'Close', 'Open':'Open', 'High':'High', 'Low':'Low'}), "FDR"
|
| 148 |
except: pass
|
|
@@ -152,10 +166,6 @@ def get_chart_data(ticker):
|
|
| 152 |
except: pass
|
| 153 |
return pd.DataFrame(), "None"
|
| 154 |
|
| 155 |
-
def parse_stream(stream):
|
| 156 |
-
for chunk in stream:
|
| 157 |
-
if chunk.choices: yield chunk.choices[0].delta.content or ""
|
| 158 |
-
|
| 159 |
# ์๊ฐํ
|
| 160 |
def plot_candle_chart(hist, title, source):
|
| 161 |
fig = go.Figure(data=[go.Candlestick(x=hist.index, open=hist['Open'], high=hist['High'], low=hist['Low'], close=hist['Close'], name="Price")])
|
|
@@ -200,7 +210,6 @@ with st.sidebar:
|
|
| 200 |
st.divider()
|
| 201 |
menu = st.radio("MENU", ["๐ AI ์ค๋งํธ ๊ฒ์", "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋"], index=0)
|
| 202 |
|
| 203 |
-
# --- 1. AI ์ค๋งํธ ๊ฒ์ ---
|
| 204 |
if menu == "๐ AI ์ค๋งํธ ๊ฒ์":
|
| 205 |
st.subheader("๐ง ๋ค๊ตญ์ด AI ์ฃผ์ ๋น์")
|
| 206 |
c1, c2 = st.columns([3, 1])
|
|
@@ -208,22 +217,19 @@ if menu == "๐ AI ์ค๋งํธ ๊ฒ์":
|
|
| 208 |
with c2: btn = st.button("๋ถ์", use_container_width=True)
|
| 209 |
|
| 210 |
if btn:
|
| 211 |
-
with st.status("๐ง AI๊ฐ
|
| 212 |
-
# 1. AI์๊ฒ ํ์ง์ด ํค์๋ ์์ฑ ์์ฒญ
|
| 213 |
info = identify_target_with_ai(query)
|
| 214 |
if info:
|
| 215 |
-
name = info.get('name')
|
| 216 |
-
|
| 217 |
-
eng_key = info.get('english_keyword')
|
| 218 |
-
native_key = info.get('native_keyword') # ์ฌ๊ธฐ๊ฐ ํต์ฌ!
|
| 219 |
|
| 220 |
-
st.info(f"๐ก ํ๊ฒ: **{name} ({ticker})**
|
| 221 |
|
| 222 |
-
#
|
| 223 |
h, src = get_chart_data(ticker)
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
st.write("๐ฐ
|
| 227 |
news_data = get_polyglot_news(eng_key, native_key)
|
| 228 |
|
| 229 |
curr, pct = "N/A", 0
|
|
@@ -237,7 +243,7 @@ if menu == "๐ AI ์ค๋งํธ ๊ฒ์":
|
|
| 237 |
c_web, p_web, s_web = get_price_data_robust(ticker, name)
|
| 238 |
if c_web:
|
| 239 |
curr, pct, src = c_web, p_web, s_web
|
| 240 |
-
status.update(label="โ ๏ธ ์ฐจํธ ์์ (์น
|
| 241 |
else:
|
| 242 |
status.update(label="โ ๋ฐ์ดํฐ ์์", state="complete", expanded=False)
|
| 243 |
|
|
@@ -249,35 +255,18 @@ if menu == "๐ AI ์ค๋งํธ ๊ฒ์":
|
|
| 249 |
if not h.empty: st.plotly_chart(plot_candle_chart(h, name, src), use_container_width=True)
|
| 250 |
else: st.warning(f"์ฐจํธ ๋ฐ์ดํฐ ๋ถ์ฌ. (Source: {src})")
|
| 251 |
|
| 252 |
-
with st.expander(
|
| 253 |
-
st.text(news_data)
|
| 254 |
|
| 255 |
st.subheader(f"๐ง AI Insight: {name}")
|
| 256 |
-
|
| 257 |
-
prompt = f"""
|
| 258 |
-
[ROLE] Polyglot Investment Analyst (Korean/English/Vietnamese/Indonesian/etc).
|
| 259 |
-
[TARGET] {name} ({ticker})
|
| 260 |
-
[USER QUERY] "{query}"
|
| 261 |
-
[DATA] Price:{curr}, Trend:{pct:.2f}%
|
| 262 |
-
|
| 263 |
-
[NEWS CONTEXT (English & Local)]
|
| 264 |
-
{news_data}
|
| 265 |
-
|
| 266 |
-
[TASK]
|
| 267 |
-
1. Translate & Summarize key local news from the context (if any).
|
| 268 |
-
2. Analyze recent sentiment (3 months).
|
| 269 |
-
3. Verdict (Buy/Hold/Sell).
|
| 270 |
-
|
| 271 |
-
[LANG] Korean.
|
| 272 |
-
"""
|
| 273 |
msg = [{"role": "user", "content": prompt}]
|
| 274 |
stream = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=msg, stream=True)
|
| 275 |
st.write_stream(parse_stream(stream))
|
| 276 |
else: st.error("์ธ์ ์คํจ")
|
| 277 |
|
| 278 |
-
# --- 2. ์์ฅ ์ค์บ๋ ---
|
| 279 |
elif menu == "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋":
|
| 280 |
st.subheader("๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ ๊ดํ")
|
|
|
|
| 281 |
c1, c2 = st.columns([3, 1])
|
| 282 |
with c1: target = st.selectbox("๊ตญ๊ฐ", list(MARKET_SAMPLES.keys()))
|
| 283 |
with c2: scan = st.button("์ค์บ ์์")
|
|
@@ -289,6 +278,7 @@ elif menu == "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋":
|
|
| 289 |
|
| 290 |
for i, t in enumerate(tickers):
|
| 291 |
name_display = TICKER_NAMES.get(t, t)
|
|
|
|
| 292 |
p, c, s = get_price_data_robust(t, name_display)
|
| 293 |
if p is not None:
|
| 294 |
results.append({'Name': name_display, 'Ticker': t, 'Price': p, 'Change(%)': c, 'Source': s})
|
|
@@ -297,8 +287,8 @@ elif menu == "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋":
|
|
| 297 |
if results:
|
| 298 |
df = pd.DataFrame(results).sort_values('Change(%)', ascending=False)
|
| 299 |
st.plotly_chart(plot_bar_chart(df), use_container_width=True)
|
| 300 |
-
disp_cols = ['Name', 'Ticker', 'Price', 'Change(%)', 'Source']
|
| 301 |
|
|
|
|
| 302 |
c_up, c_down = st.columns(2)
|
| 303 |
with c_up:
|
| 304 |
st.success("๐ฅ Top Gainers")
|
|
|
|
| 8 |
import json
|
| 9 |
import datetime
|
| 10 |
import re
|
| 11 |
+
import time
|
| 12 |
+
import random # ๐ฒ ์ธ๊ฐ์ ์ธ ๋๋ค ๋๋ ์ด๋ฅผ ์ํด ์ถ๊ฐ
|
| 13 |
|
| 14 |
# 1. ํ์ด์ง ์ค์
|
| 15 |
st.set_page_config(page_title="Pocket Quant AI", page_icon="๐ง ", layout="wide")
|
|
|
|
| 22 |
st.stop()
|
| 23 |
|
| 24 |
# ==============================================================================
|
| 25 |
+
# ๐ต๏ธ [์์ฅ์ ] ๊ฐ์ง ๋ช
ํจ (User-Agent)
|
| 26 |
# ==============================================================================
|
| 27 |
+
FAKE_HEADERS = {
|
| 28 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 29 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 30 |
+
'Accept-Language': 'en-US,en;q=0.5',
|
| 31 |
+
'Referer': 'https://www.google.com/'
|
| 32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# ==============================================================================
|
| 35 |
+
# ๐ธ๏ธ [ํต์ฌ ์์ง] 3์ค ๊ทธ๋ฌผ๋ง ๋ด์ค ์์ง๊ธฐ (์ฌ๋์ธ ์ฒ)
|
| 36 |
# ==============================================================================
|
| 37 |
+
def fetch_news_robust(keyword):
|
|
|
|
|
|
|
|
|
|
| 38 |
summary = ""
|
| 39 |
seen_urls = set()
|
|
|
|
| 40 |
try:
|
| 41 |
with DDGS() as ddgs:
|
| 42 |
+
# 1๋จ๊ณ: ์ต๊ทผ 1๋ฌ (์๊ฒฉ)
|
| 43 |
+
results = list(ddgs.news(keyword, timelimit="m", max_results=3))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# 2๋จ๊ณ: ์์ผ๋ฉด ์ ์ฒด ๊ธฐ๊ฐ (์ํ)
|
| 46 |
+
if not results:
|
| 47 |
+
time.sleep(random.uniform(0.5, 1.0)) # 0.5~1์ด ์ฌ๊ธฐ
|
| 48 |
+
results = list(ddgs.news(keyword, max_results=3))
|
| 49 |
+
|
| 50 |
+
# 3๋จ๊ณ: ์น ๋ฌธ์ ๊ฒ์ (๋ด์คํญ ์ฐจ๋จ ์ฐํ)
|
| 51 |
+
if not results:
|
| 52 |
+
time.sleep(random.uniform(0.5, 1.0))
|
| 53 |
+
results = list(ddgs.text(f"{keyword} latest news", max_results=3))
|
| 54 |
+
|
| 55 |
+
for r in results:
|
| 56 |
+
url = r.get('url') or r.get('href')
|
| 57 |
+
if url not in seen_urls:
|
| 58 |
+
date = r.get('date', 'Unknown')
|
| 59 |
+
title = r.get('title', '')
|
| 60 |
+
body = r.get('body', '')
|
| 61 |
+
summary += f"[{date}] {title}: {body}\n"
|
| 62 |
+
seen_urls.add(url)
|
| 63 |
+
|
| 64 |
+
except Exception as e: return f"News Error: {e}"
|
| 65 |
+
return summary if summary else "No news found via Deep Search."
|
| 66 |
|
| 67 |
# ==============================================================================
|
| 68 |
+
# ๐ ๏ธ [ํต์ฌ ์์ง] ๋ฐ์ดํฐ ์์ง (์ฌ๋์ธ ์ฒ ๊ฒ์ ํฌํจ)
|
| 69 |
# ==============================================================================
|
| 70 |
+
def scrape_price_from_web(name, ticker):
|
| 71 |
+
"""
|
| 72 |
+
์ตํ์ ์๋จ: ์น ๊ฒ์์ผ๋ก ๊ฐ๊ฒฉ ๋ฏ์ด์ค๊ธฐ
|
| 73 |
+
"""
|
| 74 |
+
try:
|
| 75 |
+
# DDGS๋ ๋ด๋ถ์ ์ผ๋ก User-Agent๋ฅผ ๊ด๋ฆฌํ์ง๋ง, ๋๋ฌด ๋น ๋ฅด๋ฉด ์ฐจ๋จ๋จ
|
| 76 |
+
time.sleep(random.uniform(1.0, 2.0)) # 1~2์ด ๋๋ ์ด (์ฌ๋์ฒ๋ผ)
|
| 77 |
+
|
| 78 |
+
with DDGS() as ddgs:
|
| 79 |
+
query = f"{name} {ticker} stock price quote today"
|
| 80 |
+
results = ddgs.text(query, max_results=2)
|
| 81 |
+
blob = " ".join([r['body'] for r in results])
|
| 82 |
+
|
| 83 |
+
# ์ซ์ ์ถ์ถ (์ผํ ํฌํจ, ์์์ ํฌํจ)
|
| 84 |
+
match = re.search(r'(\d{1,3}(,\d{3})*(\.\d+)?)', blob)
|
| 85 |
+
if match:
|
| 86 |
+
price = float(match.group(0).replace(",", ""))
|
| 87 |
+
return price, 0.0
|
| 88 |
+
return None, None
|
| 89 |
+
except: return None, None
|
| 90 |
+
|
| 91 |
def get_price_data_robust(ticker, name):
|
| 92 |
+
# 1. FDR (ํ๊ตญ/๋ฒ ํธ๋จ/์ธ๋๋ค์์ ๊ฐ๋ ฅ)
|
| 93 |
try:
|
| 94 |
fdr_symbol = ticker
|
| 95 |
if ".VN" in ticker: fdr_symbol = ticker.split('.')[0]
|
|
|
|
| 105 |
return curr, pct, "FDR"
|
| 106 |
except: pass
|
| 107 |
|
| 108 |
+
# 2. Yahoo (๋ฏธ๊ตญ/์ผ๋ณธ ๊ฐ๋ ฅ)
|
| 109 |
try:
|
| 110 |
stock = yf.Ticker(ticker)
|
| 111 |
price = stock.fast_info.last_price
|
|
|
|
| 115 |
return price, pct, "Yahoo"
|
| 116 |
except: pass
|
| 117 |
|
| 118 |
+
# 3. Web Search (์ฌ๋์ธ ์ฒ ๊ฒ์)
|
| 119 |
+
price, pct = scrape_price_from_web(name, ticker)
|
| 120 |
+
if price is not None: return price, pct, "Web Search ๐ต๏ธ"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
return None, None, "Fail"
|
| 123 |
|
| 124 |
+
# ==============================================================================
|
| 125 |
+
# ๐ง [์์ง] AI ์ง๋ฅํ
|
| 126 |
+
# ==============================================================================
|
| 127 |
+
def identify_target_with_ai(user_query):
|
| 128 |
+
try:
|
| 129 |
+
prompt = f"""
|
| 130 |
+
[ROLE] Entity Resolver. [QUERY] "{user_query}"
|
| 131 |
+
[TASK] Identify company, ticker, dual keywords.
|
| 132 |
+
[OUTPUT JSON] {{ "name": "Eng Name", "ticker": "TICKER", "eng_key": "Name stock news", "native_key": "Local Name + news keyword" }}
|
| 133 |
+
"""
|
| 134 |
+
messages = [{"role": "user", "content": prompt}]
|
| 135 |
+
response = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=200)
|
| 136 |
+
content = re.sub(r"```json|```", "", response.choices[0].message.content.strip()).strip()
|
| 137 |
+
return json.loads(content)
|
| 138 |
+
except: return None
|
| 139 |
+
|
| 140 |
+
def get_polyglot_news(eng_key, native_key):
|
| 141 |
+
summary = "=== Global ===\n" + fetch_news_robust(eng_key)
|
| 142 |
+
if eng_key != native_key:
|
| 143 |
+
summary += "\n\n=== Local ===\n" + fetch_news_robust(native_key)
|
| 144 |
+
return summary
|
| 145 |
+
|
| 146 |
+
def parse_stream(stream):
|
| 147 |
+
for chunk in stream:
|
| 148 |
+
if chunk.choices: yield chunk.choices[0].delta.content or ""
|
| 149 |
+
|
| 150 |
# ์ฐจํธ ๋ฐ์ดํฐ
|
| 151 |
def get_chart_data(ticker):
|
| 152 |
try:
|
|
|
|
| 155 |
elif ".JK" in ticker: fdr_symbol = f"IDX:{ticker.split('.')[0]}"
|
| 156 |
elif ".T" in ticker: fdr_symbol = f"TSE:{ticker.split('.')[0]}"
|
| 157 |
start = (datetime.datetime.now() - datetime.timedelta(days=90)).strftime('%Y-%m-%d')
|
| 158 |
+
# [์์ฅ์ ] requests๋ฅผ ์ง์ ์ฐ๋ FDR ๋ด๋ถ ๋ก์ง์ ๊ฑด๋๋ฆฌ๊ธฐ ํ๋ค์ง๋ง,
|
| 159 |
+
# ๋๋ถ๋ถ Investing.com์ ๋ด ์ฐจ๋จ์ด ๋ํจ.
|
| 160 |
hist = fdr.DataReader(fdr_symbol, start)
|
| 161 |
if not hist.empty: return hist.rename(columns={'Close':'Close', 'Open':'Open', 'High':'High', 'Low':'Low'}), "FDR"
|
| 162 |
except: pass
|
|
|
|
| 166 |
except: pass
|
| 167 |
return pd.DataFrame(), "None"
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
# ์๊ฐํ
|
| 170 |
def plot_candle_chart(hist, title, source):
|
| 171 |
fig = go.Figure(data=[go.Candlestick(x=hist.index, open=hist['Open'], high=hist['High'], low=hist['Low'], close=hist['Close'], name="Price")])
|
|
|
|
| 210 |
st.divider()
|
| 211 |
menu = st.radio("MENU", ["๐ AI ์ค๋งํธ ๊ฒ์", "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋"], index=0)
|
| 212 |
|
|
|
|
| 213 |
if menu == "๐ AI ์ค๋งํธ ๊ฒ์":
|
| 214 |
st.subheader("๐ง ๋ค๊ตญ์ด AI ์ฃผ์ ๋น์")
|
| 215 |
c1, c2 = st.columns([3, 1])
|
|
|
|
| 217 |
with c2: btn = st.button("๋ถ์", use_container_width=True)
|
| 218 |
|
| 219 |
if btn:
|
| 220 |
+
with st.status("๐ง AI๊ฐ ์ฌ๋์ฒ๋ผ ๊ฒ์ ์ค...", expanded=True) as status:
|
|
|
|
| 221 |
info = identify_target_with_ai(query)
|
| 222 |
if info:
|
| 223 |
+
name, ticker = info.get('name'), info.get('ticker')
|
| 224 |
+
eng_key, native_key = info.get('eng_key'), info.get('native_key')
|
|
|
|
|
|
|
| 225 |
|
| 226 |
+
st.info(f"๐ก ํ๊ฒ: **{name} ({ticker})**")
|
| 227 |
|
| 228 |
+
# ๋ฐ์ดํฐ ์์ง
|
| 229 |
h, src = get_chart_data(ticker)
|
| 230 |
|
| 231 |
+
# ๋ด์ค ์์ง (์ฌ๋์ธ ์ฒ ๋๋ ์ด ํฌํจ)
|
| 232 |
+
st.write("๐ฐ ๋ด์ค ๊ฒ์ ์ค...")
|
| 233 |
news_data = get_polyglot_news(eng_key, native_key)
|
| 234 |
|
| 235 |
curr, pct = "N/A", 0
|
|
|
|
| 243 |
c_web, p_web, s_web = get_price_data_robust(ticker, name)
|
| 244 |
if c_web:
|
| 245 |
curr, pct, src = c_web, p_web, s_web
|
| 246 |
+
status.update(label="โ ๏ธ ์ฐจํธ ์์ (์น ๊ฒ์ ์ฑ๊ณต)", state="complete", expanded=False)
|
| 247 |
else:
|
| 248 |
status.update(label="โ ๋ฐ์ดํฐ ์์", state="complete", expanded=False)
|
| 249 |
|
|
|
|
| 255 |
if not h.empty: st.plotly_chart(plot_candle_chart(h, name, src), use_container_width=True)
|
| 256 |
else: st.warning(f"์ฐจํธ ๋ฐ์ดํฐ ๋ถ์ฌ. (Source: {src})")
|
| 257 |
|
| 258 |
+
with st.expander("๐ฐ ๋ด์ค ์๋ฌธ"): st.text(news_data)
|
|
|
|
| 259 |
|
| 260 |
st.subheader(f"๐ง AI Insight: {name}")
|
| 261 |
+
prompt = f"[ROLE] Analyst. [TARGET] {name} ({ticker}). [DATA] Price:{curr}, Trend:{pct:.2f}%. [NEWS] {news_data}. [TASK] Sentiment & Verdict. [LANG] Korean."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
msg = [{"role": "user", "content": prompt}]
|
| 263 |
stream = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=msg, stream=True)
|
| 264 |
st.write_stream(parse_stream(stream))
|
| 265 |
else: st.error("์ธ์ ์คํจ")
|
| 266 |
|
|
|
|
| 267 |
elif menu == "๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ค์บ๋":
|
| 268 |
st.subheader("๐ก ๊ธ๋ก๋ฒ ์์ฅ ์ ๊ดํ")
|
| 269 |
+
st.caption("โป ์ฌ๋์ฒ๋ผ ์ฒ์ฒํ ๊ฒ์ํ๋ฏ๋ก ์๋๊ฐ ์กฐ๊ธ ๋๋ฆด ์ ์์ต๋๋ค. (์ฐจ๋จ ๋ฐฉ์ง)")
|
| 270 |
c1, c2 = st.columns([3, 1])
|
| 271 |
with c1: target = st.selectbox("๊ตญ๊ฐ", list(MARKET_SAMPLES.keys()))
|
| 272 |
with c2: scan = st.button("์ค์บ ์์")
|
|
|
|
| 278 |
|
| 279 |
for i, t in enumerate(tickers):
|
| 280 |
name_display = TICKER_NAMES.get(t, t)
|
| 281 |
+
# ์ฌ๊ธฐ ๋ด๋ถ์์ Web Search ๋ฐ๋ ์ ๋๋ค sleep์ด ์ ์ฉ๋จ
|
| 282 |
p, c, s = get_price_data_robust(t, name_display)
|
| 283 |
if p is not None:
|
| 284 |
results.append({'Name': name_display, 'Ticker': t, 'Price': p, 'Change(%)': c, 'Source': s})
|
|
|
|
| 287 |
if results:
|
| 288 |
df = pd.DataFrame(results).sort_values('Change(%)', ascending=False)
|
| 289 |
st.plotly_chart(plot_bar_chart(df), use_container_width=True)
|
|
|
|
| 290 |
|
| 291 |
+
disp_cols = ['Name', 'Ticker', 'Price', 'Change(%)', 'Source']
|
| 292 |
c_up, c_down = st.columns(2)
|
| 293 |
with c_up:
|
| 294 |
st.success("๐ฅ Top Gainers")
|