Yescia commited on
Commit
c02ed13
ยท
verified ยท
1 Parent(s): d9a522e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
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")