# Architecture ## High-Level ``` Browser (Next.js 14) <--HTTP/SSE/WS--> FastAPI (Python) | Recharts | Transformers + Torch | Shadcn/ui | pynvml/psutil | Tailwind | fpdf2 ``` ## Frontend (`frontend/src`) - `app/layout.tsx`: RTL, `Tajawal` font, `Sidebar` + `MobileNav` - `app/page.tsx`: Dashboard (KPIs + ModelLoader + TelemetryCharts) - `app/playground/page.tsx`: `Playground.tsx` (SSE streaming, TPS/TTFT) - `app/benchmark/page.tsx`: `BenchmarkPanel.tsx` (preset + CustomDatasetPanel + progress SSE modal) - `app/hardware/page.tsx`: `TelemetryCharts` + Power vs VRAM scatter - `app/models/page.tsx`: Validate + scan `GET /api/model/list` - `app/share/[token]/page.tsx`: Public report view via `GET /api/share/{token}` - `lib/api.ts`: `API_BASE = process.env.NEXT_PUBLIC_API_URL`, `apiFetch`, `streamGenerate`, `runBenchmarkStream` - `components/ui/*`: Button, Card, Input, Badge, Tabs, Select, Progress (Shadcn) ## Backend (`backend/app`) - `main.py`: `FastAPI` + `CORSMiddleware(*)` + 7 routers - `model_manager.py`: `ModelManager` singleton - `load_model(path, dtype, quantization, device_map)` → `AutoModelForCausalLM.from_pretrained` with `BitsAndBytesConfig` + `torch_dtype` + `device_map` - Demo fallback if `!HAS_TRANSFORMERS` or `CUDA` error - `generate_stream(req)` → `TextIteratorStreamer` + `Thread` or `_demo_generate` - `telemetry.py`: `TelemetryService` — `psutil` + `pynvml.nvmlDeviceGetMemoryInfo` + `history[600]` - `benchmark.py`: `BENCHMARK_SUITES` 15 tasks + `evaluate_answer()` (Exact/Regex/LLM) + `run_benchmark()` - `dataset_parser.py`: `scan_folder()` → `parse_file()` (CSV/JSON/JSONL/TXT/MD) + `detect_language()` + `infer_category()` - `schemas.py`: Pydantic models (`ModelLoadRequest`, `GenerateRequest`, `TelemetrySnapshot`, `BenchmarkReport`) - `routers/`: - `model.py`: `/api/model/*` - `inference.py`: `/api/generate` (SSE) - `telemetry.py`: `/api/telemetry/` + `GET /history` + `WS /ws/telemetry` - `benchmark.py`: `/api/benchmark/*` including `POST /run-stream` (SSE) - `custom_benchmark.py`: `/api/benchmark/custom/*` - `share.py`: `/api/share/*` (in-memory `share_store`) - `export.py`: `/api/export/{json,csv,pdf}` (Unicode font via `tahoma.ttf`) ## Data Flow 1. **Load:** UI `POST /api/model/load` → `model_manager.load_model()` → `info` with `num_parameters` estimated from file size 2. **Chat:** UI `POST /api/generate stream=true` → `StreamingResponse` `data: {"type":"token",...}` → UI updates `tps` 3. **Benchmark:** UI `POST /api/benchmark/run-stream` → SSE `progress` per task → `task_done` → `done` with `BenchmarkReport` 4. **Telemetry:** Poll `GET /api/telemetry/` every 1.2s + `history` for charts 5. **Export:** `GET /api/export/pdf?report_id=` → `fpdf2` with `Arabic` font + `reshaper` ## Persistence - `reports_store: Dict[str, BenchmarkReport]` (in-memory, lost on restart) - `share_store: Dict[str, report_id]` (in-memory) - For production, replace with `sqlite`/`postgres`. ## Security Notes - No auth (local-first). For public, add proxy auth and restrict `folder_path` to allowlist.