Spaces:
Running
Running
Swayambhoo Jain
commited on
Commit
·
43ba4e0
1
Parent(s):
ac9e7e2
adding streaming experience
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import requests
|
|
2 |
import json
|
3 |
import streamlit as st
|
4 |
import sys
|
|
|
5 |
|
6 |
st.set_page_config(page_title="Samba-CoE_v0.1 Chatbot")
|
7 |
|
@@ -73,16 +74,25 @@ if prompt := st.chat_input():
|
|
73 |
with st.chat_message("user"):
|
74 |
st.write(prompt)
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Generate a new response if last message is not from assistant
|
77 |
if st.session_state.messages[-1]["role"] != "assistant":
|
78 |
with st.chat_message("assistant"):
|
79 |
with st.spinner("Thinking..."):
|
80 |
response = generate_response(prompt)
|
81 |
placeholder = st.empty()
|
82 |
-
|
83 |
-
for item in response:
|
84 |
-
full_response += item
|
85 |
-
placeholder.markdown(full_response)
|
86 |
|
87 |
-
message = {"role": "assistant", "content":
|
88 |
st.session_state.messages.append(message)
|
|
|
2 |
import json
|
3 |
import streamlit as st
|
4 |
import sys
|
5 |
+
import time
|
6 |
|
7 |
st.set_page_config(page_title="Samba-CoE_v0.1 Chatbot")
|
8 |
|
|
|
74 |
with st.chat_message("user"):
|
75 |
st.write(prompt)
|
76 |
|
77 |
+
def clean_response(response):
|
78 |
+
if '[INST' in response:
|
79 |
+
return response.split('[INST')[0]
|
80 |
+
elif '[/INST' in response:
|
81 |
+
return response.split('[/INST')[0]
|
82 |
+
return response
|
83 |
+
|
84 |
+
def stream_response(response):
|
85 |
+
for word in response.split(" "):
|
86 |
+
yield word + " "
|
87 |
+
time.sleep(0.02)
|
88 |
+
|
89 |
# Generate a new response if last message is not from assistant
|
90 |
if st.session_state.messages[-1]["role"] != "assistant":
|
91 |
with st.chat_message("assistant"):
|
92 |
with st.spinner("Thinking..."):
|
93 |
response = generate_response(prompt)
|
94 |
placeholder = st.empty()
|
95 |
+
st.write_stream(stream_response(response))
|
|
|
|
|
|
|
96 |
|
97 |
+
message = {"role": "assistant", "content": response}
|
98 |
st.session_state.messages.append(message)
|