Spaces:
Sleeping
Sleeping
Really-amin
commited on
Commit
•
5296912
1
Parent(s):
4e479bc
Upload 5 files
Browse files- admin_dashboard.py +190 -0
- admin_dashboard_filemanager.py +145 -0
- admin_dashboard_sub.py +218 -0
- app (1).py +132 -0
- requirements (4).txt +6 -0
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,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pathlib import Path
|
3 |
+
from datetime import datetime
|
4 |
+
import shutil
|
5 |
+
import zipfile
|
6 |
+
from PIL import Image
|
7 |
+
import time
|
8 |
+
|
9 |
+
# تنظیمات صفحه
|
10 |
+
st.set_page_config(page_title="مدیریت فایلها", layout="centered", page_icon="📁")
|
11 |
+
|
12 |
+
# CSS برای طراحی گلس مورفیسم و نئومورفیسم
|
13 |
+
CUSTOM_CSS = """
|
14 |
+
<style>
|
15 |
+
body {
|
16 |
+
font-family: 'Vazir', sans-serif;
|
17 |
+
background: linear-gradient(135deg, #ece9e6, #ffffff);
|
18 |
+
}
|
19 |
+
.dashboard-container {
|
20 |
+
max-width: 800px;
|
21 |
+
margin: auto;
|
22 |
+
padding: 20px;
|
23 |
+
}
|
24 |
+
.glass-card, .glass-button, .drag-drop {
|
25 |
+
backdrop-filter: blur(10px);
|
26 |
+
background: rgba(255, 255, 255, 0.15);
|
27 |
+
border-radius: 20px;
|
28 |
+
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.15), -4px -4px 10px rgba(255, 255, 255, 0.7);
|
29 |
+
padding: 20px;
|
30 |
+
margin: 15px 0;
|
31 |
+
transition: all 0.3s ease;
|
32 |
+
}
|
33 |
+
.glass-button {
|
34 |
+
color: #ffffff;
|
35 |
+
background: linear-gradient(135deg, #4a90e2, #50e3c2);
|
36 |
+
border: none;
|
37 |
+
font-size: 18px;
|
38 |
+
cursor: pointer;
|
39 |
+
width: 100%;
|
40 |
+
padding: 10px 20px;
|
41 |
+
margin-top: 10px;
|
42 |
+
border-radius: 20px;
|
43 |
+
transition: all 0.3s ease;
|
44 |
+
}
|
45 |
+
.glass-button:hover {
|
46 |
+
background: linear-gradient(135deg, #50e3c2, #4a90e2);
|
47 |
+
}
|
48 |
+
.drag-drop {
|
49 |
+
border: 2px dashed #4a90e2;
|
50 |
+
padding: 40px;
|
51 |
+
text-align: center;
|
52 |
+
cursor: pointer;
|
53 |
+
}
|
54 |
+
</style>
|
55 |
+
"""
|
56 |
+
st.markdown(CUSTOM_CSS, unsafe_allow_html=True)
|
57 |
+
|
58 |
+
class FileManager:
|
59 |
+
def __init__(self):
|
60 |
+
self.root_path = Path('uploads')
|
61 |
+
self.root_path.mkdir(exist_ok=True)
|
62 |
+
|
63 |
+
def upload_file(self, file):
|
64 |
+
"""آپلود فایل با نوار پیشرفت"""
|
65 |
+
dest_path = self.root_path / file.name
|
66 |
+
with open(dest_path, "wb") as f:
|
67 |
+
total_size = file.size
|
68 |
+
chunk_size = 1024
|
69 |
+
bytes_read = 0
|
70 |
+
while bytes_read < total_size:
|
71 |
+
data = file.read(chunk_size)
|
72 |
+
f.write(data)
|
73 |
+
bytes_read += len(data)
|
74 |
+
st.progress(bytes_read / total_size)
|
75 |
+
return f"فایل '{file.name}' با موفقیت آپلود شد."
|
76 |
+
|
77 |
+
def list_files(self, file_type=None):
|
78 |
+
"""لیست فایلها با فیلتر نوع فایل"""
|
79 |
+
files = [f.name for f in self.root_path.iterdir() if f.is_file() and (not file_type or f.suffix == file_type)]
|
80 |
+
return files
|
81 |
+
|
82 |
+
def delete_file(self, filename):
|
83 |
+
"""حذف فایل"""
|
84 |
+
path = self.root_path / filename
|
85 |
+
if path.exists():
|
86 |
+
path.unlink()
|
87 |
+
return f"فایل '{filename}' حذف شد."
|
88 |
+
return f"فایل '{filename}' یافت نشد."
|
89 |
+
|
90 |
+
def preview_file(self, filename):
|
91 |
+
"""پیشنمایش فایل"""
|
92 |
+
path = self.root_path / filename
|
93 |
+
if path.suffix in ['.jpg', '.jpeg', '.png']:
|
94 |
+
return Image.open(path)
|
95 |
+
elif path.suffix == '.txt':
|
96 |
+
with open(path, "r", encoding="utf-8") as f:
|
97 |
+
return f.read(300)
|
98 |
+
return "پیشنمایش در دسترس نیست."
|
99 |
+
|
100 |
+
def compress_files(self, files):
|
101 |
+
"""فشردهسازی فایلها"""
|
102 |
+
zip_name = f"compressed_{datetime.now().strftime('%Y%m%d%H%M%S')}.zip"
|
103 |
+
zip_path = self.root_path / zip_name
|
104 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
105 |
+
for file in files:
|
106 |
+
file_path = self.root_path / file
|
107 |
+
zipf.write(file_path, file)
|
108 |
+
st.progress((files.index(file) + 1) / len(files))
|
109 |
+
return f"فایلها با نام '{zip_name}' فشرده شدند."
|
110 |
+
|
111 |
+
file_manager = FileManager()
|
112 |
+
|
113 |
+
# آپلود فایل
|
114 |
+
st.title("📁 مدیریت فایلها")
|
115 |
+
uploaded_file = st.file_uploader("فایل خود را آپلود کنید", type=["jpg", "jpeg", "png", "txt"], accept_multiple_files=False)
|
116 |
+
|
117 |
+
if uploaded_file:
|
118 |
+
st.write(file_manager.upload_file(uploaded_file))
|
119 |
+
|
120 |
+
# لیست فایلها و فیلتر نوع فایل
|
121 |
+
st.subheader("فایلهای آپلود شده")
|
122 |
+
file_type = st.selectbox("نمایش فایلهای:", ["همه", ".jpg", ".txt", ".png"], index=0)
|
123 |
+
files = file_manager.list_files(None if file_type == "همه" else file_type)
|
124 |
+
|
125 |
+
# نمایش منوی زمینه و عملیاتها
|
126 |
+
for file in files:
|
127 |
+
col1, col2, col3, col4 = st.columns([3, 1, 1, 1])
|
128 |
+
with col1:
|
129 |
+
if st.button(f"پیشنمایش {file}"):
|
130 |
+
preview = file_manager.preview_file(file)
|
131 |
+
if isinstance(preview, str):
|
132 |
+
st.text(preview)
|
133 |
+
else:
|
134 |
+
st.image(preview)
|
135 |
+
with col2:
|
136 |
+
if st.button(f"حذف {file}"):
|
137 |
+
st.write(file_manager.delete_file(file))
|
138 |
+
with col3:
|
139 |
+
st.download_button(label=f"دانلود {file}", data=open(file_manager.root_path / file, 'rb').read(), file_name=file)
|
140 |
+
|
141 |
+
# فشردهسازی فایل��ها
|
142 |
+
st.subheader("فشردهسازی فایلها")
|
143 |
+
selected_files = st.multiselect("فایلهای موردنظر را انتخاب کنید", files)
|
144 |
+
if st.button("فشردهسازی فایلها"):
|
145 |
+
st.write(file_manager.compress_files(selected_files))
|
admin_dashboard_sub.py
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import React, { useState, useEffect } from 'react';
|
2 |
+
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';
|
3 |
+
import { Upload, RefreshCw, Database, FileText, Settings } from 'lucide-react';
|
4 |
+
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card';
|
5 |
+
import { Alert, AlertDescription } from '@/components/ui/alert';
|
6 |
+
import { Button } from '@/components/ui/button';
|
7 |
+
|
8 |
+
const AdminDashboard = () => {
|
9 |
+
// Sample data for learning chart
|
10 |
+
const [learningData, setLearningData] = useState([]);
|
11 |
+
const [isUpdating, setIsUpdating] = useState(false);
|
12 |
+
const [chatHistory, setChatHistory] = useState([]);
|
13 |
+
const [message, setMessage] = useState('');
|
14 |
+
const [stats, setStats] = useState({});
|
15 |
+
const [plotData, setPlotData] = useState([]);
|
16 |
+
|
17 |
+
const handleKnowledgeUpdate = () => {
|
18 |
+
setIsUpdating(true);
|
19 |
+
// Simulate update
|
20 |
+
setTimeout(() => setIsUpdating(false), 2000);
|
21 |
+
};
|
22 |
+
|
23 |
+
const handleChatSubmit = async () => {
|
24 |
+
const response = await fetch('http://localhost:7860/api/chat', {
|
25 |
+
method: 'POST',
|
26 |
+
headers: {
|
27 |
+
'Content-Type': 'application/json'
|
28 |
+
},
|
29 |
+
body: JSON.stringify({ message, history: chatHistory })
|
30 |
+
});
|
31 |
+
const data = await response.json();
|
32 |
+
setChatHistory(data.history);
|
33 |
+
setMessage('');
|
34 |
+
};
|
35 |
+
|
36 |
+
const handleFileUpload = async (file) => {
|
37 |
+
const formData = new FormData();
|
38 |
+
formData.append('file', file);
|
39 |
+
const response = await fetch('http://localhost:7860/api/upload', {
|
40 |
+
method: 'POST',
|
41 |
+
body: formData
|
42 |
+
});
|
43 |
+
const data = await response.json();
|
44 |
+
alert(data.message);
|
45 |
+
};
|
46 |
+
|
47 |
+
const handleShowStats = async () => {
|
48 |
+
const response = await fetch('http://localhost:7860/api/stats');
|
49 |
+
const data = await response.json();
|
50 |
+
setStats(data.stats);
|
51 |
+
setPlotData(data.plot);
|
52 |
+
};
|
53 |
+
|
54 |
+
useEffect(() => {
|
55 |
+
// Fetch initial learning data
|
56 |
+
const fetchLearningData = async () => {
|
57 |
+
const response = await fetch('http://localhost:7860/api/learning_data');
|
58 |
+
const data = await response.json();
|
59 |
+
setLearningData(data);
|
60 |
+
};
|
61 |
+
fetchLearningData();
|
62 |
+
}, []);
|
63 |
+
|
64 |
+
return (
|
65 |
+
<div className="p-6 max-w-7xl mx-auto space-y-6">
|
66 |
+
{/* Header */}
|
67 |
+
<div className="flex justify-between items-center">
|
68 |
+
<h1 className="text-3xl font-bold">Admin Dashboard</h1>
|
69 |
+
<Button
|
70 |
+
onClick={handleKnowledgeUpdate}
|
71 |
+
className="bg-blue-600 hover:bg-blue-700"
|
72 |
+
disabled={isUpdating}
|
73 |
+
>
|
74 |
+
<RefreshCw className={`w-4 h-4 mr-2 ${isUpdating ? 'animate-spin' : ''}`} />
|
75 |
+
Update Knowledge Base
|
76 |
+
</Button>
|
77 |
+
</div>
|
78 |
+
|
79 |
+
{/* Statistics Cards */}
|
80 |
+
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
81 |
+
<Card>
|
82 |
+
<CardContent className="pt-6">
|
83 |
+
<div className="text-2xl font-bold">{stats.average_response_time || '89%'}</div>
|
84 |
+
<div className="text-sm text-gray-500">Current Learning Rate</div>
|
85 |
+
</CardContent>
|
86 |
+
</Card>
|
87 |
+
|
88 |
+
<Card>
|
89 |
+
<CardContent className="pt-6">
|
90 |
+
<div className="text-2xl font-bold">{stats.total_interactions || '1,234'}</div>
|
91 |
+
<div className="text-sm text-gray-500">Number of Answered Questions</div>
|
92 |
+
</CardContent>
|
93 |
+
</Card>
|
94 |
+
|
95 |
+
<Card>
|
96 |
+
<CardContent className="pt-6">
|
97 |
+
<div className="text-2xl font-bold">{stats.user_satisfaction || '95%'}</div>
|
98 |
+
<div className="text-sm text-gray-500">User Satisfaction</div>
|
99 |
+
</CardContent>
|
100 |
+
</Card>
|
101 |
+
</div>
|
102 |
+
|
103 |
+
{/* Learning Chart */}
|
104 |
+
<Card>
|
105 |
+
<CardHeader>
|
106 |
+
<CardTitle>Learning Rate Trend</CardTitle>
|
107 |
+
</CardHeader>
|
108 |
+
<CardContent>
|
109 |
+
<div className="h-[400px] w-full">
|
110 |
+
<LineChart
|
111 |
+
width={800}
|
112 |
+
height={400}
|
113 |
+
data={plotData}
|
114 |
+
margin={{ top: 5, right: 30, left: 20, bottom: 5 }}
|
115 |
+
>
|
116 |
+
<CartesianGrid strokeDasharray="3 3" />
|
117 |
+
<XAxis dataKey="date" />
|
118 |
+
<YAxis />
|
119 |
+
<Tooltip />
|
120 |
+
<Legend />
|
121 |
+
<Line
|
122 |
+
type="monotone"
|
123 |
+
dataKey="rate"
|
124 |
+
stroke="#2563eb"
|
125 |
+
name="Learning Rate"
|
126 |
+
strokeWidth={2}
|
127 |
+
/>
|
128 |
+
</LineChart>
|
129 |
+
</div>
|
130 |
+
</CardContent>
|
131 |
+
</Card>
|
132 |
+
|
133 |
+
{/* Operation Buttons */}
|
134 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
135 |
+
<Button className="h-24 text-lg justify-start p-6" variant="outline">
|
136 |
+
<FileText className="w-6 h-6 mr-4" />
|
137 |
+
Manage Files
|
138 |
+
</Button>
|
139 |
+
|
140 |
+
<Button className="h-24 text-lg justify-start p-6" variant="outline">
|
141 |
+
<Settings className="w-6 h-6 mr-4" />
|
142 |
+
Learning Settings
|
143 |
+
</Button>
|
144 |
+
</div>
|
145 |
+
|
146 |
+
{/* Alerts and Announcements */}
|
147 |
+
<Alert>
|
148 |
+
<AlertDescription>
|
149 |
+
Last knowledge base update: Yesterday at 15:30
|
150 |
+
</AlertDescription>
|
151 |
+
</Alert>
|
152 |
+
|
153 |
+
{/* Chat */}
|
154 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
155 |
+
<Card>
|
156 |
+
<CardHeader>
|
157 |
+
<CardTitle>Chat</CardTitle>
|
158 |
+
</CardHeader>
|
159 |
+
<CardContent>
|
160 |
+
<div className="h-[400px] w-full overflow-y-auto">
|
161 |
+
{chatHistory.map((msg, index) => (
|
162 |
+
<div key={index} className="p-2 border-b">
|
163 |
+
{msg}
|
164 |
+
</div>
|
165 |
+
))}
|
166 |
+
</div>
|
167 |
+
<div className="flex mt-4">
|
168 |
+
<input
|
169 |
+
type="text"
|
170 |
+
className="flex-grow p-2 border rounded-l"
|
171 |
+
value={message}
|
172 |
+
onChange={(e) => setMessage(e.target.value)}
|
173 |
+
onKeyPress={(e) => e.key === 'Enter' && handleChatSubmit()}
|
174 |
+
/>
|
175 |
+
<button
|
176 |
+
className="p-2 bg-blue-600 text-white rounded-r"
|
177 |
+
onClick={handleChatSubmit}
|
178 |
+
>
|
179 |
+
Send
|
180 |
+
</button>
|
181 |
+
</div>
|
182 |
+
</CardContent>
|
183 |
+
</Card>
|
184 |
+
</div>
|
185 |
+
|
186 |
+
{/* Upload Documents */}
|
187 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
188 |
+
<Card>
|
189 |
+
<CardHeader>
|
190 |
+
<CardTitle>Upload Documents</CardTitle>
|
191 |
+
</CardHeader>
|
192 |
+
<CardContent>
|
193 |
+
<input type="file" onChange={(e) => handleFileUpload(e.target.files[0])} />
|
194 |
+
</CardContent>
|
195 |
+
</Card>
|
196 |
+
</div>
|
197 |
+
|
198 |
+
{/* Statistics */}
|
199 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
200 |
+
<Card>
|
201 |
+
<CardHeader>
|
202 |
+
<CardTitle>Statistics</CardTitle>
|
203 |
+
</CardHeader>
|
204 |
+
<CardContent>
|
205 |
+
<button className="p-2 bg-blue-600 text-white rounded" onClick={handleShowStats}>
|
206 |
+
Show Stats
|
207 |
+
</button>
|
208 |
+
<div className="mt-4">
|
209 |
+
<pre>{JSON.stringify(stats, null, 2)}</pre>
|
210 |
+
</div>
|
211 |
+
</CardContent>
|
212 |
+
</Card>
|
213 |
+
</div>
|
214 |
+
</div>
|
215 |
+
);
|
216 |
+
};
|
217 |
+
|
218 |
+
export default AdminDashboard;
|
app (1).py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
# تنظیمات ادمین
|
5 |
+
ADMIN_USERNAME = "admin"
|
6 |
+
ADMIN_PASSWORD = "password"
|
7 |
+
|
8 |
+
# CSS برای ظاهر برنامه
|
9 |
+
CUSTOM_CSS = """
|
10 |
+
<style>
|
11 |
+
body {
|
12 |
+
font-family: 'Vazir', sans-serif;
|
13 |
+
background: linear-gradient(135deg, #e8effa, #ffffff);
|
14 |
+
}
|
15 |
+
.chat-container {
|
16 |
+
max-width: 400px;
|
17 |
+
margin: 0 auto;
|
18 |
+
border-radius: 15px;
|
19 |
+
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
|
20 |
+
}
|
21 |
+
.header {
|
22 |
+
background: linear-gradient(135deg, #4a90e2, #50e3c2);
|
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;
|
32 |
+
border-radius: 15px;
|
33 |
+
width: fit-content;
|
34 |
+
max-width: 80%;
|
35 |
+
font-size: 15px;
|
36 |
+
}
|
37 |
+
.message.user {
|
38 |
+
align-self: flex-end;
|
39 |
+
background: #4a90e2;
|
40 |
+
color: white;
|
41 |
+
}
|
42 |
+
.message.bot {
|
43 |
+
align-self: flex-start;
|
44 |
+
background: #ff6b6b;
|
45 |
+
color: white;
|
46 |
+
}
|
47 |
+
.footer {
|
48 |
+
display: flex;
|
49 |
+
padding: 10px;
|
50 |
+
background: #f5f5f5;
|
51 |
+
align-items: center;
|
52 |
+
}
|
53 |
+
.footer input[type="text"] {
|
54 |
+
flex: 1;
|
55 |
+
padding: 10px;
|
56 |
+
border: none;
|
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 |
+
if "model_loaded" not in st.session_state:
|
69 |
+
st.session_state.model_loaded = False
|
70 |
+
|
71 |
+
def load_model(self):
|
72 |
+
"""بارگذاری مدل و توکنایزر تنها در صورت نیاز"""
|
73 |
+
if not st.session_state.model_loaded:
|
74 |
+
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
75 |
+
st.session_state.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
76 |
+
st.session_state.model = AutoModelForCausalLM.from_pretrained(model_name)
|
77 |
+
st.session_state.chat_pipeline = pipeline(
|
78 |
+
"text-generation",
|
79 |
+
model=st.session_state.model,
|
80 |
+
tokenizer=st.session_state.tokenizer
|
81 |
+
)
|
82 |
+
st.session_state.model_loaded = True
|
83 |
+
|
84 |
+
def generate_response(self, message):
|
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 |
+
return username == ADMIN_USERNAME and password == ADMIN_PASSWORD
|
95 |
+
|
96 |
+
# ذخیره تاریخچه مکالمات و وضعیت ورود ادمین در session_state
|
97 |
+
if "conversation_history" not in st.session_state:
|
98 |
+
st.session_state.conversation_history = []
|
99 |
+
|
100 |
+
# ایجاد نمونه چتبات
|
101 |
+
chatbot = ModernChatbot()
|
102 |
+
|
103 |
+
# آیکون و پاپآپ ورود ادمین
|
104 |
+
if st.sidebar.button("ورود ادمین"):
|
105 |
+
username = st.sidebar.text_input("نام کاربری")
|
106 |
+
password = st.sidebar.text_input("رمز عبور", type="password")
|
107 |
+
if st.sidebar.button("تایید"):
|
108 |
+
if chatbot.login_admin(username, password):
|
109 |
+
st.session_state.admin_logged_in = True
|
110 |
+
st.sidebar.success("ورود موفقیتآمیز بود")
|
111 |
+
else:
|
112 |
+
st.sidebar.error("نام کاربری یا رمز عبور اشتباه است")
|
113 |
+
|
114 |
+
# نمایش مکالمات
|
115 |
+
st.markdown("<div class='chat-container'><div class='header'>چتبات هوشمند</div>", unsafe_allow_html=True)
|
116 |
+
for sender, message in st.session_state.conversation_history:
|
117 |
+
align_class = "user" if sender == "شما" else "bot"
|
118 |
+
st.markdown(f"<div class='message {align_class}'>{sender}: {message}</div>", unsafe_allow_html=True)
|
119 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
120 |
+
|
121 |
+
# ورودی پیام کاربر
|
122 |
+
user_message = st.text_input("پیام خود را اینجا بنویسید...", key="user_message")
|
123 |
+
if st.button("ارسال") and user_message:
|
124 |
+
st.session_state.conversation_history.append(("شما", user_message))
|
125 |
+
response = chatbot.generate_response(user_message)
|
126 |
+
st.session_state.conversation_history.append(("ربات", response))
|
127 |
+
st.experimental_rerun()
|
128 |
+
|
129 |
+
# پاکسازی مکالمات
|
130 |
+
if st.sidebar.button("پاک کردن تاریخچه مکالمات"):
|
131 |
+
st.session_state.conversation_history = []
|
132 |
+
st.experimental_rerun()
|
requirements (4).txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
gradio
|
5 |
+
requests
|
6 |
+
|