Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,20 @@
|
|
| 1 |
import sys
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
sys.path.append(str(CADFUSION_DIR))
|
| 7 |
|
| 8 |
from cadfusion.inference import InferenceRunner
|
| 9 |
from cadfusion.utils.config import load_config
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
CONFIG_PATH = hf_hub_download(
|
| 15 |
repo_id="microsoft/CADFusion",
|
| 16 |
filename="adapter_config.json",
|
|
@@ -23,7 +27,36 @@ CHECKPOINT_PATH = hf_hub_download(
|
|
| 23 |
revision="v1_1"
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
| 27 |
config = load_config(CONFIG_PATH)
|
| 28 |
runner = InferenceRunner(config=config)
|
| 29 |
runner.load_checkpoint(CHECKPOINT_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sys
|
| 2 |
from pathlib import Path
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# Step 1: Add CADFusion repo to sys.path
|
| 8 |
+
# -------------------------------
|
| 9 |
+
CADFUSION_DIR = Path("CADFusion") # make sure this folder exists in your Space
|
| 10 |
sys.path.append(str(CADFUSION_DIR))
|
| 11 |
|
| 12 |
from cadfusion.inference import InferenceRunner
|
| 13 |
from cadfusion.utils.config import load_config
|
| 14 |
|
| 15 |
+
# -------------------------------
|
| 16 |
+
# Step 2: Download config & checkpoint from Hugging Face Hub
|
| 17 |
+
# -------------------------------
|
| 18 |
CONFIG_PATH = hf_hub_download(
|
| 19 |
repo_id="microsoft/CADFusion",
|
| 20 |
filename="adapter_config.json",
|
|
|
|
| 27 |
revision="v1_1"
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# -------------------------------
|
| 31 |
+
# Step 3: Initialize CADFusion runner
|
| 32 |
+
# -------------------------------
|
| 33 |
config = load_config(CONFIG_PATH)
|
| 34 |
runner = InferenceRunner(config=config)
|
| 35 |
runner.load_checkpoint(CHECKPOINT_PATH)
|
| 36 |
+
|
| 37 |
+
# -------------------------------
|
| 38 |
+
# Step 4: Define CAD generation function
|
| 39 |
+
# -------------------------------
|
| 40 |
+
def generate_cad(prompt: str):
|
| 41 |
+
try:
|
| 42 |
+
outputs = runner.infer_from_text(prompt)
|
| 43 |
+
out_file = "output.stl"
|
| 44 |
+
mesh = outputs.to_trimesh()
|
| 45 |
+
mesh.export(out_file)
|
| 46 |
+
return out_file
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error during inference: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# -------------------------------
|
| 51 |
+
# Step 5: Gradio interface
|
| 52 |
+
# -------------------------------
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=generate_cad,
|
| 55 |
+
inputs=gr.Textbox(label="Enter CAD description"),
|
| 56 |
+
outputs=gr.File(label="Generated STL Model"),
|
| 57 |
+
title="CADFusion - Text to CAD",
|
| 58 |
+
description="Enter a natural language description and generate a 3D CAD mesh (STL)."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch()
|