Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- 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 |
-
|
| 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 |
-
|
| 59 |
-
|
| 60 |
-
# ํ๋กฌํํธ ์์ฑ
|
| 61 |
prompt = f"""
|
| 62 |
-
[
|
| 63 |
-
๋๋
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
[์ข
๋ชฉ ๋ฐ์ดํฐ]
|
| 67 |
-
- ์ข
๋ชฉ
|
| 68 |
-
- ํ์ฌ๊ฐ: {curr:,.0f}
|
| 69 |
-
-
|
| 70 |
-
-
|
| 71 |
-
-
|
| 72 |
-
- PBR (์ฃผ๊ฐ์์์ฐ๋น์จ): {info.get('priceToBook', 'N/A')}
|
| 73 |
|
| 74 |
-
[
|
| 75 |
-
# ๐ 3์ค ์์ฝ
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
# ๐ก ํฌ์ ์๊ฒฌ
|
| 79 |
-
(๋งค์ / ๋งค๋ / ๊ด๋ง ์ค ํ๋๋ฅผ ์ ํํ๊ณ ์ด์ ๋ฅผ ๊ตต๊ฒ ํ์)
|
| 80 |
-
|
| 81 |
-
# ๐ ์์ธ ๋ถ์
|
| 82 |
-
(์ถ์ธ, ๋ฐธ๋ฅ์์ด์
๋ฑ์ ์ข
ํฉ์ ์ผ๋ก ์์ )
|
| 83 |
"""
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
with st.expander("๐ข
|
| 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 |
-
|
|
|
|
| 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 |
-
|
| 109 |
-
st.subheader(f"๐ Qwen's Insight: {user_input}")
|
| 110 |
|
|
|
|
| 111 |
messages = [
|
| 112 |
-
{"role": "system", "content": "You are a
|
| 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=
|
| 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("์ข
๋ชฉ์ ์ฐพ์ ์ ์์ต๋๋ค.")
|