Spaces:
Running
Running
github-actions
commited on
Commit
•
28c58dc
1
Parent(s):
7873f3c
Sync updates from source repository
Browse files
app.py
CHANGED
@@ -1,18 +1,18 @@
|
|
1 |
from omegaconf import OmegaConf
|
2 |
from query import VectaraQuery
|
3 |
import os
|
4 |
-
import
|
5 |
-
import json
|
6 |
import uuid
|
7 |
|
8 |
import streamlit as st
|
9 |
from streamlit_pills import pills
|
10 |
from streamlit_feedback import streamlit_feedback
|
11 |
|
12 |
-
from
|
|
|
13 |
|
14 |
max_examples = 6
|
15 |
-
languages = {'English': 'eng', 'Spanish': 'spa', 'French': '
|
16 |
'Portuguese': 'por', 'Italian': 'ita', 'Japanese': 'jpn', 'Korean': 'kor', 'Russian': 'rus', 'Turkish': 'tur', 'Persian (Farsi)': 'fas',
|
17 |
'Vietnamese': 'vie', 'Thai': 'tha', 'Hebrew': 'heb', 'Dutch': 'nld', 'Indonesian': 'ind', 'Polish': 'pol', 'Ukrainian': 'ukr',
|
18 |
'Romanian': 'ron', 'Swedish': 'swe', 'Czech': 'ces', 'Greek': 'ell', 'Bengali': 'ben', 'Malay (or Malaysian)': 'msa', 'Urdu': 'urd'}
|
@@ -21,36 +21,6 @@ languages = {'English': 'eng', 'Spanish': 'spa', 'French': 'frs', 'Chinese': 'zh
|
|
21 |
if 'device_id' not in st.session_state:
|
22 |
st.session_state.device_id = str(uuid.uuid4())
|
23 |
|
24 |
-
headers = {
|
25 |
-
'Content-Type': 'application/json',
|
26 |
-
'Accept': '*/*'
|
27 |
-
}
|
28 |
-
amp_api_key = os.getenv('AMPLITUDE_TOKEN')
|
29 |
-
|
30 |
-
def thumbs_feedback(feedback, **kwargs):
|
31 |
-
"""
|
32 |
-
Sends feedback to Amplitude Analytics
|
33 |
-
"""
|
34 |
-
data = {
|
35 |
-
"api_key": amp_api_key,
|
36 |
-
"events": [{
|
37 |
-
"device_id": st.session_state.device_id,
|
38 |
-
"event_type": "provided_feedback",
|
39 |
-
"event_properties": {
|
40 |
-
"Space Name": kwargs.get("title", "Unknown Space Name"),
|
41 |
-
"Demo Type": "chatbot",
|
42 |
-
"query": kwargs.get("prompt", "No user input"),
|
43 |
-
"response": kwargs.get("response", "No chat response"),
|
44 |
-
"feedback": feedback["score"],
|
45 |
-
"Response Language": st.session_state.language
|
46 |
-
}
|
47 |
-
}]
|
48 |
-
}
|
49 |
-
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
50 |
-
if response.status_code != 200:
|
51 |
-
print(f"Request failed with status code {response.status_code}. Response Text: {response.text}")
|
52 |
-
|
53 |
-
st.session_state.feedback_key += 1
|
54 |
|
55 |
if "feedback_key" not in st.session_state:
|
56 |
st.session_state.feedback_key = 0
|
@@ -61,6 +31,12 @@ def isTrue(x) -> bool:
|
|
61 |
return x.strip().lower() == 'true'
|
62 |
|
63 |
def launch_bot():
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
def generate_response(question):
|
65 |
response = vq.submit_query(question, languages[st.session_state.language])
|
66 |
return response
|
@@ -113,9 +89,16 @@ def launch_bot():
|
|
113 |
cfg.language = st.selectbox('Language:', languages.keys())
|
114 |
if st.session_state.language != cfg.language:
|
115 |
st.session_state.language = cfg.language
|
116 |
-
|
117 |
st.rerun()
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
st.markdown("---")
|
120 |
st.markdown(
|
121 |
"## How this works?\n"
|
@@ -129,7 +112,7 @@ def launch_bot():
|
|
129 |
st.markdown(f"<center> <h2> Vectara AI Assistant: {cfg.title} </h2> </center>", unsafe_allow_html=True)
|
130 |
|
131 |
if "messages" not in st.session_state.keys():
|
132 |
-
|
133 |
|
134 |
# Display chat messages
|
135 |
for message in st.session_state.messages:
|
@@ -157,40 +140,32 @@ def launch_bot():
|
|
157 |
if st.session_state.messages[-1]["role"] != "assistant":
|
158 |
with st.chat_message("assistant"):
|
159 |
if cfg.streaming:
|
160 |
-
stream = generate_streaming_response(prompt)
|
161 |
-
response = st.write_stream(stream)
|
162 |
else:
|
163 |
with st.spinner("Thinking..."):
|
164 |
response = generate_response(prompt)
|
165 |
st.write(response)
|
|
|
|
|
166 |
message = {"role": "assistant", "content": response}
|
167 |
st.session_state.messages.append(message)
|
168 |
|
169 |
# Send query and response to Amplitude Analytics
|
170 |
-
|
171 |
-
"
|
172 |
-
"
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
"Space Name": cfg["title"],
|
177 |
-
"Demo Type": "chatbot",
|
178 |
-
"query": st.session_state.messages[-2]["content"],
|
179 |
-
"response": st.session_state.messages[-1]["content"],
|
180 |
-
"Response Language": st.session_state.language
|
181 |
-
}
|
182 |
-
}]
|
183 |
-
}
|
184 |
-
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
185 |
-
if response.status_code != 200:
|
186 |
-
print(f"Amplitude request failed with status code {response.status_code}. Response Text: {response.text}")
|
187 |
st.rerun()
|
188 |
|
189 |
if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How may I help you?"):
|
190 |
streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
|
191 |
-
kwargs = {"
|
192 |
-
"
|
193 |
-
"
|
|
|
194 |
|
195 |
if __name__ == "__main__":
|
196 |
launch_bot()
|
|
|
1 |
from omegaconf import OmegaConf
|
2 |
from query import VectaraQuery
|
3 |
import os
|
4 |
+
from PIL import Image
|
|
|
5 |
import uuid
|
6 |
|
7 |
import streamlit as st
|
8 |
from streamlit_pills import pills
|
9 |
from streamlit_feedback import streamlit_feedback
|
10 |
|
11 |
+
from utils import thumbs_feedback, send_amplitude_data, escape_dollars_outside_latex
|
12 |
+
|
13 |
|
14 |
max_examples = 6
|
15 |
+
languages = {'English': 'eng', 'Spanish': 'spa', 'French': 'fra', 'Chinese': 'zho', 'German': 'deu', 'Hindi': 'hin', 'Arabic': 'ara',
|
16 |
'Portuguese': 'por', 'Italian': 'ita', 'Japanese': 'jpn', 'Korean': 'kor', 'Russian': 'rus', 'Turkish': 'tur', 'Persian (Farsi)': 'fas',
|
17 |
'Vietnamese': 'vie', 'Thai': 'tha', 'Hebrew': 'heb', 'Dutch': 'nld', 'Indonesian': 'ind', 'Polish': 'pol', 'Ukrainian': 'ukr',
|
18 |
'Romanian': 'ron', 'Swedish': 'swe', 'Czech': 'ces', 'Greek': 'ell', 'Bengali': 'ben', 'Malay (or Malaysian)': 'msa', 'Urdu': 'urd'}
|
|
|
21 |
if 'device_id' not in st.session_state:
|
22 |
st.session_state.device_id = str(uuid.uuid4())
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
if "feedback_key" not in st.session_state:
|
26 |
st.session_state.feedback_key = 0
|
|
|
31 |
return x.strip().lower() == 'true'
|
32 |
|
33 |
def launch_bot():
|
34 |
+
def reset():
|
35 |
+
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
36 |
+
st.session_state.ex_prompt = None
|
37 |
+
st.session_state.first_turn = True
|
38 |
+
|
39 |
+
|
40 |
def generate_response(question):
|
41 |
response = vq.submit_query(question, languages[st.session_state.language])
|
42 |
return response
|
|
|
89 |
cfg.language = st.selectbox('Language:', languages.keys())
|
90 |
if st.session_state.language != cfg.language:
|
91 |
st.session_state.language = cfg.language
|
92 |
+
reset()
|
93 |
st.rerun()
|
94 |
|
95 |
+
st.markdown("\n")
|
96 |
+
bc1, _ = st.columns([1, 1])
|
97 |
+
with bc1:
|
98 |
+
if st.button('Start Over'):
|
99 |
+
reset()
|
100 |
+
st.rerun()
|
101 |
+
|
102 |
st.markdown("---")
|
103 |
st.markdown(
|
104 |
"## How this works?\n"
|
|
|
112 |
st.markdown(f"<center> <h2> Vectara AI Assistant: {cfg.title} </h2> </center>", unsafe_allow_html=True)
|
113 |
|
114 |
if "messages" not in st.session_state.keys():
|
115 |
+
reset()
|
116 |
|
117 |
# Display chat messages
|
118 |
for message in st.session_state.messages:
|
|
|
140 |
if st.session_state.messages[-1]["role"] != "assistant":
|
141 |
with st.chat_message("assistant"):
|
142 |
if cfg.streaming:
|
143 |
+
stream = generate_streaming_response(prompt)
|
144 |
+
response = st.write_stream(stream)
|
145 |
else:
|
146 |
with st.spinner("Thinking..."):
|
147 |
response = generate_response(prompt)
|
148 |
st.write(response)
|
149 |
+
|
150 |
+
response = escape_dollars_outside_latex(response)
|
151 |
message = {"role": "assistant", "content": response}
|
152 |
st.session_state.messages.append(message)
|
153 |
|
154 |
# Send query and response to Amplitude Analytics
|
155 |
+
send_amplitude_data(
|
156 |
+
user_query=st.session_state.messages[-2]["content"],
|
157 |
+
chat_response=st.session_state.messages[-1]["content"],
|
158 |
+
demo_name=cfg["title"],
|
159 |
+
language=st.session_state.language
|
160 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
st.rerun()
|
162 |
|
163 |
if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How may I help you?"):
|
164 |
streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
|
165 |
+
kwargs = {"user_query": st.session_state.messages[-2]["content"],
|
166 |
+
"chat_response": st.session_state.messages[-1]["content"],
|
167 |
+
"demo_name": cfg["title"],
|
168 |
+
"response_language": st.session_state.language})
|
169 |
|
170 |
if __name__ == "__main__":
|
171 |
launch_bot()
|
utils.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import re
|
5 |
+
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
headers = {
|
9 |
+
'Content-Type': 'application/json',
|
10 |
+
'Accept': '*/*'
|
11 |
+
}
|
12 |
+
|
13 |
+
def thumbs_feedback(feedback, **kwargs):
|
14 |
+
"""
|
15 |
+
Sends feedback to Amplitude Analytics
|
16 |
+
"""
|
17 |
+
|
18 |
+
send_amplitude_data(
|
19 |
+
user_query=kwargs.get("user_query", "No user input"),
|
20 |
+
chat_response=kwargs.get("chat_response", "No bot response"),
|
21 |
+
demo_name=kwargs.get("demo_name", "Unknown"),
|
22 |
+
language = kwargs.get("response_language", "Unknown"),
|
23 |
+
feedback=feedback["score"],
|
24 |
+
)
|
25 |
+
st.session_state.feedback_key += 1
|
26 |
+
|
27 |
+
def send_amplitude_data(user_query, chat_response, demo_name, language, feedback=None):
|
28 |
+
# Send query and response to Amplitude Analytics
|
29 |
+
data = {
|
30 |
+
"api_key": os.getenv('AMPLITUDE_TOKEN'),
|
31 |
+
"events": [{
|
32 |
+
"device_id": st.session_state.device_id,
|
33 |
+
"event_type": "submitted_query",
|
34 |
+
"event_properties": {
|
35 |
+
"Space Name": demo_name,
|
36 |
+
"Demo Type": "chatbot",
|
37 |
+
"query": user_query,
|
38 |
+
"response": chat_response,
|
39 |
+
"Response Language": language
|
40 |
+
}
|
41 |
+
}]
|
42 |
+
}
|
43 |
+
if feedback:
|
44 |
+
data["events"][0]["event_properties"]["feedback"] = feedback
|
45 |
+
|
46 |
+
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
47 |
+
if response.status_code != 200:
|
48 |
+
print(f"Amplitude request failed with status code {response.status_code}. Response Text: {response.text}")
|
49 |
+
|
50 |
+
def escape_dollars_outside_latex(text):
|
51 |
+
# Define a regex pattern to find LaTeX equations (either single $ or double $$)
|
52 |
+
pattern = re.compile(r'(\$\$.*?\$\$|\$.*?\$)')
|
53 |
+
latex_matches = pattern.findall(text)
|
54 |
+
|
55 |
+
# Placeholder to temporarily store LaTeX equations
|
56 |
+
placeholders = {}
|
57 |
+
for i, match in enumerate(latex_matches):
|
58 |
+
placeholder = f'__LATEX_PLACEHOLDER_{i}__'
|
59 |
+
placeholders[placeholder] = match
|
60 |
+
text = text.replace(match, placeholder)
|
61 |
+
|
62 |
+
# Escape dollar signs in the rest of the text
|
63 |
+
text = text.replace('$', '\\$')
|
64 |
+
|
65 |
+
# Replace placeholders with the original LaTeX equations
|
66 |
+
for placeholder, original in placeholders.items():
|
67 |
+
text = text.replace(placeholder, original)
|
68 |
+
return text
|