benjamin5607 commited on
Commit
fbf1364
ยท
verified ยท
1 Parent(s): c17319d

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +77 -81
streamlit_app.py CHANGED
@@ -4,7 +4,7 @@ import pandas as pd
4
  import requests
5
  import plotly.graph_objects as go
6
  from huggingface_hub import InferenceClient
7
- from duckduckgo_search import DDGS # ๐Ÿฆ† ๋‰ด์Šค๋ฅผ ๋ฌผ์–ด์˜ค๋Š” ์˜ค๋ฆฌ
8
 
9
  # 1. ํŽ˜์ด์ง€ ์„ค์ •
10
  st.set_page_config(
@@ -30,88 +30,76 @@ else:
30
  st.error("๐Ÿšจ ์„ค์ • ์˜ค๋ฅ˜: Secrets์— 'HF_TOKEN'์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
31
  st.stop()
32
 
33
- # --- [ํ•ต์‹ฌ] ๋ฌด๋ฃŒ ๋‰ด์Šค ๊ฒ€์ƒ‰ ํ•จ์ˆ˜ (DuckDuckGo) ---
34
- def get_company_news(keyword):
35
  """
36
- ํ•ด๋‹น ๊ธฐ์—…์˜ ์ตœ์‹  ๋‰ด์Šค 5๊ฐœ๋ฅผ ๊ธ์–ด์™€์„œ ์š”์•ฝ๋ณธ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
 
37
  """
 
38
  try:
39
- news_summary = ""
40
- # ๊ฒ€์ƒ‰์–ด์— 'stock news'๋ฅผ ๋ถ™์—ฌ์„œ ์ฃผ์‹ ๊ด€๋ จ ๋‰ด์Šค๋งŒ ์ฐพ์Œ
41
  with DDGS() as ddgs:
42
- # ์˜์–ด๋กœ ๊ฒ€์ƒ‰ํ•ด์•ผ ๊ฒฐ๊ณผ๊ฐ€ ํ’๋ถ€ํ•จ (AI๊ฐ€ ์ฝ๊ณ  ๋ฒˆ์—ญํ•ด์ค„ ๊ฑฐ์ž„)
43
- results = ddgs.text(f"{keyword} stock news", max_results=5)
 
44
 
45
- for i, r in enumerate(results):
46
- news_summary += f"{i+1}. {r['title']}: {r['body']}\n"
 
 
 
 
 
 
 
 
 
47
 
48
- return news_summary if news_summary else "No recent news found."
49
  except Exception as e:
50
- return f"News search failed: {e}"
51
 
52
- # --- ํ‹ฐ์ปค ์ด๋ฆ„ํ‘œ (ํ™•์žฅ๋จ) ---
53
  TICKER_NAMES = {
54
  # ๐Ÿ‡บ๐Ÿ‡ธ ๋ฏธ๊ตญ
55
  "AAPL": "Apple", "NVDA": "NVIDIA", "TSLA": "Tesla", "AMZN": "Amazon", "MSFT": "Microsoft",
56
  "GOOGL": "Google", "AMD": "AMD", "META": "Meta", "NFLX": "Netflix", "INTC": "Intel",
57
  "JPM": "JPMorgan", "V": "Visa", "JNJ": "Johnson&Johnson", "WMT": "Walmart", "PG": "P&G",
58
  "KO": "Coca-Cola", "PEP": "Pepsi", "COST": "Costco", "DIS": "Disney", "XOM": "Exxon",
59
-
60
  # ๐Ÿ‡ฐ๐Ÿ‡ท ํ•œ๊ตญ
61
  "005930.KS": "์‚ผ์„ฑ์ „์ž", "000660.KS": "SKํ•˜์ด๋‹‰์Šค", "035420.KS": "NAVER", "035720.KS": "์นด์นด์˜ค",
62
  "005380.KS": "ํ˜„๋Œ€์ฐจ", "000270.KS": "๊ธฐ์•„", "051910.KS": "LGํ™”ํ•™", "006400.KS": "์‚ผ์„ฑSDI",
63
  "105560.KS": "KB๊ธˆ์œต", "055550.KS": "์‹ ํ•œ์ง€์ฃผ", "005490.KS": "POSCOํ™€๋”ฉ์Šค", "068270.KS": "์…€ํŠธ๋ฆฌ์˜จ",
64
  "207940.KS": "์‚ผ๋ฐ”", "032830.KQ": "์•Œํ…Œ์˜ค์  ", "086520.KQ": "์—์ฝ”ํ”„๋กœ", "247540.KQ": "์—์ฝ”ํ”„๋กœ๋น„์— ",
65
-
66
  # ๐Ÿ‡ฏ๐Ÿ‡ต ์ผ๋ณธ
67
  "7203.T": "Toyota", "6758.T": "Sony", "9984.T": "SoftBank Grp", "9434.T": "SoftBank Corp",
68
  "8035.T": "Tokyo Elec", "6861.T": "Keyence", "7974.T": "Nintendo", "8306.T": "MUFG",
69
  "9432.T": "NTT", "6501.T": "Hitachi", "6954.T": "Fanuc", "7267.T": "Honda",
70
-
71
  # ๐Ÿ‡จ๐Ÿ‡ณ ์ค‘๊ตญ/ํ™์ฝฉ
72
  "9988.HK": "Alibaba", "0700.HK": "Tencent", "3690.HK": "Meituan", "1211.HK": "BYD",
73
  "1810.HK": "Xiaomi", "0941.HK": "China Mobile", "0939.HK": "CCB", "0883.HK": "CNOOC",
74
-
75
  # ๐Ÿ‡น๐Ÿ‡ผ ๋Œ€๋งŒ
76
  "2330.TW": "TSMC", "2454.TW": "MediaTek", "2317.TW": "Foxconn", "2308.TW": "Delta Elec",
77
-
78
  # ๐Ÿ‡ฎ๐Ÿ‡ณ ์ธ๋„
79
  "RELIANCE.NS": "Reliance", "TCS.NS": "TCS", "HDFCBANK.NS": "HDFC Bank", "INFY.NS": "Infosys",
80
-
81
- # ๐Ÿ‡ป๐Ÿ‡ณ ๋ฒ ํŠธ๋‚จ (ETF ๋Œ€์ฒด ํฌํ•จ)
82
  "VNM": "VanEck Vietnam ETF", "VIC.VN": "Vingroup", "VHM.VN": "Vinhomes", "VCB.VN": "Vietcombank",
83
- "VNM.VN": "Vinamilk", "HPG.VN": "Hoa Phat", "MSN.VN": "Masan Group",
84
-
85
- # ๐Ÿ‡ฎ๐Ÿ‡ฉ ์ธ๋„๋„ค์‹œ์•„
86
- "BBCA.JK": "BCA", "BBRI.JK": "BRI", "TLKM.JK": "Telkom", "BMRI.JK": "Mandiri",
87
- "ASII.JK": "Astra", "GOTO.JK": "GoTo",
88
-
89
- # ๐Ÿ‡ธ๐Ÿ‡ฌ ์‹ฑ๊ฐ€ํฌ๋ฅด
90
- "D05.SI": "DBS", "O39.SI": "OCBC", "U11.SI": "UOB", "Z74.SI": "Singtel", "C52.SI": "SIA",
91
-
92
- # ๐Ÿ‡ฒ๐Ÿ‡พ ๋ง๋ ˆ์ด์‹œ์•„
93
- "1155.KL": "Maybank", "1023.KL": "CIMB", "1295.KL": "Public Bank", "5183.KL": "Petronas Chem",
94
-
95
- # ๐Ÿ‡น๐Ÿ‡ญ ํƒœ๊ตญ
96
- "PTT.BK": "PTT", "AOT.BK": "AOT", "CPALL.BK": "CP All", "ADVANC.BK": "AIS", "SCC.BK": "Siam Cement",
97
-
98
- # ๐Ÿ‡ต๐Ÿ‡ญ ํ•„๋ฆฌํ•€
99
- "SM.PS": "SM Inv", "SMPH.PS": "SM Prime", "BDO.PS": "BDO", "ALI.PS": "Ayala Land"
100
  }
101
 
102
  MARKET_SAMPLES = {
103
- "๐Ÿ‡ฐ๐Ÿ‡ท ํ•œ๊ตญ (Korea)": ["005930.KS", "000660.KS", "035420.KS", "035720.KS", "005380.KS", "000270.KS", "051910.KS", "006400.KS", "105560.KS", "055550.KS", "005490.KS", "068270.KS", "207940.KS", "032830.KQ", "086520.KQ", "247540.KQ"],
104
- "๐Ÿ‡บ๐Ÿ‡ธ ๋ฏธ๊ตญ (USA)": ["AAPL", "NVDA", "TSLA", "AMZN", "MSFT", "GOOGL", "AMD", "META", "NFLX", "INTC", "JPM", "V", "JNJ", "WMT", "PG", "KO", "PEP", "COST", "DIS", "XOM"],
105
- "๐Ÿ‡ฏ๐Ÿ‡ต ์ผ๋ณธ (Japan)": ["7203.T", "6758.T", "9984.T", "9434.T", "8035.T", "6861.T", "7974.T", "8306.T", "9432.T", "6501.T", "6954.T", "7267.T", "8316.T", "8411.T", "6902.T", "4063.T"],
106
- "๐Ÿ‡จ๐Ÿ‡ณ ์ค‘๊ตญ/ํ™์ฝฉ": ["9988.HK", "0700.HK", "3690.HK", "1211.HK", "1810.HK", "0941.HK", "0939.HK", "0883.HK", "2318.HK", "9618.HK", "9888.HK", "2015.HK"],
107
- "๐Ÿ‡น๐Ÿ‡ผ ๋Œ€๋งŒ (Taiwan)": ["2330.TW", "2454.TW", "2317.TW", "2308.TW", "2881.TW", "2882.TW", "1301.TW", "1303.TW", "2382.TW", "3711.TW", "2891.TW"],
108
- "๐Ÿ‡ฎ๐Ÿ‡ณ ์ธ๋„ (India)": ["RELIANCE.NS", "TCS.NS", "HDFCBANK.NS", "INFY.NS", "ICICIBANK.NS", "BHARTIARTL.NS", "SBIN.NS", "HINDUNILVR.NS", "ITC.NS", "LICI.NS"],
109
- "๐Ÿ‡ป๐Ÿ‡ณ ๋ฒ ํŠธ๋‚จ (Vietnam)": ["VNM", "VIC.VN", "VHM.VN", "VCB.VN", "VNM.VN", "HPG.VN", "GAS.VN", "MSN.VN"],
110
- "๐Ÿ‡ฎ๐Ÿ‡ฉ ์ธ๋„๋„ค์‹œ์•„": ["BBCA.JK", "BBRI.JK", "TLKM.JK", "BMRI.JK", "ASII.JK", "UNVR.JK", "BBNI.JK", "ADRO.JK", "GOTO.JK", "AMMN.JK"],
111
- "๐Ÿ‡ธ๐Ÿ‡ฌ ์‹ฑ๊ฐ€ํฌ๋ฅด": ["D05.SI", "O39.SI", "U11.SI", "Z74.SI", "C52.SI", "A17U.SI", "C38U.SI", "G13.SI"],
112
- "๐Ÿ‡ฒ๐Ÿ‡พ ๋ง๋ ˆ์ด์‹œ์•„": ["1155.KL", "1023.KL", "1295.KL", "5183.KL", "5347.KL", "6033.KL", "4065.KL"],
113
- "๐Ÿ‡น๐Ÿ‡ญ ํƒœ๊ตญ": ["PTT.BK", "AOT.BK", "CPALL.BK", "ADVANC.BK", "SCC.BK", "BDMS.BK", "KBANK.BK"],
114
- "๐Ÿ‡ต๐Ÿ‡ญ ํ•„๋ฆฌํ•€": ["SM.PS", "SMPH.PS", "BDO.PS", "ALI.PS", "BPI.PS", "ICT.PS", "JFC.PS", "AC.PS"]
115
  }
116
 
117
  # --- ์กฑ๋ณด ๋ฐ ๊ฒ€์ƒ‰ ํ—ฌํผ ---
@@ -178,8 +166,7 @@ def plot_candle_chart(hist, title):
178
  def plot_bar_chart(df):
179
  colors = ['#00FF00' if x > 0 else '#FF0000' for x in df['Change(%)']]
180
  fig = go.Figure(go.Bar(
181
- x=df['Name'], y=df['Change(%)'],
182
- marker_color=colors,
183
  text=df['Change(%)'].apply(lambda x: f"{x:.2f}%"), textposition='auto'
184
  ))
185
  fig.update_layout(
@@ -197,29 +184,30 @@ with st.sidebar:
197
  st.divider()
198
  menu = st.radio("MENU", ["๐Ÿ” ์ข…๋ชฉ ๋ถ„์„ (AI Analysis)", "๐Ÿ“ก ์‹œ์žฅ ์Šค์บ๋„ˆ (Market Watch)"], index=0)
199
  st.divider()
200
- st.caption("Data: Yahoo + Naver + Web News")
201
 
202
  # ==============================================================================
203
- # 1. ์ข…๋ชฉ ๋ถ„์„ ๋ชจ๋“œ (๋‰ด์Šค ํ†ตํ•ฉ)
204
  # ==============================================================================
205
  if "์ข…๋ชฉ ๋ถ„์„" in menu:
206
- st.subheader("๐Ÿ” AI Investment Analyst")
207
  c1, c2 = st.columns([3, 1])
208
  with c1: user_input = st.text_input("Ticker", "Samsung", label_visibility="collapsed", placeholder="์ข…๋ชฉ๋ช… (์˜ˆ: ์‚ผ์„ฑ์ „์ž, Tesla)")
209
- with c2: analyze_btn = st.button("Go", use_container_width=True)
210
 
211
  if analyze_btn:
212
- with st.status("๐Ÿš€ Gathering Data (Price + News)...", expanded=True) as status:
213
  ticker = search_ticker(user_input)
214
 
215
  if ticker:
216
  try:
217
- # 1. ๋‰ด์Šค ์ˆ˜์ง‘ (๋น„๋™๊ธฐ์ฒ˜๋Ÿผ ๋จผ์ € ์‹คํ–‰)
218
- st.write("๐Ÿ“ฐ ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ์ตœ์‹  ๋‰ด์Šค๋ฅผ ๊ธ์–ด์˜ค๋Š” ์ค‘...")
219
- news_data = get_company_news(user_input) # ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰
 
220
 
221
  # 2. ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘
222
- st.write(f"๐Ÿ“ˆ '{ticker}' ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์—ฐ๊ฒฐ ์ค‘...")
223
  stock = yf.Ticker(ticker)
224
  hist = stock.history(period="3mo")
225
 
@@ -236,7 +224,7 @@ if "์ข…๋ชฉ ๋ถ„์„" in menu:
236
  pbr = info.get('priceToBook', 'N/A')
237
  currency = info.get('currency', '')
238
 
239
- status.update(label="โœ… Analysis Ready!", state="complete", expanded=False)
240
 
241
  st.divider()
242
  m1, m2, m3, m4 = st.columns(4)
@@ -247,43 +235,51 @@ if "์ข…๋ชฉ ๋ถ„์„" in menu:
247
 
248
  st.plotly_chart(plot_candle_chart(hist, ticker), use_container_width=True)
249
 
250
- # --- ๋‰ด์Šค ๋ณด์—ฌ์ฃผ๊ธฐ ---
251
- with st.expander("๐Ÿ“ฐ AI๊ฐ€ ์ฐธ๊ณ ํ•œ ์‹ค์‹œ๊ฐ„ ๋‰ด์Šค ์›๋ฌธ ๋ณด๊ธฐ (ํด๋ฆญ)"):
252
  st.text(news_data)
 
253
 
254
- # --- AI ๋ถ„์„ ---
255
- st.subheader(f"๐Ÿง  AI Insight: {user_input}")
256
  prompt = f"""
257
- [ROLE] Professional Investment Analyst.
 
258
 
259
- [TASK]
260
- Analyze {user_input} ({ticker}) by combining Financial Data AND Recent News.
 
261
 
262
  [FINANCIAL DATA]
263
  - Price: {curr} {currency} ({change_pct:.2f}%)
264
- - PER: {per}
265
  - PBR: {pbr}
266
 
267
- [LATEST NEWS & ISSUES]
268
  {news_data}
269
 
270
- [INSTRUCTION]
271
- 1. **Market Sentiment:** Analyze the vibe based on the news (e.g., new product, scandal, earnings).
272
- 2. **Valuation:** Is it cheap or expensive based on PER/PBR?
273
- 3. **Conclusion:** Buy/Sell/Hold recommendation with clear reasons.
 
 
274
 
275
- **IMPORTANT:** Reply in the User's Language. Use Markdown. No Code Blocks.
 
 
 
276
  """
277
  messages = [{"role": "user", "content": prompt}]
278
  stream = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=messages, stream=True)
279
  with st.container(border=True): st.write_stream(parse_stream(stream))
280
 
281
- else: status.update(label="โŒ No Data", state="error"); st.error("No Data")
282
- except Exception as e: status.update(label="โŒ Error", state="error"); st.error(f"Error: {e}")
283
- else: status.update(label="โŒ Not Found", state="error"); st.error("Not Found")
284
 
285
  # ==============================================================================
286
- # 2. ์‹œ์žฅ ์Šค์บ๋„ˆ (Top 10 ์œ ์ง€)
287
  # ==============================================================================
288
  elif "์‹œ์žฅ ์Šค์บ๋„ˆ" in menu:
289
  st.subheader("๐Ÿ“ก Global Market Watch (Top 10)")
@@ -326,6 +322,6 @@ elif "์‹œ์žฅ ์Šค์บ๋„ˆ" in menu:
326
  with c2:
327
  st.error(f"๐Ÿ’ง {target_market} Top 10 Losers")
328
  st.dataframe(df.tail(10).sort_values('Change(%)').style.format({"Price": "{:,.2f}", "Change(%)": "{:,.2f}%"}), use_container_width=True)
329
- else: st.warning("No valid price data.")
330
- else: st.warning("Data download failed.")
331
  except Exception as e: st.error(f"Scan failed: {e}")
 
4
  import requests
5
  import plotly.graph_objects as go
6
  from huggingface_hub import InferenceClient
7
+ from duckduckgo_search import DDGS
8
 
9
  # 1. ํŽ˜์ด์ง€ ์„ค์ •
10
  st.set_page_config(
 
30
  st.error("๐Ÿšจ ์„ค์ • ์˜ค๋ฅ˜: Secrets์— 'HF_TOKEN'์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
31
  st.stop()
32
 
33
+ # --- [์—…๊ทธ๋ ˆ์ด๋“œ] ์‹ฌ์ธต ๋‰ด์Šค ๊ฒ€์ƒ‰๊ธฐ (์ตœ๊ทผ 3๊ฐœ์›” ์ด์Šˆ ์ˆ˜์ง‘) ---
34
+ def get_company_news_deep(keyword):
35
  """
36
+ ๋‹จ์ˆœ ๋‰ด์Šค ๊ฒ€์ƒ‰์ด ์•„๋‹ˆ๋ผ, '์žฌ๋ฌด', 'ํ•ฉ๋ณ‘', '๋ฆฌ์Šคํฌ', '์ „๋ง' ๋“ฑ
37
+ ํ•ต์‹ฌ ํ‚ค์›Œ๋“œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ๋‹ค๊ฐ๋„๋กœ ์ •๋ณด๋ฅผ ๊ธ์–ด์˜ต๋‹ˆ๋‹ค.
38
  """
39
+ news_summary = ""
40
  try:
 
 
41
  with DDGS() as ddgs:
42
+ # 1. ์ผ๋ฐ˜์ ์ธ ์ตœ์‹  ๋‰ด์Šค (์˜์–ด ๋ฒ ์ด์Šค๋กœ ๊ฒ€์ƒ‰ํ•ด์•ผ ์ •๋ณด๋Ÿ‰์ด ๋งŽ์Œ)
43
+ # max_results๋ฅผ 10๊ฐœ๋กœ ๋Š˜๋ ค์„œ ์ •๋ณด๋Ÿ‰์„ ํ™•๋ณด
44
+ results_gen = ddgs.text(f"{keyword} stock latest news business", max_results=7)
45
 
46
+ # 2. ํ•ต์‹ฌ ์ด์Šˆ ๊ฒ€์ƒ‰ (Earnings, Scandal, Merger)
47
+ # ์ตœ๊ทผ 3๊ฐœ์›”๊ฐ„์˜ ๊ตต์งํ•œ ์‚ฌ๊ฑด์„ ์ฐพ๊ธฐ ์œ„ํ•ด ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€
48
+ results_issues = ddgs.text(f"{keyword} stock quarterly earnings forecast risk", max_results=3)
49
+
50
+ # ๊ฒฐ๊ณผ ํ•ฉ์น˜๊ธฐ
51
+ all_results = list(results_gen) + list(results_issues)
52
+
53
+ for i, r in enumerate(all_results):
54
+ # ์ค‘๋ณต ์ œ๊ฑฐ ๋ฐ ํฌ๋งทํŒ…
55
+ if r['title'] not in news_summary:
56
+ news_summary += f"[{i+1}] {r['title']} : {r['body']}\n"
57
 
58
+ return news_summary if news_summary else "No major news found via DuckDuckGo."
59
  except Exception as e:
60
+ return f"News search error: {e}"
61
 
62
+ # --- ํ‹ฐ์ปค ๋งคํ•‘ (์ƒ๋žต ์—†์ด ์œ ์ง€) ---
63
  TICKER_NAMES = {
64
  # ๐Ÿ‡บ๐Ÿ‡ธ ๋ฏธ๊ตญ
65
  "AAPL": "Apple", "NVDA": "NVIDIA", "TSLA": "Tesla", "AMZN": "Amazon", "MSFT": "Microsoft",
66
  "GOOGL": "Google", "AMD": "AMD", "META": "Meta", "NFLX": "Netflix", "INTC": "Intel",
67
  "JPM": "JPMorgan", "V": "Visa", "JNJ": "Johnson&Johnson", "WMT": "Walmart", "PG": "P&G",
68
  "KO": "Coca-Cola", "PEP": "Pepsi", "COST": "Costco", "DIS": "Disney", "XOM": "Exxon",
 
69
  # ๐Ÿ‡ฐ๐Ÿ‡ท ํ•œ๊ตญ
70
  "005930.KS": "์‚ผ์„ฑ์ „์ž", "000660.KS": "SKํ•˜์ด๋‹‰์Šค", "035420.KS": "NAVER", "035720.KS": "์นด์นด์˜ค",
71
  "005380.KS": "ํ˜„๋Œ€์ฐจ", "000270.KS": "๊ธฐ์•„", "051910.KS": "LGํ™”ํ•™", "006400.KS": "์‚ผ์„ฑSDI",
72
  "105560.KS": "KB๊ธˆ์œต", "055550.KS": "์‹ ํ•œ์ง€์ฃผ", "005490.KS": "POSCOํ™€๋”ฉ์Šค", "068270.KS": "์…€ํŠธ๋ฆฌ์˜จ",
73
  "207940.KS": "์‚ผ๋ฐ”", "032830.KQ": "์•Œํ…Œ์˜ค์  ", "086520.KQ": "์—์ฝ”ํ”„๋กœ", "247540.KQ": "์—์ฝ”ํ”„๋กœ๋น„์— ",
 
74
  # ๐Ÿ‡ฏ๐Ÿ‡ต ์ผ๋ณธ
75
  "7203.T": "Toyota", "6758.T": "Sony", "9984.T": "SoftBank Grp", "9434.T": "SoftBank Corp",
76
  "8035.T": "Tokyo Elec", "6861.T": "Keyence", "7974.T": "Nintendo", "8306.T": "MUFG",
77
  "9432.T": "NTT", "6501.T": "Hitachi", "6954.T": "Fanuc", "7267.T": "Honda",
 
78
  # ๐Ÿ‡จ๐Ÿ‡ณ ์ค‘๊ตญ/ํ™์ฝฉ
79
  "9988.HK": "Alibaba", "0700.HK": "Tencent", "3690.HK": "Meituan", "1211.HK": "BYD",
80
  "1810.HK": "Xiaomi", "0941.HK": "China Mobile", "0939.HK": "CCB", "0883.HK": "CNOOC",
 
81
  # ๐Ÿ‡น๐Ÿ‡ผ ๋Œ€๋งŒ
82
  "2330.TW": "TSMC", "2454.TW": "MediaTek", "2317.TW": "Foxconn", "2308.TW": "Delta Elec",
 
83
  # ๐Ÿ‡ฎ๐Ÿ‡ณ ์ธ๋„
84
  "RELIANCE.NS": "Reliance", "TCS.NS": "TCS", "HDFCBANK.NS": "HDFC Bank", "INFY.NS": "Infosys",
85
+ # ๐Ÿ‡ป๐Ÿ‡ณ ๋ฒ ํŠธ๋‚จ & ๋™๋‚จ์•„
 
86
  "VNM": "VanEck Vietnam ETF", "VIC.VN": "Vingroup", "VHM.VN": "Vinhomes", "VCB.VN": "Vietcombank",
87
+ "BBCA.JK": "BCA", "BBRI.JK": "BRI", "D05.SI": "DBS", "1155.KL": "Maybank", "PTT.BK": "PTT", "SM.PS": "SM Inv"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  }
89
 
90
  MARKET_SAMPLES = {
91
+ "๐Ÿ‡ฐ๐Ÿ‡ท ํ•œ๊ตญ (Korea)": ["005930.KS", "000660.KS", "035420.KS", "035720.KS", "005380.KS", "000270.KS", "051910.KS", "006400.KS", "105560.KS", "068270.KS"],
92
+ "๐Ÿ‡บ๐Ÿ‡ธ ๋ฏธ๊ตญ (USA)": ["AAPL", "NVDA", "TSLA", "AMZN", "MSFT", "GOOGL", "AMD", "META", "NFLX", "INTC", "JPM", "V", "KO"],
93
+ "๐Ÿ‡ฏ๐Ÿ‡ต ์ผ๋ณธ (Japan)": ["7203.T", "6758.T", "9984.T", "8035.T", "6861.T", "7974.T", "8306.T", "6501.T"],
94
+ "๐Ÿ‡จ๐Ÿ‡ณ ์ค‘๊ตญ/ํ™์ฝฉ": ["9988.HK", "0700.HK", "3690.HK", "1211.HK", "1810.HK", "0941.HK"],
95
+ "๐Ÿ‡น๐Ÿ‡ผ ๋Œ€๋งŒ (Taiwan)": ["2330.TW", "2454.TW", "2317.TW", "2308.TW"],
96
+ "๐Ÿ‡ฎ๐Ÿ‡ณ ์ธ๋„ (India)": ["RELIANCE.NS", "TCS.NS", "HDFCBANK.NS", "INFY.NS"],
97
+ "๐Ÿ‡ป๐Ÿ‡ณ ๋ฒ ํŠธ๋‚จ (Vietnam)": ["VNM", "VIC.VN", "VHM.VN", "VCB.VN", "VNM.VN", "HPG.VN"],
98
+ "๐Ÿ‡ฎ๐Ÿ‡ฉ ์ธ๋„๋„ค์‹œ์•„": ["BBCA.JK", "BBRI.JK", "TLKM.JK", "BMRI.JK", "ASII.JK"],
99
+ "๐Ÿ‡ธ๐Ÿ‡ฌ ์‹ฑ๊ฐ€ํฌ๋ฅด": ["D05.SI", "O39.SI", "U11.SI", "Z74.SI"],
100
+ "๐Ÿ‡ฒ๐Ÿ‡พ ๋ง๋ ˆ์ด์‹œ์•„": ["1155.KL", "1023.KL", "1295.KL", "5183.KL"],
101
+ "๐Ÿ‡น๐Ÿ‡ญ ํƒœ๊ตญ": ["PTT.BK", "AOT.BK", "CPALL.BK", "ADVANC.BK"],
102
+ "๐Ÿ‡ต๐Ÿ‡ญ ํ•„๋ฆฌํ•€": ["SM.PS", "SMPH.PS", "BDO.PS", "ALI.PS"]
103
  }
104
 
105
  # --- ์กฑ๋ณด ๋ฐ ๊ฒ€์ƒ‰ ํ—ฌํผ ---
 
166
  def plot_bar_chart(df):
167
  colors = ['#00FF00' if x > 0 else '#FF0000' for x in df['Change(%)']]
168
  fig = go.Figure(go.Bar(
169
+ x=df['Name'], y=df['Change(%)'], marker_color=colors,
 
170
  text=df['Change(%)'].apply(lambda x: f"{x:.2f}%"), textposition='auto'
171
  ))
172
  fig.update_layout(
 
184
  st.divider()
185
  menu = st.radio("MENU", ["๐Ÿ” ์ข…๋ชฉ ๋ถ„์„ (AI Analysis)", "๐Ÿ“ก ์‹œ์žฅ ์Šค์บ๋„ˆ (Market Watch)"], index=0)
186
  st.divider()
187
+ st.caption("Data: Yahoo + Naver + Global News")
188
 
189
  # ==============================================================================
190
+ # 1. ์ข…๋ชฉ ๋ถ„์„ ๋ชจ๋“œ (์‹ฌ์ธต ๋‰ด์Šค ์ ์šฉ)
191
  # ==============================================================================
192
  if "์ข…๋ชฉ ๋ถ„์„" in menu:
193
+ st.subheader("๐Ÿ” AI Investment Analyst (Deep Dive)")
194
  c1, c2 = st.columns([3, 1])
195
  with c1: user_input = st.text_input("Ticker", "Samsung", label_visibility="collapsed", placeholder="์ข…๋ชฉ๋ช… (์˜ˆ: ์‚ผ์„ฑ์ „์ž, Tesla)")
196
+ with c2: analyze_btn = st.button("Deep Analyze", use_container_width=True)
197
 
198
  if analyze_btn:
199
+ with st.status("๐Ÿš€ 1๋‹จ๊ณ„: ๊ธ€๋กœ๋ฒŒ ๋‰ด์Šค ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค...", expanded=True) as status:
200
  ticker = search_ticker(user_input)
201
 
202
  if ticker:
203
  try:
204
+ # 1. ๋‰ด์Šค ์‹ฌ์ธต ์ˆ˜์ง‘
205
+ st.write(f"๐ŸŒ ์ „ ์„ธ๊ณ„ ์›น์„ ๋’ค์ ธ์„œ '{user_input}'์˜ ์ตœ๊ทผ 3๊ฐœ์›” ์ด์Šˆ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค...")
206
+ # ๊ฒ€์ƒ‰์–ด์— 'English' ๊ฐ™์€ ์ œํ•œ์„ ๋‘์ง€ ์•Š๊ณ , AI๊ฐ€ ์•Œ์•„์„œ ํ•ด์„ํ•˜๊ฒŒ๋” ์›๋ฌธ ์ˆ˜์ง‘
207
+ news_data = get_company_news_deep(user_input)
208
 
209
  # 2. ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘
210
+ st.write(f"๐Ÿ“ˆ '{ticker}' ์žฌ๋ฌด/์ฐจํŠธ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ ์ค‘...")
211
  stock = yf.Ticker(ticker)
212
  hist = stock.history(period="3mo")
213
 
 
224
  pbr = info.get('priceToBook', 'N/A')
225
  currency = info.get('currency', '')
226
 
227
+ status.update(label="โœ… ๋ถ„์„ ์ค€๋น„ ์™„๋ฃŒ! AI๊ฐ€ ๋ฆฌํฌํŠธ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.", state="complete", expanded=False)
228
 
229
  st.divider()
230
  m1, m2, m3, m4 = st.columns(4)
 
235
 
236
  st.plotly_chart(plot_candle_chart(hist, ticker), use_container_width=True)
237
 
238
+ # --- ๋‰ด์Šค ํŽผ์ณ๋ณด๊ธฐ ---
239
+ with st.expander("๐ŸŒ AI๊ฐ€ ์ˆ˜์ง‘ํ•œ ๋‹ค๊ตญ์–ด ๋‰ด์Šค ์›๋ฌธ ๋ณด๊ธฐ (ํด๋ฆญ)"):
240
  st.text(news_data)
241
+ st.caption("โ€ป ์œ„ ๋‰ด์Šค๋“ค์€ DuckDuckGo์—์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ˆ˜์ง‘๋œ ์›๋ฌธ์ž…๋‹ˆ๋‹ค.")
242
 
243
+ # --- AI ์‹ฌ์ธต ๋ถ„์„ ํ”„๋กฌํ”„ํŠธ ---
244
+ st.subheader(f"๐Ÿง  Deep Insight: {user_input}")
245
  prompt = f"""
246
+ [ROLE]
247
+ You are a Polyglot Global Investment Analyst (Korean/English/Chinese proficient).
248
 
249
+ [TASK]
250
+ Perform a deep-dive analysis on {user_input} ({ticker}) for a Korean investor.
251
+ Integrate Financial Data with the 'Narrative' from recent news (approx. past 3 months).
252
 
253
  [FINANCIAL DATA]
254
  - Price: {curr} {currency} ({change_pct:.2f}%)
255
+ - PER: {per} (Valuation)
256
  - PBR: {pbr}
257
 
258
+ [GLOBAL NEWS CONTEXT (Unstructured)]
259
  {news_data}
260
 
261
+ [INSTRUCTIONS]
262
+ 1. **News Analysis (Core):** Identify key events (Earnings, Mergers, Regulatory issues, Product launches) from the news.
263
+ - If news is in English/Foreign languages, translate the essence to Korean.
264
+ - Filter out noise; focus on what impacts the stock price.
265
+ 2. **Valuation Check:** Is the stock cheap or expensive given the news context?
266
+ 3. **Strategy:** Provide a clear Buy/Sell/Hold opinion based on the "Data + Narrative".
267
 
268
+ **OUTPUT FORMAT:**
269
+ - Use Markdown. No code blocks.
270
+ - Language: **Korean (ํ•œ๊ตญ์–ด)**.
271
+ - Tone: Professional yet easy to understand.
272
  """
273
  messages = [{"role": "user", "content": prompt}]
274
  stream = client.chat.completions.create(model="Qwen/Qwen2.5-72B-Instruct", messages=messages, stream=True)
275
  with st.container(border=True): st.write_stream(parse_stream(stream))
276
 
277
+ else: status.update(label="โŒ ๋ฐ์ดํ„ฐ ์—†์Œ", state="error"); st.error("์ฐจํŠธ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
278
+ except Exception as e: status.update(label="โŒ ์—๋Ÿฌ", state="error"); st.error(f"Error: {e}")
279
+ else: status.update(label="โŒ ๊ฒ€์ƒ‰ ์‹คํŒจ", state="error"); st.error("์ข…๋ชฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
280
 
281
  # ==============================================================================
282
+ # 2. ์‹œ์žฅ ์Šค์บ๋„ˆ
283
  # ==============================================================================
284
  elif "์‹œ์žฅ ์Šค์บ๋„ˆ" in menu:
285
  st.subheader("๐Ÿ“ก Global Market Watch (Top 10)")
 
322
  with c2:
323
  st.error(f"๐Ÿ’ง {target_market} Top 10 Losers")
324
  st.dataframe(df.tail(10).sort_values('Change(%)').style.format({"Price": "{:,.2f}", "Change(%)": "{:,.2f}%"}), use_container_width=True)
325
+ else: st.warning("์œ ํšจํ•œ ๊ฐ€๊ฒฉ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
326
+ else: st.warning("๋ฐ์ดํ„ฐ ๋‹ค์šด๋กœ๋“œ ์‹คํŒจ.")
327
  except Exception as e: st.error(f"Scan failed: {e}")