File size: 3,770 Bytes
0a4421d bea8151 7f0bd23 0a4421d 6b95b0d 0a4421d b9f4efc 6b95b0d 0a4421d 7c0b89e bea8151 7c0b89e bea8151 7c0b89e bea8151 7c0b89e b9f4efc 6b95b0d 0a4421d 7f0bd23 bea8151 7f0bd23 bea8151 7f0bd23 bea8151 7f0bd23 0a4421d 7418a86 0a4421d 7f0bd23 b9f4efc 6b95b0d 0a4421d 6b95b0d 0a4421d 7c0b89e 0a4421d 6b95b0d b9f4efc bea8151 b9f4efc |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import gradio as gr
import os
import modules.version_info as version_info
def getVersions():
#return html_versions
return version_info.versions_html()
def load_data(query_params, model_3d, image_slider):
# set default values or pull from querystring
model_url = query_params.get("3d", None) if query_params else None
hm_url = query_params.get("hm", None) if query_params else None
img_url = query_params.get("image", None) if query_params else None
slider_images = []
if hm_url:
slider_images.append(hm_url)
if img_url:
slider_images.append(img_url)
if not slider_images:
slider_images = ["images/beeuty_545jlbh1_v12_alpha96_300dpi.png", "images/beeuty_545jlbh1_v12_alpha96_300dpi_depth.png"]
if not model_url:
model_url = "models/beeuty_545jlbh1_300dpi.glb"
return model_url, slider_images
def process_upload(files):
"""
Process uploaded files and assign them to the appropriate component based on file extension.
Files with extensions in [".glb", ".gltf", ".obj", ".ply"] are sent to the Model3D component.
Files with extensions in [".png", ".jpg", ".jpeg"] are sent to the ImageSlider component.
Limits: 1 file for the 3D model and up to 2 files for the image slider.
"""
model_file = None
image_files = []
# ensure files is a list
if not isinstance(files, list):
files = [files]
for f in files:
# f can be a file path (string) or an object with attribute `name`
file_name = f.name if hasattr(f, "name") else f
ext = os.path.splitext(file_name)[1].lower()
if ext in [".glb", ".gltf", ".obj", ".ply"]:
if not model_file: # limit to one model file
model_file = file_name
elif ext in [".png", ".jpg", ".jpeg"]:
if len(image_files) < 2: # limit to two image files
image_files.append(file_name)
return model_file, image_files
gr.set_static_paths(paths=["images/", "models/", "assets/"])
with gr.Blocks(css_paths="style_20250314.css", title="3D viewer", theme='Surn/Beeuty',delete_cache=(21600,86400)) as viewer3d:
gr.Markdown("# 3D Model Viewer")
with gr.Row():
with gr.Column():
model_3d = gr.Model3D(
label="3D Model",
value=None,
height=480,
elem_id="model_3d", key="model_3d"
)
image_slider = gr.ImageSlider(
label="2D Images",
value=None,
height=480,
elem_id="image_slider", key="image_slider"
)
with gr.Row():
upload_btn = gr.UploadButton(
"Upload", elem_id="upload_btn", key="upload_btn",
file_count="multiple",
file_types=[".glb", ".gltf", ".obj", ".png", ".jpg", ".ply"]
)
with gr.Row():
gr.HTML(value=getVersions(), visible=True, elem_id="versions")
# Use JavaScript to pass the query parameters to your callback.
viewer3d.load(
load_data,
inputs=[gr.JSON(), model_3d, image_slider],
outputs=[model_3d, image_slider],
js="""() => {
const params = Object.fromEntries(new URLSearchParams(window.location.search));
return params;
}"""
)
# Process uploaded files to update the Model3D or ImageSlider component.
upload_btn.upload(
process_upload,
inputs=upload_btn,
outputs=[model_3d, image_slider]
)
if __name__ == "__main__":
viewer3d.launch(
allowed_paths=["assets", "assets/", "./assets", "images/", "./images", 'e:/TMP', 'models/'],
favicon_path="./assets/favicon.ico", show_api=False, strict_cors=False
) |