Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 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
|
| 56 |
continue
|
| 57 |
except requests.exceptions.RequestException as e:
|
| 58 |
-
st.error("β API
|
| 59 |
-
print(f"[API
|
| 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("
|
| 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("
|
| 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:
|