Spaces:
Runtime error
Runtime error
File size: 2,102 Bytes
02029ac 87b9171 02029ac 0ef3448 fa66d55 0ef3448 02029ac 0ef3448 02029ac 44d0f5d 02029ac 87b9171 0ef3448 02029ac 0ef3448 02029ac 4c34f80 87b9171 02029ac 0ef3448 02029ac 0ef3448 87b9171 0ef3448 02029ac 0ef3448 02029ac 0ef3448 02029ac 0ef3448 02029ac 0ef3448 02029ac 3f2d339 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import gradio as gr
from PIL import Image
from diffusers import StableDiffusionImg2ImgPipeline, DDIMScheduler
import torch
# Detect device
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.float16 if device == "cuda" else torch.float32
# Load the model with appropriate dtype
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1",
torch_dtype=dtype,
use_safetensors=True
).to(device)
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
# Resize uploaded image to selected aspect ratio
def resize_to_aspect(image, aspect_ratio):
width, height = image.size
aspect_map = {
"1:1": (min(width, height), min(width, height)),
"16:9": (width, int(width * 9 / 16)),
"4:5": (width, int(width * 5 / 4)),
"9:16": (int(height * 9 / 16), height)
}
target_w, target_h = aspect_map.get(aspect_ratio, (width, height))
image = image.resize((target_w, target_h))
return image
def resize_to_512(image):
return image.resize((768, 768))
#
# Generate new image from prompt and reference image
def generate_img(product_img, prompt, aspect_ratio):
resized_img = resize_to_aspect(product_img, aspect_ratio).convert("RGB")
resized_img = resize_to_512(resized_img) # Required by the model
output = pipe(prompt=prompt, negative_prompt="cartoon, anime, painting, blurry, unrealistic, low quality", image=resized_img, strength=0.75, guidance_scale=7.5)
return output.images[0]
# Gradio UI
gr.Interface(
fn=generate_img,
inputs=[
gr.Image(type="pil", label="Upload Product Image"),
gr.Textbox(label="Prompt", placeholder="Describe what you want to generate"),
gr.Dropdown(["1:1", "16:9", "4:5", "9:16"], label="Aspect Ratio", value="1:1")
],
# outputs=gr.Image(label="Generated Image", type="pil"),
outputs=gr.Image(label='Download Image'),
title="Image-to-Image Product Generator",
description="upload a product image, describe your idea, and select the output aspect ratio."
).launch(share=True, ssr_mode=False) |