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