Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import random | |
from datetime import datetime | |
# ----------------------------- | |
# ๐ง ํจ์ ์ ์ ์์ญ | |
# ----------------------------- | |
def chat_with_solar(user_question, history, api_key): | |
""" | |
Solar Pro API๋ฅผ ํธ์ถํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์ฑ๋ด ์๋ต์ ์์ฑํฉ๋๋ค. | |
""" | |
url = "https://api.upstage.ai/v1/chat/completions" | |
headers = { | |
"Authorization": f"Bearer {api_key}", | |
"Content-Type": "application/json" | |
} | |
system_prompt = """ | |
๋น์ ์ AI ์ด๋ณด ํ์ต์์ ์ง๋ฌธ์ ์ฝ๊ฒ ์ค๋ช ํด์ฃผ๋ ์น์ ํ AI์ ๋๋ค. | |
์์ฌ๊ฒฐ์ ๋๋ฌด(Decision Tree)๋ฅผ ์ค์ฌ์ผ๋ก AI ๊ฐ๋ ์ ์ดํดํ ์ ์๋๋ก ๋์์ฃผ์ธ์. | |
""" | |
messages = [{"role": "system", "content": system_prompt}] | |
for user, bot in history: | |
messages.append({"role": "user", "content": user}) | |
messages.append({"role": "assistant", "content": bot}) | |
messages.append({"role": "user", "content": user_question}) | |
payload = { | |
"model": "solar-pro", | |
"messages": messages, | |
"temperature": 0, | |
"max_tokens": 2048 | |
} | |
try: | |
response = requests.post(url, headers=headers, json=payload) | |
if response.status_code != 200: | |
return None, history, f"์ํ ์ฝ๋: {response.status_code}" | |
bot_reply = response.json()["choices"][0]["message"]["content"] | |
history.append((user_question, bot_reply)) | |
return bot_reply, history, None | |
except Exception as e: | |
return None, history, f"์์ธ ๋ฐ์: {str(e)}" | |
# ----------------------------- | |
# ๐ฅ๏ธ Streamlit UI ์์ญ | |
# ----------------------------- | |
st.set_page_config(page_title="Decision Tree Chatbot Activity", layout="wide") | |
st.title("๐ณ Decision Tree Chatbot Activity with Solar") | |
st.markdown(""" | |
์ด ์น ์ฑ์ ์ด๊ธ์๋ฅผ ์ํ ์์ฌ๊ฒฐ์ ๋๋ฌด ํ์ต ์ค์ต ๋๊ตฌ์ ๋๋ค. | |
**Upstage์ Solar Pro LLM**์ ์ฌ์ฉํ์ฌ ์ค์ ์ฑ๋ด๊ณผ ๋ํํ๋ฉฐ AI๊ฐ ์ด๋ป๊ฒ ํ๋จํ๋์ง ์ฒดํํด๋ณด์ธ์! | |
""") | |
# ์ด๊ธฐ ์ํ ์ค์ | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
# ์ฌ์ด๋๋ฐ: API ํค ์ ๋ ฅ | |
with st.sidebar: | |
st.header("๐ Solar API Key") | |
api_key = st.text_input("Enter your Upstage API key", type="password") | |
# ๋ ์ด์์ ๋ถํ | |
col1, col2 = st.columns(2) | |
# ์ข์ธก: ์ฑ๋ด ์ธํฐํ์ด์ค | |
with col1: | |
st.subheader("๐ค ์ฑ๋ด์๊ฒ ์ง๋ฌธํ๊ธฐ (with Solar Pro)") | |
user_input = st.text_input("๊ถ๊ธํ ๊ฑธ ๋ฌผ์ด๋ณด์ธ์! (์: ์์ฌ๊ฒฐ์ ๋๋ฌด๊ฐ ๋ญ์ผ?)") | |
if user_input and api_key: | |
with st.spinner("Solar์ ๋ํ ์ค..."): | |
response, st.session_state.chat_history, error = chat_with_solar(user_input, st.session_state.chat_history, api_key) | |
if error: | |
st.error(error) | |
else: | |
st.markdown(f"**๋น์ :** {user_input}") | |
st.markdown(f"**์ฑ๋ด:** {response}") | |
elif user_input and not api_key: | |
st.warning("๋จผ์ API ํค๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์!") | |
if st.session_state.chat_history: | |
with st.expander("๐ฌ ์ง๋ ๋ํ ๋ณด๊ธฐ"): | |
for user, bot in st.session_state.chat_history: | |
st.markdown(f"- **๋น์ :** {user}") | |
st.markdown(f" **์ฑ๋ด:** {bot}") | |
# ์ฐ์ธก: ํ์ต์ ๋ฉ๋ชจ์ฅ | |
with col2: | |
st.subheader("๐ ๋ฉ๋ชจ์ฅ: ๋์ ์ดํด ๊ธฐ๋ก") | |
notes = st.text_area("์ค๋ ์๊ฒ ๋ ์ ์ด๋ ๊ถ๊ธํ ์ ์ ์ ๋ฆฌํด๋ณด์ธ์:") | |
if st.button("๋๋ค ์ง๋ฌธ ์ ์๋ฐ๊ธฐ"): | |
sample_questions = [ | |
"์์ฌ๊ฒฐ์ ๋๋ฌด๊ฐ ๋ญ์ผ?", | |
"์กฐ๊ฑด์ด ๋ฐ๋๋ฉด ๊ฒฐ๊ณผ๋ ๋ฐ๋๋ ์ด์ ๋ ๋ญ์ผ?", | |
"AI๊ฐ ์ด๋ป๊ฒ ๊ฒฐ์ ์ ๋ด๋ฆฌ๋์ง ์ค๋ช ํด์ค.", | |
"๋ด๊ฐ ๋ง๋ ๊ฒฐ์ ๋๋ฌด๋ฅผ ๋ ๋๋ํ๊ฒ ๋ง๋ค๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํด?", | |
"์์ฌ๊ฒฐ์ ๋๋ฌด๋ ์ ๊ฒฝ๋ง์ ์ฐจ์ด๋ฅผ ์๋ ค์ค." | |
] | |
st.info(f"์์ ์ง๋ฌธ: '{random.choice(sample_questions)}'") | |
if notes: | |
today = datetime.now().strftime("%Y-%m-%d") | |
filename = f"{today}_studynote.txt" | |
st.download_button( | |
label="๐ฅ ๋ฉ๋ชจ ๋ค์ด๋ก๋(txt)", | |
data=notes, | |
file_name=filename, | |
mime="text/plain" | |
) | |
st.markdown("---") | |
st.markdown("Made with โค๏ธ using Solar Pro API ยท Upstage ยท Streamlit") | |