Yescia commited on
Commit
6b3a25b
Β·
verified Β·
1 Parent(s): 4720161

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -14,19 +14,27 @@ api_key = st.text_input("πŸ”‘ Upstage API Key (starts with 'up_')", type="passwo
14
  user_input = st.text_input("πŸ’¬ Your Message", "")
15
 
16
  st.markdown("### 🧠 Custom System Prompt")
17
- custom_prompt = st.text_area("μ‚¬μš©μž μ •μ˜ μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ", "You are a helpful assistant.", height=100)
 
 
 
 
 
 
18
 
19
- # --- Default prompt μ„€μ • ---
 
 
20
  default_prompt = "You are a helpful assistant."
21
 
22
- # --- μ„Έμ…˜ μ΄ˆκΈ°ν™” ---
23
  if "default_messages" not in st.session_state:
24
  st.session_state.default_messages = [{"role": "system", "content": default_prompt}]
25
  if "custom_messages" not in st.session_state or st.session_state.custom_prompt != custom_prompt:
26
  st.session_state.custom_messages = [{"role": "system", "content": custom_prompt}]
27
  st.session_state.custom_prompt = custom_prompt
28
 
29
- # --- Solar Pro API 호좜 ν•¨μˆ˜ ---
30
  def solar_pro_chat(messages, api_key):
31
  url = "https://api.upstage.ai/v1/chat/completions"
32
  headers = {
@@ -51,15 +59,15 @@ def solar_pro_chat(messages, api_key):
51
  content = json.loads(event.data)["choices"][0]["delta"].get("content", "")
52
  yield content
53
  except Exception as e:
54
- st.error("⚠️ 응닡 처리 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.")
55
- print(f"[SSE νŒŒμ‹± 였λ₯˜] {e}")
56
  continue
57
  except requests.exceptions.RequestException as e:
58
- st.error("❌ API 호좜 μ‹€νŒ¨: API ν‚€ λ˜λŠ” λ„€νŠΈμ›Œν¬ μƒνƒœ 확인")
59
- print(f"[API μš”μ²­ 였λ₯˜] {e}")
60
  return
61
 
62
- # --- λΉ„κ΅μš© 응닡 처리 ---
63
  if st.button("πŸš€ Compare Responses") and api_key and user_input:
64
  st.session_state.default_messages.append({"role": "user", "content": user_input})
65
  st.session_state.custom_messages.append({"role": "user", "content": user_input})
@@ -70,7 +78,7 @@ if st.button("πŸš€ Compare Responses") and api_key and user_input:
70
  st.subheader("πŸ”Ή Default Prompt")
71
  default_response = ""
72
  default_area = st.empty()
73
- with st.spinner("Default 응닡 생성 쀑..."):
74
  for chunk in solar_pro_chat(st.session_state.default_messages, api_key):
75
  default_response += chunk
76
  default_area.markdown(f"**πŸ€– Bot:** {default_response}")
@@ -80,13 +88,13 @@ if st.button("πŸš€ Compare Responses") and api_key and user_input:
80
  st.subheader("πŸ”Έ Custom Prompt")
81
  custom_response = ""
82
  custom_area = st.empty()
83
- with st.spinner("Custom 응닡 생성 쀑..."):
84
  for chunk in solar_pro_chat(st.session_state.custom_messages, api_key):
85
  custom_response += chunk
86
  custom_area.markdown(f"**πŸ€– Bot:** {custom_response}")
87
  st.session_state.custom_messages.append({"role": "assistant", "content": custom_response})
88
 
89
- # --- μ±„νŒ… νžˆμŠ€ν† λ¦¬ λ‹€μš΄λ‘œλ“œ ---
90
  def generate_csv(messages, prompt_label):
91
  rows = [{"role": "system", "content": prompt_label}]
92
  for msg in messages:
 
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 = {
 
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})
 
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}")
 
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: