benjamin5607 commited on
Commit
ed69bee
ยท
verified ยท
1 Parent(s): 8a5149d

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +32 -50
streamlit_app.py CHANGED
@@ -3,11 +3,11 @@ import yfinance as yf
3
  import FinanceDataReader as fdr
4
  from huggingface_hub import InferenceClient
5
 
6
- # 1. ํŽ˜์ด์ง€ ์„ค์ • (๊น”๋”ํ•˜๊ฒŒ)
7
  st.set_page_config(page_title="Pocket Quant", page_icon="๐Ÿ“ˆ", layout="centered")
8
 
9
  st.title("๐Ÿ‡จ๐Ÿ‡ณ Qwen ํˆฌ์ž ์ž๋ฌธ๊ฐ€")
10
- st.caption("๊ฒฐ๊ณผ๋กœ ๋งํ•ฉ๋‹ˆ๋‹ค. ๊ณผ์ •์€ ๊ฑฐ๋“ค ๋ฟ.")
11
 
12
  # 2. ํ† ํฐ ํ™•์ธ
13
  if "HF_TOKEN" in st.secrets:
@@ -16,12 +16,12 @@ else:
16
  st.error("๐Ÿšจ ์„ค์ • ์˜ค๋ฅ˜: Secrets์— 'HF_TOKEN'์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
17
  st.stop()
18
 
19
- # 3. ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ (์บ์‹ฑ)
20
  @st.cache_data
21
  def get_stock_list():
22
  return fdr.StockListing('KRX')
23
 
24
- # 4. ์ข…๋ชฉ ๊ฒ€์ƒ‰ ๋กœ์ง
25
  def search_ticker(input_text):
26
  if input_text.isascii(): return input_text.upper()
27
  df = get_stock_list()
@@ -36,87 +36,69 @@ user_input = st.text_input("๋ถ„์„ํ•  ์ข…๋ชฉ (์˜ˆ: ์‚ผ์„ฑ์ „์ž, ์นด์นด์˜ค, NVDA
36
 
37
  if st.button("๐Ÿš€ AI ๋ถ„์„ ์‹œ์ž‘", use_container_width=True):
38
 
39
- # --- [UI ํ•ต์‹ฌ] ์ง„ํ–‰ ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” Status ์ปจํ…Œ์ด๋„ˆ ---
40
- with st.status("๐Ÿค– AI๊ฐ€ ์—ด์‹ฌํžˆ ๋ถ„์„ ์ค‘์ž…๋‹ˆ๋‹ค...", expanded=True) as status:
41
 
42
- st.write("1๏ธโƒฃ ์ข…๋ชฉ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค...")
43
  ticker = search_ticker(user_input)
44
 
45
  if ticker:
46
- st.write(f"2๏ธโƒฃ '{ticker}'์˜ ์ตœ์‹  ์‹œ์„ธ๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค...")
47
  try:
48
  stock = yf.Ticker(ticker)
49
  hist = stock.history(period="1mo")
50
  info = stock.info
51
 
52
  if not hist.empty:
53
- # ๋ฐ์ดํ„ฐ ๊ณ„์‚ฐ
54
  curr = hist['Close'].iloc[-1]
55
  start = hist['Close'].iloc[0]
56
  change = ((curr - start) / start) * 100
57
 
58
- st.write("3๏ธโƒฃ Qwen์—๊ฒŒ ๋„˜๊ธธ ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑ ์ค‘์ž…๋‹ˆ๋‹ค...")
59
-
60
- # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
61
  prompt = f"""
62
- [์—ญํ• ]
63
- ๋„ˆ๋Š” ์›”๊ฐ€์—์„œ 20๋…„ ๊ฒฝ๋ ฅ์„ ๊ฐ€์ง„ ๋ฒ ํ…Œ๋ž‘ ํŽ€๋“œ๋งค๋‹ˆ์ €์•ผ.
64
- ์ดˆ๋ณด ํˆฌ์ž์ž๋ฅผ ์œ„ํ•ด ์•„์ฃผ ์‰ฝ๊ณ  ๋ช…์พŒํ•˜๊ฒŒ ๋ถ„์„ ๋ฆฌํฌํŠธ๋ฅผ ์จ์ค˜.
65
-
 
66
  [์ข…๋ชฉ ๋ฐ์ดํ„ฐ]
67
- - ์ข…๋ชฉ๋ช…: {user_input} ({ticker})
68
- - ํ˜„์žฌ๊ฐ€: {curr:,.0f} (ํ†ตํ™”: {info.get('currency', 'KRW')})
69
- - ์ตœ๊ทผ 1๋‹ฌ ์ˆ˜์ต๋ฅ : {change:.2f}%
70
- - 52์ฃผ ์ตœ๊ณ ๊ฐ€: {info.get('fiftyTwoWeekHigh', 'N/A')}
71
- - PER (์ฃผ๊ฐ€์ˆ˜์ต๋น„์œจ): {info.get('trailingPE', 'N/A')}
72
- - PBR (์ฃผ๊ฐ€์ˆœ์ž์‚ฐ๋น„์œจ): {info.get('priceToBook', 'N/A')}
73
 
74
- [์ž‘์„ฑ ์–‘์‹]
75
- # ๐Ÿ“Š 3์ค„ ์š”์•ฝ
76
- (์—ฌ๊ธฐ์— ํ•ต์‹ฌ๋งŒ 3์ค„๋กœ ์š”์•ฝ)
77
-
78
- # ๐Ÿ’ก ํˆฌ์ž ์˜๊ฒฌ
79
- (๋งค์ˆ˜ / ๋งค๋„ / ๊ด€๋ง ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ  ์ด์œ ๋ฅผ ๊ตต๊ฒŒ ํ‘œ์‹œ)
80
-
81
- # ๐Ÿ“ ์ƒ์„ธ ๋ถ„์„
82
- (์ถ”์„ธ, ๋ฐธ๋ฅ˜์—์ด์…˜ ๋“ฑ์„ ์ข…ํ•ฉ์ ์œผ๋กœ ์„œ์ˆ )
83
  """
84
 
85
- # --- [UI ํ•ต์‹ฌ] ๋ฐ์ดํ„ฐ๋Š” ์ˆจ๊น€ (๊ถ๊ธˆํ•œ ์‚ฌ๋žŒ๋งŒ ํด๋ฆญ) ---
86
- with st.expander("๐Ÿ”ข AI๊ฐ€ ์ฐธ๊ณ ํ•œ ์›๋ณธ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ (ํด๋ฆญ)"):
87
- st.json(info) # ๋ณต์žกํ•œ ์ œ์ด์Šจ ๋ฐ์ดํ„ฐ๋Š” ์—ฌ๊ธฐ์— ์ˆจ๊น€
88
- st.write("---")
89
- st.write("AI์—๊ฒŒ ๋ณด๋‚ธ ์งˆ๋ฌธ(Prompt):")
90
  st.code(prompt)
91
 
92
- # ์ƒํƒœ ์—…๋ฐ์ดํŠธ: ์™„๋ฃŒ๋˜๋ฉด ๋ฐ•์Šค๊ฐ€ ์ž๋™์œผ๋กœ ์ ‘ํž˜!
93
- status.update(label="โœ… ๋ถ„์„์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!", state="complete", expanded=False)
94
-
95
- # ---------------------------------------------------
96
- # ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์ง„์งœ ๊ฒฐ๊ณผ ํ™”๋ฉด
97
- # ---------------------------------------------------
98
 
99
- st.divider() # ๊ตฌ๋ถ„์„ 
 
100
 
101
- # 1. ์ฐจํŠธ ์˜์—ญ
102
  col1, col2 = st.columns([1, 2])
103
  with col1:
104
  st.metric("ํ˜„์žฌ ์ฃผ๊ฐ€", f"{curr:,.0f}", f"{change:.2f}%")
105
  with col2:
106
  st.line_chart(hist['Close'], height=150)
107
 
108
- # 2. AI ๋ฆฌํฌํŠธ ์˜์—ญ (์ŠคํŠธ๋ฆฌ๋ฐ)
109
- st.subheader(f"๐Ÿ‰ Qwen's Insight: {user_input}")
110
 
 
111
  messages = [
112
- {"role": "system", "content": "You are a helpful financial assistant."},
113
  {"role": "user", "content": prompt}
114
  ]
115
 
116
  stream = client.chat.completions.create(
117
  model="Qwen/Qwen2.5-72B-Instruct",
118
  messages=messages,
119
- max_tokens=1000,
120
  stream=True
121
  )
122
 
@@ -124,10 +106,10 @@ if st.button("๐Ÿš€ AI ๋ถ„์„ ์‹œ์ž‘", use_container_width=True):
124
 
125
  else:
126
  status.update(label="โŒ ๋ฐ์ดํ„ฐ ์—†์Œ", state="error")
127
- st.error("๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
128
  except Exception as e:
129
  status.update(label="โŒ ์—๋Ÿฌ ๋ฐœ์ƒ", state="error")
130
  st.error(f"์˜ค๋ฅ˜: {e}")
131
  else:
132
  status.update(label="โŒ ์ข…๋ชฉ ์ฐพ๊ธฐ ์‹คํŒจ", state="error")
133
- st.error("ํ•ด๋‹น ์ข…๋ชฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
 
3
  import FinanceDataReader as fdr
4
  from huggingface_hub import InferenceClient
5
 
6
+ # 1. ํŽ˜์ด์ง€ ์„ค์ •
7
  st.set_page_config(page_title="Pocket Quant", page_icon="๐Ÿ“ˆ", layout="centered")
8
 
9
  st.title("๐Ÿ‡จ๐Ÿ‡ณ Qwen ํˆฌ์ž ์ž๋ฌธ๊ฐ€")
10
+ st.caption("์ฝ”๋“œ ๋ฐ•์Šค ์—†์ด, ๊น”๋”ํ•œ ๋ฆฌํฌํŠธ๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
11
 
12
  # 2. ํ† ํฐ ํ™•์ธ
13
  if "HF_TOKEN" in st.secrets:
 
16
  st.error("๐Ÿšจ ์„ค์ • ์˜ค๋ฅ˜: Secrets์— 'HF_TOKEN'์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
17
  st.stop()
18
 
19
+ # 3. ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ
20
  @st.cache_data
21
  def get_stock_list():
22
  return fdr.StockListing('KRX')
23
 
24
+ # 4. ์ข…๋ชฉ ๊ฒ€์ƒ‰
25
  def search_ticker(input_text):
26
  if input_text.isascii(): return input_text.upper()
27
  df = get_stock_list()
 
36
 
37
  if st.button("๐Ÿš€ AI ๋ถ„์„ ์‹œ์ž‘", use_container_width=True):
38
 
39
+ with st.status("๐Ÿค– AI๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์”น๊ณ  ๋œฏ๊ณ  ๋ง›๋ณด๋Š” ์ค‘...", expanded=True) as status:
 
40
 
 
41
  ticker = search_ticker(user_input)
42
 
43
  if ticker:
 
44
  try:
45
  stock = yf.Ticker(ticker)
46
  hist = stock.history(period="1mo")
47
  info = stock.info
48
 
49
  if not hist.empty:
 
50
  curr = hist['Close'].iloc[-1]
51
  start = hist['Close'].iloc[0]
52
  change = ((curr - start) / start) * 100
53
 
54
+ # --- [์ˆ˜์ •๋œ ๋ถ€๋ถ„] ํ”„๋กฌํ”„ํŠธ์— '์ ˆ๋Œ€ ์ฝ”๋“œ ์“ฐ์ง€ ๋งˆ' ๋ช…๋ น ์ถ”๊ฐ€ ---
 
 
55
  prompt = f"""
56
+ [์ง€์‹œ์‚ฌํ•ญ]
57
+ ๋„ˆ๋Š” ์ฃผ์‹ ํˆฌ์ž ์ „๋ฌธ๊ฐ€์•ผ. ์•„๋ž˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ณ  ๋ณด๊ณ ์„œ๋ฅผ ์ž‘์„ฑํ•ด.
58
+ **์ค‘์š”: ๋‹ต๋ณ€์„ ์ฝ”๋“œ ๋ธ”๋ก(```)์œผ๋กœ ์ ˆ๋Œ€ ๊ฐ์‹ธ์ง€ ๋งˆ. ๊ทธ๋ƒฅ ์ค„๊ธ€(Raw Markdown)๋กœ ์จ.**
59
+ ์ œ๋ชฉ(#)๊ณผ ๋ณผ๋“œ์ฒด(**)๋งŒ ์‚ฌ์šฉํ•ด์„œ ๊ฐ€๋…์„ฑ ์žˆ๊ฒŒ ๊พธ๋ฉฐ์ค˜.
60
+
61
  [์ข…๋ชฉ ๋ฐ์ดํ„ฐ]
62
+ - ์ข…๋ชฉ: {user_input} ({ticker})
63
+ - ํ˜„์žฌ๊ฐ€: {curr:,.0f}
64
+ - ์ถ”์„ธ: {change:.2f}%
65
+ - PER: {info.get('trailingPE', 'N/A')}
66
+ - PBR: {info.get('priceToBook', 'N/A')}
 
67
 
68
+ [๋ณด๊ณ ์„œ ๋ชฉ์ฐจ]
69
+ ## ๐Ÿ“Š 3์ค„ ์š”์•ฝ
70
+ ## ๐Ÿ’ก ํˆฌ์ž ์˜๊ฒฌ (๋งค์ˆ˜/๋งค๋„/๊ด€๋ง)
71
+ ## ๐Ÿ“ ์ƒ์„ธ ๋ถ„์„
 
 
 
 
 
72
  """
73
 
74
+ # ๋ฐ์ดํ„ฐ ์ˆจ๊น€ ์ฒ˜๋ฆฌ
75
+ with st.expander("๐Ÿ”ข ๋ฐ์ดํ„ฐ ์›๋ณธ & ํ”„๋กฌํ”„ํŠธ ํ™•์ธ"):
76
+ st.json(info)
 
 
77
  st.code(prompt)
78
 
79
+ status.update(label="โœ… ๋ถ„์„ ์™„๋ฃŒ! ๋ฆฌํฌํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.", state="complete", expanded=False)
 
 
 
 
 
80
 
81
+ # ๊ฒฐ๊ณผ ํ™”๋ฉด
82
+ st.divider()
83
 
 
84
  col1, col2 = st.columns([1, 2])
85
  with col1:
86
  st.metric("ํ˜„์žฌ ์ฃผ๊ฐ€", f"{curr:,.0f}", f"{change:.2f}%")
87
  with col2:
88
  st.line_chart(hist['Close'], height=150)
89
 
90
+ st.subheader(f"๐Ÿ‰ Qwen ๋ฆฌํฌํŠธ: {user_input}")
 
91
 
92
+ # ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€์—๋„ '์ฝ”๋“œ ๊ธˆ์ง€' ์„ธ๋‡Œ
93
  messages = [
94
+ {"role": "system", "content": "You are a financial analyst. Do NOT use code blocks in your response. Output raw markdown text only."},
95
  {"role": "user", "content": prompt}
96
  ]
97
 
98
  stream = client.chat.completions.create(
99
  model="Qwen/Qwen2.5-72B-Instruct",
100
  messages=messages,
101
+ max_tokens=1500, # ๋ง์„ ์ข€ ๋” ๊ธธ๊ฒŒ ํ•˜๋„๋ก ๋Š˜๋ฆผ
102
  stream=True
103
  )
104
 
 
106
 
107
  else:
108
  status.update(label="โŒ ๋ฐ์ดํ„ฐ ์—†์Œ", state="error")
109
+ st.error("๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")
110
  except Exception as e:
111
  status.update(label="โŒ ์—๋Ÿฌ ๋ฐœ์ƒ", state="error")
112
  st.error(f"์˜ค๋ฅ˜: {e}")
113
  else:
114
  status.update(label="โŒ ์ข…๋ชฉ ์ฐพ๊ธฐ ์‹คํŒจ", state="error")
115
+ st.error("์ข…๋ชฉ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")