opsiclear-admin commited on
Commit
4ebb70a
·
verified ·
1 Parent(s): dd8aa83

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +14 -22
app.py CHANGED
@@ -328,12 +328,17 @@ def end_session(req: gr.Request):
328
 
329
 
330
  def remove_background(input: Image.Image) -> Image.Image:
331
- with tempfile.NamedTemporaryFile(suffix='.png') as f:
332
- input = input.convert('RGB')
333
- input.save(f.name)
334
- output = rmbg_client.predict(handle_file(f.name), api_name="/image")[0][0]
335
- output = Image.open(output)
336
- return output
 
 
 
 
 
337
 
338
 
339
  def preprocess_image(input: Image.Image) -> Image.Image:
@@ -547,19 +552,14 @@ def image_to_3d(
547
  )
548
  mesh = outputs[0]
549
  mesh.simplify(16777216) # nvdiffrast limit
550
- render_images = render_utils.render_snapshot(mesh, resolution=1024, r=2, fov=36, nviews=STEPS, envmap=envmap)
551
  state = pack_state(latents)
552
  torch.cuda.empty_cache()
553
- return state, render_images
554
 
555
-
556
- def build_preview_html(state, render_images):
557
- """Encode rendered images to base64 and build the HTML preview.
558
- No GPU needed — pure CPU work."""
559
- # Encode 48 images in parallel
560
  def encode_preview_image(args):
561
  m_idx, s_idx, render_key = args
562
- img_base64 = image_to_base64(Image.fromarray(render_images[render_key][s_idx]))
563
  return (m_idx, s_idx, img_base64)
564
 
565
  encode_tasks = [
@@ -571,7 +571,6 @@ def build_preview_html(state, render_images):
571
  with ThreadPoolExecutor(max_workers=8) as executor:
572
  encoded_results = list(executor.map(encode_preview_image, encode_tasks))
573
 
574
- # Build HTML from encoded results
575
  encoded_map = {(m, s): b64 for m, s, b64 in encoded_results}
576
  images_html = ""
577
  for m_idx, mode in enumerate(MODES):
@@ -588,7 +587,6 @@ def build_preview_html(state, render_images):
588
  loading="eager">
589
  """
590
 
591
- # Button Row HTML
592
  btns_html = ""
593
  for idx, mode in enumerate(MODES):
594
  active_class = "active" if idx == DEFAULT_MODE else ""
@@ -599,7 +597,6 @@ def build_preview_html(state, render_images):
599
  title="{mode['name']}">
600
  """
601
 
602
- # Assemble the full component
603
  full_html = f"""
604
  <div class="previewer-container">
605
  <div class="tips-wrapper">
@@ -754,7 +751,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate"),
754
 
755
  output_buf = gr.State()
756
  processed_buf = gr.State()
757
- render_buf = gr.State()
758
  selected_img_idx = gr.State(value=None)
759
 
760
 
@@ -803,10 +799,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate"),
803
  tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
804
  multiimage_algo, tex_multiimage_algo
805
  ],
806
- outputs=[output_buf, render_buf],
807
- ).then(
808
- build_preview_html,
809
- inputs=[output_buf, render_buf],
810
  outputs=[output_buf, preview_output],
811
  )
812
 
 
328
 
329
 
330
  def remove_background(input: Image.Image) -> Image.Image:
331
+ try:
332
+ with tempfile.NamedTemporaryFile(suffix='.png') as f:
333
+ input = input.convert('RGB')
334
+ input.save(f.name)
335
+ output = rmbg_client.predict(handle_file(f.name), api_name="/image")[0][0]
336
+ output = Image.open(output)
337
+ return output
338
+ except Exception as e:
339
+ print(f"[WARNING] Background removal failed: {e}. Using image as-is.")
340
+ # Return original image with a full-white alpha channel
341
+ return input.convert('RGBA')
342
 
343
 
344
  def preprocess_image(input: Image.Image) -> Image.Image:
 
552
  )
553
  mesh = outputs[0]
554
  mesh.simplify(16777216) # nvdiffrast limit
555
+ images = render_utils.render_snapshot(mesh, resolution=1024, r=2, fov=36, nviews=STEPS, envmap=envmap)
556
  state = pack_state(latents)
557
  torch.cuda.empty_cache()
 
558
 
559
+ # --- HTML Construction ---
 
 
 
 
560
  def encode_preview_image(args):
561
  m_idx, s_idx, render_key = args
562
+ img_base64 = image_to_base64(Image.fromarray(images[render_key][s_idx]))
563
  return (m_idx, s_idx, img_base64)
564
 
565
  encode_tasks = [
 
571
  with ThreadPoolExecutor(max_workers=8) as executor:
572
  encoded_results = list(executor.map(encode_preview_image, encode_tasks))
573
 
 
574
  encoded_map = {(m, s): b64 for m, s, b64 in encoded_results}
575
  images_html = ""
576
  for m_idx, mode in enumerate(MODES):
 
587
  loading="eager">
588
  """
589
 
 
590
  btns_html = ""
591
  for idx, mode in enumerate(MODES):
592
  active_class = "active" if idx == DEFAULT_MODE else ""
 
597
  title="{mode['name']}">
598
  """
599
 
 
600
  full_html = f"""
601
  <div class="previewer-container">
602
  <div class="tips-wrapper">
 
751
 
752
  output_buf = gr.State()
753
  processed_buf = gr.State()
 
754
  selected_img_idx = gr.State(value=None)
755
 
756
 
 
799
  tex_slat_guidance_strength, tex_slat_guidance_rescale, tex_slat_sampling_steps, tex_slat_rescale_t,
800
  multiimage_algo, tex_multiimage_algo
801
  ],
 
 
 
 
802
  outputs=[output_buf, preview_output],
803
  )
804