Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import random
|
4 |
+
|
5 |
+
# -----------------------------
|
6 |
+
# ๐ง ํจ์ ์ ์ ์์ญ
|
7 |
+
# -----------------------------
|
8 |
+
|
9 |
+
def chat_with_solar(user_question, history, api_key):
|
10 |
+
"""
|
11 |
+
Solar Pro API๋ฅผ ํธ์ถํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์ฑ๋ด ์๋ต์ ์์ฑํฉ๋๋ค.
|
12 |
+
"""
|
13 |
+
url = "https://api.upstage.ai/v1/chat/completions"
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {api_key}",
|
16 |
+
"Content-Type": "application/json"
|
17 |
+
}
|
18 |
+
|
19 |
+
system_prompt = """
|
20 |
+
๋น์ ์ AI ์ด๋ณด ํ์ต์์ ์ง๋ฌธ์ ์ฝ๊ฒ ์ค๋ช
ํด์ฃผ๋ ์น์ ํ AI์
๋๋ค.
|
21 |
+
์์ฌ๊ฒฐ์ ๋๋ฌด(Decision Tree)๋ฅผ ์ค์ฌ์ผ๋ก AI ๊ฐ๋
์ ์ดํดํ ์ ์๋๋ก ๋์์ฃผ์ธ์.
|
22 |
+
"""
|
23 |
+
|
24 |
+
messages = [{"role": "system", "content": system_prompt}]
|
25 |
+
for user, bot in history:
|
26 |
+
messages.append({"role": "user", "content": user})
|
27 |
+
messages.append({"role": "assistant", "content": bot})
|
28 |
+
messages.append({"role": "user", "content": user_question})
|
29 |
+
|
30 |
+
payload = {
|
31 |
+
"model": "solar-pro",
|
32 |
+
"messages": messages,
|
33 |
+
"temperature": 0,
|
34 |
+
"max_tokens": 2048
|
35 |
+
}
|
36 |
+
|
37 |
+
try:
|
38 |
+
response = requests.post(url, headers=headers, json=payload)
|
39 |
+
if response.status_code != 200:
|
40 |
+
return None, history, f"์ํ ์ฝ๋: {response.status_code}"
|
41 |
+
bot_reply = response.json()["choices"][0]["message"]["content"]
|
42 |
+
history.append((user_question, bot_reply))
|
43 |
+
return bot_reply, history, None
|
44 |
+
except Exception as e:
|
45 |
+
return None, history, f"์์ธ ๋ฐ์: {str(e)}"
|
46 |
+
|
47 |
+
# -----------------------------
|
48 |
+
# ๐ฅ๏ธ Streamlit UI ์์ญ
|
49 |
+
# -----------------------------
|
50 |
+
|
51 |
+
st.set_page_config(page_title="Decision Tree Chatbot Activity", layout="wide")
|
52 |
+
|
53 |
+
st.title("๐ณ Decision Tree Chatbot Activity with Solar")
|
54 |
+
|
55 |
+
st.markdown("""
|
56 |
+
์ด ์น ์ฑ์ ์ด๊ธ์๋ฅผ ์ํ ์์ฌ๊ฒฐ์ ๋๋ฌด ํ์ต ์ค์ต ๋๊ตฌ์
๋๋ค.
|
57 |
+
**Upstage์ Solar Pro LLM**์ ์ฌ์ฉํ์ฌ ์ค์ ์ฑ๋ด๊ณผ ๋ํํ๋ฉฐ AI๊ฐ ์ด๋ป๊ฒ ํ๋จํ๋์ง ์ฒดํํด๋ณด์ธ์!
|
58 |
+
""")
|
59 |
+
|
60 |
+
# ์ด๊ธฐ ์ํ ์ค์
|
61 |
+
if "chat_history" not in st.session_state:
|
62 |
+
st.session_state.chat_history = []
|
63 |
+
|
64 |
+
# ์ฌ์ด๋๋ฐ: API ํค ์
๋ ฅ
|
65 |
+
with st.sidebar:
|
66 |
+
st.header("๐ Solar API Key")
|
67 |
+
api_key = st.text_input("Enter your Upstage API key", type="password")
|
68 |
+
|
69 |
+
# ๋ ์ด์์ ๋ถํ
|
70 |
+
col1, col2 = st.columns(2)
|
71 |
+
|
72 |
+
# ์ข์ธก: ์ฑ๋ด ์ธํฐํ์ด์ค
|
73 |
+
with col1:
|
74 |
+
st.subheader("๐ค ์ฑ๋ด์๊ฒ ์ง๋ฌธํ๊ธฐ (with Solar Pro)")
|
75 |
+
user_input = st.text_input("๊ถ๊ธํ ๊ฑธ ๋ฌผ์ด๋ณด์ธ์! (์: ์์ฌ๊ฒฐ์ ๋๋ฌด๊ฐ ๋ญ์ผ?)")
|
76 |
+
|
77 |
+
if user_input and api_key:
|
78 |
+
with st.spinner("Solar์ ๋ํ ์ค..."):
|
79 |
+
response, st.session_state.chat_history, error = chat_with_solar(user_input, st.session_state.chat_history, api_key)
|
80 |
+
if error:
|
81 |
+
st.error(error)
|
82 |
+
else:
|
83 |
+
st.markdown(f"**๋น์ :** {user_input}")
|
84 |
+
st.markdown(f"**์ฑ๋ด:** {response}")
|
85 |
+
|
86 |
+
elif user_input and not api_key:
|
87 |
+
st.warning("๋จผ์ API ํค๋ฅผ ์
๋ ฅํด์ฃผ์ธ์!")
|
88 |
+
|
89 |
+
if st.session_state.chat_history:
|
90 |
+
with st.expander("๐ฌ ์ง๋ ๋ํ ๋ณด๊ธฐ"):
|
91 |
+
for user, bot in st.session_state.chat_history:
|
92 |
+
st.markdown(f"- **๋น์ :** {user}")
|
93 |
+
st.markdown(f" **์ฑ๋ด:** {bot}")
|
94 |
+
|
95 |
+
# ์ฐ์ธก: ํ์ต์ ๋ฉ๋ชจ์ฅ
|
96 |
+
with col2:
|
97 |
+
st.subheader("๐ ๋ฉ๋ชจ์ฅ: ๋์ ์ดํด ๊ธฐ๋ก")
|
98 |
+
notes = st.text_area("์ค๋ ์๊ฒ ๋ ์ ์ด๋ ๊ถ๊ธํ ์ ์ ์ ๋ฆฌํด๋ณด์ธ์:")
|
99 |
+
if st.button("๋๋ค ์ง๋ฌธ ์ ์๋ฐ๊ธฐ"):
|
100 |
+
sample_questions = [
|
101 |
+
"์์ฌ๊ฒฐ์ ๋๋ฌด๊ฐ ๋ญ์ผ?",
|
102 |
+
"์กฐ๊ฑด์ด ๋ฐ๋๋ฉด ๊ฒฐ๊ณผ๋ ๋ฐ๋๋ ์ด์ ๋ ๋ญ์ผ?",
|
103 |
+
"AI๊ฐ ์ด๋ป๊ฒ ๊ฒฐ์ ์ ๋ด๋ฆฌ๋์ง ์ค๋ช
ํด์ค.",
|
104 |
+
"๋ด๊ฐ ๋ง๋ ๊ฒฐ์ ๋๋ฌด๋ฅผ ๋ ๋๋ํ๊ฒ ๋ง๋ค๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํด?",
|
105 |
+
"์์ฌ๊ฒฐ์ ๋๋ฌด๋ ์ ๊ฒฝ๋ง์ ์ฐจ์ด๋ฅผ ์๋ ค์ค."
|
106 |
+
]
|
107 |
+
st.info(f"์์ ์ง๋ฌธ: '{random.choice(sample_questions)}'")
|
108 |
+
|
109 |
+
st.markdown("---")
|
110 |
+
st.markdown("Made with โค๏ธ using Solar Pro API ยท Upstage ยท Streamlit")
|