Spaces:
Sleeping
Sleeping
File size: 4,442 Bytes
c02ed13 75b58db c02ed13 75b58db c02ed13 75b58db d7f664e 75b58db 8c10710 75b58db c02ed13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
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")
|