File size: 2,358 Bytes
c27a247
 
 
 
 
 
 
9fe0c24
c27a247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from collections import defaultdict
import os
from huggingface_hub import HfApi, hf_hub_download


HUB_TOKEN = os.getenv("HUB_TOKEN")
REPO_ID = "ShapeNet/shapenetcore-gltf"


def get_dataset_classes():
    hf_api = HfApi()
    info = hf_api.dataset_info(repo_id=REPO_ID, token=HUB_TOKEN)
    dataset_classes = defaultdict(list)

    for file in info.siblings:
        if ".gltf" in file.rfilename:
            class_name = file.rfilename.split("/")[0]
            dataset_classes[class_name].append(file.rfilename)

    print(dataset_classes)
    return dataset_classes


dataset_dict = get_dataset_classes()
dataset_classes = list(dataset_dict.keys())
default_models = dataset_dict[dataset_classes[0]]


def load_mesh(mesh_file_name):
    return mesh_file_name, mesh_file_name


def update(asset_name):
    split_model_path = asset_name.split("/")
    asset_path = hf_hub_download(
        repo_id=REPO_ID,
        filename=split_model_path[1],
        subfolder=split_model_path[0],
        repo_type="dataset",
        use_auth_token=HUB_TOKEN,
    )
    print(asset_name)
    return asset_path


def update_models(class_name):
    model_choices = dataset_dict[class_name]
    return gr.Dropdown.update(choices=model_choices)


def update_model_list(class_name):
    model_choices = dataset_dict[class_name]
    return gr.Dropdown.update(choices=model_choices, value=model_choices[0])


def update_asset_path(model_name):
    return REPO_ID + "/" + model_name


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            inp = gr.Dropdown(choices=dataset_classes, interactive=True, label="3D Model Class", value=dataset_classes[0])
            out1 = gr.Dropdown(choices=default_models, interactive=True, label="3D Model", value=default_models[0])
            out3 = gr.Textbox(value=REPO_ID + "/" + out1.value, label="Asset Path")
        out2 = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0],  label="3D Model")

    # Update second dropdown
    inp.change(fn=update_model_list, inputs=inp, outputs=out1)

    # Update 3D model view
    inp.change(fn=update, inputs=out1, outputs=out2)
    out1.change(fn=update, inputs=out1, outputs=out2)

    # Update path to asset
    inp.change(fn=update_asset_path, inputs=out1, outputs=out3)
    out1.change(fn=update_asset_path, inputs=out1, outputs=out3)


demo.launch()