Spaces:
Sleeping
Sleeping
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", "<br>") | |
if i < len(code_blocks): | |
formatted_text += f'<pre style="white-space: pre-wrap; word-wrap: break-word;"><code>{html.escape(code_blocks[i])}</code></pre>' | |
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""" | |
<div style="display: flex; align-items: center; margin-bottom: 10px; justify-content: {message_alignment};"> | |
<div style="background: {message_bg_color}; color: white; border-radius: 20px; padding: 10px; margin-right: 5px; max-width: 75%; font-size: 14px;"> | |
{text} \n </div> | |
<img src="{avatar_url}" class="{avatar_class}" alt="avatar" style="width: 50px; height: 50px;" /> | |
</div> | |
""", | |
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""" | |
<div style="display: flex; align-items: center; margin-bottom: 10px; justify-content: {message_alignment};"> | |
<img src="{avatar_url}" class="{avatar_class}" alt="avatar" style="width: 50px; height: 50px;" /> | |
<div style="background: {message_bg_color}; color: white; border-radius: 20px; padding: 10px; margin-right: 5px; max-width: 75%; font-size: 14px;"> | |
{text} \n </div> | |
</div> | |
""", | |
unsafe_allow_html=True, | |
) | |
else: | |
raise |