HoLa-BRep / app.py
YuXingyao's picture
fresh start
7b127f4
#Frontend
import sys
import gradio as gr
from pathlib import Path
import os
sys.path.insert(0, "/data")
from app.AppLayout import *
from app.GeneratingMethod import *
from app.ModelDirector import *
from app.DataProcessor import *
os.environ["HF_HOME"] = "/data/.huggingface"
os.environ["TORCH_HOME"] = "/data/.cache/torch"
# Theme
theme = gr.themes.Soft(
primary_hue="slate",
text_size="lg",
font=['IBM Plex Sans', 'ui-sans-serif', 'system-ui', gr.themes.GoogleFont('sans-serif')],
).set(
block_background_fill='*primary_200',
button_primary_background_fill='*primary_100',
body_background_fill='*secondary_50',
)
force_light = """
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'light') {
url.searchParams.set('__theme', 'light');
window.location.href = url.href;
}
}
"""
# 自定义CSS样式
custom_css = """
.gr-tabs.gr-tab-label {
text-align: center;
}
button[role="tab"] {
font-size: 20px;
}
div[role="tablist"] {
height: var(--size-12);
}
#top-row {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
#button-group {
display: flex;
gap: 10px;
justify-content: flex-end;
}
.small-button {
max-width: 80px;
padding: 6px 10px;
font-size: 14px;
}
@media (min-width: 1024px) {
div[role="tablist"] {
/* 电脑端居中 */
justify-content: center;
}
}
.tabs {
margin-top: 20px;
}
div[data-testid="markdown"] span p:not(:first-child) {
margin-top: unset;
}
div[data-testid="markdown"] pre code {
font-family: 'IBM Plex Sans', 'ui-sans-serif', 'system-ui', 'sans-serif';
font-size: 14px;
}
/* 媒体查询 */
@media (max-width: 768px) {
button[role="tab"] {
font-size: 15px;
}
p.title1 {
font-size: 46px !important;
letter-spacing: unset !important;
}
p.title2 {
font-size: 21px !important;
}
p.title4 {
font-size: 16px !important;
}
p.title5 {
font-size: 12px !important;
}
}
h2.heading {
font-size: 23px;
}
@media (max-width: 550px) {
.title3-responsive {
gap: 0 !important;
}
.title3-responsive span:first-child,
.title3-responsive span:last-child {
width: 20px !important;
}
.title3-responsive span:nth-child(2) {
padding: 2px 6px !important;
font-size: 12px;
}
}
@media (min-width: 551px) and (max-width: 768px) {
.title3-responsive {
gap: 0 !important;
}
.title3-responsive span:first-child,
.title3-responsive span:last-child {
width: 50px !important;
}
.title3-responsive span:nth-child(2) {
font-size: 15px;
}
}
@media (min-width: 769px) and (max-width: 1024px) {
.title3-responsive span:first-child,
.title3-responsive span:last-child {
width: 70px !important;
}
.title3-responsive span:nth-child(2) {
font-size: 20px;
}
}
@media (max-width: 768px) {
.mobile-break {
display: block;
}
}
@media (min-width: 769px) {
.mobile-break {
display: none;
}
}
"""
DEMO_NUM = 4
WIREFRAME_FILE = 0
SOLID_FILE = 1
STEP_FILE = 2
BACKEND_CONDITION_DICT = {
'Unconditional': 'uncond',
'Point Cloud' : 'pc',
'Text' : 'txt',
'Sketch' : 'sketch',
'SVR' : 'single_img',
'MVR': 'multi_img'
}
# Dynamically registered functions
def switch_model(user_state: dict, generate_mode: str, model_index: int, offset: int):
model_index = (model_index + offset) % DEMO_NUM
generate_mode = BACKEND_CONDITION_DICT[generate_mode]
# Check if the condition has been generated
if generate_mode not in user_state.keys():
return model_index, gr.update(value='empty.obj', label=f'Wireframe{model_index + 1}'), gr.update(value='empty.obj', label=f'Solid{model_index + 1}'), gr.update( value=["app/examples/empty_examples/sample.stl", "app/examples/empty_examples/sample.ply", "app/examples/empty_examples/sample.step"], label=f'Models{model_index + 1}')
# Check if model_index exceeds the number of current valid models
if model_index >= len(user_state[generate_mode]):
return model_index, gr.update(value='empty.obj', label=f'Wireframe{model_index + 1}'), gr.update(value='empty.obj', label=f'Solid{model_index + 1}'), gr.update( value=["app/examples/empty_examples/sample.stl", "app/examples/empty_examples/sample.ply", "app/examples/empty_examples/sample.step"], label=f'Models{model_index + 1}')
wireframe_model = user_state[generate_mode][model_index][WIREFRAME_FILE]
solid_model = user_state[generate_mode][model_index][SOLID_FILE]
if not (os.path.exists(wireframe_model) and os.path.exists(solid_model)):
gr.Warning("The operation is too frequent!", title="Frequent Operation")
return gr.update(), gr.update(), gr.update(), gr.update()
return model_index, gr.Model3D(wireframe_model, label=f'Wireframe{model_index + 1}'), gr.Model3D(solid_model, label=f'Solid{model_index + 1}'), gr.Files(user_state[generate_mode][model_index], label=f'Models{model_index + 1}', interactive=False)
def set_generating_type(mode):
return gr.Text(mode, visible=False)
def make_Chinese_descriptions():
return (title_cn,
description_cn,
UncondLayout().get_Chinese_note(),
PCLayout().get_Chinese_note(),
SketchLayout().get_Chinese_note(),
TextLayout().get_Chinese_note(),
SVRLayout().get_Chinese_note(),
MVRLayout().get_Chinese_note(),
notification_mvr_cn,
gr.update(label="无条件"),
gr.update(label="点云"),
gr.update(label="草图"),
gr.update(label="文本"),
gr.update(label="单视图"),
gr.update(label="多视图"),
gr.update(label="多视图输入注意事项:"),
gr.update(value="生成"),
gr.update(value="生成"),
gr.update(value="生成"),
gr.update(value="生成"),
gr.update(value="生成"),
gr.update(value="生成"),
gr.update(value="上一个"),
gr.update(value="下一个"),
gr.update(label="实体"),
gr.update(label="线框"),
gr.update(label="下载"),
citation_cn
)
def make_English_descriptions():
return (title_en,
description_en,
UncondLayout().get_English_note(),
PCLayout().get_English_note(),
SketchLayout().get_English_note(),
TextLayout().get_English_note(),
SVRLayout().get_English_note(),
MVRLayout().get_English_note(),
notification_mvr_en,
gr.update(label="Unconditional"),
gr.update(label="Point Cloud"),
gr.update(label="Sketch"),
gr.update(label="Text"),
gr.update(label="SVR"),
gr.update(label="MVR"),
gr.update(label="MVR input notification:"),
gr.update(value="generate"),
gr.update(value="generate"),
gr.update(value="generate"),
gr.update(value="generate"),
gr.update(value="generate"),
gr.update(value="generate"),
gr.update(value="Last"),
gr.update(value="Next"),
gr.update(label="Solid"),
gr.update(label="Wireframe"),
gr.update(label="Download"),
citation_en
)
# Declarations for pre-rendering
model_solid = gr.Model3D(label=f'Solid1', value='empty.obj', key="Solid")
model_wireframe = gr.Model3D(label=f'Wireframe1', value='empty.obj', key="Wireframe")
step_file = gr.File(label=f'Step', file_count='single', file_types=['.step'], interactive=False, visible=False)
download_files = gr.Files(label=f"Models1", value=["app/examples/empty_examples/sample.stl", "app/examples/empty_examples/sample.ply", "app/examples/empty_examples/sample.step"], interactive=False, key="Downloads")
input_tab = gr.Tabs()
generating_type = gr.Text("Unconditional",visible=False)
title_en = gr.Markdown(
"""
<h1 style='display: block; position: relative; text-align: center; text-rendering: optimizelegibility;'>
<p class='title1' style='font-size: 100px; text-align: center;'>
HoLa-BRep
</p>
<p class='title2' style='font-size: 32px; text-align: center;'>
HoLa: B-Rep Generation using a Holistic Latent Representation
</p>
<p class='title3-responsive' style='font-size: 22px; text-align: center; display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: nowrap;'>
<span style="width: 100px; height: 1px; background-color: #999;"></span>
<span style="color: #3b3891; padding: 4px 8px; border-radius: 8px; font-weight: bold;">
ACM Trans. on Graphics (SIGGRAPH) 2025
</span>
<span style="width: 100px; height: 1px; background-color: #999;"></span>
</p>
<p class='title4' style='font-size: 20px; text-align: center;'>
Yilin Liu, Duoteng Xu, Xingyao Yu, Xiang Xu, Daniel Cohen-Or, Hao Zhang, Hui Huang*
</p>
<p class='title5' style='font-size: 20px; text-align: center;'>
(Visual Computing Research Center, Shenzhen University)
</p>
</h1>
"""
)
title_cn = gr.Markdown(
"""
<h1 style='display: block; position: relative; text-align: center; text-rendering: optimizelegibility;'>
<p class='title1' style='font-size: 100px; text-align: center;'>
HoLa-BRep
</p>
<p class='title2' style='font-size: 32px; text-align: center;'>
HoLa: B-Rep Generation using a Holistic Latent Representation
</p>
<p class='title3-responsive' style='font-size: 22px; text-align: center; display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: nowrap;'>
<span style="width: 100px; height: 1px; background-color: #999;"></span>
<span style="color: #3b3891; padding: 4px 8px; border-radius: 8px; font-weight: bold;">
ACM Trans. on Graphics (SIGGRAPH) 2025
</span>
<span style="width: 100px; height: 1px; background-color: #999;"></span>
</p>
<p class='title4' style='font-size: 20px; text-align: center;'>
<span>刘奕林, 许铎腾, 余星耀, 徐翔, </span>
<br class="mobile-break">
<span>Daniel Cohen-Or, 张皓, 黄惠*</span>
</p>
<p class='title5' style='font-size: 20px; text-align: center;'>
(深圳大学可视计算研究中心)
</p>
</h1>
"""
)
description_en = gr.Markdown(
"""
# <h2 class='heading'>What is HoLa-BRep?</h2>
HoLa-BRep is a generative model that produces CAD models in boundary representation (BRep) based on various conditions, including point cloud, single-view image, multi-view images, single-view sketch or text description.
It contains **1 unified** BRep variational encoder (VAE) to encode a BRep model's topological and geometric information into a holistic latent space, and a latent diffusion model (LDM) to generate such latent from multiple modalities.
Compared with the state-of-the-art method, HoLa-BRep only has 1 unified VAE and the corresponding latent space and 1 LDM for generation, so it is easier to train the model without any inter-dependency of the model. This is extremely useful when incorporating more modalities and even mix-modality training.
# <h2 class='heading'>How to use it?</h2>
+ Please refer to the example below for more details. You can select the desired **modality** below and upload your own data.
+ We generate **4** plausible BRep models for each input(**about 3 minutes**) and visualize them in the 3D viewer.
+ Try to explore the generated BRep models by rotating, zooming, and panning the 3D viewer, or **download** either the wireframe, surface mesh, or solid BRep model as OBJ or STEP files.
# <h2 class='heading'>Project page</h2>
+ https://vcc.tech/research/2025/HolaBRep
"""
)
description_cn = gr.Markdown(
"""
# <h2 class='heading'>HoLa-BRep是什么?</h2>
HoLa-BRep 是一个多模态CAD生成模型,它支持输入点云、单视角图像、多视角图像、单视角草图或文本描述等多种模态条件,生成边界表示 (BRep) 的 CAD 模型。
它包含**1个统一**的 BRep变分自编码器 (VAE),可将 BRep 模型的拓扑和几何信息编码到一个结构化的低维隐空间,以及一个隐式扩散模型(LDM)用于从多种模态生成这种BRep结构化嵌入。
与目前国内外领先技术相比,HoLa-BRep 只有1个 自编码器和1个扩散模型的特性极大地减少了训练的复杂程度并且利于向更大规模的训练拓展。同时这种单个结构化隐空间的设计模式也解决了现有方法多个模型相互依赖复杂的问题。在结合更多模态甚至混合模态训练时能显著提升训练效率。
# <h2 class='heading'>如何使用?</h2>
+ 请参考下面的示例。您可以在下面选择所需的**模式**并上传自己的数据。
+ 我们会为每个输入生成 4 个可选的 BRep 模型(**大约3分钟**),并在 3D 查看器中可视化。
+ 你可以通过旋转、缩放和平移等操作查看生成的 BRep 模型,也可以将线框、曲面网格或实体 BRep 模型下载为 OBJ 或 STEP 文件。
# <h2 class='heading'>项目主页</h2>
+ https://vcc.tech/research/2025/HolaBRep
"""
)
citation_en = gr.Markdown(
value=
"""
<h2 class='heading' style='margin-top: 16px;'>Citation</h2>
If our work is helpful for your research or applications, please cite us via:
<br>
```
@article{HolaBRep25,
title={HoLa: B-Rep Generation using a Holistic Latent Representation},
author={Yilin Liu and Duoteng Xu and Xinyao Yu and Xiang Xu and Daniel Cohen-Or and Hao Zhang and Hui Huang},
journal={ACM Transactions on Graphics (SIGGRAPH)},
volume={44},
number={4},
year={2025},
}
```
""",
height=300,
)
citation_cn = gr.Markdown(
value=
"""
<h2 class='heading' style='margin-top: 16px;'>引用</h2>
如果我们的工作对您的研究或者应用有帮助,请通过以下方式进行引用:
<br>
```
@article{HolaBRep25,
title={HoLa: B-Rep Generation using a Holistic Latent Representation},
author={Yilin Liu and Duoteng Xu and Xinyao Yu and Xiang Xu and Daniel Cohen-Or and Hao Zhang and Hui Huang},
journal={ACM Transactions on Graphics (SIGGRAPH)},
volume={44},
number={4},
year={2025},
}
```
""",
height=300,
)
notification_mvr_en = gr.Markdown("**You can take and upload photos of objects as shown below.**")
notification_mvr_cn = gr.Markdown("**您可以按如下方式拍摄并上传物体照片**")
descriptions = []
# Main body
with gr.Blocks(js=force_light, theme=theme, css=custom_css) as inference:
with gr.Row(elem_id="top-row"):
gr.HTML(
"""
<div style="text-align: left;">
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FYuXingyao%2FHoLa-BRep">
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FYuXingyao%2FHoLa-BRep&labelColor=%23d9e3f0&countColor=%23555555" />
</a>
</div>
"""
)
with gr.Row(elem_id="button-group"):
btn_cn = gr.Button("中文", elem_classes="small-button")
btn_en = gr.Button("English", elem_classes="small-button")
btn_cn.click(fn=make_Chinese_descriptions, outputs=descriptions)
btn_en.click(fn=make_English_descriptions, outputs=descriptions)
title_en.render()
descriptions.append(title_en)
description_en.render()
descriptions.append(description_en)
user_state = gr.BrowserState({
"user_id" : None,
"user_output_dir" : None,
})
generating_type.render()
with gr.Row():
# Input Column
with gr.Column() as input_col:
with gr.Tabs() as input_tab:
with gr.Tab("Unconditional") as uncond_tab:
uncond_layout = UncondLayout()
uncond_description = uncond_layout.get_English_note()
descriptions.append(uncond_description)
uncond_input_components = uncond_layout.get_input_components()
uncond_button = gr.Button("Generate")
uncond_button.click(
fn=UncondGeneratingMethod().generate(),
inputs=[*uncond_input_components, user_state],
outputs=[model_wireframe, model_solid, step_file, download_files, user_state]
)
with gr.Tab("Point Cloud") as pc_tab:
pc_layout = PCLayout()
pc_description = pc_layout.get_English_note()
descriptions.append(pc_description)
pc_input_components = pc_layout.get_input_components()
pc_button = gr.Button("Generate")
pc_button.click(
fn=ConditionedGeneratingMethod(PointCloudDirector(), PointCloudProcessor(), DEMO_NUM).generate(),
inputs=[user_state, *pc_input_components],
outputs=[user_state, model_wireframe, model_solid, step_file, download_files]
)
with gr.Tab("Sketch") as sketch_tab:
sketch_layout = SketchLayout()
sketch_description = sketch_layout.get_English_note()
descriptions.append(sketch_description)
sketch_input_components = sketch_layout.get_input_components()
sketch_button = gr.Button("Generate")
sketch_button.click(
fn=ConditionedGeneratingMethod(SketchDirector(), SingleImageProcessor(), DEMO_NUM).generate(),
inputs=[user_state, *sketch_input_components],
outputs=[user_state, model_wireframe, model_solid, step_file, download_files]
)
with gr.Tab("Text") as text_tab:
text_layout = TextLayout()
text_description = text_layout.get_English_note()
descriptions.append(text_description)
text_input_components = text_layout.get_input_components()
text_button = gr.Button("Generate")
text_button.click(
fn=ConditionedGeneratingMethod(TextDirector(), TextProcessor(), DEMO_NUM).generate(),
inputs=[user_state, *text_input_components],
outputs=[user_state, model_wireframe, model_solid, step_file, download_files]
)
with gr.Tab("SVR") as svr_tab:
svr_layout = SVRLayout()
svr_description = svr_layout.get_English_note()
descriptions.append(svr_description)
svr_input_components = svr_layout.get_input_components()
svr_button = gr.Button("Generate")
svr_button.click(
fn=ConditionedGeneratingMethod(SVRDirector(), SingleImageProcessor(), DEMO_NUM).generate(),
inputs=[user_state, *svr_input_components],
outputs=[user_state, model_wireframe, model_solid, step_file, download_files]
)
with gr.Tab("MVR") as mvr_tab:
mvr_layout = MVRLayout()
mvr_description = mvr_layout.get_English_note()
descriptions.append(mvr_description)
with gr.Accordion("MVR input notification:", open=False) as mvr_notification:
notification_mvr_en.render()
gr.Image(value='app/examples/mvr.png',show_download_button=False, show_label=False,show_share_button=False,interactive=False)
with gr.Row():
mvr_input_components = mvr_layout.get_input_components()
mvr_button = gr.Button("Generate")
mvr_button.click(
fn=ConditionedGeneratingMethod(MVRDirector(), MultiImageProcessor(), DEMO_NUM).generate(),
inputs=[user_state, *mvr_input_components],
outputs=[user_state, model_wireframe, model_solid, step_file, download_files]
)
uncond_tab.select(fn=set_generating_type, inputs=gr.Text(uncond_tab.label, visible=False), outputs=generating_type)
pc_tab.select(fn=set_generating_type, inputs=gr.Text(pc_tab.label, visible=False), outputs=generating_type)
sketch_tab.select(fn=set_generating_type, inputs=gr.Text(sketch_tab.label, visible=False), outputs=generating_type)
svr_tab.select(fn=set_generating_type, inputs=gr.Text(svr_tab.label, visible=False), outputs=generating_type)
mvr_tab.select(fn=set_generating_type, inputs=gr.Text(mvr_tab.label, visible=False), outputs=generating_type)
text_tab.select(fn=set_generating_type, inputs=gr.Text(text_tab.label, visible=False), outputs=generating_type)
descriptions.append(notification_mvr_en)
descriptions.append(uncond_tab)
descriptions.append(pc_tab)
descriptions.append(sketch_tab)
descriptions.append(text_tab)
descriptions.append(svr_tab)
descriptions.append(mvr_tab)
descriptions.append(mvr_notification)
descriptions.append(uncond_button)
descriptions.append(pc_button)
descriptions.append(sketch_button)
descriptions.append(text_button)
descriptions.append(svr_button)
descriptions.append(mvr_button)
# Output demonstration
with gr.Column() as output_col:
with gr.Tabs():
with gr.Tab("Solid") as solid_tab:
model_solid.render()
with gr.Tab("Wireframe") as wireframe_tab:
model_wireframe.render()
with gr.Tab("Download") as download_tab:
step_file.render()
download_files.render()
model_index = gr.Number(value=0, visible=False)
with gr.Row() as switch_row:
last_button = gr.Button("Last")
next_button = gr.Button("Next")
last_button.click(
fn=switch_model,
inputs=[user_state, generating_type, model_index, gr.Number(-1, visible=False)],
outputs=[model_index, model_wireframe, model_solid, download_files])
next_button.click(
fn=switch_model,
inputs=[user_state, generating_type, model_index, gr.Number(1, visible=False)],
outputs=[model_index, model_wireframe, model_solid, download_files])
descriptions.append(last_button)
descriptions.append(next_button)
descriptions.append(solid_tab)
descriptions.append(wireframe_tab)
descriptions.append(download_tab)
# Examples
@gr.render(inputs=[generating_type], triggers=[generating_type.change, inference.load])
def show_examples(generate_mode):
if generate_mode == "Unconditional":
pass
elif generate_mode == "Point Cloud":
pc_samples=[
[Path("app/examples/pc_examples") / sample_number / "pc.png"] for sample_number in os.listdir("app/examples/pc_examples") if sample_number != "take_photo.py"
]
with gr.Row():
def dummy_pc_func(pic_path):
return Path(pic_path[0]).with_suffix(".ply").as_posix()
for i in range(len(pc_samples)):
with gr.Column(min_width=100):
dummy_image = gr.Image(type="filepath", format="png", visible=False)
point_cloud_data = gr.Dataset(
label=f"Example{i+1}",
components=[dummy_image],
samples=[pc_samples[i]],
layout="table"
)
point_cloud_data.click(dummy_pc_func, inputs=point_cloud_data, outputs=pc_input_components)
elif generate_mode == "Text":
text_data = gr.Dataset(
components=text_input_components,
samples=[
["The object is a rectangular prism with two protruding L-shaped sections on opposite sides."],
["This design creates a rectangular plate with rounded edges. The plate measures about 0.3214 units in length, 0.75 units in width, and 0.0429 units in height. The rounded edges give the plate a smooth, aesthetically pleasing appearance."],
["The U-shaped bracket has a flat top and a curved bottom. The design begins by creating a new coordinate system with specific Euler angles and a translation vector. A two-dimensional sketch is then drawn, forming a complex shape with multiple lines and arcs. This sketch is scaled down, rotated, and translated to align with the coordinate system. The sketch is extruded to create a three-dimensional model. The final dimensions of the bracket are approximately 0.7 units in length, 0.75 units in width, and 0.19 units in height. The bracket is designed to integrate seamlessly with other components, providing a sturdy and functional structure."]
],
layout='table',
label="Examples",
headers=["Prompt"]
)
def dummy_func(text):
return gr.Text(text[0])
text_data.click(fn=dummy_func, inputs=text_data, outputs=text_input_components)
elif generate_mode == "Sketch":
with gr.Row():
for i in range(12):
with gr.Column(min_width=100):
example = gr.Examples(
inputs=sketch_input_components,
examples=[
[f"app/examples/sketch_examples/{i + 1}.png"]
],
label=f"Example{i+1}"
)
elif generate_mode == "SVR":
with gr.Row():
for i in range(12):
with gr.Column(min_width=100):
example = gr.Examples(
inputs=svr_input_components,
examples=[
[f"app/examples/svr_examples/{i + 1}.png"]
],
label=f"Example{i+1}"
)
elif generate_mode == "MVR":
with gr.Row():
for i in range(4):
file_num = ["00017462", "00131007", "00189220", "00218887"]
with gr.Column():
example = gr.Examples(
inputs=mvr_input_components,
examples=[
[f"app/examples/mvr_examples/{file_num[i]}_img0.png", f"app/examples/mvr_examples/{file_num[i]}_img1.png", f"app/examples/mvr_examples/{file_num[i]}_img2.png", f"app/examples/mvr_examples/{file_num[i]}_img3.png"],
],
label=f"Example{i+1}"
)
citation_en.render()
descriptions.append(citation_en)
if __name__ == "__main__":
inference.launch(allowed_paths=['/data'], server_name='0.0.0.0', server_port=7860)