wanda222 commited on
Commit
1e5561b
Β·
verified Β·
1 Parent(s): 6b3a25b

Update app.py

Browse files

translate it to Korean

Files changed (1) hide show
  1. app.py +60 -33
app.py CHANGED
@@ -6,36 +6,47 @@ from datetime import datetime
6
  import requests
7
  import sseclient
8
 
9
- st.set_page_config(page_title="Solar Prompt Comparator", page_icon="🌞")
10
- st.title("🌞 System Prompt Comparison Chat")
 
11
 
12
- # --- Input UI ---
13
- api_key = st.text_input("πŸ”‘ Upstage API Key (starts with 'up_')", type="password")
14
- user_input = st.text_input("πŸ’¬ Your Message", "")
15
 
16
- st.markdown("### 🧠 Custom System Prompt")
 
17
  st.markdown("""
18
- **What is a System Prompt?**
19
- A system prompt is a special instruction given to the AI before the conversation begins.
20
- It tells the AI how it should behave β€” like setting the tone, role, or personality.
21
- For example, you can ask it to be a polite assistant, a creative writer, or a strict grammar checker.
22
- Changing the system prompt can lead to very different responses, even if the user's question stays the same.
23
  """)
24
 
25
- custom_prompt = st.text_area("✏️ Write your own system prompt below:", "You are a helpful assistant.", height=100)
26
-
27
- # --- Default prompt setting ---
28
  default_prompt = "You are a helpful assistant."
29
 
30
- # --- Session Reset ---
31
  if "default_messages" not in st.session_state:
32
  st.session_state.default_messages = [{"role": "system", "content": default_prompt}]
33
  if "custom_messages" not in st.session_state or st.session_state.custom_prompt != custom_prompt:
34
  st.session_state.custom_messages = [{"role": "system", "content": custom_prompt}]
35
  st.session_state.custom_prompt = custom_prompt
36
 
37
- # --- Solar Pro API call ---
38
  def solar_pro_chat(messages, api_key):
 
 
 
 
 
 
 
 
 
 
39
  url = "https://api.upstage.ai/v1/chat/completions"
40
  headers = {
41
  "Authorization": f"Bearer {api_key}",
@@ -59,43 +70,57 @@ def solar_pro_chat(messages, api_key):
59
  content = json.loads(event.data)["choices"][0]["delta"].get("content", "")
60
  yield content
61
  except Exception as e:
62
- st.error("⚠️ An error occurred while processing the response.")
63
- print(f"[SSE parsing error] {e}")
64
  continue
65
  except requests.exceptions.RequestException as e:
66
- st.error("❌ API call failed: Check your API key or network status")
67
- print(f"[API request error] {e}")
68
  return
69
 
70
- # --- Response processing for comparison ---
71
- if st.button("πŸš€ Compare Responses") and api_key and user_input:
 
72
  st.session_state.default_messages.append({"role": "user", "content": user_input})
73
  st.session_state.custom_messages.append({"role": "user", "content": user_input})
74
 
 
75
  col1, col2 = st.columns(2)
76
 
 
77
  with col1:
78
- st.subheader("πŸ”Ή Default Prompt")
79
  default_response = ""
80
  default_area = st.empty()
81
- with st.spinner("Generating default response..."):
82
  for chunk in solar_pro_chat(st.session_state.default_messages, api_key):
83
  default_response += chunk
84
- default_area.markdown(f"**πŸ€– Bot:** {default_response}")
85
  st.session_state.default_messages.append({"role": "assistant", "content": default_response})
86
 
 
87
  with col2:
88
- st.subheader("πŸ”Έ Custom Prompt")
89
  custom_response = ""
90
  custom_area = st.empty()
91
- with st.spinner("Generating custom response..."):
92
  for chunk in solar_pro_chat(st.session_state.custom_messages, api_key):
93
  custom_response += chunk
94
- custom_area.markdown(f"**πŸ€– Bot:** {custom_response}")
95
  st.session_state.custom_messages.append({"role": "assistant", "content": custom_response})
96
 
97
- # --- Download chat history ---
98
  def generate_csv(messages, prompt_label):
 
 
 
 
 
 
 
 
 
 
99
  rows = [{"role": "system", "content": prompt_label}]
100
  for msg in messages:
101
  if msg["role"] != "system":
@@ -105,21 +130,23 @@ def generate_csv(messages, prompt_label):
105
  df.to_csv(output, index=False)
106
  return output.getvalue()
107
 
 
108
  now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
109
- st.markdown("### ⬇️ Download Chat Histories")
 
 
110
  col1, col2 = st.columns(2)
111
  with col1:
112
  st.download_button(
113
- label="Download Default Chat",
114
  data=generate_csv(st.session_state.default_messages, default_prompt),
115
  file_name=f"default_chat_{now}.csv",
116
  mime="text/csv",
117
  )
118
  with col2:
119
  st.download_button(
120
- label="Download Custom Chat",
121
  data=generate_csv(st.session_state.custom_messages, custom_prompt),
122
  file_name=f"custom_chat_{now}.csv",
123
  mime="text/csv",
124
  )
125
-
 
6
  import requests
7
  import sseclient
8
 
9
+ # νŽ˜μ΄μ§€ μ„€μ •
10
+ st.set_page_config(page_title="Solar ν”„λ‘¬ν”„νŠΈ 비ꡐ기", page_icon="🌞")
11
+ st.title("🌞 μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ 비ꡐ 챗봇")
12
 
13
+ # --- μž…λ ₯ UI ---
14
+ api_key = st.text_input("πŸ”‘ Upstage API Keyλ₯Ό μž…λ ₯ν•˜μ„Έμš” ('up_'둜 μ‹œμž‘)", type="password")
15
+ user_input = st.text_input("πŸ’¬ μ‚¬μš©μž λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”", "")
16
 
17
+ # --- μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ μ•ˆλ‚΄ 및 μž…λ ₯ ---
18
+ st.markdown("### 🧠 μ»€μŠ€ν…€ μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ")
19
  st.markdown("""
20
+ **μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈλž€ λ¬΄μ—‡μΈκ°€μš”?**
21
+ μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈλŠ” AIμ—κ²Œ λŒ€ν™”λ₯Ό μ‹œμž‘ν•˜κΈ° 전에 μ£Όμ–΄μ§€λŠ” νŠΉλ³„ν•œ μ§€μ‹œλ¬Έμž…λ‹ˆλ‹€.
22
+ AIκ°€ μ–΄λ–€ μ—­ν• μ΄λ‚˜ 말투, μ„±κ²©μœΌλ‘œ λŒ€ν™”μ— μž„ν•΄μ•Ό ν•˜λŠ”μ§€λ₯Ό μ•Œλ €μ£ΌλŠ” 역할을 ν•©λ‹ˆλ‹€.
23
+ 예λ₯Ό λ“€μ–΄, AIμ—κ²Œ 예의 λ°”λ₯Έ λΉ„μ„œμ²˜λŸΌ ν–‰λ™ν•˜κ²Œ ν•˜κ±°λ‚˜, 창의적인 μž‘κ°€, μ—„κ²©ν•œ 문법 κ²€μ‚¬μžμ²˜λŸΌ ν–‰λ™ν•˜κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
24
+ 같은 μ§ˆλ¬Έμ΄λΌλ„ μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈμ— 따라 μ™„μ „νžˆ λ‹€λ₯Έ 응닡이 λ‚˜μ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.
25
  """)
26
 
27
+ # κΈ°λ³Έ μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈμ™€ μ‚¬μš©μž μ»€μŠ€ν…€ ν”„λ‘¬ν”„νŠΈ μž…λ ₯
28
+ custom_prompt = st.text_area("✏️ μ•„λž˜μ— μ›ν•˜λŠ” μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈλ₯Ό μž…λ ₯ν•˜μ„Έμš”:", "You are a helpful assistant.", height=100)
 
29
  default_prompt = "You are a helpful assistant."
30
 
31
+ # --- μ„Έμ…˜ μƒνƒœ μ΄ˆκΈ°ν™” ---
32
  if "default_messages" not in st.session_state:
33
  st.session_state.default_messages = [{"role": "system", "content": default_prompt}]
34
  if "custom_messages" not in st.session_state or st.session_state.custom_prompt != custom_prompt:
35
  st.session_state.custom_messages = [{"role": "system", "content": custom_prompt}]
36
  st.session_state.custom_prompt = custom_prompt
37
 
38
+ # --- Solar Pro API 호좜 ν•¨μˆ˜ ---
39
  def solar_pro_chat(messages, api_key):
40
+ """
41
+ Solar Pro API에 λ©”μ‹œμ§€λ₯Ό 보내고 슀트리밍 ν˜•νƒœλ‘œ 응닡을 λ°›μ•„μ˜€λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
42
+
43
+ Parameters:
44
+ messages (list): μ‹œμŠ€ν…œ/μ‚¬μš©μž λ©”μ‹œμ§€ λͺ©λ‘
45
+ api_key (str): Upstage API ν‚€
46
+
47
+ Returns:
48
+ generator: AI의 응닡을 슀트리밍 ν˜•νƒœλ‘œ ν•œ 쀄씩 λ°˜ν™˜
49
+ """
50
  url = "https://api.upstage.ai/v1/chat/completions"
51
  headers = {
52
  "Authorization": f"Bearer {api_key}",
 
70
  content = json.loads(event.data)["choices"][0]["delta"].get("content", "")
71
  yield content
72
  except Exception as e:
73
+ st.error("⚠️ 응닡 처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.")
74
+ print(f"[SSE νŒŒμ‹± 였λ₯˜] {e}")
75
  continue
76
  except requests.exceptions.RequestException as e:
77
+ st.error("❌ API 호좜 μ‹€νŒ¨: API ν‚€λ‚˜ λ„€νŠΈμ›Œν¬ μƒνƒœλ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”.")
78
+ print(f"[API μš”μ²­ 였λ₯˜] {e}")
79
  return
80
 
81
+ # --- 비ꡐ μ‹€ν–‰ ---
82
+ if st.button("πŸš€ 응닡 λΉ„κ΅ν•˜κΈ°") and api_key and user_input:
83
+ # μ‚¬μš©μž μž…λ ₯을 λ©”μ‹œμ§€μ— μΆ”κ°€
84
  st.session_state.default_messages.append({"role": "user", "content": user_input})
85
  st.session_state.custom_messages.append({"role": "user", "content": user_input})
86
 
87
+ # 두 개의 컬럼으둜 κ²°κ³Ό λ‚˜λˆ„κΈ°
88
  col1, col2 = st.columns(2)
89
 
90
+ # κΈ°λ³Έ ν”„λ‘¬ν”„νŠΈ 응닡
91
  with col1:
92
+ st.subheader("πŸ”Ή κΈ°λ³Έ ν”„λ‘¬ν”„νŠΈ")
93
  default_response = ""
94
  default_area = st.empty()
95
+ with st.spinner("κΈ°λ³Έ 응닡 생성 쀑..."):
96
  for chunk in solar_pro_chat(st.session_state.default_messages, api_key):
97
  default_response += chunk
98
+ default_area.markdown(f"**πŸ€– 봇:** {default_response}")
99
  st.session_state.default_messages.append({"role": "assistant", "content": default_response})
100
 
101
+ # μ»€μŠ€ν…€ ν”„λ‘¬ν”„νŠΈ 응닡
102
  with col2:
103
+ st.subheader("πŸ”Έ μ»€μŠ€ν…€ ν”„λ‘¬ν”„νŠΈ")
104
  custom_response = ""
105
  custom_area = st.empty()
106
+ with st.spinner("μ»€μŠ€ν…€ 응닡 생성 쀑..."):
107
  for chunk in solar_pro_chat(st.session_state.custom_messages, api_key):
108
  custom_response += chunk
109
+ custom_area.markdown(f"**πŸ€– 봇:** {custom_response}")
110
  st.session_state.custom_messages.append({"role": "assistant", "content": custom_response})
111
 
112
+ # --- μ±„νŒ… λ‚΄μ—­ CSV둜 μ €μž₯ ---
113
  def generate_csv(messages, prompt_label):
114
+ """
115
+ μ£Όμ–΄μ§„ λ©”μ‹œμ§€λ₯Ό CSV둜 λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
116
+
117
+ Parameters:
118
+ messages (list): μ‹œμŠ€ν…œ/μ‚¬μš©μž/AI λ©”μ‹œμ§€ 리슀트
119
+ prompt_label (str): μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ ν…μŠ€νŠΈ
120
+
121
+ Returns:
122
+ str: CSV ν˜•μ‹μ˜ λ¬Έμžμ—΄
123
+ """
124
  rows = [{"role": "system", "content": prompt_label}]
125
  for msg in messages:
126
  if msg["role"] != "system":
 
130
  df.to_csv(output, index=False)
131
  return output.getvalue()
132
 
133
+ # ν˜„μž¬ μ‹œκ°μœΌλ‘œ 파일λͺ… 생성
134
  now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
135
+
136
+ # --- λ‹€μš΄λ‘œλ“œ UI ---
137
+ st.markdown("### ⬇️ μ±„νŒ… λ‚΄μ—­ λ‹€μš΄λ‘œλ“œ")
138
  col1, col2 = st.columns(2)
139
  with col1:
140
  st.download_button(
141
+ label="κΈ°λ³Έ 응닡 λ‹€μš΄λ‘œλ“œ",
142
  data=generate_csv(st.session_state.default_messages, default_prompt),
143
  file_name=f"default_chat_{now}.csv",
144
  mime="text/csv",
145
  )
146
  with col2:
147
  st.download_button(
148
+ label="μ»€μŠ€ν…€ 응닡 λ‹€μš΄λ‘œλ“œ",
149
  data=generate_csv(st.session_state.custom_messages, custom_prompt),
150
  file_name=f"custom_chat_{now}.csv",
151
  mime="text/csv",
152
  )