Spaces:
Sleeping
Sleeping
Really-amin
commited on
Commit
•
2edf006
1
Parent(s):
5dd3a3f
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,58 @@
|
|
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-
|
12 |
-
--
|
13 |
-
--
|
14 |
-
--
|
15 |
-
--
|
16 |
-
--
|
17 |
-
--text-color: #333;
|
18 |
}
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
|
25 |
-
.chat-
|
26 |
-
background: var(--
|
27 |
-
backdrop-filter: blur(4px);
|
28 |
-
-webkit-backdrop-filter: blur(4px);
|
29 |
border-radius: 20px;
|
30 |
-
|
31 |
-
box-shadow: 0 8px
|
32 |
-
|
33 |
-
|
|
|
34 |
}
|
35 |
|
36 |
.message {
|
37 |
display: flex;
|
38 |
-
margin:
|
39 |
-
animation: fadeIn 0.
|
40 |
}
|
41 |
|
42 |
.message-content {
|
43 |
-
padding: 15px 20px;
|
44 |
-
border-radius: 15px;
|
45 |
max-width: 80%;
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
}
|
50 |
|
51 |
.user-message {
|
@@ -55,8 +62,8 @@ MODERN_CSS = """
|
|
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 {
|
@@ -64,51 +71,62 @@ MODERN_CSS = """
|
|
64 |
}
|
65 |
|
66 |
.bot-message .message-content {
|
67 |
-
background:
|
68 |
-
|
69 |
-
color: var(--text-color);
|
70 |
border-bottom-left-radius: 5px;
|
71 |
}
|
72 |
|
73 |
-
.
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
border-radius: 15px;
|
77 |
-
|
78 |
-
|
79 |
-
margin-top: 20px;
|
80 |
-
display: flex;
|
81 |
-
align-items: center;
|
82 |
}
|
83 |
|
84 |
.stTextInput input {
|
85 |
-
|
86 |
-
border: none !important;
|
87 |
-
padding: 10px 15px !important;
|
88 |
border-radius: 10px !important;
|
89 |
-
|
90 |
-
|
|
|
91 |
direction: rtl;
|
92 |
}
|
93 |
|
|
|
|
|
|
|
|
|
|
|
94 |
.stButton > button {
|
95 |
-
background:
|
96 |
color: white !important;
|
97 |
-
border: none !important;
|
98 |
border-radius: 10px !important;
|
99 |
-
padding:
|
100 |
font-family: 'Vazirmatn', sans-serif !important;
|
101 |
-
|
102 |
-
|
103 |
-
|
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 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
}
|
113 |
|
114 |
@keyframes fadeIn {
|
@@ -116,127 +134,172 @@ MODERN_CSS = """
|
|
116 |
to { opacity: 1; transform: translateY(0); }
|
117 |
}
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
border-radius: 15px;
|
124 |
-
border: 1px solid var(--glass-border);
|
125 |
-
padding: 20px;
|
126 |
-
margin: 20px 0;
|
127 |
}
|
128 |
|
129 |
-
.
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
}
|
134 |
</style>
|
135 |
"""
|
136 |
|
137 |
-
class
|
138 |
def __init__(self):
|
139 |
if 'messages' not in st.session_state:
|
140 |
st.session_state.messages = []
|
141 |
-
if '
|
142 |
-
st.session_state.
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
def chat_response(self, message):
|
145 |
if not message.strip():
|
146 |
return
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
"
|
152 |
-
"
|
153 |
-
|
154 |
-
|
155 |
-
st.session_state.messages.append({"role": "user", "content": message})
|
156 |
|
157 |
# شبیهسازی تایپ کردن ربات
|
158 |
with st.empty():
|
159 |
-
for
|
160 |
-
time.sleep(0.
|
161 |
-
st.write("
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
bot_response = random.choice(responses)
|
164 |
-
st.session_state.messages.append({
|
165 |
-
|
166 |
-
|
167 |
-
|
|
|
|
|
|
|
168 |
|
169 |
def main():
|
170 |
st.set_page_config(
|
171 |
-
page_title="چتبات هوشمند",
|
172 |
-
page_icon="
|
173 |
-
layout="
|
174 |
-
initial_sidebar_state="expanded"
|
175 |
)
|
176 |
|
177 |
-
|
178 |
-
st.markdown(MODERN_CSS, unsafe_allow_html=True)
|
179 |
|
180 |
-
#
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
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.
|
212 |
-
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
|
213 |
|
214 |
-
|
215 |
-
|
216 |
-
message_class = "user-message" if msg["role"] == "user" else "bot-message"
|
217 |
st.markdown(f"""
|
218 |
-
<div class=
|
219 |
-
<
|
|
|
220 |
</div>
|
221 |
""", unsafe_allow_html=True)
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
if __name__ == "__main__":
|
241 |
-
chatbot =
|
242 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from datetime import datetime
|
3 |
import time
|
4 |
import random
|
5 |
|
6 |
+
CUSTOM_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-bg: #f0f2f5;
|
12 |
+
--chat-bg: #ffffff;
|
13 |
+
--primary-color: #2979ff;
|
14 |
+
--secondary-color: #f50057;
|
15 |
+
--text-primary: #262626;
|
16 |
+
--text-secondary: #666666;
|
|
|
17 |
}
|
18 |
|
19 |
+
body {
|
20 |
+
font-family: 'Vazirmatn', sans-serif !important;
|
21 |
+
background-color: var(--primary-bg) !important;
|
22 |
+
color: var(--text-primary);
|
23 |
+
}
|
24 |
+
|
25 |
+
.main-header {
|
26 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
|
27 |
+
color: white;
|
28 |
+
padding: 1rem;
|
29 |
+
border-radius: 15px;
|
30 |
+
margin-bottom: 2rem;
|
31 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
32 |
+
text-align: center;
|
33 |
}
|
34 |
|
35 |
+
.chat-window {
|
36 |
+
background: var(--chat-bg);
|
|
|
|
|
37 |
border-radius: 20px;
|
38 |
+
padding: 1.5rem;
|
39 |
+
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
|
40 |
+
max-height: 600px;
|
41 |
+
overflow-y: auto;
|
42 |
+
margin-bottom: 2rem;
|
43 |
}
|
44 |
|
45 |
.message {
|
46 |
display: flex;
|
47 |
+
margin-bottom: 1.5rem;
|
48 |
+
animation: fadeIn 0.3s ease-in-out;
|
49 |
}
|
50 |
|
51 |
.message-content {
|
|
|
|
|
52 |
max-width: 80%;
|
53 |
+
padding: 1rem 1.5rem;
|
54 |
+
border-radius: 15px;
|
55 |
+
position: relative;
|
56 |
}
|
57 |
|
58 |
.user-message {
|
|
|
62 |
.user-message .message-content {
|
63 |
background: var(--primary-color);
|
64 |
color: white;
|
|
|
65 |
border-bottom-right-radius: 5px;
|
66 |
+
margin-left: auto;
|
67 |
}
|
68 |
|
69 |
.bot-message {
|
|
|
71 |
}
|
72 |
|
73 |
.bot-message .message-content {
|
74 |
+
background: #f1f1f1;
|
75 |
+
color: var(--text-primary);
|
|
|
76 |
border-bottom-left-radius: 5px;
|
77 |
}
|
78 |
|
79 |
+
.timestamp {
|
80 |
+
font-size: 0.75rem;
|
81 |
+
color: var(--text-secondary);
|
82 |
+
margin-top: 0.25rem;
|
83 |
+
text-align: right;
|
84 |
+
}
|
85 |
+
|
86 |
+
.input-area {
|
87 |
+
background: white;
|
88 |
+
padding: 1rem;
|
89 |
border-radius: 15px;
|
90 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
91 |
+
margin-top: 1rem;
|
|
|
|
|
|
|
92 |
}
|
93 |
|
94 |
.stTextInput input {
|
95 |
+
border: 2px solid #e0e0e0 !important;
|
|
|
|
|
96 |
border-radius: 10px !important;
|
97 |
+
padding: 0.75rem 1rem !important;
|
98 |
+
font-size: 1rem !important;
|
99 |
+
transition: all 0.3s ease;
|
100 |
direction: rtl;
|
101 |
}
|
102 |
|
103 |
+
.stTextInput input:focus {
|
104 |
+
border-color: var(--primary-color) !important;
|
105 |
+
box-shadow: 0 0 0 2px rgba(41, 121, 255, 0.2) !important;
|
106 |
+
}
|
107 |
+
|
108 |
.stButton > button {
|
109 |
+
background: var(--primary-color) !important;
|
110 |
color: white !important;
|
|
|
111 |
border-radius: 10px !important;
|
112 |
+
padding: 0.75rem 2rem !important;
|
113 |
font-family: 'Vazirmatn', sans-serif !important;
|
114 |
+
font-weight: 500 !important;
|
115 |
+
transition: all 0.3s ease !important;
|
116 |
+
border: none !important;
|
|
|
117 |
}
|
118 |
|
119 |
.stButton > button:hover {
|
120 |
transform: translateY(-2px);
|
121 |
+
box-shadow: 0 4px 8px rgba(41, 121, 255, 0.3) !important;
|
122 |
+
}
|
123 |
+
|
124 |
+
.typing-indicator {
|
125 |
+
padding: 1rem;
|
126 |
+
background: #f1f1f1;
|
127 |
+
border-radius: 10px;
|
128 |
+
margin-bottom: 1rem;
|
129 |
+
animation: pulse 1.5s infinite;
|
130 |
}
|
131 |
|
132 |
@keyframes fadeIn {
|
|
|
134 |
to { opacity: 1; transform: translateY(0); }
|
135 |
}
|
136 |
|
137 |
+
@keyframes pulse {
|
138 |
+
0% { opacity: 0.5; }
|
139 |
+
50% { opacity: 1; }
|
140 |
+
100% { opacity: 0.5; }
|
|
|
|
|
|
|
|
|
141 |
}
|
142 |
|
143 |
+
.status-bar {
|
144 |
+
background: rgba(255, 255, 255, 0.9);
|
145 |
+
padding: 0.5rem 1rem;
|
146 |
+
border-radius: 10px;
|
147 |
+
margin-bottom: 1rem;
|
148 |
+
display: flex;
|
149 |
+
align-items: center;
|
150 |
+
justify-content: space-between;
|
151 |
+
font-size: 0.9rem;
|
152 |
+
color: var(--text-secondary);
|
153 |
+
}
|
154 |
+
|
155 |
+
.prompt-suggestions {
|
156 |
+
display: flex;
|
157 |
+
gap: 0.5rem;
|
158 |
+
flex-wrap: wrap;
|
159 |
+
margin: 1rem 0;
|
160 |
+
}
|
161 |
+
|
162 |
+
.prompt-suggestion {
|
163 |
+
background: #f1f1f1;
|
164 |
+
padding: 0.5rem 1rem;
|
165 |
+
border-radius: 20px;
|
166 |
+
font-size: 0.9rem;
|
167 |
+
cursor: pointer;
|
168 |
+
transition: all 0.3s ease;
|
169 |
+
}
|
170 |
+
|
171 |
+
.prompt-suggestion:hover {
|
172 |
+
background: var(--primary-color);
|
173 |
+
color: white;
|
174 |
}
|
175 |
</style>
|
176 |
"""
|
177 |
|
178 |
+
class EnhancedChatbot:
|
179 |
def __init__(self):
|
180 |
if 'messages' not in st.session_state:
|
181 |
st.session_state.messages = []
|
182 |
+
if 'last_activity' not in st.session_state:
|
183 |
+
st.session_state.last_activity = datetime.now()
|
184 |
+
|
185 |
+
def format_timestamp(self):
|
186 |
+
return datetime.now().strftime("%H:%M")
|
187 |
+
|
188 |
+
def get_bot_status(self):
|
189 |
+
return "🟢 آنلاین" if (datetime.now() - st.session_state.last_activity).seconds < 300 else "🔴 آفلاین"
|
190 |
+
|
191 |
def chat_response(self, message):
|
192 |
if not message.strip():
|
193 |
return
|
194 |
|
195 |
+
# اضافه کردن پیام کاربر
|
196 |
+
timestamp = self.format_timestamp()
|
197 |
+
st.session_state.messages.append({
|
198 |
+
"role": "user",
|
199 |
+
"content": message,
|
200 |
+
"timestamp": timestamp
|
201 |
+
})
|
|
|
202 |
|
203 |
# شبیهسازی تایپ کردن ربات
|
204 |
with st.empty():
|
205 |
+
for _ in range(3):
|
206 |
+
time.sleep(0.5)
|
207 |
+
st.write("در حال تایپ...")
|
208 |
|
209 |
+
# پاسخ ربات
|
210 |
+
responses = [
|
211 |
+
"بله، متوجه شدم. چطور میتونم کمکتون کنم؟",
|
212 |
+
"این موضوع جالبیه! میتونید بیشتر توضیح بدید؟",
|
213 |
+
"من اینجا هستم تا به شما کمک کنم. سوال دیگهای دارید؟",
|
214 |
+
"اجازه بدید بیشتر در این مورد صحبت کنیم...",
|
215 |
+
]
|
216 |
+
|
217 |
bot_response = random.choice(responses)
|
218 |
+
st.session_state.messages.append({
|
219 |
+
"role": "assistant",
|
220 |
+
"content": bot_response,
|
221 |
+
"timestamp": self.format_timestamp()
|
222 |
+
})
|
223 |
+
|
224 |
+
st.session_state.last_activity = datetime.now()
|
225 |
|
226 |
def main():
|
227 |
st.set_page_config(
|
228 |
+
page_title="چتبات هوشمند | پشتیبانی آنلاین",
|
229 |
+
page_icon="💬",
|
230 |
+
layout="wide"
|
|
|
231 |
)
|
232 |
|
233 |
+
st.markdown(CUSTOM_CSS, unsafe_allow_html=True)
|
|
|
234 |
|
235 |
+
# هدر اصلی
|
236 |
+
st.markdown("""
|
237 |
+
<div class="main-header">
|
238 |
+
<h1>🤖 چتبات هوشمند</h1>
|
239 |
+
<p>پاسخگوی 24 ساعته شما هستیم</p>
|
240 |
+
</div>
|
241 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
|
243 |
+
# ستونبندی صفحه
|
244 |
+
col1, col2 = st.columns([3, 1])
|
|
|
245 |
|
246 |
+
with col1:
|
247 |
+
# نوار وضعیت
|
|
|
248 |
st.markdown(f"""
|
249 |
+
<div class="status-bar">
|
250 |
+
<span>{chatbot.get_bot_status()}</span>
|
251 |
+
<span>آخرین فعالیت: {chatbot.format_timestamp()}</span>
|
252 |
</div>
|
253 |
""", unsafe_allow_html=True)
|
254 |
+
|
255 |
+
# پنجره چت
|
256 |
+
st.markdown('<div class="chat-window">', unsafe_allow_html=True)
|
257 |
+
|
258 |
+
for msg in st.session_state.messages:
|
259 |
+
message_class = "user-message" if msg["role"] == "user" else "bot-message"
|
260 |
+
st.markdown(f"""
|
261 |
+
<div class="message {message_class}">
|
262 |
+
<div class="message-content">
|
263 |
+
{msg["content"]}
|
264 |
+
<div class="timestamp">{msg["timestamp"]}</div>
|
265 |
+
</div>
|
266 |
+
</div>
|
267 |
+
""", unsafe_allow_html=True)
|
268 |
+
|
269 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
270 |
+
|
271 |
+
# بخش ورودی پیام
|
272 |
+
st.markdown('<div class="input-area">', unsafe_allow_html=True)
|
273 |
+
|
274 |
+
# پیشنهادات سریع
|
275 |
+
suggestions = ["سلام", "درباره خدمات", "تماس با ما", "قیمتها"]
|
276 |
+
st.markdown('<div class="prompt-suggestions">', unsafe_allow_html=True)
|
277 |
+
for suggestion in suggestions:
|
278 |
+
st.markdown(f'<span class="prompt-suggestion">{suggestion}</span>', unsafe_allow_html=True)
|
279 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
280 |
+
|
281 |
+
# ورودی پیام
|
282 |
user_input = st.text_input("", placeholder="پیام خود را اینجا بنویسید...", key="user_input")
|
283 |
+
if st.button("ارسال"):
|
|
|
|
|
284 |
if user_input:
|
285 |
chatbot.chat_response(user_input)
|
286 |
st.experimental_rerun()
|
287 |
+
|
288 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
289 |
|
290 |
+
# ستون اطلاعات جانبی
|
291 |
+
with col2:
|
292 |
+
st.markdown("""
|
293 |
+
<div style="background: white; padding: 1rem; border-radius: 15px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
294 |
+
<h3>💡 راهنمای استفاده</h3>
|
295 |
+
<ul>
|
296 |
+
<li>برای شروع گفتگو کافیست پیام خود را تایپ کنید</li>
|
297 |
+
<li>از پیشنهادات سریع بالای صفحه استفاده کنید</li>
|
298 |
+
<li>پشتیبانی 24 ساعته در خدمت شماست</li>
|
299 |
+
</ul>
|
300 |
+
</div>
|
301 |
+
""", unsafe_allow_html=True)
|
302 |
|
303 |
if __name__ == "__main__":
|
304 |
+
chatbot = EnhancedChatbot()
|
305 |
main()
|