Skip to content

Commit d78512e

Browse files
committed
escape markdown special chars
Signed-off-by: Gianluca Capuzzi <gianluca.posta78@gmail.com>
1 parent ec45e85 commit d78512e

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ vectordb.txt
2323

2424
# Local dataset
2525
dataset/
26+
dataset*/
2627

2728
# Editor e sistema
2829
.DS_Store
2930
.idea/
3031
.vscode/
3132

3233
responses.txt
33-
vectordb.txt
34-
secrets.toml
34+
secrets.toml
35+
users.db

app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
st.markdown(body=gethomepage(), unsafe_allow_html=True)
88

99
# Check if user is authenticated
10-
if not st.experimental_user.is_logged_in:
10+
if not st.user.is_logged_in:
1111
if st.button("Log in or Sign up"):
1212
st.login("auth0")
1313
st.stop()
@@ -19,22 +19,22 @@
1919
st.session_state.username = None
2020

2121
# Only access user info if available
22-
if hasattr(st.experimental_user, "email"):
22+
if hasattr(st.user, "email"):
2323
conn = create_connection()
2424
if conn is not None:
2525
create_table(conn)
2626

2727
# Check if user exists
28-
user_data = get_user(conn, st.experimental_user.email)
28+
user_data = get_user(conn, st.user.email)
2929
if user_data is not None:
3030
st.session_state['user_type'] = user_data[3]
3131
st.session_state['username'] = user_data[1]
3232
else:
3333
user_type = 'guest'
34-
username = st.experimental_user.email
34+
username = st.user.email
3535
st.session_state['user_type'] = user_type
3636
st.session_state['username'] = username
37-
insert_user(conn, username, st.experimental_user.email, user_type)
37+
insert_user(conn, username, st.user.email, user_type)
3838

3939
# Show menu
4040
menu()

pages/chatbot.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from utils import load_yaml_file
1+
from utils import load_yaml_file, escape_markdown
22
from main import get_ragchain
33
import streamlit as st
44
from menu import menu_with_redirect
@@ -46,7 +46,8 @@
4646
# -------------------------------
4747
for message in user_chat:
4848
with st.chat_message(message["role"], avatar=logo_path if message["role"] == "assistant" else None):
49-
st.write(message["content"])
49+
#st.write(message["content"])
50+
st.markdown(escape_markdown(message["content"]))
5051

5152
# -------------------------------
5253
# Handle user input
@@ -67,7 +68,8 @@
6768
response = rag_chain.invoke({"input": query})
6869
# save response in a text file
6970
print(response, file=open('responses.txt', 'a', encoding='utf-8'))
70-
st.markdown(response["answer"])
71+
# Display the response escaping markdown special characters
72+
st.markdown(escape_markdown(response["answer"]))
7173

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

utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,7 @@ def save_transcript(video_id, output_folder):
269269

270270
except Exception as e:
271271
print(f"Error reading transcript: {e}")
272+
273+
# Function to escape markdown special characters
274+
def escape_markdown(text: str) -> str:
275+
return re.sub(r'([\\`*_{}\[\]()#+\-!$])', r'\\\1', text)

0 commit comments

Comments
 (0)