CarlCochet commited on
Commit
c27a247
1 Parent(s): 7f7ff09

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from collections import defaultdict
3
+ import os
4
+ from huggingface_hub import HfApi, hf_hub_download
5
+
6
+
7
+ HUB_TOKEN = os.getenv("HUB_TOKEN")
8
+ REPO_ID = "simulate-explorer/shapenetcore-gltf"
9
+
10
+
11
+ def get_dataset_classes():
12
+ hf_api = HfApi()
13
+ info = hf_api.dataset_info(repo_id=REPO_ID, token=HUB_TOKEN)
14
+ dataset_classes = defaultdict(list)
15
+
16
+ for file in info.siblings:
17
+ if ".gltf" in file.rfilename:
18
+ class_name = file.rfilename.split("/")[0]
19
+ dataset_classes[class_name].append(file.rfilename)
20
+
21
+ print(dataset_classes)
22
+ return dataset_classes
23
+
24
+
25
+ dataset_dict = get_dataset_classes()
26
+ dataset_classes = list(dataset_dict.keys())
27
+ default_models = dataset_dict[dataset_classes[0]]
28
+
29
+
30
+ def load_mesh(mesh_file_name):
31
+ return mesh_file_name, mesh_file_name
32
+
33
+
34
+ def update(asset_name):
35
+ split_model_path = asset_name.split("/")
36
+ asset_path = hf_hub_download(
37
+ repo_id=REPO_ID,
38
+ filename=split_model_path[1],
39
+ subfolder=split_model_path[0],
40
+ repo_type="dataset",
41
+ use_auth_token=HUB_TOKEN,
42
+ )
43
+ print(asset_name)
44
+ return asset_path
45
+
46
+
47
+ def update_models(class_name):
48
+ model_choices = dataset_dict[class_name]
49
+ return gr.Dropdown.update(choices=model_choices)
50
+
51
+
52
+ def update_model_list(class_name):
53
+ model_choices = dataset_dict[class_name]
54
+ return gr.Dropdown.update(choices=model_choices, value=model_choices[0])
55
+
56
+
57
+ def update_asset_path(model_name):
58
+ return REPO_ID + "/" + model_name
59
+
60
+
61
+ with gr.Blocks() as demo:
62
+ with gr.Row():
63
+ with gr.Column():
64
+ inp = gr.Dropdown(choices=dataset_classes, interactive=True, label="3D Model Class", value=dataset_classes[0])
65
+ out1 = gr.Dropdown(choices=default_models, interactive=True, label="3D Model", value=default_models[0])
66
+ out3 = gr.Textbox(value=REPO_ID + "/" + out1.value, label="Asset Path")
67
+ out2 = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model")
68
+
69
+ # Update second dropdown
70
+ inp.change(fn=update_model_list, inputs=inp, outputs=out1)
71
+
72
+ # Update 3D model view
73
+ inp.change(fn=update, inputs=out1, outputs=out2)
74
+ out1.change(fn=update, inputs=out1, outputs=out2)
75
+
76
+ # Update path to asset
77
+ inp.change(fn=update_asset_path, inputs=out1, outputs=out3)
78
+ out1.change(fn=update_asset_path, inputs=out1, outputs=out3)
79
+
80
+
81
+ demo.launch()