Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +23 -16
streamlit_app.py
CHANGED
|
@@ -7,7 +7,7 @@ from huggingface_hub import InferenceClient
|
|
| 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:
|
|
@@ -31,12 +31,21 @@ 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 |
# 5. ๋ฉ์ธ UI
|
| 35 |
-
user_input = st.text_input("๋ถ์ํ ์ข
๋ชฉ (์: ์ผ์ฑ์ ์, ์นด์นด์ค
|
| 36 |
|
| 37 |
if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
| 38 |
|
| 39 |
-
with st.status("๐ค AI๊ฐ ๋ฐ์ดํฐ๋ฅผ
|
| 40 |
|
| 41 |
ticker = search_ticker(user_input)
|
| 42 |
|
|
@@ -51,13 +60,12 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 51 |
start = hist['Close'].iloc[0]
|
| 52 |
change = ((curr - start) / start) * 100
|
| 53 |
|
| 54 |
-
#
|
| 55 |
prompt = f"""
|
| 56 |
[์ง์์ฌํญ]
|
| 57 |
๋๋ ์ฃผ์ ํฌ์ ์ ๋ฌธ๊ฐ์ผ. ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ ๋ณด๊ณ ์๋ฅผ ์์ฑํด.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
[์ข
๋ชฉ ๋ฐ์ดํฐ]
|
| 62 |
- ์ข
๋ชฉ: {user_input} ({ticker})
|
| 63 |
- ํ์ฌ๊ฐ: {curr:,.0f}
|
|
@@ -71,16 +79,14 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 71 |
## ๐ ์์ธ ๋ถ์
|
| 72 |
"""
|
| 73 |
|
| 74 |
-
# ๋ฐ์ดํฐ ์จ๊น
|
| 75 |
-
with st.expander("๐ข ๋ฐ์ดํฐ ์๋ณธ
|
| 76 |
st.json(info)
|
| 77 |
-
st.code(prompt)
|
| 78 |
|
| 79 |
-
status.update(label="โ
๋ถ์ ์๋ฃ!
|
| 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}%")
|
|
@@ -89,20 +95,21 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 89 |
|
| 90 |
st.subheader(f"๐ Qwen ๋ฆฌํฌํธ: {user_input}")
|
| 91 |
|
| 92 |
-
# ์์คํ
๋ฉ์์ง์๋ '์ฝ๋ ๊ธ์ง' ์ธ๋
|
| 93 |
messages = [
|
| 94 |
-
{"role": "system", "content": "You are a financial analyst.
|
| 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 |
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
else:
|
| 108 |
status.update(label="โ ๋ฐ์ดํฐ ์์", state="error")
|
|
|
|
| 7 |
st.set_page_config(page_title="Pocket Quant", page_icon="๐", layout="centered")
|
| 8 |
|
| 9 |
st.title("๐จ๐ณ Qwen ํฌ์ ์๋ฌธ๊ฐ")
|
| 10 |
+
st.caption("AI์ ๊ป์ง์ ๊ฐ์ ๋ก ๋ฒ๊ฒจ์ ๋ณด์ฌ์ค๋๋ค.")
|
| 11 |
|
| 12 |
# 2. ํ ํฐ ํ์ธ
|
| 13 |
if "HF_TOKEN" in st.secrets:
|
|
|
|
| 31 |
market = res.iloc[0]['Market']
|
| 32 |
return f"{code}.KS" if market == 'KOSPI' else f"{code}.KQ"
|
| 33 |
|
| 34 |
+
# --- [ํต์ฌ] AI๊ฐ ๋ณด๋ด๋ ์คํธ๋ฆผ์์ ์ฝ๋๋ฐ์ค(```) ๊ธฐํธ๋ง ๋ฐ๋ผ๋ด๋ ํจ์ ---
|
| 35 |
+
def parse_stream(stream):
|
| 36 |
+
for chunk in stream:
|
| 37 |
+
content = chunk.choices[0].delta.content
|
| 38 |
+
if content:
|
| 39 |
+
# ```markdown, ```, ` ๊ธฐํธ๋ฅผ ์ญ์ ํด๋ฒ๋ฆผ
|
| 40 |
+
cleaned = content.replace("```markdown", "").replace("```", "")
|
| 41 |
+
yield cleaned
|
| 42 |
+
|
| 43 |
# 5. ๋ฉ์ธ UI
|
| 44 |
+
user_input = st.text_input("๋ถ์ํ ์ข
๋ชฉ (์: ์ผ์ฑ์ ์, ์นด์นด์ค)", "์ผ์ฑ์ ์")
|
| 45 |
|
| 46 |
if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
| 47 |
|
| 48 |
+
with st.status("๐ค AI๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ ์ค์
๋๋ค...", expanded=True) as status:
|
| 49 |
|
| 50 |
ticker = search_ticker(user_input)
|
| 51 |
|
|
|
|
| 60 |
start = hist['Close'].iloc[0]
|
| 61 |
change = ((curr - start) / start) * 100
|
| 62 |
|
| 63 |
+
# ํ๋กฌํํธ: '์ฝ๋ ์ฐ์ง ๋ง๋ผ'๊ณ ํ๋ฒ ๋ ๊ฐ์กฐ
|
| 64 |
prompt = f"""
|
| 65 |
[์ง์์ฌํญ]
|
| 66 |
๋๋ ์ฃผ์ ํฌ์ ์ ๋ฌธ๊ฐ์ผ. ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ ๋ณด๊ณ ์๋ฅผ ์์ฑํด.
|
| 67 |
+
๊ฐ๋
์ฑ์ ์ํด ์ ๋ชฉ(#)๊ณผ ๊ฐ์กฐ(**)๋ฅผ ์ฌ์ฉํด.
|
| 68 |
+
|
|
|
|
| 69 |
[์ข
๋ชฉ ๋ฐ์ดํฐ]
|
| 70 |
- ์ข
๋ชฉ: {user_input} ({ticker})
|
| 71 |
- ํ์ฌ๊ฐ: {curr:,.0f}
|
|
|
|
| 79 |
## ๐ ์์ธ ๋ถ์
|
| 80 |
"""
|
| 81 |
|
| 82 |
+
# ๋ฐ์ดํฐ ์จ๊น
|
| 83 |
+
with st.expander("๐ข ๋ฐ์ดํฐ ์๋ณธ ๋ณด๊ธฐ"):
|
| 84 |
st.json(info)
|
|
|
|
| 85 |
|
| 86 |
+
status.update(label="โ
๋ถ์ ์๋ฃ!", state="complete", expanded=False)
|
| 87 |
|
| 88 |
# ๊ฒฐ๊ณผ ํ๋ฉด
|
| 89 |
st.divider()
|
|
|
|
| 90 |
col1, col2 = st.columns([1, 2])
|
| 91 |
with col1:
|
| 92 |
st.metric("ํ์ฌ ์ฃผ๊ฐ", f"{curr:,.0f}", f"{change:.2f}%")
|
|
|
|
| 95 |
|
| 96 |
st.subheader(f"๐ Qwen ๋ฆฌํฌํธ: {user_input}")
|
| 97 |
|
|
|
|
| 98 |
messages = [
|
| 99 |
+
{"role": "system", "content": "You are a financial analyst. Write in plain markdown. Do NOT use code blocks."},
|
| 100 |
{"role": "user", "content": prompt}
|
| 101 |
]
|
| 102 |
|
| 103 |
+
# AI ํธ์ถ
|
| 104 |
stream = client.chat.completions.create(
|
| 105 |
model="Qwen/Qwen2.5-72B-Instruct",
|
| 106 |
messages=messages,
|
| 107 |
+
max_tokens=1500,
|
| 108 |
stream=True
|
| 109 |
)
|
| 110 |
|
| 111 |
+
# [์ฌ๊ธฐ!] ์ฐ๋ฆฌ๊ฐ ๋ง๋ ํํฐ ํจ์(parse_stream)๋ฅผ ํต๊ณผ์์ผ์ ์ถ๋ ฅ
|
| 112 |
+
st.write_stream(parse_stream(stream))
|
| 113 |
|
| 114 |
else:
|
| 115 |
status.update(label="โ ๋ฐ์ดํฐ ์์", state="error")
|