Skip to content

Commit 0c1fa80

Browse files
authored
Issue 141: streamlit shows currency it formats as math formula issue fixed (#145)
Signed-off-by: RAWx18 <[email protected]>
1 parent 7003211 commit 0c1fa80

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/mvt/pages/admin_responses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
import shutil
66
from menu import menu_with_redirect
7-
from utils import load_yaml_file_with_db_prompts
7+
from utils import load_yaml_file_with_db_prompts, escape_markdown
88
from database import create_connection, get_all_responses_with_documents, migrate_text_file_to_database
99

1010
# Redirect to app.py if not logged in, otherwise show the navigation menu
@@ -299,7 +299,7 @@ def display_source_document(doc, index):
299299

300300
# Answer
301301
st.markdown("**🤖 Answer:**")
302-
st.markdown(answer)
302+
st.markdown(escape_markdown(answer))
303303

304304
# Source documents
305305
if context:

src/mvt/pages/chatbot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from utils import load_yaml_file_with_db_prompts
1+
from utils import load_yaml_file_with_db_prompts, escape_markdown
22
from main import get_ragchain
33
import streamlit as st
44
from menu import menu_with_redirect
@@ -81,7 +81,7 @@ def save_feedback(username, msg_idx, feedback_type, response_snippet, reason=Non
8181
# -------------------------------
8282
for idx, message in enumerate(user_chat):
8383
with st.chat_message(message["role"], avatar=logo_path if message["role"] == "assistant" else None):
84-
st.write(message["content"])
84+
st.markdown(escape_markdown(message["content"]))
8585
# Only show feedback for assistant messages that are not the initial greeting
8686
if message["role"] == "assistant" and not (idx == 0 and message["content"] == "How may I help you?"):
8787
feedback_key = f"feedback_{username}_{idx}"
@@ -147,7 +147,7 @@ def save_feedback(username, msg_idx, feedback_type, response_snippet, reason=Non
147147
save_message(username, "user", prompt)
148148

149149
with st.chat_message("user"):
150-
st.write(prompt)
150+
st.markdown(escape_markdown(prompt))
151151

152152
with st.chat_message("assistant", avatar=logo_path):
153153
with st.spinner("Thinking..."):
@@ -197,7 +197,7 @@ def save_feedback(username, msg_idx, feedback_type, response_snippet, reason=Non
197197

198198
# Keep text file backup for now (can be removed later)
199199
print(response, file=open('responses.txt', 'a', encoding='utf-8'))
200-
st.markdown(response["answer"])
200+
st.markdown(escape_markdown(response["answer"]))
201201

202202
reply_msg = {"role": "assistant", "content": response["answer"]}
203203
user_chat.append(reply_msg)

src/mvt/utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,21 @@ def save_transcript(video_id, output_folder):
302302
print(f"Saved transcript in: {file_path}")
303303

304304
except Exception as e:
305-
print(f"Error reading transcript: {e}")
305+
print(f"Error reading transcript: {e}")
306+
307+
308+
# Markdown escape function to prevent special characters from being interpreted as markdown
309+
MARKDOWN_ESCAPE_RE = re.compile(r'([\\`*_{}\[\]()#+\-!$])')
310+
311+
def escape_markdown(text: str) -> str:
312+
"""
313+
Escape special markdown characters to prevent them from being interpreted as markdown syntax.
314+
This is particularly useful for currency symbols like $ that Streamlit interprets as math mode.
315+
316+
Args:
317+
text (str): The text to escape
318+
319+
Returns:
320+
str: The text with markdown special characters escaped
321+
"""
322+
return MARKDOWN_ESCAPE_RE.sub(r'\\\1', text)

0 commit comments

Comments
 (0)