Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,236 +1,242 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
| 3 |
import random
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
# تنظیمات
|
| 7 |
-
|
| 8 |
-
ADMIN_PASSWORD = "password"
|
| 9 |
-
|
| 10 |
-
# CSS سفارشی برای ظاهر چتبات و حالت تیره و روشن
|
| 11 |
-
CUSTOM_CSS = """
|
| 12 |
<style>
|
| 13 |
-
:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
.chat-container {
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 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 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 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 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
def chat_response(self, message):
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
responses = [
|
| 149 |
-
"
|
| 150 |
-
"
|
| 151 |
-
"
|
| 152 |
-
"
|
| 153 |
]
|
| 154 |
-
bot_message = random.choice(responses)
|
| 155 |
-
self.conversation_history.append(("ربات", bot_message))
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
|
| 166 |
def clear_history(self):
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
st.markdown(
|
| 179 |
-
|
| 180 |
-
#
|
| 181 |
-
st.
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
if st.
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
st.experimental_rerun()
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
st.
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
st.experimental_rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
+
# تنظیمات استایل مدرن با گلس مورفیسم و نئومورفیسم
|
| 6 |
+
MODERN_CSS = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
<style>
|
| 8 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;700&display=swap');
|
| 9 |
+
|
| 10 |
+
:root {
|
| 11 |
+
--primary-color: rgba(74, 144, 226, 0.9);
|
| 12 |
+
--secondary-color: rgba(255, 107, 107, 0.9);
|
| 13 |
+
--bg-color: rgba(240, 242, 245, 1);
|
| 14 |
+
--glass-bg: rgba(255, 255, 255, 0.25);
|
| 15 |
+
--glass-border: rgba(255, 255, 255, 0.18);
|
| 16 |
+
--shadow-color: rgba(31, 38, 135, 0.37);
|
| 17 |
+
--text-color: #333;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
.stApp {
|
| 21 |
+
background: linear-gradient(45deg, #e6e9f0 0%, #eef1f5 100%);
|
| 22 |
+
font-family: 'Vazirmatn', sans-serif;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.chat-container {
|
| 26 |
+
background: var(--glass-bg);
|
| 27 |
+
backdrop-filter: blur(4px);
|
| 28 |
+
-webkit-backdrop-filter: blur(4px);
|
| 29 |
+
border-radius: 20px;
|
| 30 |
+
border: 1px solid var(--glass-border);
|
| 31 |
+
box-shadow: 0 8px 32px 0 var(--shadow-color);
|
| 32 |
+
padding: 20px;
|
| 33 |
+
margin: 20px 0;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.message {
|
| 37 |
+
display: flex;
|
| 38 |
+
margin: 15px 0;
|
| 39 |
+
animation: fadeIn 0.5s ease-out;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
.message-content {
|
| 43 |
+
padding: 15px 20px;
|
| 44 |
+
border-radius: 15px;
|
| 45 |
+
max-width: 80%;
|
| 46 |
+
box-shadow:
|
| 47 |
+
8px 8px 16px rgba(174, 174, 192, 0.4),
|
| 48 |
+
-8px -8px 16px rgba(255, 255, 255, 0.65);
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
.user-message {
|
| 52 |
+
justify-content: flex-end;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
.user-message .message-content {
|
| 56 |
+
background: var(--primary-color);
|
| 57 |
+
color: white;
|
| 58 |
+
margin-left: auto;
|
| 59 |
+
border-bottom-right-radius: 5px;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
.bot-message {
|
| 63 |
+
justify-content: flex-start;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
.bot-message .message-content {
|
| 67 |
+
background: var(--glass-bg);
|
| 68 |
+
border: 1px solid var(--glass-border);
|
| 69 |
+
color: var(--text-color);
|
| 70 |
+
border-bottom-left-radius: 5px;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.input-container {
|
| 74 |
+
background: var(--glass-bg);
|
| 75 |
+
backdrop-filter: blur(4px);
|
| 76 |
+
border-radius: 15px;
|
| 77 |
+
border: 1px solid var(--glass-border);
|
| 78 |
+
padding: 10px;
|
| 79 |
+
margin-top: 20px;
|
| 80 |
+
display: flex;
|
| 81 |
+
align-items: center;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.stTextInput input {
|
| 85 |
+
background: rgba(255, 255, 255, 0.1) !important;
|
| 86 |
+
border: none !important;
|
| 87 |
+
padding: 10px 15px !important;
|
| 88 |
+
border-radius: 10px !important;
|
| 89 |
+
color: var(--text-color) !important;
|
| 90 |
+
backdrop-filter: blur(4px) !important;
|
| 91 |
+
direction: rtl;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
.stButton > button {
|
| 95 |
+
background: linear-gradient(45deg, var(--primary-color), var(--secondary-color)) !important;
|
| 96 |
+
color: white !important;
|
| 97 |
+
border: none !important;
|
| 98 |
+
border-radius: 10px !important;
|
| 99 |
+
padding: 10px 25px !important;
|
| 100 |
+
font-family: 'Vazirmatn', sans-serif !important;
|
| 101 |
+
transition: transform 0.3s ease, box-shadow 0.3s ease !important;
|
| 102 |
+
box-shadow:
|
| 103 |
+
4px 4px 8px rgba(174, 174, 192, 0.4),
|
| 104 |
+
-4px -4px 8px rgba(255, 255, 255, 0.65) !important;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
.stButton > button:hover {
|
| 108 |
+
transform: translateY(-2px);
|
| 109 |
+
box-shadow:
|
| 110 |
+
6px 6px 12px rgba(174, 174, 192, 0.4),
|
| 111 |
+
-6px -6px 12px rgba(255, 255, 255, 0.65) !important;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
@keyframes fadeIn {
|
| 115 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 116 |
+
to { opacity: 1; transform: translateY(0); }
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/* Admin Panel Styling */
|
| 120 |
+
.admin-panel {
|
| 121 |
+
background: var(--glass-bg);
|
| 122 |
+
backdrop-filter: blur(4px);
|
| 123 |
+
border-radius: 15px;
|
| 124 |
+
border: 1px solid var(--glass-border);
|
| 125 |
+
padding: 20px;
|
| 126 |
+
margin: 20px 0;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
.admin-panel h3 {
|
| 130 |
+
color: var(--primary-color);
|
| 131 |
+
font-weight: 500;
|
| 132 |
+
margin-bottom: 15px;
|
| 133 |
+
}
|
| 134 |
</style>
|
| 135 |
"""
|
| 136 |
|
|
|
|
|
|
|
|
|
|
| 137 |
class ModernChatbot:
|
| 138 |
def __init__(self):
|
| 139 |
+
if 'messages' not in st.session_state:
|
| 140 |
+
st.session_state.messages = []
|
| 141 |
+
if 'admin_logged_in' not in st.session_state:
|
| 142 |
+
st.session_state.admin_logged_in = False
|
| 143 |
+
|
| 144 |
def chat_response(self, message):
|
| 145 |
+
if not message.strip():
|
| 146 |
+
return
|
| 147 |
+
|
|
|
|
| 148 |
responses = [
|
| 149 |
+
"چطور میتونم کمکتون کنم؟",
|
| 150 |
+
"متوجه شدم. لطفاً بیشتر توضیح بدید.",
|
| 151 |
+
"بله، کاملاً درسته. میتونم راهنماییتون کنم.",
|
| 152 |
+
"این موضوع جالبیه. نظر شما چیه؟"
|
| 153 |
]
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
st.session_state.messages.append({"role": "user", "content": message})
|
| 156 |
+
|
| 157 |
+
# شبیهسازی تایپ کردن ربات
|
| 158 |
+
with st.empty():
|
| 159 |
+
for i in range(3):
|
| 160 |
+
time.sleep(0.3)
|
| 161 |
+
st.write("..." + "." * i)
|
| 162 |
+
|
| 163 |
+
bot_response = random.choice(responses)
|
| 164 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_response})
|
| 165 |
|
| 166 |
def clear_history(self):
|
| 167 |
+
st.session_state.messages = []
|
| 168 |
+
|
| 169 |
+
def main():
|
| 170 |
+
st.set_page_config(
|
| 171 |
+
page_title="چتبات هوشمند",
|
| 172 |
+
page_icon="🤖",
|
| 173 |
+
layout="centered",
|
| 174 |
+
initial_sidebar_state="expanded"
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# اعمال CSS سفارشی
|
| 178 |
+
st.markdown(MODERN_CSS, unsafe_allow_html=True)
|
| 179 |
+
|
| 180 |
+
# پنل مدیریت در سایدبار
|
| 181 |
+
with st.sidebar:
|
| 182 |
+
st.markdown("<div class='admin-panel'>", unsafe_allow_html=True)
|
| 183 |
+
st.header("🔐 پنل مدیریت")
|
| 184 |
+
|
| 185 |
+
if not st.session_state.admin_logged_in:
|
| 186 |
+
admin_username = st.text_input("نام کاربری", placeholder="نام کاربری را وارد کنید")
|
| 187 |
+
admin_password = st.text_input("رمز عبور", type="password", placeholder="رمز عبور را وارد کنید")
|
| 188 |
+
|
| 189 |
+
if st.button("ورود", key="login_button"):
|
| 190 |
+
if admin_username == "admin" and admin_password == "password":
|
| 191 |
+
st.session_state.admin_logged_in = True
|
| 192 |
+
st.success("ورود موفقیتآمیز بود! 🎉")
|
| 193 |
+
st.experimental_rerun()
|
| 194 |
+
else:
|
| 195 |
+
st.error("نام کاربری یا رمز عبور اشتباه است! ❌")
|
| 196 |
+
|
| 197 |
+
else:
|
| 198 |
+
st.success("شما وارد شدهاید! 👋")
|
| 199 |
+
if st.button("پاک کردن تاریخچه گفتگو", key="clear_history"):
|
| 200 |
+
chatbot.clear_history()
|
| 201 |
+
st.success("تاریخچه گفتگو پاک شد! 🗑️")
|
| 202 |
+
st.experimental_rerun()
|
| 203 |
+
|
| 204 |
+
if st.button("خروج", key="logout"):
|
| 205 |
+
st.session_state.admin_logged_in = False
|
| 206 |
st.experimental_rerun()
|
| 207 |
+
|
| 208 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 209 |
+
|
| 210 |
+
# بخش اصلی چت
|
| 211 |
+
st.title("💬 چتبات هوشمند")
|
| 212 |
+
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
|
| 213 |
+
|
| 214 |
+
# نمایش پیامها
|
| 215 |
+
for msg in st.session_state.messages:
|
| 216 |
+
message_class = "user-message" if msg["role"] == "user" else "bot-message"
|
| 217 |
+
st.markdown(f"""
|
| 218 |
+
<div class='message {message_class}'>
|
| 219 |
+
<div class='message-content'>{msg["content"]}</div>
|
| 220 |
+
</div>
|
| 221 |
+
""", unsafe_allow_html=True)
|
| 222 |
+
|
| 223 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 224 |
+
|
| 225 |
+
# بخش ورود پیام
|
| 226 |
+
st.markdown("<div class='input-container'>", unsafe_allow_html=True)
|
| 227 |
+
col1, col2 = st.columns([4, 1])
|
| 228 |
+
|
| 229 |
+
with col1:
|
| 230 |
+
user_input = st.text_input("", placeholder="پیام خود را اینجا بنویسید...", key="user_input")
|
| 231 |
+
|
| 232 |
+
with col2:
|
| 233 |
+
if st.button("ارسال", key="send"):
|
| 234 |
+
if user_input:
|
| 235 |
+
chatbot.chat_response(user_input)
|
| 236 |
+
st.experimental_rerun()
|
| 237 |
+
|
| 238 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 239 |
+
|
| 240 |
+
if __name__ == "__main__":
|
| 241 |
+
chatbot = ModernChatbot()
|
| 242 |
+
main()
|
|
|