Huiwenshi commited on
Commit
136f296
1 Parent(s): db387e8

Upload app_hg.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app_hg.py +34 -83
app_hg.py CHANGED
@@ -207,16 +207,18 @@ def gen_save_folder(max_size=30):
207
  return save_folder
208
 
209
 
210
- # @spaces.GPU
211
- def textgen_pipe(text, seed=0, step=0, SEED=0, STEP=0, color='face', bake=False, render=True, max_faces=12000,
212
- force=False, front='auto', others=[180], align_times=3):
213
  save_folder = gen_save_folder()
214
- image = worker_t2i(text, seed, step)
215
- image.save(save_folder + '/img.png')
216
- img_nobg = worker_xbg(image, force=True)
 
 
217
  img_nobg.save(save_folder + '/img_nobg.png')
218
  yield img_nobg, None, None, None, None, None
219
- res_img, pils = worker_i2v(img_nobg, SEED, STEP)
220
  save_gif(pils, save_folder + '/views.gif')
221
  views_img, cond_img = res_img[0], res_img[1]
222
  img_array = np.asarray(views_img, dtype=np.uint8)
@@ -226,7 +228,7 @@ def textgen_pipe(text, seed=0, step=0, SEED=0, STEP=0, color='face', bake=False,
226
  yield img_nobg, show_img, None, None, None, None
227
  do_texture_mapping = color == 'face'
228
  worker_v23(
229
- views_img, cond_img, seed = SEED,
230
  save_folder = save_folder,
231
  target_face_count = max_faces,
232
  do_texture_mapping = do_texture_mapping
@@ -240,9 +242,7 @@ def textgen_pipe(text, seed=0, step=0, SEED=0, STEP=0, color='face', bake=False,
240
  obj_dst = worker_baker(save_folder, force, front, others, align_times)
241
  glb_dst = obj_dst.replace(".obj", ".glb")
242
  yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, None
243
- if not render:
244
- yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, None
245
- else:
246
  baked_obj_list = sorted(glob(save_folder + '/view_*/bake/mesh.obj'))
247
  obj_dst = baked_obj_list[-1] if len(baked_obj_list)>=1 else save_folder+'/mesh.obj'
248
  assert os.path.exists(obj_dst), f"{obj_dst} file not found"
@@ -250,49 +250,6 @@ def textgen_pipe(text, seed=0, step=0, SEED=0, STEP=0, color='face', bake=False,
250
  worker_gif(obj_dst, gif_dst_path=gif_dst)
251
  yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, gif_dst
252
 
253
- # @spaces.GPU
254
- def imggen_pipe(image, do_removebg=True, SEED=0, STEP=0, color='face', bake=False, render=True, max_faces=12000,
255
- force=False, front='auto', others=[180], align_times=3):
256
- save_folder = gen_save_folder()
257
- if isinstance(image, str):image = Image.open(image)
258
- img_nobg = worker_xbg(image, force=do_removebg)
259
- img_nobg.save(save_folder + '/img_nobg.png')
260
- yield img_nobg, None, None, None, None, None
261
- res_img, pils = worker_i2v(img_nobg, SEED, STEP)
262
- save_gif(pils, save_folder + '/views.gif')
263
- views_img, cond_img = res_img[0], res_img[1]
264
- img_array = np.asarray(views_img, dtype=np.uint8)
265
- show_img = rearrange(img_array, '(n h) (m w) c -> (n m) h w c', n=3, m=2)
266
- show_img = rearrange(show_img[worker_i2v.order, ...], '(n m) h w c -> (n h) (m w) c', n=2, m=3)
267
- show_img = Image.fromarray(show_img)
268
- yield img_nobg, show_img, None, None, None, None
269
- do_texture_mapping = color == 'face'
270
- worker_v23(
271
- views_img, cond_img, seed = SEED,
272
- save_folder = save_folder,
273
- target_face_count = max_faces,
274
- do_texture_mapping = do_texture_mapping
275
- )
276
- glb_v23 = save_folder + '/mesh.glb' if do_texture_mapping else None
277
- obj_v23 = save_folder + '/mesh.obj'
278
- obj_v23 = save_folder + '/mesh_vertex_colors.obj'
279
- yield img_nobg, show_img, obj_v23, glb_v23, None, None
280
- glb_dst = None
281
- if do_texture_mapping and bake:
282
- obj_dst = worker_baker(save_folder, force, front, others, align_times)
283
- glb_dst = obj_dst.replace(".obj", ".glb")
284
- yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, None
285
- if not render:
286
- yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, None
287
- else:
288
- baked_obj_list = sorted(glob(save_folder + '/view_*/bake/mesh.obj'))
289
- obj_dst = baked_obj_list[-1] if len(baked_obj_list)>=1 else save_folder+'/mesh.obj'
290
- assert os.path.exists(obj_dst), f"{obj_dst} file not found"
291
- gif_dst = obj_dst.replace(".obj", ".gif")
292
- worker_gif(obj_dst, gif_dst_path=gif_dst)
293
- yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, gif_dst
294
-
295
-
296
  def check_image_available(image):
297
  if image is None:
298
  return "Please upload image", gr.update()
@@ -350,11 +307,11 @@ with gr.Blocks() as demo:
350
  with gr.Column():
351
  text = gr.TextArea('一只黑白相间的熊猫在白色背景上居中坐着,呈现出卡通风格和可爱氛围。',
352
  lines=3, max_lines=20, label='Input text (within 70 words)')
353
- with gr.Row():
354
- textgen_submit = gr.Button("Generate", variant="primary")
355
  with gr.Row():
356
  gr.Examples(examples=example_ts, inputs=[text], label="Text examples", examples_per_page=10)
357
-
 
 
358
  with gr.Tab("Image to 3D"):
359
  with gr.Row():
360
  input_image = gr.Image(label="Input image", width=256, height=256, type="pil",
@@ -371,18 +328,18 @@ with gr.Blocks() as demo:
371
  interactive=True
372
  )
373
  imggen_submit = gr.Button("Generate", variant="primary")
374
-
375
  mode = gr.Radio(
376
  choices=['Vertex color', 'Face color', 'Baking'],
377
  label="Texture mode",
378
  value='Baking',
379
  interactive=True
380
  )
381
-
382
  with gr.Accordion("Custom settings", open=False):
383
  color = gr.Radio(choices=["vertex", "face"], label="Color", value="face")
384
  with gr.Row():
385
-
386
  render = gr.Checkbox(
387
  label="Do Rendering",
388
  value=True,
@@ -393,11 +350,11 @@ with gr.Blocks() as demo:
393
  value=True if BAKE_AVAILEBLE else False,
394
  interactive=True if BAKE_AVAILEBLE else False
395
  )
396
-
397
  with gr.Row():
398
  seed = gr.Number(value=0, label="T2I seed", precision=0, interactive=True)
399
  SEED = gr.Number(value=0, label="Gen seed", precision=0, interactive=True)
400
-
401
  step = gr.Slider(
402
  value=25,
403
  minimum=15,
@@ -465,7 +422,7 @@ with gr.Blocks() as demo:
465
  )
466
 
467
  gr.Markdown(CONST_NOTE)
468
-
469
  ###### Output region
470
 
471
  with gr.Column(scale=3):
@@ -483,7 +440,7 @@ with gr.Blocks() as demo:
483
  type="pil",
484
  interactive=False
485
  )
486
-
487
  result_3dobj = gr.Model3D(
488
  clear_color=[0.0, 0.0, 0.0, 0.0],
489
  label="OBJ vertex color",
@@ -492,7 +449,7 @@ with gr.Blocks() as demo:
492
  camera_position=[90, 90, None],
493
  interactive=False
494
  )
495
-
496
  result_3dglb_texture = gr.Model3D(
497
  clear_color=[0.0, 0.0, 0.0, 0.0],
498
  label="GLB face color",
@@ -508,9 +465,9 @@ with gr.Blocks() as demo:
508
  visible=True,
509
  camera_position=[90, 90, None],
510
  interactive=False)
511
-
512
  result_gif = gr.Image(label="GIF", interactive=False)
513
-
514
  with gr.Row():
515
  gr.Markdown(
516
  "Due to Gradio limitations, OBJ files are displayed with vertex shading only, "
@@ -522,29 +479,23 @@ with gr.Blocks() as demo:
522
  #===============================================================
523
  # gradio running code
524
  #===============================================================
525
-
 
 
526
  textgen_submit.click(
527
- fn=textgen_pipe,
528
- inputs=[text, seed, step, SEED, STEP, color, bake, render, max_faces, force_bake,
529
  front_baking, other_views, align_times],
530
  outputs=[rembg_image, result_image, result_3dobj, result_3dglb_texture, result_3dglb_baked, result_gif],
531
  )
532
-
533
  imggen_submit.click(
534
- fn=imggen_pipe,
535
- inputs=[input_image, removebg, SEED, STEP, color, bake, render, max_faces, force_bake,
536
  front_baking, other_views, align_times],
537
  outputs=[rembg_image, result_image, result_3dobj, result_3dglb_texture, result_3dglb_baked, result_gif],
538
  )
539
 
540
- #===============================================================
541
- # start gradio server
542
- #===============================================================
543
- CONST_PORT = 8080
544
- CONST_MAX_QUEUE = 1
545
- CONST_SERVER = '0.0.0.0'
546
-
547
- demo.queue(max_size=CONST_MAX_QUEUE)
548
- # demo.launch(server_name=CONST_SERVER, server_port=CONST_PORT)
549
  demo.launch()
550
-
 
207
  return save_folder
208
 
209
 
210
+ # @spaces.GPU(duration=150)
211
+ def gen_pipe(text, image=None, do_removebg=True, sseed=0, sstep=0, SSEED=0, SSTEP=0, color='face',
212
+ bake=False, render=True, max_faces=12000, force=False, front='auto', others=[180], align_times=3):
213
  save_folder = gen_save_folder()
214
+ image_gen = image is not None
215
+ if not image_gen:
216
+ image = worker_t2i(text, sseed, sstep)
217
+ image.save(save_folder + '/img.png')
218
+ img_nobg = worker_xbg(image, force=do_removebg if image_gen else True)
219
  img_nobg.save(save_folder + '/img_nobg.png')
220
  yield img_nobg, None, None, None, None, None
221
+ res_img, pils = worker_i2v(img_nobg, seed=SSEED, steps=SSTEP)
222
  save_gif(pils, save_folder + '/views.gif')
223
  views_img, cond_img = res_img[0], res_img[1]
224
  img_array = np.asarray(views_img, dtype=np.uint8)
 
228
  yield img_nobg, show_img, None, None, None, None
229
  do_texture_mapping = color == 'face'
230
  worker_v23(
231
+ views_img, cond_img, seed = SSEED,
232
  save_folder = save_folder,
233
  target_face_count = max_faces,
234
  do_texture_mapping = do_texture_mapping
 
242
  obj_dst = worker_baker(save_folder, force, front, others, align_times)
243
  glb_dst = obj_dst.replace(".obj", ".glb")
244
  yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, None
245
+ if do_texture_mapping and render:
 
 
246
  baked_obj_list = sorted(glob(save_folder + '/view_*/bake/mesh.obj'))
247
  obj_dst = baked_obj_list[-1] if len(baked_obj_list)>=1 else save_folder+'/mesh.obj'
248
  assert os.path.exists(obj_dst), f"{obj_dst} file not found"
 
250
  worker_gif(obj_dst, gif_dst_path=gif_dst)
251
  yield img_nobg, show_img, obj_v23, glb_v23, glb_dst, gif_dst
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  def check_image_available(image):
254
  if image is None:
255
  return "Please upload image", gr.update()
 
307
  with gr.Column():
308
  text = gr.TextArea('一只黑白相间的熊猫在白色背景上居中坐着,呈现出卡通风格和可爱氛围。',
309
  lines=3, max_lines=20, label='Input text (within 70 words)')
 
 
310
  with gr.Row():
311
  gr.Examples(examples=example_ts, inputs=[text], label="Text examples", examples_per_page=10)
312
+ with gr.Row():
313
+ textgen_submit = gr.Button("Generate", variant="primary")
314
+
315
  with gr.Tab("Image to 3D"):
316
  with gr.Row():
317
  input_image = gr.Image(label="Input image", width=256, height=256, type="pil",
 
328
  interactive=True
329
  )
330
  imggen_submit = gr.Button("Generate", variant="primary")
331
+
332
  mode = gr.Radio(
333
  choices=['Vertex color', 'Face color', 'Baking'],
334
  label="Texture mode",
335
  value='Baking',
336
  interactive=True
337
  )
338
+
339
  with gr.Accordion("Custom settings", open=False):
340
  color = gr.Radio(choices=["vertex", "face"], label="Color", value="face")
341
  with gr.Row():
342
+
343
  render = gr.Checkbox(
344
  label="Do Rendering",
345
  value=True,
 
350
  value=True if BAKE_AVAILEBLE else False,
351
  interactive=True if BAKE_AVAILEBLE else False
352
  )
353
+
354
  with gr.Row():
355
  seed = gr.Number(value=0, label="T2I seed", precision=0, interactive=True)
356
  SEED = gr.Number(value=0, label="Gen seed", precision=0, interactive=True)
357
+
358
  step = gr.Slider(
359
  value=25,
360
  minimum=15,
 
422
  )
423
 
424
  gr.Markdown(CONST_NOTE)
425
+
426
  ###### Output region
427
 
428
  with gr.Column(scale=3):
 
440
  type="pil",
441
  interactive=False
442
  )
443
+
444
  result_3dobj = gr.Model3D(
445
  clear_color=[0.0, 0.0, 0.0, 0.0],
446
  label="OBJ vertex color",
 
449
  camera_position=[90, 90, None],
450
  interactive=False
451
  )
452
+
453
  result_3dglb_texture = gr.Model3D(
454
  clear_color=[0.0, 0.0, 0.0, 0.0],
455
  label="GLB face color",
 
465
  visible=True,
466
  camera_position=[90, 90, None],
467
  interactive=False)
468
+
469
  result_gif = gr.Image(label="GIF", interactive=False)
470
+
471
  with gr.Row():
472
  gr.Markdown(
473
  "Due to Gradio limitations, OBJ files are displayed with vertex shading only, "
 
479
  #===============================================================
480
  # gradio running code
481
  #===============================================================
482
+
483
+ none = gr.State(None)
484
+
485
  textgen_submit.click(
486
+ fn=gen_pipe,
487
+ inputs=[text, none, removebg, seed, step, SEED, STEP, color, bake, render, max_faces, force_bake,
488
  front_baking, other_views, align_times],
489
  outputs=[rembg_image, result_image, result_3dobj, result_3dglb_texture, result_3dglb_baked, result_gif],
490
  )
491
+
492
  imggen_submit.click(
493
+ fn=gen_pipe,
494
+ inputs=[none, input_image, removebg, seed, step, SEED, STEP, color, bake, render, max_faces, force_bake,
495
  front_baking, other_views, align_times],
496
  outputs=[rembg_image, result_image, result_3dobj, result_3dglb_texture, result_3dglb_baked, result_gif],
497
  )
498
 
499
+ demo.queue(max_size=0)
500
+ # demo.launch(server_name='0.0.0.0', server_port=8080)
 
 
 
 
 
 
 
501
  demo.launch()