|
| 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) |
0 commit comments