Skip to content

Commit 87a7975

Browse files
authored
Merge auto dashboard
Add auto-generated LMS dashboard & wrappers
2 parents b942819 + f57e70c commit 87a7975

File tree

7 files changed

+855
-0
lines changed

7 files changed

+855
-0
lines changed

dashboard/dashboard.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
2+
# Dashboard runnable script for TaleemAI (created by auto-deploy)
3+
import io, os, pandas as pd, numpy as np
4+
from IPython.display import display, HTML, clear_output
5+
import ipywidgets as widgets
6+
# Attempt to import wrappers from taleem_modules
7+
import sys
8+
from pathlib import Path
9+
tm_dir = Path(__file__).resolve().parents[1] / "taleem_modules"
10+
if str(tm_dir) not in sys.path:
11+
sys.path.insert(0, str(tm_dir))
12+
try:
13+
import wrappers
14+
except Exception:
15+
wrappers = None
16+
17+
# Styling (light white-blue)
18+
display(HTML("<style> .tcard{background:#ffffff;border-radius:12px;padding:12px;margin:8px 0;box-shadow:0 2px 8px rgba(32,60,120,0.08);} .header{background:#f5f9ff;padding:12px;border-radius:10px;margin-bottom:12px} </style>"))
19+
display(HTML("<div class='header'><h2 style='color:#1f4db7;margin:6px 0'>TaleemAI — LMS Dashboard (Teacher / Student / Parent)</h2><div style='color:#4b5563'>Light / modern white-blue theme</div></div>"))
20+
21+
# Widgets
22+
role = widgets.Dropdown(options=["Teacher","Student","Parent"], value="Teacher", description="Role:")
23+
uploader = widgets.FileUpload(accept='.csv,.xlsx', multiple=False, description='Upload CSV')
24+
nlp_translate = widgets.Checkbox(value=True, description='Enable translation/summarize')
25+
run_btn = widgets.Button(description='Run Pipeline', button_style='primary')
26+
save_btn = widgets.Button(description='Save Results', button_style='success')
27+
out = widgets.Output(layout={'border':'1px solid #e6eefc','padding':'10px'})
28+
29+
left = widgets.VBox([role, uploader, nlp_translate, run_btn, save_btn])
30+
main = widgets.VBox([out])
31+
layout = widgets.HBox([left, main], layout=widgets.Layout(align_items='flex-start'))
32+
display(layout)
33+
34+
_store = {}
35+
def run_pipeline(b):
36+
with out:
37+
clear_output()
38+
print("Running pipeline for role:", role.value)
39+
df = None
40+
if uploader.value:
41+
uploaded = list(uploader.value.values())[0]
42+
name = uploaded['metadata']['name']
43+
content = uploaded['content']
44+
bio = io.BytesIO(content)
45+
try:
46+
if name.lower().endswith('.csv'): df = pd.read_csv(bio)
47+
else: df = pd.read_excel(bio)
48+
print(f"Loaded uploaded file: {name} ({df.shape[0]} rows)")
49+
# Save to repo/data/
50+
repo_data_dir = Path(__file__).resolve().parents[1] / "data"
51+
repo_data_dir.mkdir(parents=True, exist_ok=True)
52+
save_path = repo_data_dir / name
53+
df.to_csv(save_path, index=False)
54+
print("Saved uploaded file to:", save_path)
55+
except Exception as e:
56+
print("Failed to load uploaded file:", e)
57+
return
58+
else:
59+
# try to load sample from repo/data/sample.csv
60+
sample = Path(__file__).resolve().parents[1] / "data" / "sample.csv"
61+
if sample.exists():
62+
df = pd.read_csv(sample)
63+
print("Loaded sample data:", sample.name)
64+
else:
65+
print("No upload and no sample.csv present. Create/upload one and run again.")
66+
return
67+
68+
# Preprocess
69+
try:
70+
df = wrappers.preprocess_df(df)
71+
print("Applied preprocess_df() from wrappers.")
72+
except Exception:
73+
print("Used default preprocessing.")
74+
75+
display(df.head(5))
76+
77+
# Role-specific output
78+
if role.value == "Teacher":
79+
print("\\n--- Teacher Overview ---")
80+
try:
81+
analytics = wrappers.analytics_summary(df)
82+
print("Analytics summary:", analytics)
83+
except Exception:
84+
print("Fallback analytics:")
85+
display(df.describe(include='all'))
86+
try:
87+
recs = wrappers.generate_recommendations(df)
88+
print("\\nTop recommendations (sample):")
89+
display(recs.head(10))
90+
except Exception:
91+
print("No recommendations function available. Fallback shown above.")
92+
elif role.value == "Student":
93+
print("\\n--- Student View ---")
94+
display(df.head(10))
95+
try:
96+
recs = wrappers.generate_recommendations(df)
97+
if 'index' in recs.columns:
98+
recs = recs.set_index('index')
99+
idx = df.index[0]
100+
if idx in recs.index:
101+
print("Your recommendations:")
102+
display(recs.loc[[idx]])
103+
except Exception:
104+
pass
105+
else:
106+
print("\\n--- Parent Snapshot ---")
107+
display(df.head(5))
108+
try:
109+
recs = wrappers.generate_recommendations(df)
110+
display(recs.sort_values('risk_score', ascending=False).head(5))
111+
except Exception:
112+
pass
113+
114+
# NLP / translate demonstration (sample)
115+
if nlp_translate.value:
116+
textcol = None
117+
for c in df.columns:
118+
if df[c].dtype == object and df[c].astype(str).str.len().mean() > 10:
119+
textcol = c
120+
break
121+
if textcol:
122+
sample_text = " ".join(df[textcol].astype(str).head(3).tolist())
123+
try:
124+
translated = wrappers.translate_text(sample_text)
125+
print("\\n--- NLP / Translation sample ---")
126+
print(translated)
127+
except Exception:
128+
print("\\nNLP translate not available.")
129+
# save processed results
130+
results_dir = Path(__file__).resolve().parents[1] / "results"
131+
results_dir.mkdir(parents=True, exist_ok=True)
132+
out_file = results_dir / f"processed_{int(time.time())}.csv"
133+
df.to_csv(out_file, index=False)
134+
print("\\nSaved processed results to:", out_file)
135+
_store['last_results'] = str(out_file)
136+
137+
run_btn.on_click(run_pipeline)
138+
139+
def save_results(b):
140+
with out:
141+
if 'last_results' in _store:
142+
print("Last processed results saved at:", _store['last_results'])
143+
else:
144+
print("No processed results to save. Run pipeline first.")
145+
save_btn.on_click(save_results)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# =============================
2+
# ✅ TaleemAI — Self-Running Notebook (No Upload Needed)
3+
# This code is ready to execute in a Python environment or a single Jupyter cell.
4+
# =============================
5+
6+
# 1️⃣ Install dependencies (uncomment if running in an environment without them)
7+
# !pip install pandas numpy scikit-learn matplotlib seaborn --quiet
8+
9+
# 2️⃣ Import libraries
10+
import pandas as pd
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
import seaborn as sns
14+
from sklearn.model_selection import train_test_split
15+
from sklearn.preprocessing import LabelEncoder
16+
from sklearn.ensemble import RandomForestClassifier
17+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
18+
19+
# 3️⃣ Create a balanced sample dataset
20+
np.random.seed(42)
21+
num_samples = 200
22+
23+
data = {
24+
'gender': np.random.choice(['Male', 'Female'], num_samples),
25+
'country': np.random.choice(['Pakistan', 'India', 'Bangladesh', 'Nepal'], num_samples),
26+
'age': np.random.randint(16, 22, num_samples),
27+
'study_hours': np.random.uniform(1, 10, num_samples).round(2),
28+
'attendance': np.random.uniform(60, 100, num_samples).round(2),
29+
'final_grade': np.random.choice(['Pass', 'Fail'], num_samples, p=[0.7, 0.3])
30+
}
31+
32+
df = pd.DataFrame(data)
33+
print("✅ Sample dataset created successfully!")
34+
print(df.head())
35+
36+
# 4️⃣ Encode categorical columns
37+
le = LabelEncoder()
38+
df['gender'] = le.fit_transform(df['gender'])
39+
df['country'] = le.fit_transform(df['country'])
40+
df['final_grade'] = le.fit_transform(df['final_grade']) # 'Pass' will be 1, 'Fail' will be 0 (or vice-versa depending on lexical order)
41+
42+
# 5️⃣ Split data
43+
X = df.drop('final_grade', axis=1)
44+
y = df['final_grade']
45+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
46+
47+
# 6️⃣ Train model
48+
model = RandomForestClassifier(n_estimators=100, random_state=42)
49+
model.fit(X_train, y_train)
50+
51+
# 7️⃣ Evaluate
52+
y_pred = model.predict(X_test)
53+
acc = accuracy_score(y_test, y_pred)
54+
55+
print(f"\n✅ Model Accuracy: {acc*100:.2f}%\n")
56+
print("Classification Report:\n", classification_report(y_test, y_pred))
57+
58+
# 8️⃣ Visualize Confusion Matrix
59+
plt.figure(figsize=(5,4))
60+
sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues')
61+
plt.title("Confusion Matrix")
62+
plt.xlabel("Predicted")
63+
plt.ylabel("Actual")
64+
plt.show()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# =============================
2+
# ✅ TaleemAI — Self-Running Notebook (No Upload Needed)
3+
# This code is ready to execute in a Python environment or a single Jupyter cell.
4+
# =============================
5+
6+
# 1️⃣ Install dependencies (uncomment if running in an environment without them)
7+
# !pip install pandas numpy scikit-learn matplotlib seaborn --quiet
8+
9+
# 2️⃣ Import libraries
10+
import pandas as pd
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
import seaborn as sns
14+
from sklearn.model_selection import train_test_split
15+
from sklearn.preprocessing import LabelEncoder
16+
from sklearn.ensemble import RandomForestClassifier
17+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
18+
19+
# 3️⃣ Create a balanced sample dataset
20+
np.random.seed(42)
21+
num_samples = 200
22+
23+
data = {
24+
'gender': np.random.choice(['Male', 'Female'], num_samples),
25+
'country': np.random.choice(['Pakistan', 'India', 'Bangladesh', 'Nepal'], num_samples),
26+
'age': np.random.randint(16, 22, num_samples),
27+
'study_hours': np.random.uniform(1, 10, num_samples).round(2),
28+
'attendance': np.random.uniform(60, 100, num_samples).round(2),
29+
'final_grade': np.random.choice(['Pass', 'Fail'], num_samples, p=[0.7, 0.3])
30+
}
31+
32+
df = pd.DataFrame(data)
33+
print("✅ Sample dataset created successfully!")
34+
print(df.head())
35+
36+
# 4️⃣ Encode categorical columns
37+
le = LabelEncoder()
38+
df['gender'] = le.fit_transform(df['gender'])
39+
df['country'] = le.fit_transform(df['country'])
40+
df['final_grade'] = le.fit_transform(df['final_grade']) # 'Pass' will be 1, 'Fail' will be 0 (or vice-versa depending on lexical order)
41+
42+
# 5️⃣ Split data
43+
X = df.drop('final_grade', axis=1)
44+
y = df['final_grade']
45+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
46+
47+
# 6️⃣ Train model
48+
model = RandomForestClassifier(n_estimators=100, random_state=42)
49+
model.fit(X_train, y_train)
50+
51+
# 7️⃣ Evaluate
52+
y_pred = model.predict(X_test)
53+
acc = accuracy_score(y_test, y_pred)
54+
55+
print(f"\n✅ Model Accuracy: {acc*100:.2f}%\n")
56+
print("Classification Report:\n", classification_report(y_test, y_pred))
57+
58+
# 8️⃣ Visualize Confusion Matrix
59+
plt.figure(figsize=(5,4))
60+
sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues')
61+
plt.title("Confusion Matrix")
62+
plt.xlabel("Predicted")
63+
plt.ylabel("Actual")
64+
plt.show()

taleem_modules/model_training.txt

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# =============================
2+
# ✅ TaleemAI — Self-Running Notebook (No Upload Needed)
3+
# This code is ready to execute in a Python environment or a single Jupyter cell.
4+
# =============================
5+
6+
# 1️⃣ Install dependencies (uncomment if running in an environment without them)
7+
# !pip install pandas numpy scikit-learn matplotlib seaborn --quiet
8+
9+
# 2️⃣ Import libraries
10+
import pandas as pd
11+
import numpy as np
12+
import matplotlib.pyplot as plt
13+
import seaborn as sns
14+
from sklearn.model_selection import train_test_split
15+
from sklearn.preprocessing import LabelEncoder
16+
from sklearn.ensemble import RandomForestClassifier
17+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
18+
19+
# 3️⃣ Create a balanced sample dataset
20+
np.random.seed(42)
21+
num_samples = 200
22+
23+
data = {
24+
'gender': np.random.choice(['Male', 'Female'], num_samples),
25+
'country': np.random.choice(['Pakistan', 'India', 'Bangladesh', 'Nepal'], num_samples),
26+
'age': np.random.randint(16, 22, num_samples),
27+
'study_hours': np.random.uniform(1, 10, num_samples).round(2),
28+
'attendance': np.random.uniform(60, 100, num_samples).round(2),
29+
'final_grade': np.random.choice(['Pass', 'Fail'], num_samples, p=[0.7, 0.3])
30+
}
31+
32+
df = pd.DataFrame(data)
33+
print("✅ Sample dataset created successfully!")
34+
print(df.head())
35+
36+
# 4️⃣ Encode categorical columns
37+
le = LabelEncoder()
38+
df['gender'] = le.fit_transform(df['gender'])
39+
df['country'] = le.fit_transform(df['country'])
40+
df['final_grade'] = le.fit_transform(df['final_grade']) # 'Pass' will be 1, 'Fail' will be 0 (or vice-versa depending on lexical order)
41+
42+
# 5️⃣ Split data
43+
X = df.drop('final_grade', axis=1)
44+
y = df['final_grade']
45+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
46+
47+
# 6️⃣ Train model
48+
model = RandomForestClassifier(n_estimators=100, random_state=42)
49+
model.fit(X_train, y_train)
50+
51+
# 7️⃣ Evaluate
52+
y_pred = model.predict(X_test)
53+
acc = accuracy_score(y_test, y_pred)
54+
55+
print(f"\n✅ Model Accuracy: {acc*100:.2f}%\n")
56+
print("Classification Report:\n", classification_report(y_test, y_pred))
57+
58+
# 8️⃣ Visualize Confusion Matrix
59+
plt.figure(figsize=(5,4))
60+
sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues')
61+
plt.title("Confusion Matrix")
62+
plt.xlabel("Predicted")
63+
plt.ylabel("Actual")
64+
plt.show()
65+
66+
import subprocess
67+
import os
68+
69+
# --- Configuration ---
70+
CLASSIFICATION_NOTEBOOK = "classification_model.ipynb"
71+
FORECASTING_NOTEBOOK = "forecasting_model.ipynb"
72+
73+
# --- Function to execute a notebook ---
74+
def execute_notebook(notebook_path):
75+
"""Executes a Jupyter notebook using nbconvert."""
76+
if not os.path.exists(notebook_path):
77+
print(f"Error: Notebook '{notebook_path}' not found. Please ensure it exists.")
78+
return False
79+
print(f"Executing notebook: {notebook_path}...")
80+
try:
81+
subprocess.run(
82+
["jupyter", "nbconvert", "--to", "notebook", "--execute", notebook_path, "--inplace"],
83+
check=True,
84+
capture_output=True,
85+
text=True
86+
)
87+
print(f"Successfully executed {notebook_path}")
88+
return True
89+
except subprocess.CalledProcessError as e:
90+
print(f"Error executing notebook {notebook_path}:")
91+
print(f"Stdout: {e.stdout}")
92+
print(f"Stderr: {e.stderr}")
93+
return False
94+
except FileNotFoundError:
95+
print("Error: 'jupyter' command not found. Please ensure Jupyter is installed and in your PATH.")
96+
print("You might need to install it: pip install jupyter")
97+
return False
98+
99+
# --- Main training pipeline ---
100+
print("Starting central training pipeline...")
101+
102+
# --- Step 1: Run Classification Model ---
103+
print("\n--- Running Classification Model Training ---")
104+
if execute_notebook(CLASSIFICATION_NOTEBOOK):
105+
print("Classification model training completed.")
106+
else:
107+
print("Classification model training failed. Please check the logs.")
108+
109+
# --- Step 2: Run Forecasting Model ---
110+
print("\n--- Running Forecasting Model Training ---")
111+
if execute_notebook(FORECASTING_NOTEBOOK):
112+
print("Forecasting model training completed.")
113+
else:
114+
print("Forecasting model training failed. Please check the logs.")
115+
116+
print("\nCentral training pipeline finished.")

0 commit comments

Comments
 (0)