File size: 6,343 Bytes
bf58554
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f652a35
59542b8
f652a35
46dc086
 
 
f652a35
46dc086
f652a35
46dc086
f652a35
46dc086
f652a35
 
 
 
 
46dc086
f652a35
46dc086
f652a35
 
 
46dc086
f652a35
 
 
 
46dc086
f652a35
 
 
 
 
 
46dc086
f652a35
46dc086
f652a35
46dc086
f652a35
 
 
46dc086
f652a35
 
46dc086
 
f652a35
46dc086
f652a35
 
 
46dc086
f652a35
 
 
46dc086
f652a35
46dc086
f652a35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46dc086
 
f652a35
46dc086
f652a35
46dc086
f652a35
 
 
 
 
46dc086
 
f652a35
46dc086
f652a35
 
 
 
46dc086
f652a35
46dc086
f652a35
46dc086
f652a35
46dc086
f652a35
46dc086
f652a35
46dc086
f652a35
 
 
 
46dc086
 
f652a35
 
 
 
 
 
 
 
 
 
 
 
 
46dc086
 
 
f652a35
 
 
 
46dc086
 
 
f652a35
 
 
 
46dc086
f652a35
46dc086
f652a35
 
 
 
46dc086
f652a35
46dc086
 
f652a35
 
 
 
 
46dc086
 
 
f652a35
 
 
 
 
 
46dc086
 
59542b8
bf58554
f652a35
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
language:
- en
license: apache-2.0
library_name: transformers
tags:
- cad
- cadquery
- 3d-modeling
- code-generation
- mechanical-engineering
- text-to-cad
- geometry
- python
- gemma
- fine-tuned
pipeline_tag: text-generation
base_model: unsloth/gemma-3n-E4B-it
datasets:
- text-to-cadquery
widget:
- text: "Create a simple gear with 12 teeth"
  example_title: "Gear Generation"
- text: "Generate a phone case for iPhone 15"
  example_title: "Phone Case"
- text: "Make a rectangular bracket with mounting holes"
  example_title: "Bracket Design"
- text: "Design a simple pulley with a 50mm diameter"
  example_title: "Pulley Design"
model-index:
- name: C3D-v0
  results:
  - task:
      type: text-generation
      name: CAD Code Generation
    dataset:
      name: text-to-cadquery
      type: text-to-cadquery
    metrics:
    - name: Code Execution Success Rate
      type: execution_success
      value: "High"
      verified: false
---

# 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](https://github.com/unxversal/c3d) - 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](https://github.com/Text-to-CadQuery/Text-to-CadQuery)
- **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)

```bash
# 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)

```bash
# Install C3D
npm install -g c3d

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

### Direct Model Usage

```python
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**:
```python
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

- **Main Project**: [C3D on GitHub](https://github.com/unxversal/c3d)
- **Ollama Model**: [joshuaokolo/C3Dv0](https://ollama.com/joshuaokolo/C3Dv0)
- **GGUF Version**: [C3D-v0-gguf](https://huggingface.co/numinousmuses/C3D-v0-gguf)
- **Dataset**: [Text-to-CadQuery](https://github.com/Text-to-CadQuery/Text-to-CadQuery)

## Citation

```bibtex
@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](https://bento.me/joshuaokolo)