Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,156 +3,237 @@ import time
|
|
| 3 |
import random
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
<style>
|
| 9 |
-
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700&display=swap');
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
--button-bg: #10b981;
|
| 16 |
-
--button-hover: #0d946b;
|
| 17 |
-
--button-text: #ffffff;
|
| 18 |
-
}
|
| 19 |
|
| 20 |
body {
|
| 21 |
font-family: 'Vazirmatn', sans-serif;
|
| 22 |
-
background-color:
|
| 23 |
-
direction: rtl;
|
| 24 |
}
|
| 25 |
|
| 26 |
.chat-container {
|
| 27 |
-
max-width:
|
| 28 |
-
margin:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
box-shadow: 0 4px
|
| 32 |
}
|
| 33 |
|
| 34 |
-
.
|
| 35 |
-
background: linear-gradient(
|
| 36 |
color: white;
|
| 37 |
-
padding:
|
| 38 |
text-align: center;
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
.chat-messages {
|
|
|
|
|
|
|
| 45 |
height: 500px;
|
| 46 |
overflow-y: auto;
|
| 47 |
-
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
.message {
|
| 51 |
-
margin-bottom:
|
| 52 |
-
padding:
|
| 53 |
-
border-radius:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
.message.user {
|
| 57 |
-
background: #
|
| 58 |
-
|
| 59 |
}
|
| 60 |
|
| 61 |
.message.bot {
|
| 62 |
-
background: #
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
.quick-replies {
|
| 67 |
display: flex;
|
| 68 |
-
gap: 0.5rem;
|
| 69 |
-
margin-top: 1rem;
|
| 70 |
flex-wrap: wrap;
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
.quick-reply {
|
| 74 |
-
background:
|
| 75 |
-
color:
|
| 76 |
-
padding:
|
| 77 |
-
border-radius:
|
| 78 |
cursor: pointer;
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
text-align: center;
|
| 82 |
}
|
| 83 |
|
| 84 |
.quick-reply:hover {
|
| 85 |
-
background:
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
</style>
|
| 89 |
-
"""
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
"لطفاً مشکل خود را با جزئیات بیان کنید."
|
| 114 |
-
])
|
| 115 |
-
|
| 116 |
-
self.conversation_history.append({'type': 'bot', 'message': response})
|
| 117 |
-
|
| 118 |
-
# تنظیمات اولیه استریملیت
|
| 119 |
-
st.set_page_config(
|
| 120 |
-
page_title="پاسخگوی هوشمند بخشنامه",
|
| 121 |
-
page_icon="🤖",
|
| 122 |
-
layout="wide",
|
| 123 |
-
initial_sidebar_state="expanded"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
# ایجاد نمونه از چتبات
|
| 127 |
-
if 'chatbot' not in st.session_state:
|
| 128 |
-
st.session_state.chatbot = EnhancedChatbot()
|
| 129 |
-
|
| 130 |
-
# نمایش هدر اصلی
|
| 131 |
st.markdown("""
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
""", unsafe_allow_html=True)
|
| 136 |
|
| 137 |
# نمایش پیامها
|
| 138 |
-
|
| 139 |
-
|
|
|
|
| 140 |
st.markdown(f"""
|
| 141 |
-
<div class=
|
| 142 |
-
{message['message']}
|
|
|
|
| 143 |
</div>
|
| 144 |
""", unsafe_allow_html=True)
|
|
|
|
| 145 |
|
| 146 |
# پاسخهای سریع
|
| 147 |
-
st.markdown("
|
| 148 |
-
for reply in
|
| 149 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
st.experimental_rerun()
|
| 157 |
|
| 158 |
st.markdown("</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import random
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
# تنظیمات صفحه
|
| 7 |
+
st.set_page_config(page_title="چتبات هوشمند", page_icon="🤖", layout="wide")
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# CSS سفارشی برای بهبود ظاهر و انیمیشنها
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<style>
|
| 12 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;500;700&display=swap');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
body {
|
| 15 |
font-family: 'Vazirmatn', sans-serif;
|
| 16 |
+
background-color: #f8fafc;
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
.chat-container {
|
| 20 |
+
max-width: 800px;
|
| 21 |
+
margin: auto;
|
| 22 |
+
border-radius: 15px;
|
| 23 |
+
overflow: hidden;
|
| 24 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 25 |
}
|
| 26 |
|
| 27 |
+
.header {
|
| 28 |
+
background: linear-gradient(45deg, #4a90e2, #50e3c2);
|
| 29 |
color: white;
|
| 30 |
+
padding: 20px;
|
| 31 |
text-align: center;
|
| 32 |
+
position: relative;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.header .status {
|
| 36 |
+
position: absolute;
|
| 37 |
+
top: 20px;
|
| 38 |
+
right: 20px;
|
| 39 |
+
color: #10b981;
|
| 40 |
+
font-weight: bold;
|
| 41 |
}
|
| 42 |
|
| 43 |
.chat-messages {
|
| 44 |
+
background: #ffffff;
|
| 45 |
+
padding: 20px;
|
| 46 |
height: 500px;
|
| 47 |
overflow-y: auto;
|
| 48 |
+
border-top: 1px solid #ddd;
|
| 49 |
+
border-bottom: 1px solid #ddd;
|
| 50 |
}
|
| 51 |
|
| 52 |
.message {
|
| 53 |
+
margin-bottom: 15px;
|
| 54 |
+
padding: 10px 15px;
|
| 55 |
+
border-radius: 12px;
|
| 56 |
+
max-width: 75%;
|
| 57 |
+
animation: fadeIn 0.3s;
|
| 58 |
+
position: relative;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
@keyframes fadeIn {
|
| 62 |
+
from { opacity: 0; transform: translateY(20px); }
|
| 63 |
+
to { opacity: 1; transform: translateY(0); }
|
| 64 |
}
|
| 65 |
|
| 66 |
.message.user {
|
| 67 |
+
background: #e0f7fa;
|
| 68 |
+
align-self: flex-end;
|
| 69 |
}
|
| 70 |
|
| 71 |
.message.bot {
|
| 72 |
+
background: #f1f1f1;
|
| 73 |
+
align-self: flex-start;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.timestamp {
|
| 77 |
+
font-size: 0.8em;
|
| 78 |
+
color: #888;
|
| 79 |
+
margin-top: 5px;
|
| 80 |
+
text-align: right;
|
| 81 |
}
|
| 82 |
|
| 83 |
.quick-replies {
|
| 84 |
display: flex;
|
|
|
|
|
|
|
| 85 |
flex-wrap: wrap;
|
| 86 |
+
gap: 10px;
|
| 87 |
+
margin-top: 15px;
|
| 88 |
}
|
| 89 |
|
| 90 |
.quick-reply {
|
| 91 |
+
background: #4a90e2;
|
| 92 |
+
color: white;
|
| 93 |
+
padding: 8px 16px;
|
| 94 |
+
border-radius: 15px;
|
| 95 |
cursor: pointer;
|
| 96 |
+
font-weight: 500;
|
| 97 |
+
transition: background 0.3s ease;
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
.quick-reply:hover {
|
| 101 |
+
background: #357ab7;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
.chat-input {
|
| 105 |
+
display: flex;
|
| 106 |
+
gap: 10px;
|
| 107 |
+
align-items: center;
|
| 108 |
+
padding: 20px;
|
| 109 |
+
background: #f8fafc;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.chat-input input[type="text"] {
|
| 113 |
+
flex-grow: 1;
|
| 114 |
+
padding: 10px;
|
| 115 |
+
border-radius: 5px;
|
| 116 |
+
border: 1px solid #ddd;
|
| 117 |
+
font-size: 1rem;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
.chat-input button {
|
| 121 |
+
background: #4a90e2;
|
| 122 |
+
color: white;
|
| 123 |
+
padding: 10px 20px;
|
| 124 |
+
border: none;
|
| 125 |
+
border-radius: 5px;
|
| 126 |
+
cursor: pointer;
|
| 127 |
+
transition: background 0.3s;
|
| 128 |
}
|
| 129 |
+
|
| 130 |
+
.chat-input button:hover {
|
| 131 |
+
background: #357ab7;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
.admin-panel {
|
| 135 |
+
background: #ffffff;
|
| 136 |
+
border-radius: 10px;
|
| 137 |
+
padding: 20px;
|
| 138 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 139 |
+
margin-bottom: 20px;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.statistics {
|
| 143 |
+
padding: 10px 0;
|
| 144 |
+
border-bottom: 1px solid #ddd;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
</style>
|
| 148 |
+
""", unsafe_allow_html=True)
|
| 149 |
+
|
| 150 |
+
# دادههای اولیه
|
| 151 |
+
if 'conversation_history' not in st.session_state:
|
| 152 |
+
st.session_state.conversation_history = []
|
| 153 |
+
|
| 154 |
+
if 'is_typing' not in st.session_state:
|
| 155 |
+
st.session_state.is_typing = False
|
| 156 |
+
|
| 157 |
+
quick_replies = ["راهنمای استفاده", "تماس با پشتیبانی", "گزارش مشکل", "بخشنامههای جدید"]
|
| 158 |
+
|
| 159 |
+
# توابع چتبات
|
| 160 |
+
def get_bot_response(user_message):
|
| 161 |
+
time.sleep(1) # شبیهسازی تایپ کردن
|
| 162 |
+
return random.choice(["سلام! چطور میتونم کمک کنم؟", "لطفاً سوال خود را مطرح کنید.", "در خدمت شما هستم."])
|
| 163 |
+
|
| 164 |
+
def add_message(sender, message):
|
| 165 |
+
st.session_state.conversation_history.append({
|
| 166 |
+
'sender': sender,
|
| 167 |
+
'message': message,
|
| 168 |
+
'timestamp': datetime.now().strftime("%H:%M")
|
| 169 |
+
})
|
| 170 |
+
|
| 171 |
+
# هدر
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
st.markdown("""
|
| 173 |
+
<div class="header">
|
| 174 |
+
<h2>چتبات هوشمند</h2>
|
| 175 |
+
<div class="status">آنلاین</div>
|
| 176 |
+
</div>
|
| 177 |
""", unsafe_allow_html=True)
|
| 178 |
|
| 179 |
# نمایش پیامها
|
| 180 |
+
st.markdown("<div class='chat-container'><div class='chat-messages'>", unsafe_allow_html=True)
|
| 181 |
+
for message in st.session_state.conversation_history:
|
| 182 |
+
sender_class = "user" if message['sender'] == "user" else "bot"
|
| 183 |
st.markdown(f"""
|
| 184 |
+
<div class='message {sender_class}'>
|
| 185 |
+
<div>{message['message']}</div>
|
| 186 |
+
<div class='timestamp'>{message['timestamp']}</div>
|
| 187 |
</div>
|
| 188 |
""", unsafe_allow_html=True)
|
| 189 |
+
st.markdown("</div></div>", unsafe_allow_html=True)
|
| 190 |
|
| 191 |
# پاسخهای سریع
|
| 192 |
+
st.markdown("<div class='quick-replies'>", unsafe_allow_html=True)
|
| 193 |
+
for reply in quick_replies:
|
| 194 |
+
if st.button(reply):
|
| 195 |
+
add_message("user", reply)
|
| 196 |
+
bot_response = get_bot_response(reply)
|
| 197 |
+
add_message("bot", bot_response)
|
| 198 |
+
st.experimental_rerun()
|
| 199 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 200 |
|
| 201 |
+
# ورودی کاربر
|
| 202 |
+
st.markdown("<div class='chat-input'>", unsafe_allow_html=True)
|
| 203 |
+
user_message = st.text_input("", placeholder="پیام خود را وارد کنید...", label_visibility="collapsed")
|
| 204 |
+
send_button = st.button("ارسال")
|
| 205 |
+
|
| 206 |
+
if send_button and user_message:
|
| 207 |
+
add_message("user", user_message)
|
| 208 |
+
st.session_state.is_typing = True
|
| 209 |
+
bot_response = get_bot_response(user_message)
|
| 210 |
+
add_message("bot", bot_response)
|
| 211 |
+
st.session_state.is_typing = False
|
| 212 |
st.experimental_rerun()
|
| 213 |
|
| 214 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 215 |
+
|
| 216 |
+
# پنل مدیریت در سایدبار
|
| 217 |
+
with st.sidebar:
|
| 218 |
+
st.markdown("<div class='admin-panel'><h3>پنل مدیریت</h3>", unsafe_allow_html=True)
|
| 219 |
+
|
| 220 |
+
# نمایش آمار
|
| 221 |
+
total_messages = len(st.session_state.conversation_history)
|
| 222 |
+
user_messages = sum(1 for msg in st.session_state.conversation_history if msg['sender'] == 'user')
|
| 223 |
+
bot_messages = total_messages - user_messages
|
| 224 |
+
|
| 225 |
+
st.markdown(f"""
|
| 226 |
+
<div class="statistics">پیامهای کل: {total_messages}</div>
|
| 227 |
+
<div class="statistics">پیامهای کاربر: {user_messages}</div>
|
| 228 |
+
<div class="statistics">پیامهای ربات: {bot_messages}</div>
|
| 229 |
+
""", unsafe_allow_html=True)
|
| 230 |
+
|
| 231 |
+
# دکمه پاک کردن تاریخچه
|
| 232 |
+
if st.button("پاک کردن تاریخچه"):
|
| 233 |
+
st.session_state.conversation_history = []
|
| 234 |
+
st.experimental_rerun()
|
| 235 |
+
|
| 236 |
+
# تنظیمات ظاهری
|
| 237 |
+
st.markdown("### تنظیمات ظاهری")
|
| 238 |
+
font_size = st.slider("اندازه فونت", 12, 20, 14)
|
| 239 |
+
st.markdown(f"<style>.message {{ font-size: {font_size}px; }}</style>", unsafe_allow_html=True)
|