Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# تنظیمات
|
| 5 |
ADMIN_USERNAME = "admin"
|
| 6 |
-
ADMIN_PASSWORD = "
|
| 7 |
|
| 8 |
-
# CSS برای ظاهر
|
| 9 |
CUSTOM_CSS = """
|
| 10 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
body {
|
| 12 |
font-family: 'Vazir', sans-serif;
|
| 13 |
-
background:
|
| 14 |
}
|
|
|
|
| 15 |
.chat-container {
|
| 16 |
max-width: 400px;
|
| 17 |
margin: 0 auto;
|
| 18 |
border-radius: 15px;
|
| 19 |
-
box-shadow: 0px 4px 12px
|
|
|
|
|
|
|
| 20 |
}
|
|
|
|
| 21 |
.header {
|
| 22 |
-
background: linear-gradient(135deg,
|
| 23 |
color: white;
|
| 24 |
padding: 10px;
|
| 25 |
text-align: center;
|
| 26 |
font-weight: bold;
|
| 27 |
font-size: 18px;
|
| 28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
.message {
|
| 30 |
padding: 12px 15px;
|
| 31 |
margin: 10px 0;
|
|
@@ -33,23 +61,30 @@ body {
|
|
| 33 |
width: fit-content;
|
| 34 |
max-width: 80%;
|
| 35 |
font-size: 15px;
|
|
|
|
|
|
|
| 36 |
}
|
|
|
|
| 37 |
.message.user {
|
| 38 |
align-self: flex-end;
|
| 39 |
-
background:
|
| 40 |
-
color:
|
| 41 |
}
|
|
|
|
| 42 |
.message.bot {
|
| 43 |
align-self: flex-start;
|
| 44 |
-
background:
|
| 45 |
color: white;
|
| 46 |
}
|
|
|
|
| 47 |
.footer {
|
| 48 |
display: flex;
|
| 49 |
padding: 10px;
|
| 50 |
-
background:
|
|
|
|
| 51 |
align-items: center;
|
| 52 |
}
|
|
|
|
| 53 |
.footer input[type="text"] {
|
| 54 |
flex: 1;
|
| 55 |
padding: 10px;
|
|
@@ -57,76 +92,145 @@ body {
|
|
| 57 |
border-radius: 20px;
|
| 58 |
background: #ffffff;
|
| 59 |
margin-right: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
</style>
|
| 62 |
"""
|
|
|
|
|
|
|
| 63 |
st.markdown(CUSTOM_CSS, unsafe_allow_html=True)
|
| 64 |
|
| 65 |
class ModernChatbot:
|
| 66 |
def __init__(self):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"""تولید پاسخ با مدل بارگذاری شده"""
|
| 86 |
-
self.load_model()
|
| 87 |
-
response = st.session_state.chat_pipeline(
|
| 88 |
-
message, max_length=50, num_return_sequences=1
|
| 89 |
-
)[0]['generated_text']
|
| 90 |
-
return response
|
| 91 |
|
| 92 |
def login_admin(self, username, password):
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
st.session_state.conversation_history = []
|
| 99 |
|
| 100 |
-
# ایجاد
|
| 101 |
chatbot = ModernChatbot()
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
st.
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
align_class = "user" if sender == "شما" else "bot"
|
| 118 |
-
st.markdown(f"<div class='message {align_class}'>{sender}
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
user_message = st.text_input("پیام خود را اینجا بنویسید...", key="user_message")
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
st.session_state.conversation_history = []
|
| 132 |
st.experimental_rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import time
|
| 3 |
+
import random
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# تنظیمات اصلی چتبات
|
| 7 |
ADMIN_USERNAME = "admin"
|
| 8 |
+
ADMIN_PASSWORD = "password"
|
| 9 |
|
| 10 |
+
# CSS سفارشی برای ظاهر چتبات و حالت تیره و روشن
|
| 11 |
CUSTOM_CSS = """
|
| 12 |
<style>
|
| 13 |
+
:root {
|
| 14 |
+
--primary-color: #4a90e2;
|
| 15 |
+
--secondary-color: #ff6b6b;
|
| 16 |
+
--neutral-color: #f5f5f5;
|
| 17 |
+
--accent-color: #50e3c2;
|
| 18 |
+
--background-light: #e8effa;
|
| 19 |
+
--background-dark: #1c1c1e;
|
| 20 |
+
--text-color-light: #333;
|
| 21 |
+
--text-color-dark: #e0e0e0;
|
| 22 |
+
--shadow-color: rgba(0, 0, 0, 0.1);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
body {
|
| 26 |
font-family: 'Vazir', sans-serif;
|
| 27 |
+
background: var(--background-color);
|
| 28 |
}
|
| 29 |
+
|
| 30 |
.chat-container {
|
| 31 |
max-width: 400px;
|
| 32 |
margin: 0 auto;
|
| 33 |
border-radius: 15px;
|
| 34 |
+
box-shadow: 0px 4px 12px var(--shadow-color);
|
| 35 |
+
overflow: hidden;
|
| 36 |
+
background: var(--background-color);
|
| 37 |
}
|
| 38 |
+
|
| 39 |
.header {
|
| 40 |
+
background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
|
| 41 |
color: white;
|
| 42 |
padding: 10px;
|
| 43 |
text-align: center;
|
| 44 |
font-weight: bold;
|
| 45 |
font-size: 18px;
|
| 46 |
}
|
| 47 |
+
|
| 48 |
+
.chat-body {
|
| 49 |
+
background: var(--neutral-color);
|
| 50 |
+
padding: 15px;
|
| 51 |
+
height: 300px;
|
| 52 |
+
overflow-y: auto;
|
| 53 |
+
display: flex;
|
| 54 |
+
flex-direction: column;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
.message {
|
| 58 |
padding: 12px 15px;
|
| 59 |
margin: 10px 0;
|
|
|
|
| 61 |
width: fit-content;
|
| 62 |
max-width: 80%;
|
| 63 |
font-size: 15px;
|
| 64 |
+
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.05);
|
| 65 |
+
transition: all 0.3s ease;
|
| 66 |
}
|
| 67 |
+
|
| 68 |
.message.user {
|
| 69 |
align-self: flex-end;
|
| 70 |
+
background: var(--primary-color);
|
| 71 |
+
color: var(--text-color-light);
|
| 72 |
}
|
| 73 |
+
|
| 74 |
.message.bot {
|
| 75 |
align-self: flex-start;
|
| 76 |
+
background: var(--secondary-color);
|
| 77 |
color: white;
|
| 78 |
}
|
| 79 |
+
|
| 80 |
.footer {
|
| 81 |
display: flex;
|
| 82 |
padding: 10px;
|
| 83 |
+
background: var(--neutral-color);
|
| 84 |
+
border-top: 1px solid #ddd;
|
| 85 |
align-items: center;
|
| 86 |
}
|
| 87 |
+
|
| 88 |
.footer input[type="text"] {
|
| 89 |
flex: 1;
|
| 90 |
padding: 10px;
|
|
|
|
| 92 |
border-radius: 20px;
|
| 93 |
background: #ffffff;
|
| 94 |
margin-right: 10px;
|
| 95 |
+
font-size: 14px;
|
| 96 |
+
box-shadow: inset 3px 3px 8px rgba(0, 0, 0, 0.1);
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
.footer .icon, .footer .send-btn {
|
| 100 |
+
font-size: 20px;
|
| 101 |
+
color: var(--primary-color);
|
| 102 |
+
cursor: pointer;
|
| 103 |
+
transition: color 0.3s ease, transform 0.2s ease;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
.footer .icon:hover, .footer .send-btn:hover {
|
| 107 |
+
color: var(--secondary-color);
|
| 108 |
+
transform: scale(1.1);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
.admin-icon {
|
| 112 |
+
font-size: 40px;
|
| 113 |
+
color: var(--primary-color);
|
| 114 |
+
cursor: pointer;
|
| 115 |
+
text-align: center;
|
| 116 |
+
margin-top: 10px;
|
| 117 |
+
transition: color 0.3s ease;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
.admin-icon:hover {
|
| 121 |
+
color: var(--secondary-color);
|
| 122 |
+
transform: scale(1.1);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
h1, h2 {
|
| 126 |
+
font-size: 20px;
|
| 127 |
+
color: var(--primary-color);
|
| 128 |
+
text-align: center;
|
| 129 |
+
margin-top: 15px;
|
| 130 |
+
font-weight: bold;
|
| 131 |
}
|
| 132 |
</style>
|
| 133 |
"""
|
| 134 |
+
|
| 135 |
+
# اعمال CSS سفارشی
|
| 136 |
st.markdown(CUSTOM_CSS, unsafe_allow_html=True)
|
| 137 |
|
| 138 |
class ModernChatbot:
|
| 139 |
def __init__(self):
|
| 140 |
+
self.conversation_history = []
|
| 141 |
+
self.admin_logged_in = False
|
| 142 |
+
|
| 143 |
+
def chat_response(self, message):
|
| 144 |
+
"""دریافت پیام کاربر و اضافه کردن پاسخ به تاریخچه گفتگو"""
|
| 145 |
+
time.sleep(0.5)
|
| 146 |
+
self.conversation_history.append(("شما", message))
|
| 147 |
+
|
| 148 |
+
responses = [
|
| 149 |
+
"این یک پاسخ نمونه از ربات است.",
|
| 150 |
+
"چه کمکی از من بر میآید؟",
|
| 151 |
+
"لطفاً بیشتر توضیح دهید.",
|
| 152 |
+
"متشکرم از پیامتان!"
|
| 153 |
+
]
|
| 154 |
+
bot_message = random.choice(responses)
|
| 155 |
+
self.conversation_history.append(("ربات", bot_message))
|
| 156 |
+
|
| 157 |
+
return self.conversation_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
def login_admin(self, username, password):
|
| 160 |
+
if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
|
| 161 |
+
self.admin_logged_in = True
|
| 162 |
+
return True
|
| 163 |
+
else:
|
| 164 |
+
return False
|
| 165 |
|
| 166 |
+
def clear_history(self):
|
| 167 |
+
self.conversation_history = []
|
|
|
|
| 168 |
|
| 169 |
+
# ایجاد چتبات
|
| 170 |
chatbot = ModernChatbot()
|
| 171 |
|
| 172 |
+
# حالت روشن یا تیره
|
| 173 |
+
dark_mode = st.sidebar.checkbox("فعال کردن حالت تیره")
|
| 174 |
+
|
| 175 |
+
# تغییر تم بر اساس حالت انتخاب شده
|
| 176 |
+
background_color = "--background-dark" if dark_mode else "--background-light"
|
| 177 |
+
text_color = "--text-color-dark" if dark_mode else "--text-color-light"
|
| 178 |
+
st.markdown(f"<style>:root {{ --background-color: var({background_color}); --text-color: var({text_color}); }}</style>", unsafe_allow_html=True)
|
| 179 |
+
|
| 180 |
+
# آیکون ورود ادمین و نمایش پاپآپ
|
| 181 |
+
st.markdown("<div class='admin-icon'>👤</div>", unsafe_allow_html=True)
|
| 182 |
|
| 183 |
+
if "show_popup" not in st.session_state:
|
| 184 |
+
st.session_state["show_popup"] = False
|
| 185 |
+
|
| 186 |
+
if "admin_logged_in" not in st.session_state:
|
| 187 |
+
st.session_state["admin_logged_in"] = False
|
| 188 |
+
|
| 189 |
+
if st.session_state["show_popup"]:
|
| 190 |
+
with st.container():
|
| 191 |
+
st.markdown("<div class='popup-form'><h2 style='color: var(--primary-color); text-align:center;'>ورود ادمین</h2>", unsafe_allow_html=True)
|
| 192 |
+
username = st.text_input("نام کاربری")
|
| 193 |
+
password = st.text_input("رمز عبور", type="password")
|
| 194 |
+
if st.button("ورود"):
|
| 195 |
+
if chatbot.login_admin(username, password):
|
| 196 |
+
st.session_state["admin_logged_in"] = True
|
| 197 |
+
st.session_state["show_popup"] = False
|
| 198 |
+
st.success("ورود موفقیتآمیز بود!")
|
| 199 |
+
st.experimental_rerun()
|
| 200 |
+
else:
|
| 201 |
+
st.error("نام کاربری یا رمز عبور اشتباه است")
|
| 202 |
+
else:
|
| 203 |
+
if st.button("ورود به ادمین"):
|
| 204 |
+
st.session_state["show_popup"] = True
|
| 205 |
+
|
| 206 |
+
if st.session_state["admin_logged_in"]:
|
| 207 |
+
st.subheader("داشبورد مدیر")
|
| 208 |
+
if st.button("🗑️ حذف تاریخچه گفتگو"):
|
| 209 |
+
chatbot.clear_history()
|
| 210 |
+
st.success("تاریخچه گفتگو حذف شد.")
|
| 211 |
+
st.experimental_rerun()
|
| 212 |
+
|
| 213 |
+
# لینک به فایل داشبورد
|
| 214 |
+
if os.path.exists("admin_dashboard.py"):
|
| 215 |
+
st.markdown("[ورود به داشبورد](./admin_dashboard.py)", unsafe_allow_html=True)
|
| 216 |
+
|
| 217 |
+
# نمایش چتباکس
|
| 218 |
+
st.markdown("<div class='chat-container'><div class='header'>Chat with us</div><div class='chat-body'>", unsafe_allow_html=True)
|
| 219 |
+
|
| 220 |
+
# نمایش تاریخچه گفتگو
|
| 221 |
+
for sender, message in chatbot.conversation_history:
|
| 222 |
align_class = "user" if sender == "شما" else "bot"
|
| 223 |
+
st.markdown(f"<div class='message {align_class}'><strong>{sender}:</strong> {message}</div>", unsafe_allow_html=True)
|
| 224 |
+
|
| 225 |
+
st.markdown("</div><div class='footer'><input type='text' placeholder='Type a message...' id='user_message'/><span class='icon'>📎</span><span class='send-btn'>🚀</span></div></div>", unsafe_allow_html=True)
|
| 226 |
+
|
| 227 |
+
user_message = st.text_input("پیام خود را اینجا بنویسید...", key="user_message", label_visibility="collapsed")
|
| 228 |
+
|
| 229 |
+
if st.button("ارسال"):
|
| 230 |
+
if user_message:
|
| 231 |
+
chatbot.chat_response(user_message)
|
| 232 |
+
st.experimental_rerun()
|
| 233 |
|
| 234 |
+
if st.button("پاک کردن گفتگو") and not chatbot.admin_logged_in:
|
| 235 |
+
chatbot.clear_history()
|
|
|
|
| 236 |
st.experimental_rerun()
|