Spaces:
Runtime error
Runtime error
Add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from diffusers import AutoPipelineForImage2Image
|
| 4 |
+
from loguru import logger
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
models = [
|
| 8 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
| 9 |
+
"stabilityai/sdxl-turbo",
|
| 10 |
+
"timbrooks/instruct-pix2pix",
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@spaces.GPU
|
| 15 |
+
@logger.catch(reraise=True)
|
| 16 |
+
def generate(
|
| 17 |
+
model: str,
|
| 18 |
+
prompt: str,
|
| 19 |
+
init_image: Image.Image,
|
| 20 |
+
strength: float,
|
| 21 |
+
progress=gr.Progress(),
|
| 22 |
+
):
|
| 23 |
+
logger.info(
|
| 24 |
+
f"Starting image generation: {dict(model=model, prompt=prompt, image=init_image, strength=strength)}"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
pipe = AutoPipelineForImage2Image.from_pretrained(model).to("cuda")
|
| 28 |
+
|
| 29 |
+
# Downscale the image
|
| 30 |
+
init_image.thumbnail((1024, 1024))
|
| 31 |
+
|
| 32 |
+
def progress_callback(pipe, step_index, timestep, callback_kwargs):
|
| 33 |
+
logger.debug(
|
| 34 |
+
f"Callback: {dict(num_timesteps=pipe.num_timesteps, step_index=step_index, timestep=timestep)}"
|
| 35 |
+
)
|
| 36 |
+
progress((step_index + 1, pipe.num_timesteps))
|
| 37 |
+
return callback_kwargs
|
| 38 |
+
|
| 39 |
+
images = pipe(
|
| 40 |
+
prompt=prompt,
|
| 41 |
+
image=init_image,
|
| 42 |
+
strength=strength,
|
| 43 |
+
callback_on_step_end=progress_callback,
|
| 44 |
+
).images
|
| 45 |
+
return images[0]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=generate,
|
| 50 |
+
inputs=[
|
| 51 |
+
gr.Dropdown(
|
| 52 |
+
label="Model", choices=models, value=models[0], allow_custom_value=True
|
| 53 |
+
),
|
| 54 |
+
gr.Text(label="Prompt"),
|
| 55 |
+
gr.Image(label="Init image", type="pil"),
|
| 56 |
+
gr.Slider(label="Strength", minimum=0, maximum=1, value=0.3),
|
| 57 |
+
],
|
| 58 |
+
outputs=[gr.Image(label="Output")],
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|