Steven18 commited on
Commit
829dcd0
·
1 Parent(s): 2bc7b43

update multiimages api

Browse files
Files changed (1) hide show
  1. app.py +64 -6
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import spaces
3
  from gradio_litmodel3d import LitModel3D
4
  import json
@@ -20,6 +21,54 @@ MAX_SEED = np.iinfo(np.int32).max
20
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
21
  os.makedirs(TMP_DIR, exist_ok=True)
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def start_session(req: gr.Request):
25
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
@@ -160,8 +209,9 @@ def image_to_3d(
160
  },
161
  )
162
  else:
 
163
  outputs = pipeline.run_multi_image(
164
- [img[0] for img in multiimages],
165
  seed=seed,
166
  formats=["gaussian", "mesh"],
167
  preprocess_image=False,
@@ -335,9 +385,8 @@ def test_for_api_gen(image: Image.Image) -> Image.Image:
335
  """
336
  return image
337
 
338
- def update_is_multiimage(selected_tab: str):
339
- return "true" if selected_tab == "Multiple Images" else "false"
340
-
341
 
342
 
343
  with gr.Blocks(delete_cache=(600, 600)) as demo:
@@ -378,8 +427,17 @@ with gr.Blocks(delete_cache=(600, 600)) as demo:
378
  *NOTE: this is an experimental algorithm without training a specialized model. It may not produce the best results for all images, especially those having different poses or inconsistent details.*
379
  """)
380
 
381
- is_multiimage = gr.Radio(choices=["true", "false"],value="false",label="Use multi-image mode",visible=True)
382
- input_tabs.select(fn=update_is_multiimage,inputs=None,outputs=is_multiimage)
 
 
 
 
 
 
 
 
 
383
 
384
  with gr.Accordion(label="Generation Settings", open=False):
385
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)
 
1
  import gradio as gr
2
+ from gradio.events import SelectData
3
  import spaces
4
  from gradio_litmodel3d import LitModel3D
5
  import json
 
21
  TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')
22
  os.makedirs(TMP_DIR, exist_ok=True)
23
 
24
+ def to_pil_list(
25
+ multiimages: List[
26
+ Union[
27
+ Image.Image,
28
+ Tuple[Image.Image, str],
29
+ gr.File,
30
+ Tuple[gr.File, str],
31
+ str, # fallback: plain path
32
+ Path
33
+ ]
34
+ ]
35
+ ) -> List[Image.Image]:
36
+ """
37
+ Convert a heterogeneous `multiimages` list into a homogeneous
38
+ `List[Image.Image]`.
39
+
40
+ Accepts elements in any of the following forms:
41
+ • PIL.Image
42
+ • (PIL.Image, caption)
43
+ • gr.File (gr.File.name is the temp‑file path)
44
+ • (gr.File, caption)
45
+ • str / pathlib.Path (direct file path)
46
+
47
+ Returns:
48
+ List[Image.Image] -- guaranteed PIL images
49
+ """
50
+ pil_imgs: List[Image.Image] = []
51
+
52
+ for item in multiimages:
53
+ # Unpack tuple/list, keep first element
54
+ if isinstance(item, (tuple, list)):
55
+ item = item[0]
56
+
57
+ if isinstance(item, Image.Image): # already PIL
58
+ pil_imgs.append(item)
59
+
60
+ elif hasattr(item, "name"): # gr.File
61
+ pil_imgs.append(Image.open(item.name))
62
+
63
+ elif isinstance(item, (str, Path)): # file path
64
+ pil_imgs.append(Image.open(item))
65
+
66
+ else:
67
+ raise TypeError(
68
+ f"Unsupported element in multiimages: {type(item)}"
69
+ )
70
+
71
+ return pil_imgs
72
 
73
  def start_session(req: gr.Request):
74
  user_dir = os.path.join(TMP_DIR, str(req.session_hash))
 
209
  },
210
  )
211
  else:
212
+ pil_images = to_pil_list(multiimages)
213
  outputs = pipeline.run_multi_image(
214
+ pil_images,
215
  seed=seed,
216
  formats=["gaussian", "mesh"],
217
  preprocess_image=False,
 
385
  """
386
  return image
387
 
388
+ def update_is_multiimage(event: SelectData):
389
+ return "true" if event.index == 1 else "false"
 
390
 
391
 
392
  with gr.Blocks(delete_cache=(600, 600)) as demo:
 
427
  *NOTE: this is an experimental algorithm without training a specialized model. It may not produce the best results for all images, especially those having different poses or inconsistent details.*
428
  """)
429
 
430
+ is_multiimage = gr.Radio(
431
+ choices=["true", "false"],
432
+ value="false",
433
+ label="Use multi-image mode",
434
+ visible=True
435
+ )
436
+
437
+ input_tabs.select(
438
+ fn=update_is_multiimage,
439
+ outputs=is_multiimage
440
+ )
441
 
442
  with gr.Accordion(label="Generation Settings", open=False):
443
  seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)