Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,19 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
import torch
|
| 5 |
-
import uuid
|
| 6 |
|
| 7 |
-
#
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
|
| 10 |
-
dtype = torch.float32
|
| 11 |
|
| 12 |
-
# Load model
|
| 13 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 14 |
"runwayml/stable-diffusion-v1-5",
|
| 15 |
torch_dtype=dtype,
|
| 16 |
use_safetensors=True
|
| 17 |
).to(device)
|
| 18 |
|
| 19 |
-
# Resize to
|
| 20 |
def resize_to_aspect(image, aspect_ratio):
|
| 21 |
width, height = image.size
|
| 22 |
aspect_map = {
|
|
@@ -26,36 +24,32 @@ def resize_to_aspect(image, aspect_ratio):
|
|
| 26 |
"9:16": (int(height * 9 / 16), height)
|
| 27 |
}
|
| 28 |
target_w, target_h = aspect_map.get(aspect_ratio, (width, height))
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
def resize_to_512(image):
|
| 32 |
return image.resize((512, 512))
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
def generate_img(product_img, prompt, aspect_ratio):
|
| 36 |
resized_img = resize_to_aspect(product_img, aspect_ratio).convert("RGB")
|
| 37 |
-
resized_img = resize_to_512(resized_img)
|
| 38 |
output = pipe(prompt=prompt, image=resized_img, strength=0.75, guidance_scale=7.5)
|
| 39 |
-
|
| 40 |
-
save_path = f"/tmp/generated_{uuid.uuid4().hex}.png"
|
| 41 |
-
image.save(save_path)
|
| 42 |
-
return image, save_path
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
fn=generate_img,
|
| 47 |
inputs=[
|
| 48 |
-
gr.Image(type="pil", label="Upload Product Image"
|
| 49 |
gr.Textbox(label="Prompt", placeholder="Describe what you want to generate"),
|
| 50 |
gr.Dropdown(["1:1", "16:9", "4:5", "9:16"], label="Aspect Ratio", value="1:1")
|
| 51 |
],
|
| 52 |
-
outputs=
|
| 53 |
-
|
| 54 |
-
gr.File(label='Download Image')
|
| 55 |
-
],
|
| 56 |
title="Image-to-Image Product Generator",
|
| 57 |
-
description="
|
| 58 |
-
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# app.launch(server_name="0.0.0.0", server_port=7860, debug=True, ssr_mode=False)
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
+
# Detect device
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
|
|
|
| 9 |
|
| 10 |
+
# Load the model with appropriate dtype
|
| 11 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 12 |
"runwayml/stable-diffusion-v1-5",
|
| 13 |
torch_dtype=dtype,
|
| 14 |
use_safetensors=True
|
| 15 |
).to(device)
|
| 16 |
|
| 17 |
+
# Resize uploaded image to selected aspect ratio
|
| 18 |
def resize_to_aspect(image, aspect_ratio):
|
| 19 |
width, height = image.size
|
| 20 |
aspect_map = {
|
|
|
|
| 24 |
"9:16": (int(height * 9 / 16), height)
|
| 25 |
}
|
| 26 |
target_w, target_h = aspect_map.get(aspect_ratio, (width, height))
|
| 27 |
+
image = image.resize((target_w, target_h))
|
| 28 |
+
return image
|
| 29 |
|
| 30 |
def resize_to_512(image):
|
| 31 |
return image.resize((512, 512))
|
| 32 |
|
| 33 |
+
|
| 34 |
+
#
|
| 35 |
+
# Generate new image from prompt and reference image
|
| 36 |
def generate_img(product_img, prompt, aspect_ratio):
|
| 37 |
resized_img = resize_to_aspect(product_img, aspect_ratio).convert("RGB")
|
| 38 |
+
resized_img = resize_to_512(resized_img) # Required by the model
|
| 39 |
output = pipe(prompt=prompt, image=resized_img, strength=0.75, guidance_scale=7.5)
|
| 40 |
+
return output.images[0]
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Gradio UI
|
| 43 |
+
gr.Interface(
|
| 44 |
fn=generate_img,
|
| 45 |
inputs=[
|
| 46 |
+
gr.Image(type="pil", label="Upload Product Image"),
|
| 47 |
gr.Textbox(label="Prompt", placeholder="Describe what you want to generate"),
|
| 48 |
gr.Dropdown(["1:1", "16:9", "4:5", "9:16"], label="Aspect Ratio", value="1:1")
|
| 49 |
],
|
| 50 |
+
# outputs=gr.Image(label="Generated Image", type="pil"),
|
| 51 |
+
outputs=gr.Image(label='Download Image'),
|
|
|
|
|
|
|
| 52 |
title="Image-to-Image Product Generator",
|
| 53 |
+
description="upload a product image, describe your idea, and select the output aspect ratio."
|
|
|
|
| 54 |
|
| 55 |
+
).launch()
|
|
|