File size: 1,915 Bytes
e0d1fbf
 
d464558
 
e0d1fbf
d464558
 
 
e8919fa
 
 
e0d1fbf
e8919fa
e0d1fbf
 
 
d464558
 
 
e0d1fbf
 
 
 
 
 
 
 
 
 
 
 
d464558
 
 
e0d1fbf
 
 
d464558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
from pathlib import Path
import gradio as gr
from huggingface_hub import hf_hub_download

# -------------------------------
# Step 1: Add CADFusion repo to sys.path
# -------------------------------
# CADFusion folder must contain the 'cadfusion/' subfolder
CADFUSION_DIR = Path("CADFusion")
sys.path.append(str(CADFUSION_DIR.resolve()))

# Now Python can find the 'cadfusion' module
from cadfusion.inference import InferenceRunner
from cadfusion.utils.config import load_config

# -------------------------------
# Step 2: Download config & checkpoint from Hugging Face Hub
# -------------------------------
CONFIG_PATH = hf_hub_download(
    repo_id="microsoft/CADFusion",
    filename="adapter_config.json",
    revision="v1_1"
)

CHECKPOINT_PATH = hf_hub_download(
    repo_id="microsoft/CADFusion",
    filename="adapter_model.safetensors",
    revision="v1_1"
)

# -------------------------------
# Step 3: Initialize CADFusion runner
# -------------------------------
config = load_config(CONFIG_PATH)
runner = InferenceRunner(config=config)
runner.load_checkpoint(CHECKPOINT_PATH)

# -------------------------------
# Step 4: Define CAD generation function
# -------------------------------
def generate_cad(prompt: str):
    try:
        outputs = runner.infer_from_text(prompt)
        out_file = "output.stl"
        mesh = outputs.to_trimesh()
        mesh.export(out_file)
        return out_file
    except Exception as e:
        return f"Error during inference: {str(e)}"

# -------------------------------
# Step 5: Gradio interface
# -------------------------------
demo = gr.Interface(
    fn=generate_cad,
    inputs=gr.Textbox(label="Enter CAD description"),
    outputs=gr.File(label="Generated STL Model"),
    title="CADFusion - Text to CAD",
    description="Enter a natural language description and generate a 3D CAD mesh (STL)."
)

if __name__ == "__main__":
    demo.launch()