|
import gradio as gr |
|
from diffusers import StableDiffusionImg2ImgPipeline |
|
import torch |
|
from PIL import Image |
|
|
|
|
|
model_id = "radames/stable-diffusion-2-1-img2img" |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
pipeline = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, use_auth_token=True).to(device) |
|
|
|
def generate_image(img, prompt): |
|
|
|
image = img.resize((512, 512)) |
|
|
|
|
|
with torch.no_grad(): |
|
output_image = pipeline(prompt=prompt, init_image=image, strength=0.75, guidance_scale=7.5)["sample"][0] |
|
|
|
|
|
return output_image |
|
|
|
|
|
gr.Interface(fn=generate_image, |
|
inputs=[gr.Image(type="pil", label="Initial Image"), gr.Textbox(label="Prompt")], |
|
outputs=gr.Image(type="pil"), |
|
title="Image-to-Image with Stable Diffusion", |
|
description="Generate an image based on an initial image and a prompt.").launch() |
|
|