Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- admin_dashboard.py +190 -0
- admin_dashboard_filemanager.py +364 -0
- app.py +236 -236
admin_dashboard.py
ADDED
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import subprocess
|
3 |
+
import time
|
4 |
+
import pandas as pd
|
5 |
+
import plotly.express as px
|
6 |
+
import random
|
7 |
+
from datetime import datetime
|
8 |
+
from sklearn.linear_model import LogisticRegression
|
9 |
+
from sklearn.datasets import make_classification
|
10 |
+
from sklearn.model_selection import train_test_split
|
11 |
+
from sklearn.metrics import accuracy_score
|
12 |
+
|
13 |
+
# کلاس برای یادگیری ماشین
|
14 |
+
class MLEngine:
|
15 |
+
def __init__(self):
|
16 |
+
# ایجاد دادههای آموزشی شبیهسازی شده
|
17 |
+
self.X, self.y = make_classification(n_samples=1000, n_features=10, random_state=42)
|
18 |
+
self.model = LogisticRegression()
|
19 |
+
self.training_history = []
|
20 |
+
self.last_update_time = None
|
21 |
+
self.current_accuracy = 0
|
22 |
+
self.total_interactions = 1234
|
23 |
+
self.user_satisfaction = 95
|
24 |
+
|
25 |
+
def train_model(self):
|
26 |
+
"""آموزش مدل و ذخیره دقت آن"""
|
27 |
+
# تقسیم دادهها به مجموعههای آموزشی و آزمایشی
|
28 |
+
X_train, X_test, y_train, y_test = train_test_split(self.X, self.y, test_size=0.2, random_state=42)
|
29 |
+
self.model.fit(X_train, y_train)
|
30 |
+
|
31 |
+
# پیشبینی و محاسبه دقت
|
32 |
+
predictions = self.model.predict(X_test)
|
33 |
+
accuracy = accuracy_score(y_test, predictions)
|
34 |
+
|
35 |
+
# ذخیره دقت در تاریخچه آموزشی
|
36 |
+
self.current_accuracy = accuracy * 100 # دقت به درصد
|
37 |
+
self.training_history.append({"timestamp": datetime.now(), "accuracy": self.current_accuracy})
|
38 |
+
self.last_update_time = datetime.now()
|
39 |
+
|
40 |
+
def update_knowledge_base(self):
|
41 |
+
"""شبیهسازی بهروزرسانی پایگاه دانش"""
|
42 |
+
time.sleep(2) # شبیهسازی زمان بهروزرسانی پایگاه دانش
|
43 |
+
self.train_model() # آموزش مجدد مدل به عنوان بخشی از بهروزرسانی
|
44 |
+
|
45 |
+
def get_learning_stats(self):
|
46 |
+
"""برگرداندن آمار یادگیری شامل دقت و تاریخچه آموزشی"""
|
47 |
+
return {
|
48 |
+
"history": self.training_history,
|
49 |
+
"currentAccuracy": self.current_accuracy,
|
50 |
+
"totalInteractions": self.total_interactions,
|
51 |
+
"userSatisfaction": self.user_satisfaction,
|
52 |
+
"lastUpdate": self.last_update_time.strftime("%Y-%m-%d %H:%M:%S") if self.last_update_time else "No Update Yet"
|
53 |
+
}
|
54 |
+
|
55 |
+
# ایجاد یک نمونه از کلاس MLEngine
|
56 |
+
ml_engine = MLEngine()
|
57 |
+
ml_engine.train_model() # آموزش اولیه مدل
|
58 |
+
|
59 |
+
# تابع برای اجرای فایل `admin_dashboard_filemanager.py`
|
60 |
+
def open_file_manager():
|
61 |
+
"""اجرای فایل مدیریت فایلها و بهروزرسانی پایگاه دانش"""
|
62 |
+
try:
|
63 |
+
# اجرای فایل `admin_dashboard_filemanager.py`
|
64 |
+
subprocess.Popen(["python", "admin_dashboard_filemanager.py"])
|
65 |
+
st.success("Knowledge Base update has been initiated successfully!")
|
66 |
+
except Exception as e:
|
67 |
+
st.error(f"Error while opening file manager: {e}")
|
68 |
+
|
69 |
+
# CSS سفارشی برای طراحی گلس مورفیسم و نئومورفیسم
|
70 |
+
CUSTOM_CSS = """
|
71 |
+
<style>
|
72 |
+
body {
|
73 |
+
background: linear-gradient(135deg, #ece9e6, #ffffff);
|
74 |
+
font-family: 'Arial', sans-serif;
|
75 |
+
}
|
76 |
+
.dashboard-container {
|
77 |
+
max-width: 800px;
|
78 |
+
margin: auto;
|
79 |
+
padding: 20px;
|
80 |
+
}
|
81 |
+
.glass-card, .glass-button, .glass-chart {
|
82 |
+
backdrop-filter: blur(10px); /* شفافیت پسزمینه */
|
83 |
+
background: rgba(255, 255, 255, 0.15); /* پسزمینه شفاف */
|
84 |
+
border-radius: 20px; /* گوشههای گرد */
|
85 |
+
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.15), -4px -4px 10px rgba(255, 255, 255, 0.7); /* سایههای نئومورفیسم */
|
86 |
+
padding: 20px;
|
87 |
+
margin: 15px 0;
|
88 |
+
transition: all 0.3s ease;
|
89 |
+
}
|
90 |
+
.glass-card:hover, .glass-button:hover {
|
91 |
+
box-shadow: 6px 6px 12px rgba(0, 0, 0, 0.2), -6px -6px 12px rgba(255, 255, 255, 0.5); /* افکت سایه هنگام هاور */
|
92 |
+
transform: translateY(-5px); /* جابجایی جزئی */
|
93 |
+
}
|
94 |
+
.header {
|
95 |
+
text-align: center;
|
96 |
+
color: #333;
|
97 |
+
font-size: 24px;
|
98 |
+
font-weight: bold;
|
99 |
+
padding: 10px 0;
|
100 |
+
}
|
101 |
+
.glass-button {
|
102 |
+
display: inline-block;
|
103 |
+
color: #ffffff;
|
104 |
+
background: linear-gradient(135deg, #4a90e2, #50e3c2);
|
105 |
+
border: none;
|
106 |
+
font-size: 18px;
|
107 |
+
cursor: pointer;
|
108 |
+
text-align: center;
|
109 |
+
width: 100%;
|
110 |
+
padding: 10px 20px;
|
111 |
+
margin-top: 10px;
|
112 |
+
border-radius: 20px;
|
113 |
+
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.15), -4px -4px 10px rgba(255, 255, 255, 0.7); /* سایههای نئومورفیسم */
|
114 |
+
transition: all 0.3s ease;
|
115 |
+
}
|
116 |
+
.glass-button:hover {
|
117 |
+
background: linear-gradient(135deg, #50e3c2, #4a90e2); /* تغییر رنگ هنگام هاور */
|
118 |
+
}
|
119 |
+
.stat-card {
|
120 |
+
text-align: center;
|
121 |
+
color: #333;
|
122 |
+
padding: 15px;
|
123 |
+
}
|
124 |
+
.stat-title {
|
125 |
+
font-size: 16px;
|
126 |
+
color: #666;
|
127 |
+
}
|
128 |
+
.stat-value {
|
129 |
+
font-size: 26px;
|
130 |
+
font-weight: bold;
|
131 |
+
color: #4a90e2;
|
132 |
+
}
|
133 |
+
.glass-upload {
|
134 |
+
background: rgba(255, 255, 255, 0.25); /* پسزمینه شفاف */
|
135 |
+
border: 1px solid rgba(255, 255, 255, 0.3);
|
136 |
+
border-radius: 20px;
|
137 |
+
padding: 20px;
|
138 |
+
box-shadow: inset 4px 4px 6px rgba(0, 0, 0, 0.1), inset -4px -4px 6px rgba(255, 255, 255, 0.5); /* سایه داخلی */
|
139 |
+
}
|
140 |
+
.icon {
|
141 |
+
font-size: 24px;
|
142 |
+
color: #4a90e2;
|
143 |
+
margin-right: 8px;
|
144 |
+
}
|
145 |
+
</style>
|
146 |
+
"""
|
147 |
+
|
148 |
+
# اعمال CSS سفارشی
|
149 |
+
st.markdown(CUSTOM_CSS, unsafe_allow_html=True)
|
150 |
+
|
151 |
+
# ساخت داشبورد
|
152 |
+
st.markdown("<div class='dashboard-container'>", unsafe_allow_html=True)
|
153 |
+
st.markdown("<div class='header'>Admin Dashboard</div>", unsafe_allow_html=True)
|
154 |
+
|
155 |
+
# دکمه بهروزرسانی پایگاه دانش با طراحی گلس مورفیسم
|
156 |
+
if st.button("Update Knowledge Base"):
|
157 |
+
ml_engine.update_knowledge_base()
|
158 |
+
open_file_manager()
|
159 |
+
|
160 |
+
# نمایش آمارها با طراحی نئومورفیسم
|
161 |
+
st.subheader("Statistics")
|
162 |
+
col1, col2, col3 = st.columns(3)
|
163 |
+
|
164 |
+
# نمایش آمارهای فعلی مدل
|
165 |
+
stats = ml_engine.get_learning_stats()
|
166 |
+
col1.markdown(f"<div class='glass-card stat-card'><div class='stat-title'>📊 Accuracy</div><div class='stat-value'>{stats['currentAccuracy']:.2f}%</div></div>", unsafe_allow_html=True)
|
167 |
+
col2.markdown(f"<div class='glass-card stat-card'><div class='stat-title'>👤 Total Interactions</div><div class='stat-value'>{stats['totalInteractions']}</div></div>", unsafe_allow_html=True)
|
168 |
+
col3.markdown(f"<div class='glass-card stat-card'><div class='stat-title'>👍 User Satisfaction</div><div class='stat-value'>{stats['userSatisfaction']}%</div></div>", unsafe_allow_html=True)
|
169 |
+
|
170 |
+
# نمودار روند یادگیری با طراحی شفاف (گلس مورفیسم)
|
171 |
+
st.subheader("Learning Rate Trend")
|
172 |
+
learning_df = pd.DataFrame([
|
173 |
+
{"Date": stat["timestamp"], "Accuracy": stat["accuracy"]}
|
174 |
+
for stat in stats["history"]
|
175 |
+
])
|
176 |
+
fig = px.line(learning_df, x="Date", y="Accuracy", title="Learning Rate Over Time",
|
177 |
+
template="plotly_white", markers=True)
|
178 |
+
fig.update_traces(line=dict(color="#4a90e2", width=2))
|
179 |
+
st.plotly_chart(fig, use_container_width=True)
|
180 |
+
|
181 |
+
# آپلود فایل با طراحی نئومورفیسم
|
182 |
+
st.subheader("Manage Files")
|
183 |
+
st.markdown("<div class='glass-upload'>", unsafe_allow_html=True)
|
184 |
+
uploaded_file = st.file_uploader("Upload Document", type=["txt", "pdf", "docx"])
|
185 |
+
if uploaded_file:
|
186 |
+
st.success("File uploaded successfully!")
|
187 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
188 |
+
|
189 |
+
# پایان داشبورد
|
190 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
admin_dashboard_filemanager.py
ADDED
@@ -0,0 +1,364 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
from pathlib import Path
|
6 |
+
from datetime import datetime
|
7 |
+
import zipfile
|
8 |
+
|
9 |
+
# CSS سفارشی با طراحی گلس مورفیسم و نئومورفیسم
|
10 |
+
CUSTOM_CSS = """
|
11 |
+
.file-manager {
|
12 |
+
border-radius: 20px;
|
13 |
+
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.15), -4px -4px 10px rgba(255, 255, 255, 0.7);
|
14 |
+
backdrop-filter: blur(10px);
|
15 |
+
background-color: rgba(255, 255, 255, 0.2);
|
16 |
+
padding: 20px;
|
17 |
+
}
|
18 |
+
|
19 |
+
.custom-header {
|
20 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
21 |
+
color: white !important;
|
22 |
+
padding: 20px !important;
|
23 |
+
border-radius: 20px;
|
24 |
+
margin: -20px -20px 20px -20px !important;
|
25 |
+
}
|
26 |
+
|
27 |
+
.file-operations {
|
28 |
+
display: flex;
|
29 |
+
gap: 10px;
|
30 |
+
margin: 15px 0;
|
31 |
+
background: rgba(240, 248, 255, 0.7);
|
32 |
+
padding: 15px;
|
33 |
+
border-radius: 20px;
|
34 |
+
box-shadow: inset 4px 4px 6px rgba(0, 0, 0, 0.1), inset -4px -4px 6px rgba(255, 255, 255, 0.5);
|
35 |
+
}
|
36 |
+
|
37 |
+
.file-preview {
|
38 |
+
border-radius: 20px;
|
39 |
+
margin-top: 15px;
|
40 |
+
padding: 20px;
|
41 |
+
box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.1), -4px -4px 8px rgba(255, 255, 255, 0.5);
|
42 |
+
backdrop-filter: blur(5px);
|
43 |
+
background: rgba(255, 255, 255, 0.7);
|
44 |
+
}
|
45 |
+
|
46 |
+
.breadcrumb {
|
47 |
+
background: rgba(241, 245, 249, 0.9);
|
48 |
+
padding: 12px 20px;
|
49 |
+
border-radius: 20px;
|
50 |
+
margin-bottom: 20px;
|
51 |
+
display: flex;
|
52 |
+
align-items: center;
|
53 |
+
gap: 10px;
|
54 |
+
}
|
55 |
+
|
56 |
+
.search-box {
|
57 |
+
margin: 10px 0;
|
58 |
+
padding: 12px;
|
59 |
+
border-radius: 20px;
|
60 |
+
border: 2px solid rgba(102, 126, 234, 0.5);
|
61 |
+
width: 100%;
|
62 |
+
backdrop-filter: blur(5px);
|
63 |
+
transition: all 0.3s ease;
|
64 |
+
}
|
65 |
+
|
66 |
+
.search-box:focus {
|
67 |
+
border-color: #667eea;
|
68 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3);
|
69 |
+
}
|
70 |
+
|
71 |
+
.file-item, .folder-item {
|
72 |
+
transition: all 0.3s ease;
|
73 |
+
padding: 12px 15px;
|
74 |
+
margin: 5px 0;
|
75 |
+
border-radius: 20px;
|
76 |
+
display: flex;
|
77 |
+
justify-content: space-between;
|
78 |
+
align-items: center;
|
79 |
+
cursor: pointer;
|
80 |
+
backdrop-filter: blur(5px);
|
81 |
+
box-shadow: inset 3px 3px 5px rgba(0, 0, 0, 0.1), inset -3px -3px 5px rgba(255, 255, 255, 0.5);
|
82 |
+
}
|
83 |
+
|
84 |
+
.file-item:hover, .folder-item:hover {
|
85 |
+
background-color: rgba(255, 255, 255, 0.8);
|
86 |
+
transform: translateX(5px);
|
87 |
+
}
|
88 |
+
|
89 |
+
.status-message {
|
90 |
+
margin-top: 15px;
|
91 |
+
padding: 12px;
|
92 |
+
border-radius: 20px;
|
93 |
+
animation: slideIn 0.3s ease;
|
94 |
+
background: rgba(255, 255, 255, 0.8);
|
95 |
+
}
|
96 |
+
|
97 |
+
.operation-button {
|
98 |
+
transition: all 0.3s ease !important;
|
99 |
+
border-radius: 20px !important;
|
100 |
+
padding: 8px 16px !important;
|
101 |
+
box-shadow: 4px 4px 6px rgba(0, 0, 0, 0.1), -4px -4px 6px rgba(255, 255, 255, 0.7);
|
102 |
+
}
|
103 |
+
|
104 |
+
.operation-button:hover {
|
105 |
+
transform: translateY(-2px);
|
106 |
+
}
|
107 |
+
|
108 |
+
.preview-title {
|
109 |
+
color: #4a5568;
|
110 |
+
font-size: 1.1em;
|
111 |
+
margin-bottom: 10px;
|
112 |
+
border-bottom: 2px solid rgba(226, 232, 240, 0.8);
|
113 |
+
padding-bottom: 8px;
|
114 |
+
}
|
115 |
+
|
116 |
+
.context-menu {
|
117 |
+
position: absolute;
|
118 |
+
background: rgba(255, 255, 255, 0.95);
|
119 |
+
border-radius: 20px;
|
120 |
+
box-shadow: 4px 4px 6px rgba(0, 0, 0, 0.1), -4px -4px 6px rgba(255, 255, 255, 0.7);
|
121 |
+
padding: 5px 0;
|
122 |
+
z-index: 1000;
|
123 |
+
}
|
124 |
+
"""
|
125 |
+
|
126 |
+
class EnhancedFileManager:
|
127 |
+
def __init__(self):
|
128 |
+
self.root_path = Path('data')
|
129 |
+
self.root_path.mkdir(exist_ok=True)
|
130 |
+
self.clipboard = None
|
131 |
+
self.history = []
|
132 |
+
self.current_path = str(self.root_path)
|
133 |
+
|
134 |
+
def list_files(self, search_query=""):
|
135 |
+
path = Path(self.current_path)
|
136 |
+
files = []
|
137 |
+
folders = []
|
138 |
+
|
139 |
+
try:
|
140 |
+
for item in path.iterdir():
|
141 |
+
if search_query and search_query.lower() not in item.name.lower():
|
142 |
+
continue
|
143 |
+
|
144 |
+
modified_time = datetime.fromtimestamp(item.stat().st_mtime).strftime("%Y-%m-%d %H:%M")
|
145 |
+
|
146 |
+
if item.is_file():
|
147 |
+
size = item.stat().st_size
|
148 |
+
size_str = f"{size/1024/1024:.2f} MB" if size > 1024*1024 else f"{size/1024:.1f} KB"
|
149 |
+
icon = self._get_file_icon(item.suffix.lower())
|
150 |
+
|
151 |
+
files.append({
|
152 |
+
"icon": icon,
|
153 |
+
"name": item.name,
|
154 |
+
"type": "file",
|
155 |
+
"path": str(item),
|
156 |
+
"size": size_str,
|
157 |
+
"modified": modified_time
|
158 |
+
})
|
159 |
+
else:
|
160 |
+
items_count = len(list(item.iterdir()))
|
161 |
+
folders.append({
|
162 |
+
"icon": "📁",
|
163 |
+
"name": item.name,
|
164 |
+
"type": "folder",
|
165 |
+
"path": str(item),
|
166 |
+
"modified": modified_time,
|
167 |
+
"items_count": items_count
|
168 |
+
})
|
169 |
+
|
170 |
+
return {
|
171 |
+
"folders": sorted(folders, key=lambda x: x["name"].lower()),
|
172 |
+
"files": sorted(files, key=lambda x: x["name"].lower())
|
173 |
+
}
|
174 |
+
|
175 |
+
except Exception as e:
|
176 |
+
return {"folders": [], "files": [], "error": str(e)}
|
177 |
+
|
178 |
+
def _get_file_icon(self, extension):
|
179 |
+
icons = {
|
180 |
+
'.pdf': '📕',
|
181 |
+
'.txt': '📄',
|
182 |
+
'.doc': '📝',
|
183 |
+
'.docx': '📝',
|
184 |
+
'.xls': '📊',
|
185 |
+
'.xlsx': '📊',
|
186 |
+
'.jpg': '🖼️',
|
187 |
+
'.jpeg': '🖼️',
|
188 |
+
'.png': '🖼️',
|
189 |
+
'.gif': '🖼️',
|
190 |
+
'.zip': '🗜️',
|
191 |
+
'.rar': '🗜️',
|
192 |
+
'.mp3': '🎵',
|
193 |
+
'.mp4': '🎥',
|
194 |
+
'.py': '🐍',
|
195 |
+
'.js': '📜',
|
196 |
+
'.html': '🌐',
|
197 |
+
'.css': '🎨'
|
198 |
+
}
|
199 |
+
return icons.get(extension, '📄')
|
200 |
+
|
201 |
+
def create_folder(self, name):
|
202 |
+
if not name:
|
203 |
+
return False, self._create_status("لطفاً نام پوشه را وارد کنید", "error")
|
204 |
+
|
205 |
+
try:
|
206 |
+
new_folder = Path(self.current_path) / name
|
207 |
+
if new_folder.exists():
|
208 |
+
return False, self._create_status("پوشهای با این نام قبلاً وجود دارد", "error")
|
209 |
+
|
210 |
+
new_folder.mkdir(parents=True)
|
211 |
+
return True, self._create_status("پوشه با موفقیت ایجاد شد", "success")
|
212 |
+
except Exception as e:
|
213 |
+
return False, self._create_status(f"خطا در ایجاد پوشه: {str(e)}", "error")
|
214 |
+
|
215 |
+
def upload_file(self, file):
|
216 |
+
if file is None:
|
217 |
+
return False, self._create_status("لطفاً یک فایل انتخاب کنید", "error")
|
218 |
+
|
219 |
+
try:
|
220 |
+
dest_path = Path(self.current_path) / file.name
|
221 |
+
if dest_path.exists():
|
222 |
+
return False, self._create_status("فایلی با این نام قبلاً وجود دارد", "error")
|
223 |
+
|
224 |
+
shutil.copy2(file.name, dest_path)
|
225 |
+
return True, self._create_status("فایل با موفقیت آپلود شد", "success")
|
226 |
+
except Exception as e:
|
227 |
+
return False, self._create_status(f"خطا در آپلود: {str(e)}", "error")
|
228 |
+
|
229 |
+
def delete_item(self, path):
|
230 |
+
try:
|
231 |
+
path = Path(path)
|
232 |
+
if path.is_file():
|
233 |
+
path.unlink()
|
234 |
+
else:
|
235 |
+
shutil.rmtree(path)
|
236 |
+
return True, self._create_status("آیتم با موفقیت حذف شد", "success")
|
237 |
+
except Exception as e:
|
238 |
+
return False, self._create_status(f"خطا در حذف: {str(e)}", "error")
|
239 |
+
|
240 |
+
def compress_items(self, items):
|
241 |
+
try:
|
242 |
+
if not items:
|
243 |
+
return False, self._create_status("لطفاً حداقل یک آیتم انتخاب کنید", "error")
|
244 |
+
|
245 |
+
archive_name = f"archive_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
246 |
+
archive_path = Path(self.current_path) / f"{archive_name}.zip"
|
247 |
+
|
248 |
+
with zipfile.ZipFile(archive_path, 'w') as zipf:
|
249 |
+
for item in items:
|
250 |
+
item_path = Path(item)
|
251 |
+
if item_path.is_file():
|
252 |
+
zipf.write(item_path, item_path.name)
|
253 |
+
else:
|
254 |
+
for root, dirs, files in os.walk(item_path):
|
255 |
+
for file in files:
|
256 |
+
file_path = Path(root) / file
|
257 |
+
zipf.write(file_path, file_path.relative_to(item_path.parent))
|
258 |
+
|
259 |
+
return True, self._create_status("فایل فشرده با موفقیت ایجاد شد", "success")
|
260 |
+
except Exception as e:
|
261 |
+
return False, self._create_status(f"خطا در فشردهسازی: {str(e)}", "error")
|
262 |
+
|
263 |
+
def _create_status(self, message, status="info"):
|
264 |
+
color_map = {
|
265 |
+
"success": "#48bb78",
|
266 |
+
"error": "#f56565",
|
267 |
+
"info": "#4299e1",
|
268 |
+
"warning": "#ecc94b"
|
269 |
+
}
|
270 |
+
color = color_map.get(status, color_map["info"])
|
271 |
+
|
272 |
+
return f"""
|
273 |
+
<div class="status-message" style="
|
274 |
+
background-color: {color}10;
|
275 |
+
border-right: 4px solid {color};
|
276 |
+
color: {color};
|
277 |
+
">
|
278 |
+
{message}
|
279 |
+
</div>
|
280 |
+
"""
|
281 |
+
|
282 |
+
def create_interface():
|
283 |
+
manager = EnhancedFileManager()
|
284 |
+
|
285 |
+
with gr.Blocks(css=CUSTOM_CSS) as interface:
|
286 |
+
with gr.Column(elem_classes="file-manager"):
|
287 |
+
gr.Markdown("# 🗂️ مدیریت پیشرفته فایلها", elem_classes="custom-header")
|
288 |
+
|
289 |
+
# نوار مسیر
|
290 |
+
with gr.Row(elem_classes="breadcrumb"):
|
291 |
+
current_path = gr.Textbox(value=manager.current_path, label="", interactive=False)
|
292 |
+
home_btn = gr.Button("🏠", elem_classes="operation-button")
|
293 |
+
up_btn = gr.Button("⬆️", elem_classes="operation-button")
|
294 |
+
refresh_btn = gr.Button("🔄", elem_classes="operation-button")
|
295 |
+
|
296 |
+
# جستجو
|
297 |
+
search_box = gr.Textbox(
|
298 |
+
placeholder="🔍 جستجو در فایلها...",
|
299 |
+
elem_classes="search-box"
|
300 |
+
)
|
301 |
+
|
302 |
+
# عملیات اصلی
|
303 |
+
with gr.Row(elem_classes="file-operations"):
|
304 |
+
upload_btn = gr.UploadButton(
|
305 |
+
"📤 آپلود فایل",
|
306 |
+
elem_classes="operation-button"
|
307 |
+
)
|
308 |
+
new_folder_btn = gr.Button(
|
309 |
+
"📁 پوشه جدید",
|
310 |
+
elem_classes="operation-button"
|
311 |
+
)
|
312 |
+
compress_btn = gr.Button(
|
313 |
+
"🗜️ فشردهسازی",
|
314 |
+
elem_classes="operation-button"
|
315 |
+
)
|
316 |
+
|
317 |
+
# لیست فایلها و پیشنمایش
|
318 |
+
with gr.Row():
|
319 |
+
with gr.Column(scale=2):
|
320 |
+
file_list = gr.HTML()
|
321 |
+
with gr.Column(scale=1):
|
322 |
+
with gr.Group(elem_classes="file-preview"):
|
323 |
+
gr.Markdown("### 👁️ پیشنمایش", elem_classes="preview-title")
|
324 |
+
preview = gr.HTML()
|
325 |
+
|
326 |
+
# پیامهای وضعیت
|
327 |
+
status = gr.HTML()
|
328 |
+
|
329 |
+
# عملکردها
|
330 |
+
def update_file_list(search=""):
|
331 |
+
files_data = manager.list_files(search)
|
332 |
+
return manager._format_file_list(files_data)
|
333 |
+
|
334 |
+
def handle_upload(file):
|
335 |
+
success, message = manager.upload_file(file)
|
336 |
+
return message, update_file_list()
|
337 |
+
|
338 |
+
def handle_new_folder():
|
339 |
+
name = gr.Textbox(label="نام پوشه جدید").value
|
340 |
+
success, message = manager.create_folder(name)
|
341 |
+
return message, update_file_list()
|
342 |
+
|
343 |
+
# اتصال رویدادها
|
344 |
+
search_box.change(update_file_list, inputs=[search_box], outputs=[file_list])
|
345 |
+
upload_btn.upload(handle_upload, inputs=[upload_btn], outputs=[status, file_list])
|
346 |
+
new_folder_btn.click(handle_new_folder, outputs=[status, file_list])
|
347 |
+
refresh_btn.click(update_file_list, outputs=[file_list])
|
348 |
+
|
349 |
+
home_btn.click(
|
350 |
+
lambda: (str(manager.root_path), update_file_list()),
|
351 |
+
outputs=[current_path, file_list]
|
352 |
+
)
|
353 |
+
|
354 |
+
up_btn.click(
|
355 |
+
lambda p: (str(Path(p).parent), update_file_list()),
|
356 |
+
inputs=[current_path],
|
357 |
+
outputs=[current_path, file_list]
|
358 |
+
)
|
359 |
+
|
360 |
+
return interface
|
361 |
+
|
362 |
+
if __name__ == "__main__":
|
363 |
+
demo = create_interface()
|
364 |
+
demo.launch()
|
app.py
CHANGED
@@ -1,236 +1,236 @@
|
|
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;
|
60 |
-
border-radius: 15px;
|
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;
|
91 |
-
border: none;
|
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;'>ورود
|
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()
|
|
|
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;
|
60 |
+
border-radius: 15px;
|
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;
|
91 |
+
border: none;
|
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()
|