Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import subprocess | |
| import time | |
| import pandas as pd | |
| import plotly.express as px | |
| import random | |
| from datetime import datetime | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.datasets import make_classification | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import accuracy_score | |
| # کلاس برای یادگیری ماشین | |
| class MLEngine: | |
| def __init__(self): | |
| # ایجاد دادههای آموزشی شبیهسازی شده | |
| self.X, self.y = make_classification(n_samples=1000, n_features=10, random_state=42) | |
| self.model = LogisticRegression() | |
| self.training_history = [] | |
| self.last_update_time = None | |
| self.current_accuracy = 0 | |
| self.total_interactions = 1234 | |
| self.user_satisfaction = 95 | |
| def train_model(self): | |
| """آموزش مدل و ذخیره دقت آن""" | |
| # تقسیم دادهها به مجموعههای آموزشی و آزمایشی | |
| X_train, X_test, y_train, y_test = train_test_split(self.X, self.y, test_size=0.2, random_state=42) | |
| self.model.fit(X_train, y_train) | |
| # پیشبینی و محاسبه دقت | |
| predictions = self.model.predict(X_test) | |
| accuracy = accuracy_score(y_test, predictions) | |
| # ذخیره دقت در تاریخچه آموزشی | |
| self.current_accuracy = accuracy * 100 # دقت به درصد | |
| self.training_history.append({"timestamp": datetime.now(), "accuracy": self.current_accuracy}) | |
| self.last_update_time = datetime.now() | |
| def update_knowledge_base(self): | |
| """شبیهسازی بهروزرسانی پایگاه دانش""" | |
| time.sleep(2) # شبیهسازی زمان بهروزرسانی پایگاه دانش | |
| self.train_model() # آموزش مجدد مدل به عنوان بخشی از بهروزرسانی | |
| def get_learning_stats(self): | |
| """برگرداندن آمار یادگیری شامل دقت و تاریخچه آموزشی""" | |
| return { | |
| "history": self.training_history, | |
| "currentAccuracy": self.current_accuracy, | |
| "totalInteractions": self.total_interactions, | |
| "userSatisfaction": self.user_satisfaction, | |
| "lastUpdate": self.last_update_time.strftime("%Y-%m-%d %H:%M:%S") if self.last_update_time else "No Update Yet" | |
| } | |
| # ایجاد یک نمونه از کلاس MLEngine | |
| ml_engine = MLEngine() | |
| ml_engine.train_model() # آموزش اولیه مدل | |
| # تابع برای اجرای فایل `admin_dashboard_filemanager.py` | |
| def open_file_manager(): | |
| """اجرای فایل مدیریت فایلها و بهروزرسانی پایگاه دانش""" | |
| try: | |
| # اجرای فایل `admin_dashboard_filemanager.py` | |
| subprocess.Popen(["python", "admin_dashboard_filemanager.py"]) | |
| st.success("Knowledge Base update has been initiated successfully!") | |
| except Exception as e: | |
| st.error(f"Error while opening file manager: {e}") | |
| # CSS سفارشی برای طراحی گلس مورفیسم و نئومورفیسم | |
| CUSTOM_CSS = """ | |
| <style> | |
| body { | |
| background: linear-gradient(135deg, #ece9e6, #ffffff); | |
| font-family: 'Arial', sans-serif; | |
| } | |
| .dashboard-container { | |
| max-width: 800px; | |
| margin: auto; | |
| padding: 20px; | |
| } | |
| .glass-card, .glass-button, .glass-chart { | |
| backdrop-filter: blur(10px); /* شفافیت پسزمینه */ | |
| background: rgba(255, 255, 255, 0.15); /* پسزمینه شفاف */ | |
| border-radius: 20px; /* گوشههای گرد */ | |
| box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.15), -4px -4px 10px rgba(255, 255, 255, 0.7); /* سایههای نئومورفیسم */ | |
| padding: 20px; | |
| margin: 15px 0; | |
| transition: all 0.3s ease; | |
| } | |
| .glass-card:hover, .glass-button:hover { | |
| box-shadow: 6px 6px 12px rgba(0, 0, 0, 0.2), -6px -6px 12px rgba(255, 255, 255, 0.5); /* افکت سایه هنگام هاور */ | |
| transform: translateY(-5px); /* جابجایی جزئی */ | |
| } | |
| .header { | |
| text-align: center; | |
| color: #333; | |
| font-size: 24px; | |
| font-weight: bold; | |
| padding: 10px 0; | |
| } | |
| .glass-button { | |
| display: inline-block; | |
| color: #ffffff; | |
| background: linear-gradient(135deg, #4a90e2, #50e3c2); | |
| border: none; | |
| font-size: 18px; | |
| cursor: pointer; | |
| text-align: center; | |
| width: 100%; | |
| padding: 10px 20px; | |
| margin-top: 10px; | |
| border-radius: 20px; | |
| box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.15), -4px -4px 10px rgba(255, 255, 255, 0.7); /* سایههای نئومورفیسم */ | |
| transition: all 0.3s ease; | |
| } | |
| .glass-button:hover { | |
| background: linear-gradient(135deg, #50e3c2, #4a90e2); /* تغییر رنگ هنگام هاور */ | |
| } | |
| .stat-card { | |
| text-align: center; | |
| color: #333; | |
| padding: 15px; | |
| } | |
| .stat-title { | |
| font-size: 16px; | |
| color: #666; | |
| } | |
| .stat-value { | |
| font-size: 26px; | |
| font-weight: bold; | |
| color: #4a90e2; | |
| } | |
| .glass-upload { | |
| background: rgba(255, 255, 255, 0.25); /* پسزمینه شفاف */ | |
| border: 1px solid rgba(255, 255, 255, 0.3); | |
| border-radius: 20px; | |
| padding: 20px; | |
| box-shadow: inset 4px 4px 6px rgba(0, 0, 0, 0.1), inset -4px -4px 6px rgba(255, 255, 255, 0.5); /* سایه داخلی */ | |
| } | |
| .icon { | |
| font-size: 24px; | |
| color: #4a90e2; | |
| margin-right: 8px; | |
| } | |
| </style> | |
| """ | |
| # اعمال CSS سفارشی | |
| st.markdown(CUSTOM_CSS, unsafe_allow_html=True) | |
| # ساخت داشبورد | |
| st.markdown("<div class='dashboard-container'>", unsafe_allow_html=True) | |
| st.markdown("<div class='header'>Admin Dashboard</div>", unsafe_allow_html=True) | |
| # دکمه بهروزرسانی پایگاه دانش با طراحی گلس مورفیسم | |
| if st.button("Update Knowledge Base"): | |
| ml_engine.update_knowledge_base() | |
| open_file_manager() | |
| # نمایش آمارها با طراحی نئومورفیسم | |
| st.subheader("Statistics") | |
| col1, col2, col3 = st.columns(3) | |
| # نمایش آمارهای فعلی مدل | |
| stats = ml_engine.get_learning_stats() | |
| 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) | |
| 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) | |
| 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) | |
| # نمودار روند یادگیری با طراحی شفاف (گلس مورفیسم) | |
| st.subheader("Learning Rate Trend") | |
| learning_df = pd.DataFrame([ | |
| {"Date": stat["timestamp"], "Accuracy": stat["accuracy"]} | |
| for stat in stats["history"] | |
| ]) | |
| fig = px.line(learning_df, x="Date", y="Accuracy", title="Learning Rate Over Time", | |
| template="plotly_white", markers=True) | |
| fig.update_traces(line=dict(color="#4a90e2", width=2)) | |
| st.plotly_chart(fig, use_container_width=True) | |
| # آپلود فایل با طراحی نئومورفیسم | |
| st.subheader("Manage Files") | |
| st.markdown("<div class='glass-upload'>", unsafe_allow_html=True) | |
| uploaded_file = st.file_uploader("Upload Document", type=["txt", "pdf", "docx"]) | |
| if uploaded_file: | |
| st.success("File uploaded successfully!") | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| # پایان داشبورد | |
| st.markdown("</div>", unsafe_allow_html=True) | |