From 8158ac3d0912f33333dd4f0d841336de6c8266f1 Mon Sep 17 00:00:00 2001 From: RAWx18 Date: Thu, 17 Jul 2025 12:03:12 +0530 Subject: [PATCH] Issue 141: streamlit shows currency it formats as math formula issue fixed Signed-off-by: RAWx18 --- src/mvt/pages/admin_responses.py | 4 ++-- src/mvt/pages/chatbot.py | 8 ++++---- src/mvt/utils.py | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/mvt/pages/admin_responses.py b/src/mvt/pages/admin_responses.py index ecc1c2f..2091757 100644 --- a/src/mvt/pages/admin_responses.py +++ b/src/mvt/pages/admin_responses.py @@ -4,7 +4,7 @@ import json import shutil from menu import menu_with_redirect -from utils import load_yaml_file_with_db_prompts +from utils import load_yaml_file_with_db_prompts, escape_markdown from database import create_connection, get_all_responses_with_documents, migrate_text_file_to_database # Redirect to app.py if not logged in, otherwise show the navigation menu @@ -299,7 +299,7 @@ def display_source_document(doc, index): # Answer st.markdown("**🤖 Answer:**") - st.markdown(answer) + st.markdown(escape_markdown(answer)) # Source documents if context: diff --git a/src/mvt/pages/chatbot.py b/src/mvt/pages/chatbot.py index 7dae00a..830d419 100644 --- a/src/mvt/pages/chatbot.py +++ b/src/mvt/pages/chatbot.py @@ -1,4 +1,4 @@ -from utils import load_yaml_file_with_db_prompts +from utils import load_yaml_file_with_db_prompts, escape_markdown from main import get_ragchain import streamlit as st from menu import menu_with_redirect @@ -81,7 +81,7 @@ def save_feedback(username, msg_idx, feedback_type, response_snippet, reason=Non # ------------------------------- for idx, message in enumerate(user_chat): with st.chat_message(message["role"], avatar=logo_path if message["role"] == "assistant" else None): - st.write(message["content"]) + st.markdown(escape_markdown(message["content"])) # Only show feedback for assistant messages that are not the initial greeting if message["role"] == "assistant" and not (idx == 0 and message["content"] == "How may I help you?"): feedback_key = f"feedback_{username}_{idx}" @@ -147,7 +147,7 @@ def save_feedback(username, msg_idx, feedback_type, response_snippet, reason=Non save_message(username, "user", prompt) with st.chat_message("user"): - st.write(prompt) + st.markdown(escape_markdown(prompt)) with st.chat_message("assistant", avatar=logo_path): with st.spinner("Thinking..."): @@ -197,7 +197,7 @@ def save_feedback(username, msg_idx, feedback_type, response_snippet, reason=Non # Keep text file backup for now (can be removed later) print(response, file=open('responses.txt', 'a', encoding='utf-8')) - st.markdown(response["answer"]) + st.markdown(escape_markdown(response["answer"])) reply_msg = {"role": "assistant", "content": response["answer"]} user_chat.append(reply_msg) diff --git a/src/mvt/utils.py b/src/mvt/utils.py index f57169b..109c24f 100644 --- a/src/mvt/utils.py +++ b/src/mvt/utils.py @@ -302,4 +302,21 @@ def save_transcript(video_id, output_folder): print(f"Saved transcript in: {file_path}") except Exception as e: - print(f"Error reading transcript: {e}") \ No newline at end of file + print(f"Error reading transcript: {e}") + + +# Markdown escape function to prevent special characters from being interpreted as markdown +MARKDOWN_ESCAPE_RE = re.compile(r'([\\`*_{}\[\]()#+\-!$])') + +def escape_markdown(text: str) -> str: + """ + Escape special markdown characters to prevent them from being interpreted as markdown syntax. + This is particularly useful for currency symbols like $ that Streamlit interprets as math mode. + + Args: + text (str): The text to escape + + Returns: + str: The text with markdown special characters escaped + """ + return MARKDOWN_ESCAPE_RE.sub(r'\\\1', text) \ No newline at end of file