Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +93 -65
streamlit_app.py
CHANGED
|
@@ -3,103 +3,131 @@ 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 |
st.title("๐จ๐ณ Qwen ํฌ์ ์๋ฌธ๊ฐ")
|
| 9 |
-
st.caption("
|
| 10 |
|
| 11 |
-
# 2.
|
| 12 |
-
# Secrets์ HF_TOKEN์ด ์์ผ๋ฉด ์๋ฌ ๋ฐฉ์ง
|
| 13 |
if "HF_TOKEN" in st.secrets:
|
| 14 |
client = InferenceClient(api_key=st.secrets["HF_TOKEN"])
|
| 15 |
else:
|
| 16 |
-
st.error("๐จ ์ค์ ์ค๋ฅ:
|
| 17 |
st.stop()
|
| 18 |
|
| 19 |
-
# 3.
|
| 20 |
@st.cache_data
|
| 21 |
def get_stock_list():
|
| 22 |
return fdr.StockListing('KRX')
|
| 23 |
|
| 24 |
-
|
| 25 |
-
df_krx = get_stock_list()
|
| 26 |
-
|
| 27 |
-
# 4. ์ข
๋ชฉ ๊ฒ์ ํจ์
|
| 28 |
def search_ticker(input_text):
|
| 29 |
if input_text.isascii(): return input_text.upper()
|
| 30 |
-
|
| 31 |
-
res =
|
| 32 |
if res.empty: return None
|
| 33 |
-
|
| 34 |
code = res.iloc[0]['Code']
|
| 35 |
market = res.iloc[0]['Market']
|
| 36 |
return f"{code}.KS" if market == 'KOSPI' else f"{code}.KQ"
|
| 37 |
|
| 38 |
-
# 5.
|
| 39 |
-
user_input = st.text_input("์ข
๋ชฉ
|
| 40 |
|
| 41 |
-
if st.button("๐
|
| 42 |
-
ticker = search_ticker(user_input)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
-
# ๋ฐ์ดํฐ ์์ง
|
| 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.
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
# ---
|
| 62 |
-
with st.
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
[์ข
๋ชฉ ๋ฐ์ดํฐ]
|
| 69 |
-
- ์ข
๋ชฉ๋ช
: {user_input} ({ticker})
|
| 70 |
-
- ํ์ฌ๊ฐ: {curr:,.0f}
|
| 71 |
-
- 1๋ฌ ๋ณ๋๋ฅ : {change:.2f}%
|
| 72 |
-
- 52์ฃผ ์ต๊ณ ๊ฐ: {info.get('fiftyTwoWeekHigh', 'N/A')}
|
| 73 |
-
- 52์ฃผ ์ต์ ๊ฐ: {info.get('fiftyTwoWeekLow', 'N/A')}
|
| 74 |
-
|
| 75 |
-
[๋ถ์ ์์ฒญ]
|
| 76 |
-
1. ์ต๊ทผ 1๋ฌ๊ฐ์ ์ถ์ธ๋ฅผ ํ ๋ฌธ์ฅ์ผ๋ก ์์ฝํ์ธ์.
|
| 77 |
-
2. ํ์ฌ ๊ฐ๊ฒฉ์ด ๊ณ ์ ๋๋น ์ด๋ค ์์น์ธ์ง ์ค๋ช
ํ์ธ์.
|
| 78 |
-
3. ํฌ์ ๊ด์ (๋งค์/๋งค๋/๊ด๋ง)์ ์์งํ๊ฒ ์ ์ํ๊ณ ์ด์ ๋ฅผ ๋ง๋ถ์ด์ธ์.
|
| 79 |
-
|
| 80 |
-
๋งํฌ๋ 'ํด์์ฒด'๋ฅผ ์ฐ๊ณ , ์ด๋ชจ์ง๋ฅผ ์ ์ ํ ์ฌ์ฉํ์ธ์.
|
| 81 |
-
"""
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
st.
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
else:
|
| 101 |
-
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
| 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:
|
| 14 |
client = InferenceClient(api_key=st.secrets["HF_TOKEN"])
|
| 15 |
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()
|
| 28 |
+
res = df[df['Name'] == input_text]
|
| 29 |
if res.empty: return None
|
|
|
|
| 30 |
code = res.iloc[0]['Code']
|
| 31 |
market = res.iloc[0]['Market']
|
| 32 |
return f"{code}.KS" if market == 'KOSPI' else f"{code}.KQ"
|
| 33 |
|
| 34 |
+
# 5. ๋ฉ์ธ UI
|
| 35 |
+
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 |
+
|
| 123 |
+
st.write_stream(stream)
|
| 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("ํด๋น ์ข
๋ชฉ์ ์ฐพ์ ์ ์์ต๋๋ค.")
|