"use client"; import { useState } from "react"; import { Cpu, Zap, Terminal } from "lucide-react"; import { generateCHeader } from "@/lib/edge/c_exporter"; import GeneratedCHeader from "@/components/GeneratedCHeader"; export default function EdgeDeployment() { const [model, setModel] = useState("rf"); const [targetMcu, setTargetMcu] = useState("esp32"); const [quantize, setQuantize] = useState(true); const [code, setCode] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [copied, setCopied] = useState(false); const handleExport = async (overrideModel?: string, overrideQuantize?: boolean, overrideMcu?: string) => { const activeModel = overrideModel ?? model; const activeQuantize = overrideQuantize !== undefined ? overrideQuantize : quantize; const activeMcu = overrideMcu ?? targetMcu; setLoading(true); setError(""); try { const apiUrl = process.env.NEXT_PUBLIC_API_URL; if (apiUrl) { const res = await fetch(`${apiUrl}/export_tinyml?model_name=${activeModel}&quantize=${activeQuantize}`); if (res.ok) { const data = await res.json(); if (!data.error && (data.code || data.c_code)) { setCode(data.code || data.c_code); setLoading(false); return; } } } } catch { // Cloud API unreachable, seamlessly compile on the client } // Client-side Zero-Cloud TinyML Transpiler try { const cCode = generateCHeader(activeModel, activeQuantize, activeMcu); setCode(cCode); } catch (err: unknown) { const msg = err instanceof Error ? err.message : String(err); setError(msg || "Failed to generate C header"); } finally { setLoading(false); } }; const handleSelectModel = (val: string) => { setModel(val); }; const handleSelectMcu = (val: string) => { setTargetMcu(val); }; const handleToggleQuantize = () => { setQuantize((prev) => !prev); }; const handleCopy = () => { if (!code) return; navigator.clipboard.writeText(code); setCopied(true); setTimeout(() => setCopied(false), 2000); }; const handleDownload = () => { if (!code) return; const blob = new Blob([code], { type: "text/x-csrc" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `model_${model}_${quantize ? "int8" : "fp32"}.h`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; // Dynamic hardware budget estimates based on selected topology const getSramEstimate = () => { if (model === "rf") return quantize ? "28 Bytes" : "112 Bytes"; if (model === "svm") return quantize ? "32 Bytes" : "200 Bytes"; if (model === "small_nn") return quantize ? "48 Bytes" : "168 Bytes"; if (model === "knn") return quantize ? "64 Bytes" : "240 Bytes"; return quantize ? "24 Bytes" : "96 Bytes"; }; const getFlashEstimate = () => { if (model === "rf") return quantize ? "~ 1.6 KB" : "~ 4.2 KB"; if (model === "svm") return quantize ? "~ 0.9 KB" : "~ 2.1 KB"; if (model === "small_nn") return quantize ? "~ 1.2 KB" : "~ 2.8 KB"; if (model === "knn") return quantize ? "~ 1.8 KB" : "~ 3.4 KB"; return quantize ? "~ 0.6 KB" : "~ 1.5 KB"; }; return (
{/* Header */}
C99 EMBEDDED TRANSPILER · ZERO-MALLOC ABI

EDGE
COMPILER

{`TARGET: ${targetMcu.toUpperCase()} · ${quantize ? "INT8 FIXED-POINT" : "FP32 SINGLE-PRECISION"}`}
ABI: int predict(const float *features) · Zero-Malloc
{/* 2-Column Responsive Workspace Grid (Fixed Size on Desktop: lg:h-145) */}
{/* Config Panel - 6 Columns (Fixed Size, Zero Scroll) */}
{/* Header */}

Target Configuration

TINYML C99
{/* Target Architecture Selection (2x2 Silicon Grid) */}
Target Silicon Profile {targetMcu} active
{[ { id: "esp32", name: "ESP32-S3", clock: "240 MHz", arch: "Xtensa LX7 · SIMD", mem: "512 KB" }, { id: "arm", name: "Cortex-M4F", clock: "168 MHz", arch: "ARMv7E-M · CMSIS", mem: "FPU Unit" }, { id: "rp2040", name: "RP2040", clock: "133 MHz", arch: "Dual M0+ · ALU", mem: "264 KB" }, { id: "nrf52", name: "nRF52840", clock: "64 MHz", arch: "Cortex-M4 · BLE", mem: "256 KB" }, ].map((chip) => { const isSelected = targetMcu === chip.id; return ( ); })}
{/* Config Controls (Middle Section, Flex-1, Fits with zero scroll) */}
{/* Model Architecture Selector - 5 Ensemble Models */}
Ensemble Classifier Topology 5 Estimators
{[ { id: "rf", label: "RF", sub: "30 Trees" }, { id: "svm", label: "SVM", sub: "Linear" }, { id: "logreg", label: "LogReg", sub: "Logits" }, { id: "small_nn", label: "MLP", sub: "16-8 Net" }, { id: "knn", label: "KNN", sub: "5-NN" }, ].map((m) => { const isSelected = model === m.id; return ( ); })}
{/* Quantization Toggle */}
INT8 Fixed-Point Quantization
{quantize ? "75% SRAM reduction · Integer ALU bitshifts · 0 FPU cycles" : "Standard FP32 IEEE floating point representation"}
{/* Hardware Budget 3-Cell Telemetry Strip */}
STATIC SRAM
{getSramEstimate()}
FLASH ROM
{getFlashEstimate()}
HEAP ALLOC
0 BYTES
{/* Compile CTA - Directly connected with no gap */}
{/* Reusable Generated C Header Section - 6 Columns */}
); }