Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import requests
|
|
| 3 |
import rembg
|
| 4 |
import random
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
from PIL import Image
|
| 8 |
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
|
|
@@ -45,48 +46,56 @@ def inference(input_img, num_inference_steps, guidance_scale, seed ):
|
|
| 45 |
return result
|
| 46 |
|
| 47 |
def remove_background(result):
|
|
|
|
| 48 |
# Check if the variable is a PIL Image
|
| 49 |
if isinstance(result, Image.Image):
|
|
|
|
| 50 |
result = rembg.remove(result)
|
| 51 |
# Check if the variable is a str filepath
|
| 52 |
elif isinstance(result, str):
|
|
|
|
| 53 |
result = Image.open(result)
|
| 54 |
result = rembg.remove(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return result
|
| 56 |
-
|
| 57 |
|
| 58 |
|
|
|
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
btn = gr.Button('Submit')
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
btn.click(inference, [input_img, num_inference_steps, guidance_scale, seed ], output_img)
|
| 74 |
rm_in_bkg.input(remove_background, input_img, input_img)
|
| 75 |
rm_out_bkg.input(remove_background, output_img, output_img)
|
| 76 |
|
| 77 |
-
gr.Examples(
|
| 78 |
-
examples=[["extinguisher.png", 75, 4.0, 0],
|
| 79 |
-
['mushroom.png', 75, 4.0, 0],
|
| 80 |
-
['tianw2.png', 75, 4.0, 0],
|
| 81 |
-
['lysol.png', 75, 4.0, 0],
|
| 82 |
-
['ghost-eating-burger.png', 75, 4.0, 0]
|
| 83 |
-
],
|
| 84 |
-
inputs=[input_img, num_inference_steps, guidance_scale, seed],
|
| 85 |
-
outputs=output_img,
|
| 86 |
-
fn=inference,
|
| 87 |
-
cache_examples=True,
|
| 88 |
-
)
|
| 89 |
|
| 90 |
|
| 91 |
-
demo.launch()
|
|
|
|
| 92 |
|
|
|
|
| 3 |
import rembg
|
| 4 |
import random
|
| 5 |
import gradio as gr
|
| 6 |
+
import numpy
|
| 7 |
|
| 8 |
from PIL import Image
|
| 9 |
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
|
|
|
|
| 46 |
return result
|
| 47 |
|
| 48 |
def remove_background(result):
|
| 49 |
+
print(type(result))
|
| 50 |
# Check if the variable is a PIL Image
|
| 51 |
if isinstance(result, Image.Image):
|
| 52 |
+
print('here IF')
|
| 53 |
result = rembg.remove(result)
|
| 54 |
# Check if the variable is a str filepath
|
| 55 |
elif isinstance(result, str):
|
| 56 |
+
print('here ELIF')
|
| 57 |
result = Image.open(result)
|
| 58 |
result = rembg.remove(result)
|
| 59 |
+
elif isinstance(result, numpy.ndarray):
|
| 60 |
+
print('here ELIF 2')
|
| 61 |
+
# Convert the NumPy array to a PIL Image
|
| 62 |
+
result = Image.fromarray(result)
|
| 63 |
return result
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
+
# Create a Gradio interface for the Zero123++ model
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
+
# Display a title
|
| 69 |
+
gr.HTML("<h1><center> Interactive WebUI : Zero123++ </center></h1>")
|
| 70 |
+
gr.HTML("<h3><center> A Single Image to Consistent Multi-view Diffusion Base Model</center></h1>")
|
| 71 |
+
gr.HTML('''<center> <a href='https://arxiv.org/abs/2310.15110' target='_blank'>ArXiv</a> - <a href='https://github.com/SUDO-AI-3D/zero123plus/tree/main' target='_blank'>Code</a> </center>''')
|
| 72 |
+
with gr.Row():
|
| 73 |
+
# Input section: Allow users to upload an image
|
| 74 |
+
with gr.Column():
|
| 75 |
+
input_img = gr.Image(label='Input Image', type='filepath')
|
| 76 |
+
|
| 77 |
+
# Output section: Display the Zero123++ output image
|
| 78 |
+
with gr.Column():
|
| 79 |
+
output_img = gr.Image(label='Zero123++ Output')
|
| 80 |
+
|
| 81 |
+
# Submit button to initiate the inference
|
| 82 |
btn = gr.Button('Submit')
|
| 83 |
|
| 84 |
+
# Advanced options section with accordion for hiding/showing
|
| 85 |
+
with gr.Accordion("Advanced options:", open=False):
|
| 86 |
+
rm_in_bkg = gr.Checkbox(label='Remove Input Background')
|
| 87 |
+
rm_out_bkg = gr.Checkbox(label='Remove Output Background')
|
| 88 |
+
num_inference_steps = gr.Slider(label="Number of Inference Steps", minimum=15, maximum=100, step=1, value=75, interactive=True)
|
| 89 |
+
guidance_scale = gr.Slider(label="Classifier Free Guidance Scale", minimum=1.00, maximum=10.00, step=0.1, value=4.0, interactive=True)
|
| 90 |
+
seed = gr.Number(0, label='Seed')
|
| 91 |
+
|
| 92 |
+
|
| 93 |
btn.click(inference, [input_img, num_inference_steps, guidance_scale, seed ], output_img)
|
| 94 |
rm_in_bkg.input(remove_background, input_img, input_img)
|
| 95 |
rm_out_bkg.input(remove_background, output_img, output_img)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
+
demo.launch(debug=True)
|
| 100 |
+
|
| 101 |
|