Spaces:
Sleeping
Sleeping
david-oplatka
commited on
Commit
·
0901806
1
Parent(s):
7d06222
Add LaTeX and Start Over
Browse files
app.py
CHANGED
@@ -8,7 +8,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
|
12 |
|
13 |
|
14 |
max_examples = 6
|
@@ -31,6 +31,12 @@ def isTrue(x) -> bool:
|
|
31 |
return x.strip().lower() == 'true'
|
32 |
|
33 |
def launch_bot():
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def generate_response(question):
|
35 |
response = vq.submit_query(question, languages[st.session_state.language])
|
36 |
return response
|
@@ -83,9 +89,16 @@ def launch_bot():
|
|
83 |
cfg.language = st.selectbox('Language:', languages.keys())
|
84 |
if st.session_state.language != cfg.language:
|
85 |
st.session_state.language = cfg.language
|
86 |
-
|
87 |
st.rerun()
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
st.markdown("---")
|
90 |
st.markdown(
|
91 |
"## How this works?\n"
|
@@ -99,7 +112,7 @@ def launch_bot():
|
|
99 |
st.markdown(f"<center> <h2> Vectara AI Assistant: {cfg.title} </h2> </center>", unsafe_allow_html=True)
|
100 |
|
101 |
if "messages" not in st.session_state.keys():
|
102 |
-
|
103 |
|
104 |
# Display chat messages
|
105 |
for message in st.session_state.messages:
|
@@ -128,11 +141,13 @@ def launch_bot():
|
|
128 |
with st.chat_message("assistant"):
|
129 |
if cfg.streaming:
|
130 |
stream = generate_streaming_response(prompt)
|
131 |
-
response = st.write_stream(stream)
|
132 |
else:
|
133 |
with st.spinner("Thinking..."):
|
134 |
response = generate_response(prompt)
|
135 |
st.write(response)
|
|
|
|
|
136 |
message = {"role": "assistant", "content": response}
|
137 |
st.session_state.messages.append(message)
|
138 |
|
|
|
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
|
|
|
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:
|
|
|
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 |
|
utils.py
CHANGED
@@ -45,3 +45,23 @@ def send_amplitude_data(user_query, chat_response, demo_name, language, feedback
|
|
45 |
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
46 |
if response.status_code != 200:
|
47 |
print(f"Amplitude request failed with status code {response.status_code}. Response Text: {response.text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
46 |
if response.status_code != 200:
|
47 |
print(f"Amplitude request failed with status code {response.status_code}. Response Text: {response.text}")
|
48 |
+
|
49 |
+
def escape_dollars_outside_latex(text):
|
50 |
+
# Define a regex pattern to find LaTeX equations (either single $ or double $$)
|
51 |
+
pattern = re.compile(r'(\$\$.*?\$\$|\$.*?\$)')
|
52 |
+
latex_matches = pattern.findall(text)
|
53 |
+
|
54 |
+
# Placeholder to temporarily store LaTeX equations
|
55 |
+
placeholders = {}
|
56 |
+
for i, match in enumerate(latex_matches):
|
57 |
+
placeholder = f'__LATEX_PLACEHOLDER_{i}__'
|
58 |
+
placeholders[placeholder] = match
|
59 |
+
text = text.replace(match, placeholder)
|
60 |
+
|
61 |
+
# Escape dollar signs in the rest of the text
|
62 |
+
text = text.replace('$', '\\$')
|
63 |
+
|
64 |
+
# Replace placeholders with the original LaTeX equations
|
65 |
+
for placeholder, original in placeholders.items():
|
66 |
+
text = text.replace(placeholder, original)
|
67 |
+
return text
|