gudo7208 commited on
Commit
95e7d7a
·
verified ·
1 Parent(s): 3c0347b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +102 -7
README.md CHANGED
@@ -47,14 +47,92 @@ pip install "numpy<2.0" cadquery==2.3.1 # Optional: for code execution
47
  ### Quick Start
48
 
49
  ```python
 
 
 
50
  from transformers import AutoModelForCausalLM, AutoTokenizer
51
 
52
- model_name = "gudo7208/CAD-Coder"
53
- tokenizer = AutoTokenizer.from_pretrained(model_name)
54
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
55
-
56
- prompt = "Create a cylinder with radius 10mm and height 20mm, with a central hole of radius 5mm."
57
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  text = tokenizer.apply_chat_template(
59
  [{"role": "user", "content": prompt}],
60
  tokenize=False,
@@ -62,7 +140,24 @@ text = tokenizer.apply_chat_template(
62
  )
63
  inputs = tokenizer([text], return_tensors="pt").to(model.device)
64
  outputs = model.generate(**inputs, max_new_tokens=2048)
65
- print(tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  ```
67
 
68
  ## Performance
 
47
  ### Quick Start
48
 
49
  ```python
50
+ """CAD-Coder Inference Example"""
51
+ import re
52
+ import os
53
  from transformers import AutoModelForCausalLM, AutoTokenizer
54
 
55
+ model_path = "gudo7208/CAD-Coder"
56
+
57
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
58
+ model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
59
+
60
+ # 测试数据 (来自 cad_data_test_cot.json 第一条)
61
+ prompt = """Please create a CadQuery Python code to generate a model based on the following description. The reasoning process MUST BE enclosed within <think> </think> tags. The final CadQuery code MUST BE put in \\boxed{```python
62
+ code
63
+ ```} with ONLY the executable code inside the box, nothing else.The final model is represented by r.
64
+
65
+ In the <think> section, simulate the thought process of an engineer converting text descriptions into a CAD model. Follow these steps:
66
+
67
+ 1. **Description Analysis**:
68
+ - Break down the description into different parts or components
69
+ - Identify key parameters for each part (coordinate systems, Euler angles, translation vectors)
70
+ - Understand the spatial relationships and assembly sequence between parts
71
+
72
+ 2. **Coordinate System Planning**:
73
+ - Determine the coordinate systems used for each part
74
+ - Parse how Euler angle rotations and translation vectors are applied
75
+ - Ensure understanding of local-to-global coordinate system transformations
76
+
77
+ 3. **Sketch Construction Strategy**:
78
+ - Analyze how to create each 2D sketch (loops, lines, points)
79
+ - Determine scaling factors for each sketch
80
+ - Plan how to transform sketches into 3D space
81
+
82
+ 4. **Extrusion Operation Planning**:
83
+ - Identify extrusion parameters for each part (direction, distance)
84
+ - Understand how to add extrusions to existing solids (new or merge)
85
+ - Verify dimensions after extrusion match the description
86
+
87
+ 5. **Code Implementation Strategy**:
88
+ - Plan the sequence of CadQuery operations
89
+ - Determine necessary CadQuery functions and methods
90
+ - Consider how to organize code for clarity and readability
91
+
92
+ 6. **When scaling in CadQuery:**:
93
+ - Directly scale the size, define a scaling factor variable, and apply the scaling factor directly to all coordinates and dimensions
94
+ - Don't try to use a non-existent `.scale()` method on Workplane objects
95
+
96
+ After your thinking process, provide clean, working CadQuery Python code to create the 3D model.
97
+ Think step by step, but only keep a minimum draft for each thinking step, with 50 words at most.
98
+ description:
99
+ Start by creating a new coordinate system with Euler angles set to zero and a translation vector also set to zero. Next, draw a two-dimensional sketch on the first face. This sketch consists of a single loop made up of four lines. The first line starts at the origin (0.0, 0.0) and ends at (0.6, 0.0). The second line starts at (0.6, 0.0) and ends at (0.6, 0.375). The third line starts at (0.6, 0.375) and ends at (0.0, 0.375). Finally, the fourth line completes the loop by starting at (0.0, 0.375) and ending at the origin (0.0, 0.0). After drawing the sketch, apply a scale factor of 0.6 to the entire sketch. To transform the scaled two-dimensional sketch into a three-dimensional model, extrude the sketch 0.075 units along the normal direction. The final dimensions of the rectangular block are a length of 0.6 units, a width of 0.375 units, and a height of 0.075 units."""
100
+
101
+
102
+ def extract_code(response):
103
+ """Extract Python code from response"""
104
+ # Try \boxed{```python ... ```}
105
+ match = re.search(r'\\boxed\{```python\n(.*?)```\}', response, re.DOTALL)
106
+ if match:
107
+ return match.group(1).strip()
108
+ # Try ```python ... ```
109
+ match = re.search(r'```python\n(.*?)```', response, re.DOTALL)
110
+ if match:
111
+ return match.group(1).strip()
112
+ return response
113
+
114
+
115
+ def execute_code(code, output_path="output.py"):
116
+ """Save and execute CadQuery code"""
117
+ with open(output_path, "w", encoding="utf-8") as f:
118
+ f.write(code)
119
+ print(f"Code saved to {output_path}")
120
+
121
+ try:
122
+ exec_globals = {}
123
+ exec(code, exec_globals)
124
+ if 'r' in exec_globals:
125
+ print("Execution SUCCESS! Model 'r' created.")
126
+ return True, exec_globals['r']
127
+ else:
128
+ print("Execution completed but 'r' not found.")
129
+ return False, None
130
+ except Exception as e:
131
+ print(f"Execution FAILED: {e}")
132
+ return False, None
133
+
134
+
135
+ # Generate
136
  text = tokenizer.apply_chat_template(
137
  [{"role": "user", "content": prompt}],
138
  tokenize=False,
 
140
  )
141
  inputs = tokenizer([text], return_tensors="pt").to(model.device)
142
  outputs = model.generate(**inputs, max_new_tokens=2048)
143
+ response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
144
+
145
+ print("=" * 50)
146
+ print("Model Response:")
147
+ print("=" * 50)
148
+ print(response)
149
+
150
+ # Extract and execute
151
+ print("\n" + "=" * 50)
152
+ print("Extracted Code:")
153
+ print("=" * 50)
154
+ code = extract_code(response)
155
+ print(code)
156
+
157
+ print("\n" + "=" * 50)
158
+ print("Execution Result:")
159
+ print("=" * 50)
160
+ success, result = execute_code(code)
161
  ```
162
 
163
  ## Performance