Spaces:
Sleeping
Sleeping
Create app.py
Browse filesapp.py file for solar chat with your very own system prompt!
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from io import StringIO
|
4 |
+
from datetime import datetime
|
5 |
+
import requests
|
6 |
+
import sseclient # for streaming response
|
7 |
+
|
8 |
+
st.set_page_config(page_title="Solar Chat", page_icon="๐")
|
9 |
+
st.title("๐ Chat with your own System Prompt")
|
10 |
+
|
11 |
+
# --- User Input ---
|
12 |
+
api_key = st.text_input("๐ Upstage API Key (starts with 'up_')", type="password")
|
13 |
+
system_prompt = st.text_area("๐ง System Prompt", "You are a helpful assistant.", height=100)
|
14 |
+
user_input = st.text_input("๐ฌ Your Message", "")
|
15 |
+
|
16 |
+
# --- Session state Reset ---
|
17 |
+
if "messages" not in st.session_state or st.session_state.system_prompt != system_prompt:
|
18 |
+
st.session_state.messages = [{"role": "system", "content": system_prompt}]
|
19 |
+
st.session_state.system_prompt = system_prompt
|
20 |
+
|
21 |
+
# --- Solar Pro API call function ---
|
22 |
+
def solar_pro_chat(messages, api_key):
|
23 |
+
url = "https://api.upstage.ai/v1"
|
24 |
+
headers = {
|
25 |
+
"Authorization": f"Bearer {api_key}",
|
26 |
+
"Accept": "text/event-stream",
|
27 |
+
"Content-Type": "application/json"
|
28 |
+
}
|
29 |
+
payload = {
|
30 |
+
"model": "solar-pro",
|
31 |
+
"messages": messages,
|
32 |
+
"stream": True
|
33 |
+
}
|
34 |
+
|
35 |
+
# SSE streaming
|
36 |
+
response = requests.post(url, headers=headers, json=payload, stream=True)
|
37 |
+
client = sseclient.SSEClient(response)
|
38 |
+
full_reply = ""
|
39 |
+
for event in client.events():
|
40 |
+
if event.data == "[DONE]":
|
41 |
+
break
|
42 |
+
try:
|
43 |
+
content = eval(event.data)["choices"][0]["delta"].get("content", "")
|
44 |
+
full_reply += content
|
45 |
+
yield content
|
46 |
+
except Exception:
|
47 |
+
continue
|
48 |
+
return full_reply
|
49 |
+
|
50 |
+
# --- Send Messages ---
|
51 |
+
if st.button("Send") and api_key and user_input:
|
52 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
53 |
+
with st.spinner("Generating response..."):
|
54 |
+
response_text = ""
|
55 |
+
response_area = st.empty()
|
56 |
+
for chunk in solar_pro_chat(st.session_state.messages, api_key):
|
57 |
+
response_text += chunk
|
58 |
+
response_area.markdown(f"**๐ค Bot:** {response_text}")
|
59 |
+
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
60 |
+
|
61 |
+
# --- Chat history in bubbles ---
|
62 |
+
def render_message(role, content):
|
63 |
+
if role == "user":
|
64 |
+
st.markdown(f"""
|
65 |
+
<div style='background-color:#DCF8C6; padding:10px 15px; border-radius:10px; margin:5px 0; text-align:right;'>
|
66 |
+
<b>You:</b> {content}
|
67 |
+
</div>
|
68 |
+
""", unsafe_allow_html=True)
|
69 |
+
elif role == "assistant":
|
70 |
+
st.markdown(f"""
|
71 |
+
<div style='background-color:#F1F0F0; padding:10px 15px; border-radius:10px; margin:5px 0; text-align:left;'>
|
72 |
+
<b>Bot:</b> {content}
|
73 |
+
</div>
|
74 |
+
""", unsafe_allow_html=True)
|
75 |
+
|
76 |
+
st.markdown("### ๐๏ธ Chat History")
|
77 |
+
for msg in st.session_state.messages:
|
78 |
+
if msg["role"] != "system":
|
79 |
+
render_message(msg["role"], msg["content"])
|
80 |
+
|
81 |
+
# --- CSV Download Function ---
|
82 |
+
def generate_csv():
|
83 |
+
rows = []
|
84 |
+
rows.append({"role": "system", "content": st.session_state.system_prompt})
|
85 |
+
for msg in st.session_state.messages:
|
86 |
+
if msg["role"] != "system":
|
87 |
+
rows.append(msg)
|
88 |
+
df = pd.DataFrame(rows)
|
89 |
+
output = StringIO()
|
90 |
+
df.to_csv(output, index=False)
|
91 |
+
return output.getvalue()
|
92 |
+
|
93 |
+
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
94 |
+
filename = f"solar_chat_{now}.csv"
|
95 |
+
|
96 |
+
st.download_button(
|
97 |
+
label="โฌ๏ธ Download Chat History as CSV",
|
98 |
+
data=generate_csv(),
|
99 |
+
file_name=filename,
|
100 |
+
mime="text/csv",
|
101 |
+
)
|