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")