naturalcad / docs /architecture-plan.md
noahlee1234
NaturalCAD: Add LLM via HF Inference, GLB export, and update docs
7a5533a
# NaturalCAD Architecture Plan
## Current State (2026-04-12)
We successfully pivoted away from a complex (and broken) Fly.io worker and Hugging Face Docker setup, replacing the backend engine entirely with **Modal**.
### What's Working
- βœ… **LLM Code Generation**: Using `Qwen/Qwen2.5-Coder-32B-Instruct` via Hugging Face Serverless Inference API inside Modal.
- βœ… **CAD Execution Engine**: Modal spins up a `python:3.10-slim` container with proper Linux OpenGL libraries (`libgl1`, `libglib2.0-0`, etc). `build123d` runs locally on the T4 GPU container.
- βœ… **Artifact Upload**: Modal container runs CAD, creates STL, STEP, and a browser-ready GLB using `trimesh`, and uploads directly to Supabase Storage, returning public URLs.
- βœ… **HF Spaces UI**: Front-end UI remains on Hugging Face Spaces.
### What's Deprecated
- ❌ Fly.io backend routing and worker loops (too much complexity/overhead for MVP).
- ❌ Hugging Face native Docker CAD execution (lacks host graphics libs for VTK).
---
## Target Architecture
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ User │────▢ HF Spaces (Gradio UI)
β”‚ Prompt β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Modal Web Endpoint β”‚
β”‚ β”‚
β”‚ 1. Calls HF Inference API (Qwen 2.5) β”‚
β”‚ 2. LLM writes build123d Python script β”‚
β”‚ 3. Executes script on Modal Container β”‚
β”‚ 4. Generates STL + STEP + GLB preview β”‚
β”‚ 5. Uploads files to Supabase β”‚
β”‚ 6. Returns 3 URLs back to HF Space β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Supabase β”‚
β”‚ Storage β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
## Services
| Service | Role | Cost | Status |
|---------|------|-----|--------|
| **HF Spaces** | UI/Frontend | Free tier | βœ… Ready |
| **Modal** | Web API + LLM call + CAD Execution | Pay-per-use GPU | βœ… Ready |
| **HF Inference API**| LLM (textβ†’code) | Free within limits | βœ… Ready |
| **Supabase** | DB + Storage | Free tier | βœ… Ready |
---
## Implementation Order
1. βœ… **Create Modal function** for CAD execution
2. βœ… **Add LLM generation via HF** to the Modal container
3. βœ… **Add Supabase Artifact Upload** returning public URL
4. πŸ”² **Deploy Modal Web Endpoint** to get a live URL
5. πŸ”² **Wire HF Spaces** to hit the Modal endpoint, parsing out the STL, STEP, and GLB urls into the Gradio UI.
---
*Updated: 2026-04-12*