import html import re import streamlit as st def format_message(text): """ This function is used to format the messages in the chatbot UI. Parameters: text (str): The text to be formatted. """ text_blocks = re.split(r"```[\s\S]*?```", text) code_blocks = re.findall(r"```([\s\S]*?)```", text) text_blocks = [html.escape(block) for block in text_blocks] formatted_text = "" for i in range(len(text_blocks)): formatted_text += text_blocks[i].replace("\n", "
") if i < len(code_blocks): formatted_text += f'
{html.escape(code_blocks[i])}
' return formatted_text def message_func(text, role): """ This function is used to display the messages in the chatbot UI. Parameters: text (str): The text to be displayed. role (str): Role for the text. """ if role == "user": avatar_url = "https://avataaars.io/?avatarStyle=Transparent&topType=ShortHairShortFlat&accessoriesType=Prescription01&hairColor=Auburn&facialHairType=BeardLight&facialHairColor=Black&clotheType=Hoodie&clotheColor=PastelBlue&eyeType=Squint&eyebrowType=DefaultNatural&mouthType=Smile&skinColor=Tanned" message_alignment = "flex-end" message_bg_color = "linear-gradient(135deg, #00B2FF 0%, #006AFF 100%)" avatar_class = "user-avatar" st.write( f"""
{text} \n
avatar
""", unsafe_allow_html=True, ) elif role == "assistant": avatar_url = "https://avataaars.io/?avatarStyle=Transparent&topType=WinterHat2&accessoriesType=Kurt&hatColor=Blue01&facialHairType=MoustacheMagnum&facialHairColor=Blonde&clotheType=Overall&clotheColor=Gray01&eyeType=WinkWacky&eyebrowType=SadConcernedNatural&mouthType=Sad&skinColor=Light" message_alignment = "flex-start" message_bg_color = "#71797E" avatar_class = "bot-avatar" text = format_message(text) st.write( f"""
avatar
{text} \n
""", unsafe_allow_html=True, ) else: raise