Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import datetime
|
|
7 |
import json
|
8 |
|
9 |
_ = load_dotenv(find_dotenv())
|
10 |
-
st.set_page_config(page_icon="", layout="wide", page_title="...")
|
11 |
|
12 |
def icon(emoji: str):
|
13 |
"""Shows an emoji as a Notion-style page icon."""
|
@@ -28,10 +28,9 @@ client = Groq(
|
|
28 |
# Initialize chat history and selected model
|
29 |
if "messages" not in st.session_state:
|
30 |
st.session_state.messages = []
|
|
|
31 |
if "selected_model" not in st.session_state:
|
32 |
st.session_state.selected_model = None
|
33 |
-
if "prompts" not in st.session_state:
|
34 |
-
st.session_state.prompts = {}
|
35 |
|
36 |
# Define model details
|
37 |
models = {
|
@@ -78,7 +77,7 @@ with col2:
|
|
78 |
|
79 |
# Display chat messages from history on app rerun
|
80 |
for message in st.session_state.messages:
|
81 |
-
avatar = "" if message["role"] == "assistant" else ""
|
82 |
with st.chat_message(message["role"], avatar=avatar):
|
83 |
st.markdown(message["content"])
|
84 |
|
@@ -88,32 +87,10 @@ def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
|
88 |
if chunk.choices[0].delta.content:
|
89 |
yield chunk.choices[0].delta.content
|
90 |
|
91 |
-
|
92 |
-
save_prompt_button = st.button("Save Prompt")
|
93 |
-
if save_prompt_button:
|
94 |
-
prompt_name = st.text_input("Enter a name for the prompt:")
|
95 |
-
if prompt_name:
|
96 |
-
st.session_state.prompts[prompt_name] = st.session_state.messages[-1]["content"]
|
97 |
-
st.success(f"Prompt '{prompt_name}' saved!")
|
98 |
-
|
99 |
-
with st.expander("Load Prompt"):
|
100 |
-
load_prompt_option = st.selectbox(
|
101 |
-
"Select a saved prompt:",
|
102 |
-
options=list(st.session_state.prompts.keys()),
|
103 |
-
)
|
104 |
-
if load_prompt_option:
|
105 |
-
st.session_state.messages[-1]["content"] = st.session_state.prompts[load_prompt_option]
|
106 |
-
st.write("Prompt loaded!")
|
107 |
-
|
108 |
-
if load_prompt_option:
|
109 |
-
prompt = st.session_state.prompts[load_prompt_option]
|
110 |
-
else:
|
111 |
-
prompt = st.chat_input("Enter your prompt here...")
|
112 |
-
|
113 |
-
if prompt:
|
114 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
115 |
|
116 |
-
with st.chat_message("user", avatar=""):
|
117 |
st.markdown(prompt)
|
118 |
|
119 |
# Fetch response from Groq API
|
@@ -129,11 +106,11 @@ if prompt:
|
|
129 |
)
|
130 |
|
131 |
# Use the generator function with st.write_stream
|
132 |
-
with st.chat_message("assistant", avatar=""):
|
133 |
chat_responses_generator = generate_chat_responses(chat_completion)
|
134 |
full_response = st.write_stream(chat_responses_generator)
|
135 |
except Exception as e:
|
136 |
-
st.error(e, icon="")
|
137 |
|
138 |
# Append the full response to session_state.messages
|
139 |
if isinstance(full_response, str):
|
|
|
7 |
import json
|
8 |
|
9 |
_ = load_dotenv(find_dotenv())
|
10 |
+
st.set_page_config(page_icon="💬", layout="wide", page_title="...")
|
11 |
|
12 |
def icon(emoji: str):
|
13 |
"""Shows an emoji as a Notion-style page icon."""
|
|
|
28 |
# Initialize chat history and selected model
|
29 |
if "messages" not in st.session_state:
|
30 |
st.session_state.messages = []
|
31 |
+
|
32 |
if "selected_model" not in st.session_state:
|
33 |
st.session_state.selected_model = None
|
|
|
|
|
34 |
|
35 |
# Define model details
|
36 |
models = {
|
|
|
77 |
|
78 |
# Display chat messages from history on app rerun
|
79 |
for message in st.session_state.messages:
|
80 |
+
avatar = "🧠" if message["role"] == "assistant" else "❓"
|
81 |
with st.chat_message(message["role"], avatar=avatar):
|
82 |
st.markdown(message["content"])
|
83 |
|
|
|
87 |
if chunk.choices[0].delta.content:
|
88 |
yield chunk.choices[0].delta.content
|
89 |
|
90 |
+
if prompt := st.chat_input("Enter your prompt here..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
92 |
|
93 |
+
with st.chat_message("user", avatar="❓"):
|
94 |
st.markdown(prompt)
|
95 |
|
96 |
# Fetch response from Groq API
|
|
|
106 |
)
|
107 |
|
108 |
# Use the generator function with st.write_stream
|
109 |
+
with st.chat_message("assistant", avatar="🧠"):
|
110 |
chat_responses_generator = generate_chat_responses(chat_completion)
|
111 |
full_response = st.write_stream(chat_responses_generator)
|
112 |
except Exception as e:
|
113 |
+
st.error(e, icon="🚨")
|
114 |
|
115 |
# Append the full response to session_state.messages
|
116 |
if isinstance(full_response, str):
|