Spaces:
Sleeping
Sleeping
Create htmlTemplates.py
Browse files- htmlTemplates.py +103 -0
htmlTemplates.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
def get_base64_image(path: str) -> str:
|
| 5 |
+
"""خواندن فایل عکس و تبدیل به base64"""
|
| 6 |
+
if not os.path.exists(path):
|
| 7 |
+
raise FileNotFoundError(f"Avatar not found: {path}")
|
| 8 |
+
with open(path, "rb") as f:
|
| 9 |
+
data = f.read()
|
| 10 |
+
return base64.b64encode(data).decode("utf-8")
|
| 11 |
+
|
| 12 |
+
# Define the mapping of source names to avatar file paths
|
| 13 |
+
SOURCE_AVATAR_MAPPING = {
|
| 14 |
+
"Khamenei": "images/bot_avatar.png",
|
| 15 |
+
"Sistani": "images/bot_avatar1.png",
|
| 16 |
+
"Golpaygani": "images/bot_avatar2.png"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
BOT_AVATARS_BASE64 = {
|
| 20 |
+
source: get_base64_image(path) for source, path in SOURCE_AVATAR_MAPPING.items()
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
USER_AVATAR = get_base64_image("images/user_avatar.png")
|
| 24 |
+
|
| 25 |
+
css = '''
|
| 26 |
+
<style>
|
| 27 |
+
.chat-row {
|
| 28 |
+
display: flex;
|
| 29 |
+
align-items: flex-end;
|
| 30 |
+
margin: 1rem 0;
|
| 31 |
+
direction: rtl;
|
| 32 |
+
font-family: "Vazir", Tahoma, sans-serif;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.chat-row .avatar {
|
| 36 |
+
flex-shrink: 0;
|
| 37 |
+
margin: 0 0.5rem;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
.chat-row .avatar img {
|
| 41 |
+
width: 52px;
|
| 42 |
+
height: 52px;
|
| 43 |
+
border-radius: 50%;
|
| 44 |
+
object-fit: cover;
|
| 45 |
+
border: 2px solid #fff;
|
| 46 |
+
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
.chat-bubble {
|
| 50 |
+
max-width: 70%;
|
| 51 |
+
padding: 1rem 1.2rem;
|
| 52 |
+
border-radius: 1rem;
|
| 53 |
+
line-height: 1.8;
|
| 54 |
+
font-size: 1rem;
|
| 55 |
+
word-wrap: break-word;
|
| 56 |
+
white-space: pre-wrap;
|
| 57 |
+
animation: fadeIn 0.3s ease-in-out;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.chat-row.user {
|
| 61 |
+
justify-content: flex-end;
|
| 62 |
+
}
|
| 63 |
+
.chat-row.user .chat-bubble {
|
| 64 |
+
background: linear-gradient(145deg, #232832, #2f3542);
|
| 65 |
+
color: #e4e6eb;
|
| 66 |
+
border-bottom-right-radius: 0.4rem;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
.chat-row.bot {
|
| 70 |
+
justify-content: flex-start;
|
| 71 |
+
}
|
| 72 |
+
.chat-row.bot .chat-bubble {
|
| 73 |
+
background: linear-gradient(145deg, #3e4658, #50586c);
|
| 74 |
+
color: #f8f9fa;
|
| 75 |
+
border-bottom-left-radius: 0.4rem;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
@keyframes fadeIn {
|
| 79 |
+
from {opacity: 0; transform: translateY(10px);}
|
| 80 |
+
to {opacity: 1; transform: translateY(0);}
|
| 81 |
+
}
|
| 82 |
+
</style>
|
| 83 |
+
'''
|
| 84 |
+
|
| 85 |
+
# A function to generate the bot template with the correct base64 image
|
| 86 |
+
def get_bot_template(avatar_base64: str) -> str:
|
| 87 |
+
return f'''
|
| 88 |
+
<div class="chat-row bot">
|
| 89 |
+
<div class="avatar">
|
| 90 |
+
<img src="data:image/png;base64,{avatar_base64}">
|
| 91 |
+
</div>
|
| 92 |
+
<div class="chat-bubble">{{{{MSG}}}}</div>
|
| 93 |
+
</div>
|
| 94 |
+
'''
|
| 95 |
+
|
| 96 |
+
user_template = f'''
|
| 97 |
+
<div class="chat-row user">
|
| 98 |
+
<div class="chat-bubble">{{{{MSG}}}}</div>
|
| 99 |
+
<div class="avatar">
|
| 100 |
+
<img src="data:image/png;base64,{USER_AVATAR}">
|
| 101 |
+
</div>
|
| 102 |
+
</div>
|
| 103 |
+
'''
|