abanm commited on
Commit
7e529bb
·
verified ·
1 Parent(s): c5a798d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -23,8 +23,10 @@ except OSError as e:
23
  # Streamlit Configurations
24
  st.set_page_config(page_title="DUBSChat", page_icon=IMAGE_PATH, layout="wide")
25
 
26
- # If you are using a custom "logo" method:
27
- st.logo(IMAGE_PATH_2)
 
 
28
 
29
  # -------------------------
30
  # Utility Functions
@@ -67,8 +69,14 @@ with st.sidebar:
67
  # Reset chat
68
  if st.button("New Chat"):
69
  st.session_state["messages"] = [
70
- {"role": "system", "content": "You are DUBS, a helpful assistant capable of conversing in a friendly and knowledgeable way."},
71
- {"role": "assistant", "content": "Hello! How can I assist you today?"}
 
 
 
 
 
 
72
  ]
73
  st.session_state["session_name"] = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
74
  save_chat_history(st.session_state["session_name"], st.session_state["messages"])
@@ -90,8 +98,14 @@ with st.sidebar:
90
  # -------------------------
91
  if "messages" not in st.session_state:
92
  st.session_state["messages"] = [
93
- {"role": "system", "content": "You are DUBS, a helpful assistant capable of conversing in a friendly and knowledgeable way."},
94
- {"role": "assistant", "content": "Hello! How can I assist you today?"}
 
 
 
 
 
 
95
  ]
96
  if "session_name" not in st.session_state:
97
  st.session_state["session_name"] = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
@@ -135,6 +149,10 @@ def stream_response(prompt_text, api_key):
135
  for response in stream:
136
  if response.token.special:
137
  continue
 
 
 
 
138
  partial_text += response.token.text
139
  yield response.token.text
140
  except Exception as e:
@@ -146,28 +164,26 @@ def stream_response(prompt_text, api_key):
146
  prompt = st.chat_input()
147
 
148
  if prompt:
149
- # 1) Add the user's message to session state
150
- st.session_state["messages"].append({"role": "user", "content": prompt})
151
- st.chat_message("user").write(prompt)
152
 
153
- # 2) Build combined chat history for the model prompt
154
- chat_history = "".join(
155
- [f"<|{msg['role']}|>{msg['content']}<|end|>" for msg in st.session_state["messages"]]
156
- )
157
 
158
- with st.spinner("Dubs is thinking... Woof Woof! 🐾"):
 
159
  with st.chat_message("assistant", avatar=DUBS_PATH):
160
- full_response = ""
161
- placeholder = st.empty()
162
  response = stream_response(chat_history, HF_API_KEY)
163
  for item in response:
164
  full_response += item
165
  placeholder.markdown(full_response)
166
- placeholder.markdown(full_response)
167
-
168
- # 5) Save the final assistant message in session state
169
- st.session_state["messages"].append({"role": "assistant", "content": full_response})
170
- # 6) Persist updated chat history
171
- save_chat_history(st.session_state["session_name"], st.session_state["messages"])
172
-
173
 
 
 
 
 
 
23
  # Streamlit Configurations
24
  st.set_page_config(page_title="DUBSChat", page_icon=IMAGE_PATH, layout="wide")
25
 
26
+ # NOTE: st.logo() is not a built-in Streamlit function.
27
+ # Replace or remove if needed. For a simple image display, use:
28
+ # st.image(IMAGE_PATH_2)
29
+ st.logo(IMAGE_PATH_2) # If you have a custom method for logos, otherwise replace with st.image()
30
 
31
  # -------------------------
32
  # Utility Functions
 
69
  # Reset chat
70
  if st.button("New Chat"):
71
  st.session_state["messages"] = [
72
+ {
73
+ "role": "system",
74
+ "content": "You are DUBS, a helpful assistant capable of conversing in a friendly and knowledgeable way."
75
+ },
76
+ {
77
+ "role": "assistant",
78
+ "content": "Hello! How can I assist you today?"
79
+ }
80
  ]
81
  st.session_state["session_name"] = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
82
  save_chat_history(st.session_state["session_name"], st.session_state["messages"])
 
98
  # -------------------------
99
  if "messages" not in st.session_state:
100
  st.session_state["messages"] = [
101
+ {
102
+ "role": "system",
103
+ "content": "You are DUBS, a helpful assistant capable of conversing in a friendly and knowledgeable way."
104
+ },
105
+ {
106
+ "role": "assistant",
107
+ "content": "Hello! How can I assist you today?"
108
+ }
109
  ]
110
  if "session_name" not in st.session_state:
111
  st.session_state["session_name"] = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
 
149
  for response in stream:
150
  if response.token.special:
151
  continue
152
+ # Check for stop sequence and break if encountered
153
+ if response.token.text in gen_kwargs["stop_sequences"]:
154
+ break
155
+
156
  partial_text += response.token.text
157
  yield response.token.text
158
  except Exception as e:
 
164
  prompt = st.chat_input()
165
 
166
  if prompt:
167
+ # 1) Add the user's message to session state
168
+ st.session_state["messages"].append({"role": "user", "content": prompt})
169
+ st.chat_message("user").write(prompt)
170
 
171
+ # 2) Build combined chat history for the model prompt
172
+ chat_history = "".join(
173
+ [f"<|{msg['role']}|>{msg['content']}<|end|>" for msg in st.session_state["messages"]]
174
+ )
175
 
176
+ # 3) Generate the response under a spinner
177
+ with st.spinner("Dubs is thinking... Woof Woof! 🐾"):
178
  with st.chat_message("assistant", avatar=DUBS_PATH):
179
+ full_response = ""
180
+ placeholder = st.empty()
181
  response = stream_response(chat_history, HF_API_KEY)
182
  for item in response:
183
  full_response += item
184
  placeholder.markdown(full_response)
 
 
 
 
 
 
 
185
 
186
+ # 4) Save the final assistant message in session state
187
+ st.session_state["messages"].append({"role": "assistant", "content": full_response})
188
+ # 5) Persist updated chat history
189
+ save_chat_history(st.session_state["session_name"], st.session_state["messages"])