Spaces:
Running
on
Zero
Running
on
Zero
# import torch | |
# import random | |
# import numpy as np | |
# import gradio as gr | |
# from pytorch_lightning import seed_everything | |
# from annotator.util import resize_image, HWC3 | |
# from diffusers import StableDiffusionControlNetPipeline, ControlNetModel | |
# # # Load the controlnet model | |
# # controlnet = ControlNetModel.from_pretrained("CompVis/controlnet") | |
# # # Load the pipeline | |
# # pipe = StableDiffusionControlNetPipeline.from_pretrained( | |
# # "CompVis/stable-diffusion-v1-4", | |
# # controlnet=controlnet | |
# # ).to("cuda") | |
# controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16) | |
# pipe = StableDiffusionControlNetPipeline.from_pretrained( | |
# "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16 | |
# ) | |
# def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, low_threshold, high_threshold): | |
# with torch.no_grad(): | |
# img = resize_image(HWC3(input_image), image_resolution) | |
# if seed == -1: | |
# seed = random.randint(0, 65535) | |
# seed_everything(seed) | |
# # Generate images using the pipeline | |
# generator = torch.Generator("cuda").manual_seed(seed) | |
# images = pipe(prompt=prompt + ', ' + a_prompt, num_inference_steps=ddim_steps, guidance_scale=scale, generator=generator, num_images_per_prompt=num_samples).images | |
# results = [np.array(image) for image in images] | |
# return results | |
# block = gr.Blocks().queue() | |
# with block: | |
# with gr.Row(): | |
# gr.Markdown("## Scene Diffusion with ControlNet") | |
# with gr.Row(): | |
# with gr.Column(): | |
# input_image = gr.Image(label="Image") | |
# prompt = gr.Textbox(label="Prompt") | |
# a_prompt = gr.Textbox(label="Additional Prompt") | |
# n_prompt = gr.Textbox(label="Negative Prompt") | |
# num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1) | |
# image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64) | |
# ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1) | |
# guess_mode = gr.Checkbox(label='Guess Mode', value=False) | |
# strength = gr.Slider(label="Strength", minimum=0.0, maximum=1.0, value=0.5, step=0.1) | |
# scale = gr.Slider(label="Scale", minimum=0.1, maximum=30.0, value=10.0, step=0.1) | |
# seed = gr.Slider(label="Seed", minimum=0, maximum=10000, value=42, step=1) | |
# eta = gr.Slider(label="ETA", minimum=0.0, maximum=1.0, value=0.0, step=0.1) | |
# low_threshold = gr.Slider(label="Canny Low Threshold", minimum=1, maximum=255, value=100, step=1) | |
# high_threshold = gr.Slider(label="Canny High Threshold", minimum=1, maximum=255, value=200, step=1) | |
# submit = gr.Button("Generate") | |
# with gr.Column(): | |
# output_image = gr.Gallery(label='Output', show_label=False, elem_id="gallery") | |
# submit.click(fn=process, inputs=[input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, low_threshold, high_threshold], outputs=output_image) | |
# demo = block | |
# demo.launch() | |
import torch | |
from diffusers import StableDiffusionControlNetPipeline | |
from diffusers import ControlNetModel | |
import gradio as gr | |
from PIL import Image | |
import numpy as np | |
# 初始化 ControlNet 模型和 Stable Diffusion Pipeline | |
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16) | |
pipeline = StableDiffusionControlNetPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", controlnet=controlnet) | |
# 定义图像生成函数 | |
def generate_image(prompt: str, input_image: Image.Image): | |
# 可以在这里根据传入的图像做一些预处理(例如,使用控制网络或图像生成模型) | |
# 将输入图像转换为合适的格式 | |
input_image = input_image.convert("RGB") | |
# 生成图像 | |
result_image = pipeline(prompt=prompt, init_image=input_image, strength=0.75).images[0] | |
return result_image | |
# 创建 Gradio 界面 | |
iface = gr.Interface( | |
fn=generate_image, | |
inputs=[ | |
gr.Textbox(label="Enter a prompt", placeholder="e.g. a futuristic city at sunset"), # 提示框 | |
gr.Image(label="Upload an Image", type="pil") # 图像上传框 | |
], | |
outputs=gr.Image(label="Generated Image"), # 输出生成的图像 | |
live=True | |
) | |
# 启动 Gradio 应用 | |
iface.launch(share=True) | |