Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +11 -12
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("AI์ ๊ป์ง์ ๊ฐ์ ๋ก ๋ฒ๊ฒจ์ ๋ณด์ฌ์ค๋๋ค.")
|
| 11 |
|
| 12 |
# 2. ํ ํฐ ํ์ธ
|
| 13 |
if "HF_TOKEN" in st.secrets:
|
|
@@ -31,14 +31,18 @@ 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 |
-
|
| 38 |
-
if
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# 5. ๋ฉ์ธ UI
|
| 44 |
user_input = st.text_input("๋ถ์ํ ์ข
๋ชฉ (์: ์ผ์ฑ์ ์, ์นด์นด์ค)", "์ผ์ฑ์ ์")
|
|
@@ -60,7 +64,6 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 60 |
start = hist['Close'].iloc[0]
|
| 61 |
change = ((curr - start) / start) * 100
|
| 62 |
|
| 63 |
-
# ํ๋กฌํํธ: '์ฝ๋ ์ฐ์ง ๋ง๋ผ'๊ณ ํ๋ฒ ๋ ๊ฐ์กฐ
|
| 64 |
prompt = f"""
|
| 65 |
[์ง์์ฌํญ]
|
| 66 |
๋๋ ์ฃผ์ ํฌ์ ์ ๋ฌธ๊ฐ์ผ. ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ ๋ณด๊ณ ์๋ฅผ ์์ฑํด.
|
|
@@ -79,13 +82,11 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 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:
|
|
@@ -100,7 +101,6 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 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,
|
|
@@ -108,7 +108,6 @@ if st.button("๐ AI ๋ถ์ ์์", use_container_width=True):
|
|
| 108 |
stream=True
|
| 109 |
)
|
| 110 |
|
| 111 |
-
# [์ฌ๊ธฐ!] ์ฐ๋ฆฌ๊ฐ ๋ง๋ ํํฐ ํจ์(parse_stream)๋ฅผ ํต๊ณผ์์ผ์ ์ถ๋ ฅ
|
| 112 |
st.write_stream(parse_stream(stream))
|
| 113 |
|
| 114 |
else:
|
|
|
|
| 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 |
+
# --- [์์ ๋จ] ์์ ์ฅ์น๊ฐ ์ถ๊ฐ๋ ํํฐ ํจ์ ---
|
| 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 |
+
content = delta.content
|
| 43 |
+
# ๋งํฌ๋ค์ด ์ฝ๋ ๊ธฐํธ ์ญ์
|
| 44 |
+
cleaned = content.replace("```markdown", "").replace("```", "")
|
| 45 |
+
yield cleaned
|
| 46 |
|
| 47 |
# 5. ๋ฉ์ธ UI
|
| 48 |
user_input = st.text_input("๋ถ์ํ ์ข
๋ชฉ (์: ์ผ์ฑ์ ์, ์นด์นด์ค)", "์ผ์ฑ์ ์")
|
|
|
|
| 64 |
start = hist['Close'].iloc[0]
|
| 65 |
change = ((curr - start) / start) * 100
|
| 66 |
|
|
|
|
| 67 |
prompt = f"""
|
| 68 |
[์ง์์ฌํญ]
|
| 69 |
๋๋ ์ฃผ์ ํฌ์ ์ ๋ฌธ๊ฐ์ผ. ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ณ ๋ณด๊ณ ์๋ฅผ ์์ฑํด.
|
|
|
|
| 82 |
## ๐ ์์ธ ๋ถ์
|
| 83 |
"""
|
| 84 |
|
|
|
|
| 85 |
with st.expander("๐ข ๋ฐ์ดํฐ ์๋ณธ ๋ณด๊ธฐ"):
|
| 86 |
st.json(info)
|
| 87 |
|
| 88 |
status.update(label="โ
๋ถ์ ์๋ฃ!", state="complete", expanded=False)
|
| 89 |
|
|
|
|
| 90 |
st.divider()
|
| 91 |
col1, col2 = st.columns([1, 2])
|
| 92 |
with col1:
|
|
|
|
| 101 |
{"role": "user", "content": prompt}
|
| 102 |
]
|
| 103 |
|
|
|
|
| 104 |
stream = client.chat.completions.create(
|
| 105 |
model="Qwen/Qwen2.5-72B-Instruct",
|
| 106 |
messages=messages,
|
|
|
|
| 108 |
stream=True
|
| 109 |
)
|
| 110 |
|
|
|
|
| 111 |
st.write_stream(parse_stream(stream))
|
| 112 |
|
| 113 |
else:
|