gensym commited on
Commit
ae973c3
·
1 Parent(s): ba1ed50
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -21,7 +21,7 @@ TMP_DIR = "/tmp/Trellis-demo"
21
 
22
  os.makedirs(TMP_DIR, exist_ok=True)
23
 
24
-
25
  def preprocess_image(image: Image.Image) -> Tuple[str, Image.Image]:
26
  """
27
  Preprocess the input image.
@@ -34,6 +34,7 @@ def preprocess_image(image: Image.Image) -> Tuple[str, Image.Image]:
34
  Image.Image: The preprocessed image.
35
  """
36
  trial_id = str(uuid.uuid4())
 
37
  processed_image = pipeline.preprocess_image(image)
38
  processed_image.save(f"{TMP_DIR}/{trial_id}.png")
39
  return trial_id, processed_image
@@ -100,6 +101,9 @@ def image_to_3d(trial_id: str, seed: int, randomize_seed: bool, ss_guidance_stre
100
  """
101
  if randomize_seed:
102
  seed = np.random.randint(0, MAX_SEED)
 
 
 
103
  outputs = pipeline.run(
104
  Image.open(f"{TMP_DIR}/{trial_id}.png"),
105
  seed=seed,
@@ -202,19 +206,19 @@ with gr.Blocks() as demo:
202
  trial_id = gr.Textbox(visible=False)
203
  output_buf = gr.State()
204
 
205
- # Example images at the bottom of the page
206
- with gr.Row():
207
- examples = gr.Examples(
208
- examples=[
209
- f'assets/example_image/{image}'
210
- for image in os.listdir("assets/example_image")
211
- ],
212
- inputs=[image_prompt],
213
- fn=preprocess_image,
214
- outputs=[trial_id, image_prompt],
215
- run_on_click=True,
216
- examples_per_page=64,
217
- )
218
 
219
  # Handlers
220
  image_prompt.upload(
@@ -273,8 +277,13 @@ def cleanup_tmp_dir():
273
  cleanup_thread = threading.Thread(target=cleanup_tmp_dir)
274
  cleanup_thread.start()
275
 
 
276
  @spaces.GPU
277
  def preload():
 
 
 
 
278
  pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
279
  pipeline.cuda()
280
  try:
@@ -282,9 +291,13 @@ def preload():
282
  except:
283
  pass
284
 
285
-
286
  # Launch the Gradio app
287
  if __name__ == "__main__":
288
  global pipeline
289
- preload()
 
 
 
 
 
290
  demo.launch()
 
21
 
22
  os.makedirs(TMP_DIR, exist_ok=True)
23
 
24
+ @spaces.GPU
25
  def preprocess_image(image: Image.Image) -> Tuple[str, Image.Image]:
26
  """
27
  Preprocess the input image.
 
34
  Image.Image: The preprocessed image.
35
  """
36
  trial_id = str(uuid.uuid4())
37
+ preload()
38
  processed_image = pipeline.preprocess_image(image)
39
  processed_image.save(f"{TMP_DIR}/{trial_id}.png")
40
  return trial_id, processed_image
 
101
  """
102
  if randomize_seed:
103
  seed = np.random.randint(0, MAX_SEED)
104
+
105
+
106
+ preload()
107
  outputs = pipeline.run(
108
  Image.open(f"{TMP_DIR}/{trial_id}.png"),
109
  seed=seed,
 
206
  trial_id = gr.Textbox(visible=False)
207
  output_buf = gr.State()
208
 
209
+ # # Example images at the bottom of the page
210
+ # with gr.Row():
211
+ # examples = gr.Examples(
212
+ # examples=[
213
+ # f'assets/example_image/{image}'
214
+ # for image in os.listdir("assets/example_image")
215
+ # ],
216
+ # inputs=[image_prompt],
217
+ # fn=preprocess_image,
218
+ # outputs=[trial_id, image_prompt],
219
+ # run_on_click=True,
220
+ # examples_per_page=64,
221
+ # )
222
 
223
  # Handlers
224
  image_prompt.upload(
 
277
  cleanup_thread = threading.Thread(target=cleanup_tmp_dir)
278
  cleanup_thread.start()
279
 
280
+
281
  @spaces.GPU
282
  def preload():
283
+ global preloaded
284
+ if preloaded:
285
+ return
286
+ preloaded = True
287
  pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
288
  pipeline.cuda()
289
  try:
 
291
  except:
292
  pass
293
 
 
294
  # Launch the Gradio app
295
  if __name__ == "__main__":
296
  global pipeline
297
+ # pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
298
+ # pipeline.cuda()
299
+ # try:
300
+ # pipeline.preprocess_image(Image.fromarray(np.zeros((512, 512, 3), dtype=np.uint8))) # Preload rembg
301
+ # except:
302
+ # pass
303
  demo.launch()