angli66 commited on
Commit
624679b
1 Parent(s): 2488b02

fix queue and update Model3D description

Browse files
gradio_model3dcolor-0.0.1-py3-none-any.whl CHANGED
Binary files a/gradio_model3dcolor-0.0.1-py3-none-any.whl and b/gradio_model3dcolor-0.0.1-py3-none-any.whl differ
 
gradio_model3dnormal-0.0.1-py3-none-any.whl CHANGED
Binary files a/gradio_model3dnormal-0.0.1-py3-none-any.whl and b/gradio_model3dnormal-0.0.1-py3-none-any.whl differ
 
launch.py CHANGED
@@ -16,15 +16,19 @@ from gradio_model3dcolor import Model3DColor
16
  from gradio_model3dnormal import Model3DNormal
17
 
18
  is_local_run = os.path.exists("../SpaRP_API")
19
- code_dir = snapshot_download("sudo-ai/SpaRP_API", token=os.environ['HF_TOKEN']) if not is_local_run else "../SpaRP_API"
 
 
 
 
20
 
21
  if not is_local_run:
22
- zip_file_path = f'{code_dir}/examples.zip'
23
  # Unzipping the file into the current directory
24
- with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
25
  zip_ref.extractall(os.getcwd())
26
 
27
- with open(f'{code_dir}/api.json', 'r') as file:
28
  api_dict = json.load(file)
29
  SEGM_i_CALL = api_dict["SEGM_i_CALL"]
30
  SEGM_CALL = api_dict["SEGM_CALL"]
@@ -40,6 +44,9 @@ _DESCRIPTION = (
40
  )
41
  _PR = """
42
  <div>
 
 
 
43
  <b><em>Check out <a href="https://www.sudo.ai/3dgen">Hillbot (sudoAI)</a> for more details and advanced features.</em></b>
44
  </div>
45
  """
@@ -97,24 +104,19 @@ def create_tmp_dir():
97
  return tmp_dir
98
 
99
 
100
- def preprocess_imgs(tmp_dir, input_img, idx=None):
101
- if isinstance(input_img, list) and idx is None:
102
- for i, img_tuple in enumerate(input_img):
103
- Image.open(img_tuple[0]).save(f"{tmp_dir}/input_{i}.png")
104
- os.system(SEGM_i_CALL.replace("{tmp_dir}", tmp_dir).replace("{i}", str(i)))
105
- return [Image.open(f"{tmp_dir}/seg_{i}.png") for i in range(len(input_img))]
106
-
107
- if idx is not None:
108
- print("image idx:", int(idx))
109
- input_img = Image.open(input_img[int(idx)][0])
110
- input_img.save(f"{tmp_dir}/input.png")
111
- os.system(SEGM_CALL.replace("{tmp_dir}", tmp_dir))
112
- processed_img = Image.open(f"{tmp_dir}/seg.png")
113
- return processed_img.resize((320, 320), Image.Resampling.LANCZOS)
114
 
115
 
116
  def ply_to_glb(ply_path):
117
- script_path = f"{code_dir}/ply2glb.py"
118
  result = subprocess.run(
119
  ["python", script_path, "--", ply_path],
120
  capture_output=True,
@@ -129,8 +131,12 @@ def ply_to_glb(ply_path):
129
 
130
 
131
  def mesh_gen(tmp_dir, use_seg):
132
- os.system(UNPOSED_CALL.replace("{tmp_dir}", tmp_dir).replace("{use_seg}", str(use_seg)))
133
- os.system(MESH_CALL.replace("{tmp_dir}", tmp_dir))
 
 
 
 
134
 
135
  mesh = trimesh.load_mesh(f"{tmp_dir}/mesh.ply")
136
  vertex_normals = mesh.vertex_normals
@@ -204,7 +210,6 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
204
  rows=[2],
205
  object_fit="contain",
206
  height=400,
207
- show_share_button=False,
208
  )
209
  input_image = gr.Image(
210
  type="pil",
@@ -219,7 +224,6 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
219
  object_fit="contain",
220
  height=400,
221
  interactive=False,
222
- show_share_button=False,
223
  )
224
  with gr.Row():
225
  with gr.Column(scale=5):
@@ -260,6 +264,7 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
260
  )
261
 
262
  # Callbacks
 
263
  disable_button = lambda: gr.Button(interactive=False)
264
  enable_button = lambda: gr.Button(interactive=True)
265
  update_guide = lambda GUIDE_TEXT, icon_type="info": gr.HTML(
@@ -272,7 +277,15 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
272
 
273
  def not_cleared(content):
274
  if not content:
275
- raise ValueError # gr.Error(visible=False) doesn't work, trick for not showing error message
 
 
 
 
 
 
 
 
276
 
277
  # Upload event listener for input gallery
278
  input_gallery.upload(
@@ -282,7 +295,7 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
282
  ).success(
283
  fn=create_tmp_dir,
284
  outputs=[tmp_dir_unposed],
285
- queue=False,
286
  ).success(
287
  fn=partial(
288
  update_guide, "Removing background of the input image(s)...", "wait"
@@ -298,6 +311,10 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
298
  fn=partial(update_guide, "Click <b>Generate</b> to generate mesh.", "cursor"),
299
  outputs=[guide_text],
300
  queue=False,
 
 
 
 
301
  ).success(
302
  fn=enable_button,
303
  outputs=[run_btn],
@@ -321,14 +338,6 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
321
  fn=lambda: None,
322
  outputs=[processed_gallery],
323
  queue=False,
324
- ).success(
325
- fn=lambda: None,
326
- outputs=[mesh_output],
327
- queue=False,
328
- ).success(
329
- fn=lambda: None,
330
- outputs=[mesh_output_normal],
331
- queue=False,
332
  ).success(
333
  fn=partial(
334
  update_guide,
@@ -348,18 +357,10 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
348
  fn=disable_button,
349
  outputs=run_btn,
350
  queue=False,
351
- ).success(
352
- fn=lambda: None,
353
- outputs=[mesh_output],
354
- queue=False,
355
- ).success(
356
- fn=lambda: None,
357
- outputs=[mesh_output_normal],
358
- queue=False,
359
  ).success(
360
  fn=create_tmp_dir,
361
  outputs=tmp_dir_unposed,
362
- queue=False,
363
  ).success(
364
  fn=partial(
365
  update_guide, "Removing background of the input image(s)...", "wait"
@@ -375,6 +376,10 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
375
  fn=partial(update_guide, "Click <b>Generate</b> to generate mesh.", "cursor"),
376
  outputs=[guide_text],
377
  queue=False,
 
 
 
 
378
  ).success(
379
  fn=enable_button,
380
  outputs=run_btn,
@@ -398,11 +403,21 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
398
  fn=partial(update_guide, "Generating the mesh...", "wait"),
399
  outputs=[guide_text],
400
  queue=False,
 
 
 
 
 
401
  ).success(
402
  fn=mesh_gen,
403
  inputs=[tmp_dir_unposed, bg_removed_checkbox],
404
  outputs=[mesh_output, mesh_output_normal],
405
  queue=True,
 
 
 
 
 
406
  ).success(
407
  fn=partial(
408
  update_guide,
@@ -411,6 +426,10 @@ with gr.Blocks(title=_TITLE, css="style.css", theme=custom_theme) as demo:
411
  ),
412
  outputs=[guide_text],
413
  queue=False,
 
 
 
 
414
  ).success(
415
  fn=enable_button,
416
  outputs=[run_btn],
@@ -423,4 +442,4 @@ demo.queue().launch(
423
  inline=False,
424
  show_api=False,
425
  server_name="0.0.0.0",
426
- )
 
16
  from gradio_model3dnormal import Model3DNormal
17
 
18
  is_local_run = os.path.exists("../SpaRP_API")
19
+ code_dir = (
20
+ snapshot_download("sudo-ai/SpaRP_API", token=os.environ["HF_TOKEN"])
21
+ if not is_local_run
22
+ else "../SpaRP_API"
23
+ )
24
 
25
  if not is_local_run:
26
+ zip_file_path = f"{code_dir}/examples.zip"
27
  # Unzipping the file into the current directory
28
+ with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
29
  zip_ref.extractall(os.getcwd())
30
 
31
+ with open(f"{code_dir}/api.json", "r") as file:
32
  api_dict = json.load(file)
33
  SEGM_i_CALL = api_dict["SEGM_i_CALL"]
34
  SEGM_CALL = api_dict["SEGM_CALL"]
 
44
  )
45
  _PR = """
46
  <div>
47
+ <b><em>Project page: <a href="https://chaoxu.xyz/sparp/">https://chaoxu.xyz/sparp/</a></em></b>
48
+ </div>
49
+ <div>
50
  <b><em>Check out <a href="https://www.sudo.ai/3dgen">Hillbot (sudoAI)</a> for more details and advanced features.</em></b>
51
  </div>
52
  """
 
104
  return tmp_dir
105
 
106
 
107
+ def preprocess_imgs(tmp_dir, input_img):
108
+ for i, img_tuple in enumerate(input_img):
109
+ img = Image.open(img_tuple[0])
110
+ img.thumbnail([2048, 2048], Image.Resampling.LANCZOS)
111
+ img.save(f"{tmp_dir}/input_{i}.png")
112
+ os.system(
113
+ f"curl -L -F tmp_dir={tmp_dir} -F fn={tmp_dir}/seg_{i}.png -F image=@{tmp_dir}/input_{i}.png {SEGM_URL}/segm_img -o {tmp_dir}/seg_{i}.png"
114
+ )
115
+ return [Image.open(f"{tmp_dir}/seg_{i}.png") for i in range(len(input_img))]
 
 
 
 
 
116
 
117
 
118
  def ply_to_glb(ply_path):
119
+ script_path = "../utils/ply2glb.py"
120
  result = subprocess.run(
121
  ["python", script_path, "--", ply_path],
122
  capture_output=True,
 
131
 
132
 
133
  def mesh_gen(tmp_dir, use_seg):
134
+ os.system(
135
+ f"curl -L -F tmp_dir={tmp_dir} -F use_seg={use_seg} {UNPOSED_URL}/unposed_to_mv"
136
+ )
137
+ os.system(
138
+ f"curl -L -F tmp_dir={tmp_dir} {MESH_URL}/mv_to_mesh -o {tmp_dir}/mesh.ply"
139
+ )
140
 
141
  mesh = trimesh.load_mesh(f"{tmp_dir}/mesh.ply")
142
  vertex_normals = mesh.vertex_normals
 
210
  rows=[2],
211
  object_fit="contain",
212
  height=400,
 
213
  )
214
  input_image = gr.Image(
215
  type="pil",
 
224
  object_fit="contain",
225
  height=400,
226
  interactive=False,
 
227
  )
228
  with gr.Row():
229
  with gr.Column(scale=5):
 
264
  )
265
 
266
  # Callbacks
267
+ generating_mesh = gr.State(False)
268
  disable_button = lambda: gr.Button(interactive=False)
269
  enable_button = lambda: gr.Button(interactive=True)
270
  update_guide = lambda GUIDE_TEXT, icon_type="info": gr.HTML(
 
277
 
278
  def not_cleared(content):
279
  if not content:
280
+ raise ValueError
281
+
282
+ def toggle_mesh_generation_status(generating_mesh):
283
+ generating_mesh = not generating_mesh
284
+ return generating_mesh
285
+
286
+ def is_generating_mesh(generating_mesh):
287
+ if generating_mesh:
288
+ raise ValueError
289
 
290
  # Upload event listener for input gallery
291
  input_gallery.upload(
 
295
  ).success(
296
  fn=create_tmp_dir,
297
  outputs=[tmp_dir_unposed],
298
+ queue=True,
299
  ).success(
300
  fn=partial(
301
  update_guide, "Removing background of the input image(s)...", "wait"
 
311
  fn=partial(update_guide, "Click <b>Generate</b> to generate mesh.", "cursor"),
312
  outputs=[guide_text],
313
  queue=False,
314
+ ).success(
315
+ fn=is_generating_mesh,
316
+ inputs=[generating_mesh],
317
+ queue=False,
318
  ).success(
319
  fn=enable_button,
320
  outputs=[run_btn],
 
338
  fn=lambda: None,
339
  outputs=[processed_gallery],
340
  queue=False,
 
 
 
 
 
 
 
 
341
  ).success(
342
  fn=partial(
343
  update_guide,
 
357
  fn=disable_button,
358
  outputs=run_btn,
359
  queue=False,
 
 
 
 
 
 
 
 
360
  ).success(
361
  fn=create_tmp_dir,
362
  outputs=tmp_dir_unposed,
363
+ queue=True,
364
  ).success(
365
  fn=partial(
366
  update_guide, "Removing background of the input image(s)...", "wait"
 
376
  fn=partial(update_guide, "Click <b>Generate</b> to generate mesh.", "cursor"),
377
  outputs=[guide_text],
378
  queue=False,
379
+ ).success(
380
+ fn=is_generating_mesh,
381
+ inputs=[generating_mesh],
382
+ queue=False,
383
  ).success(
384
  fn=enable_button,
385
  outputs=run_btn,
 
403
  fn=partial(update_guide, "Generating the mesh...", "wait"),
404
  outputs=[guide_text],
405
  queue=False,
406
+ ).success(
407
+ fn=toggle_mesh_generation_status,
408
+ inputs=[generating_mesh],
409
+ outputs=[generating_mesh],
410
+ queue=False,
411
  ).success(
412
  fn=mesh_gen,
413
  inputs=[tmp_dir_unposed, bg_removed_checkbox],
414
  outputs=[mesh_output, mesh_output_normal],
415
  queue=True,
416
+ ).success(
417
+ fn=toggle_mesh_generation_status,
418
+ inputs=[generating_mesh],
419
+ outputs=[generating_mesh],
420
+ queue=False,
421
  ).success(
422
  fn=partial(
423
  update_guide,
 
426
  ),
427
  outputs=[guide_text],
428
  queue=False,
429
+ ).success(
430
+ fn=not_cleared,
431
+ inputs=[input_gallery],
432
+ queue=False,
433
  ).success(
434
  fn=enable_button,
435
  outputs=[run_btn],
 
442
  inline=False,
443
  show_api=False,
444
  server_name="0.0.0.0",
445
+ )