Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mvt/pages/admin_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/mvt/pages/chatbot.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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..."):
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion src/mvt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
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)