138 3 months ago

Fine-tuned Gemma 3n model for generating CADQuery Python code from natural language descriptions

Models

View all →

Readme

C3D-v0: AI-Powered CAD Code Generation Model

Fine-tuned Gemma 3n model for generating CADQuery Python code from natural language descriptions

Model Description

C3D-v0 is a specialized language model fine-tuned for generating 3D CAD models through Python code. Built on Google’s Gemma 3n architecture, this model transforms natural language descriptions into executable CADQuery scripts that can be rendered as 3D models.

This model is part of the C3D project - a complete text-to-CAD pipeline featuring an interactive CLI, 3D web viewer, and local AI inference.

Key Features

  • 🎯 Specialized for CAD: Fine-tuned specifically on CAD generation tasks
  • πŸ”§ CADQuery Focus: Generates clean, executable Python CADQuery code
  • πŸš€ Local Inference: Designed to run locally via Ollama
  • πŸ“ 3D Understanding: Trained on geometric and mechanical design concepts
  • ⚑ Optimized Performance: GGUF quantized for efficient inference

Training Details

Base Model

  • Architecture: Google Gemma 3n (4B parameters)
  • Base Model: unsloth/gemma-3n-E4B-it

Dataset

  • Source: Text-to-CadQuery Dataset
  • Training Size: ~48,000 examples (50% of full dataset)
  • Validation: Full validation set maintained

Training Configuration

  • Method: LoRA (Low-Rank Adaptation) fine-tuning
  • Epochs: 1 (due to resource constraints)
  • Batch Size: 2 per device, 4 gradient accumulation steps
  • Learning Rate: 2e-4
  • Platform: Trained on Kaggle/Colab free tier

Usage

Via Ollama (Recommended)

# Install the model
ollama pull joshuaokolo/C3Dv0

# Generate CAD code
ollama run joshuaokolo/C3Dv0 "Create a simple gear with 12 teeth"

Via C3D CLI (Full Experience)

# Install C3D
npm install -g c3d

# Generate with interactive 3D viewer
c3d generate "a phone case for iPhone 15"

Direct Model Usage

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# Load model and tokenizer
model_name = "numinousmuses/C3D-v0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Generate CAD code
prompt = "Create a simple rectangular bracket"
inputs = tokenizer(prompt, return_tensors="pt")

with torch.no_grad():
    outputs = model.generate(
        **inputs,
        max_new_tokens=512,
        temperature=0.8,
        top_p=0.9,
        do_sample=True
    )

generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(generated_code)

Prompt Format

The model works best with direct, descriptive prompts:

"Create a simple gear with 12 teeth"
"Generate a phone case for iPhone 15"
"Make a rectangular bracket with mounting holes"
"Design a simple pulley with a 50mm diameter"

Model Performance

  • Context Length: Up to 32k tokens (though trained at 16k)
  • Code Quality: Generates syntactically correct CADQuery code
  • Geometric Understanding: Handles basic to intermediate 3D shapes
  • Memory Usage: ~10GB RAM for full context inference

Example Output

Input: β€œCreate a simple gear with 12 teeth”

Output:

import cadquery as cq

# Create a gear with 12 teeth
gear = (
    cq.Workplane("XY")
    .circle(25)  # Outer radius
    .circle(20)  # Inner radius for teeth
    .extrude(5)  # Thickness
)

# Add teeth around the circumference
for i in range(12):
    angle = i * 30  # 360/12 = 30 degrees
    tooth = (
        cq.Workplane("XY")
        .transformed(rotate=(0, 0, angle))
        .rect(3, 8)
        .extrude(5)
    )
    gear = gear.union(tooth)

result = gear

Limitations

  • Training Scope: Limited to 50% of dataset due to resource constraints
  • Complexity: Best suited for simple to moderate complexity objects
  • Vision: Text-only model (multimodal version planned)
  • Domain: Focused on mechanical/geometric objects from training data

Roadmap

  • πŸ”„ Full Dataset Training: Complete training on entire dataset
  • πŸ‘οΈ Multimodal Support: Image-to-CAD generation capabilities
  • 🎯 Improved Prompting: Enhanced prompt engineering for better results
  • πŸ“ˆ Performance Optimization: Additional fine-tuning iterations

Related Links

Citation

@misc{c3d-v0-2024,
  title={C3D-v0: AI-Powered CAD Code Generation},
  author={Joshua Okolo},
  year={2024},
  url={https://github.com/unxversal/c3d}
}

Acknowledgments

  • Google DeepMind: For the Gemma 3n base model and competition opportunity
  • Unsloth: For providing efficient fine-tuning infrastructure
  • Text-to-CadQuery Team: For the comprehensive training dataset
  • Ollama: For local inference capabilities

License

This model inherits the license from the base Gemma 3n model. Please refer to the original Gemma license for usage terms.


Contact: Joshua Okolo | Mechanical Engineering + Computer Science @ Harvard | Portfolio