Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +77 -23
streamlit_app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
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("
|
| 10 |
-
st.caption("
|
| 11 |
|
| 12 |
# 2. ํ ํฐ ํ์ธ
|
| 13 |
if "HF_TOKEN" in st.secrets:
|
|
@@ -31,58 +33,109 @@ def search_ticker(input_text):
|
|
| 31 |
market = res.iloc[0]['Market']
|
| 32 |
return f"{code}.KS" if market == 'KOSPI' else f"{code}.KQ"
|
| 33 |
|
| 34 |
-
# --- [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def parse_stream(stream):
|
| 36 |
for chunk in stream:
|
| 37 |
-
# ๋ฐ์ดํฐ๊ฐ ์๊ณ , choices ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ง ์์์ง ํ์ธ (์ด๊ฒ ์๋ฌ ํด๊ฒฐ ํต์ฌ!)
|
| 38 |
if chunk.choices and len(chunk.choices) > 0:
|
| 39 |
delta = chunk.choices[0].delta
|
| 40 |
-
# ๋ด์ฉ(content)์ด ์์ ๋๋ง ์ฒ๋ฆฌ
|
| 41 |
if hasattr(delta, 'content') and delta.content:
|
| 42 |
-
|
| 43 |
-
# ๋งํฌ๋ค์ด ์ฝ๋ ๊ธฐํธ ์ญ์
|
| 44 |
-
cleaned = content.replace("```markdown", "").replace("```", "")
|
| 45 |
yield cleaned
|
| 46 |
|
| 47 |
# 5. ๋ฉ์ธ UI
|
| 48 |
-
user_input = st.text_input("๋ถ์ํ ์ข
๋ชฉ
|
| 49 |
|
| 50 |
-
if st.button("๐
|
| 51 |
|
| 52 |
-
with st.status("๐ค
|
| 53 |
|
| 54 |
ticker = search_ticker(user_input)
|
| 55 |
|
| 56 |
if ticker:
|
| 57 |
try:
|
|
|
|
| 58 |
stock = yf.Ticker(ticker)
|
| 59 |
hist = stock.history(period="1mo")
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if not hist.empty:
|
| 63 |
curr = hist['Close'].iloc[-1]
|
| 64 |
start = hist['Close'].iloc[0]
|
| 65 |
change = ((curr - start) / start) * 100
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
prompt = f"""
|
| 68 |
[์ง์์ฌํญ]
|
| 69 |
-
|
| 70 |
-
๊ฐ๋
์ฑ์ ์ํด ์ ๋ชฉ(#)๊ณผ ๊ฐ์กฐ(**)๋ฅผ ์ฌ์ฉํด.
|
| 71 |
|
| 72 |
-
[์ข
๋ชฉ
|
| 73 |
- ์ข
๋ชฉ: {user_input} ({ticker})
|
| 74 |
- ํ์ฌ๊ฐ: {curr:,.0f}
|
| 75 |
-
- ์ถ์ธ: {change:.2f}%
|
| 76 |
-
- PER: {
|
| 77 |
-
- PBR: {
|
| 78 |
|
| 79 |
-
[
|
| 80 |
## ๐ 3์ค ์์ฝ
|
| 81 |
-
## ๐ก
|
| 82 |
-
## ๐
|
| 83 |
"""
|
| 84 |
|
| 85 |
-
with st.expander("๐ข ๋ฐ์ดํฐ
|
|
|
|
| 86 |
st.json(info)
|
| 87 |
|
| 88 |
status.update(label="โ
๋ถ์ ์๋ฃ!", state="complete", expanded=False)
|
|
@@ -91,13 +144,14 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 91 |
col1, col2 = st.columns([1, 2])
|
| 92 |
with col1:
|
| 93 |
st.metric("ํ์ฌ ์ฃผ๊ฐ", f"{curr:,.0f}", f"{change:.2f}%")
|
|
|
|
| 94 |
with col2:
|
| 95 |
st.line_chart(hist['Close'], height=150)
|
| 96 |
|
| 97 |
st.subheader(f"๐ Qwen ๋ฆฌํฌํธ: {user_input}")
|
| 98 |
|
| 99 |
messages = [
|
| 100 |
-
{"role": "system", "content": "You are a financial analyst.
|
| 101 |
{"role": "user", "content": prompt}
|
| 102 |
]
|
| 103 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import yfinance as yf
|
| 3 |
import FinanceDataReader as fdr
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import requests
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
# 1. ํ์ด์ง ์ค์
|
| 9 |
st.set_page_config(page_title="Pocket Quant", page_icon="๐", layout="centered")
|
| 10 |
|
| 11 |
+
st.title("๐ค 0์ ์ฃผ์ ์๋ฌธ๊ฐ")
|
| 12 |
+
st.caption("ํ๊ตญ ์ฃผ์์ ๋ค์ด๋ฒ์์, ๋ฏธ๊ตญ ์ฃผ์์ ์ผํ์์ ๊ฐ์ ธ์ต๋๋ค.")
|
| 13 |
|
| 14 |
# 2. ํ ํฐ ํ์ธ
|
| 15 |
if "HF_TOKEN" in st.secrets:
|
|
|
|
| 33 |
market = res.iloc[0]['Market']
|
| 34 |
return f"{code}.KS" if market == 'KOSPI' else f"{code}.KQ"
|
| 35 |
|
| 36 |
+
# --- [ํต์ฌ] ๋ค์ด๋ฒ ๊ธ์ต ํฌ๋กค๋ง ํจ์ (ํ๊ตญ ์ฃผ์์ฉ) ---
|
| 37 |
+
def get_naver_stock_info(code):
|
| 38 |
+
try:
|
| 39 |
+
# .KS, .KQ ๋ผ๊ณ ์ซ์๋ง ๋จ๊น
|
| 40 |
+
code_num = code.split('.')[0]
|
| 41 |
+
url = f"https://finance.naver.com/item/main.naver?code={code_num}"
|
| 42 |
+
|
| 43 |
+
# ๋ธ๋ผ์ฐ์ ์ธ ์ฒ ์์ด๊ธฐ (์ ๊ทธ๋ฌ๋ฉด ๋ค์ด๋ฒ๊ฐ ์ฐจ๋จํจ)
|
| 44 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 45 |
+
res = requests.get(url, headers=headers)
|
| 46 |
+
|
| 47 |
+
# ํ์ด์ง ๋ด์ ๋ชจ๋ ํ(table)๋ฅผ ์ฝ์ด์ด
|
| 48 |
+
dfs = pd.read_html(res.text, encoding='euc-kr')
|
| 49 |
+
|
| 50 |
+
# 'PER'์ 'PBR'์ด ๋ค์ด์๋ ํ ์ฐพ๊ธฐ (๋ณดํต 4๋ฒ์งธ๋ 5๋ฒ์งธ ํ์ ์์)
|
| 51 |
+
# ๋ค์ด๋ฒ ๊ตฌ์กฐ์ '์๊ฐ์ด์ก' ๋ฑ์ด ์๋ ํ๋ฅผ ์ฐพ์์ ํ์ฑ
|
| 52 |
+
info = {}
|
| 53 |
+
|
| 54 |
+
for df in dfs:
|
| 55 |
+
# ๋ฐ์ดํฐํ๋ ์์ ๋ฌธ์์ด๋ก ๋ณํํด์ ๊ฒ์
|
| 56 |
+
str_df = str(df)
|
| 57 |
+
if "PER" in str_df or "PBR" in str_df:
|
| 58 |
+
# ํ๋ฅผ ๋๋ฉด์ ๊ฐ ์ถ์ถ (๋งค์ฐ ์์์ ์ด์ง๋ง ํ์คํ ๋ฐฉ๋ฒ)
|
| 59 |
+
for i in range(len(df)):
|
| 60 |
+
row = df.iloc[i]
|
| 61 |
+
for j in range(len(row)):
|
| 62 |
+
val = str(row[j])
|
| 63 |
+
if "PER" in val and "EPS" not in val: # PER l N/A ์ด๋ฐ์์ธ ๊ฒฝ์ฐ
|
| 64 |
+
# ๊ทธ ๋ค์ ์นธ์ด๋ ๋ค๋ค์ ์นธ์ ์ซ์๊ฐ ์์
|
| 65 |
+
try: info['trailingPE'] = float(df.iloc[i, j+1])
|
| 66 |
+
except: pass
|
| 67 |
+
if "PBR" in val:
|
| 68 |
+
try: info['priceToBook'] = float(df.iloc[i, j+1])
|
| 69 |
+
except: pass
|
| 70 |
+
|
| 71 |
+
return info
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return {} # ์คํจํ๋ฉด ๋น ๋์
๋๋ฆฌ
|
| 74 |
+
|
| 75 |
+
# --- ์คํธ๋ฆผ ํํฐ (์ฝ๋๋ฐ์ค ์ ๊ฑฐ) ---
|
| 76 |
def parse_stream(stream):
|
| 77 |
for chunk in stream:
|
|
|
|
| 78 |
if chunk.choices and len(chunk.choices) > 0:
|
| 79 |
delta = chunk.choices[0].delta
|
|
|
|
| 80 |
if hasattr(delta, 'content') and delta.content:
|
| 81 |
+
cleaned = delta.content.replace("```markdown", "").replace("```", "")
|
|
|
|
|
|
|
| 82 |
yield cleaned
|
| 83 |
|
| 84 |
# 5. ๋ฉ์ธ UI
|
| 85 |
+
user_input = st.text_input("๋ถ์ํ ์ข
๋ชฉ", "์ผ์ฑ์ ์")
|
| 86 |
|
| 87 |
+
if st.button("๐ ๋ถ์ ์คํ", use_container_width=True):
|
| 88 |
|
| 89 |
+
with st.status("๐ค ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ ๋ถ์ ์ค์
๋๋ค...", expanded=True) as status:
|
| 90 |
|
| 91 |
ticker = search_ticker(user_input)
|
| 92 |
|
| 93 |
if ticker:
|
| 94 |
try:
|
| 95 |
+
# 1. ์ฐจํธ ๋ฐ์ดํฐ (yfinance๊ฐ ์ด๊ฑด ์ํจ)
|
| 96 |
stock = yf.Ticker(ticker)
|
| 97 |
hist = stock.history(period="1mo")
|
| 98 |
+
|
| 99 |
+
# 2. ์ฌ๋ฌด ๋ฐ์ดํฐ (๊ตญ๊ฐ๋ ์ด๋๋?)
|
| 100 |
+
info = {}
|
| 101 |
+
# ํ๊ตญ ์ฃผ์(.KS, .KQ)์ด๋ฉด ๋ค์ด๋ฒ์์ ํธ์ด์ค๊ธฐ
|
| 102 |
+
if ".KS" in ticker or ".KQ" in ticker:
|
| 103 |
+
st.write("๐ฐ๐ท ํ๊ตญ ์ฃผ์: ๋ค์ด๋ฒ ๊ธ์ต์์ ์ฌ๋ฌด ์ ๋ณด ์์ง ์ค...")
|
| 104 |
+
naver_info = get_naver_stock_info(ticker)
|
| 105 |
+
info.update(stock.info) # ๊ธฐ๋ณธ ์ ๋ณด
|
| 106 |
+
info.update(naver_info) # PER, PBR ๋ฎ์ด์ฐ๊ธฐ
|
| 107 |
+
else:
|
| 108 |
+
st.write("๐บ๐ธ ๋ฏธ๊ตญ ์ฃผ์: ์ผํ ํ์ด๋ธ์ค ์ฌ์ฉ ์ค...")
|
| 109 |
+
info = stock.info
|
| 110 |
|
| 111 |
if not hist.empty:
|
| 112 |
curr = hist['Close'].iloc[-1]
|
| 113 |
start = hist['Close'].iloc[0]
|
| 114 |
change = ((curr - start) / start) * 100
|
| 115 |
|
| 116 |
+
# ๋ค์ด๋ฒ์์ ๊ฐ์ ธ์จ ๊ฐ ํ์ธ (์์ผ๋ฉด N/A)
|
| 117 |
+
per = info.get('trailingPE', 'N/A')
|
| 118 |
+
pbr = info.get('priceToBook', 'N/A')
|
| 119 |
+
|
| 120 |
prompt = f"""
|
| 121 |
[์ง์์ฌํญ]
|
| 122 |
+
์ฃผ์ ํฌ์ ๋ณด๊ณ ์๋ฅผ ์์ฑํด. ์ฝ๋ ๋ฐ์ค(```) ์ฐ์ง ๋ง๊ณ ์ค๊ธ๋ก ์จ.
|
|
|
|
| 123 |
|
| 124 |
+
[์ข
๋ชฉ ์ ๋ณด]
|
| 125 |
- ์ข
๋ชฉ: {user_input} ({ticker})
|
| 126 |
- ํ์ฌ๊ฐ: {curr:,.0f}
|
| 127 |
+
- ์ถ์ธ(1๋ฌ): {change:.2f}%
|
| 128 |
+
- PER (์ฃผ๊ฐ์์ต๋น์จ): {per} (์์น๊ฐ ๋ฎ์์๋ก ์ ํ๊ฐ)
|
| 129 |
+
- PBR (์ฃผ๊ฐ์์์ฐ๋น์จ): {pbr} (1 ๋ฏธ๋ง์ด๋ฉด ์์ฐ๊ฐ์น ๋๋น ์ ํ๊ฐ)
|
| 130 |
|
| 131 |
+
[๋ชฉ์ฐจ]
|
| 132 |
## ๐ 3์ค ์์ฝ
|
| 133 |
+
## ๐ก ๋ฐธ๋ฅ์์ด์
ํ๊ฐ (PER, PBR ๊ธฐ์ค ๊ณ ํ๊ฐ/์ ํ๊ฐ ํ๋จ)
|
| 134 |
+
## ๐ ๋งค๋งค ์ ๋ต
|
| 135 |
"""
|
| 136 |
|
| 137 |
+
with st.expander("๐ข ์์ง๋ ๋ฐ์ดํฐ ํ์ธ"):
|
| 138 |
+
st.write(f"PER: {per}, PBR: {pbr}")
|
| 139 |
st.json(info)
|
| 140 |
|
| 141 |
status.update(label="โ
๋ถ์ ์๋ฃ!", state="complete", expanded=False)
|
|
|
|
| 144 |
col1, col2 = st.columns([1, 2])
|
| 145 |
with col1:
|
| 146 |
st.metric("ํ์ฌ ์ฃผ๊ฐ", f"{curr:,.0f}", f"{change:.2f}%")
|
| 147 |
+
st.caption(f"PER: {per} | PBR: {pbr}") # ๋ฉ์ธ ํ๋ฉด์๋ ํ์
|
| 148 |
with col2:
|
| 149 |
st.line_chart(hist['Close'], height=150)
|
| 150 |
|
| 151 |
st.subheader(f"๐ Qwen ๋ฆฌํฌํธ: {user_input}")
|
| 152 |
|
| 153 |
messages = [
|
| 154 |
+
{"role": "system", "content": "You are a financial analyst. Do NOT use code blocks."},
|
| 155 |
{"role": "user", "content": prompt}
|
| 156 |
]
|
| 157 |
|