xywwww commited on
Commit
482dd21
·
verified ·
1 Parent(s): 3dc891c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -57
app.py CHANGED
@@ -1,63 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import torch
2
- import random
3
- import numpy as np
4
  import gradio as gr
5
- from pytorch_lightning import seed_everything
6
- from annotator.util import resize_image, HWC3
7
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
8
 
9
- # # Load the controlnet model
10
- # controlnet = ControlNetModel.from_pretrained("CompVis/controlnet")
 
11
 
12
- # # Load the pipeline
13
- # pipe = StableDiffusionControlNetPipeline.from_pretrained(
14
- # "CompVis/stable-diffusion-v1-4",
15
- # controlnet=controlnet
16
- # ).to("cuda")
17
- controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
18
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
19
- "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
 
 
 
 
 
 
 
 
 
 
 
 
20
  )
21
 
22
- 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):
23
- with torch.no_grad():
24
- img = resize_image(HWC3(input_image), image_resolution)
25
-
26
- if seed == -1:
27
- seed = random.randint(0, 65535)
28
- seed_everything(seed)
29
-
30
- # Generate images using the pipeline
31
- generator = torch.Generator("cuda").manual_seed(seed)
32
- images = pipe(prompt=prompt + ', ' + a_prompt, num_inference_steps=ddim_steps, guidance_scale=scale, generator=generator, num_images_per_prompt=num_samples).images
33
-
34
- results = [np.array(image) for image in images]
35
- return results
36
-
37
- block = gr.Blocks().queue()
38
- with block:
39
- with gr.Row():
40
- gr.Markdown("## Scene Diffusion with ControlNet")
41
- with gr.Row():
42
- with gr.Column():
43
- input_image = gr.Image(label="Image")
44
- prompt = gr.Textbox(label="Prompt")
45
- a_prompt = gr.Textbox(label="Additional Prompt")
46
- n_prompt = gr.Textbox(label="Negative Prompt")
47
- num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
48
- image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
49
- ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
50
- guess_mode = gr.Checkbox(label='Guess Mode', value=False)
51
- strength = gr.Slider(label="Strength", minimum=0.0, maximum=1.0, value=0.5, step=0.1)
52
- scale = gr.Slider(label="Scale", minimum=0.1, maximum=30.0, value=10.0, step=0.1)
53
- seed = gr.Slider(label="Seed", minimum=0, maximum=10000, value=42, step=1)
54
- eta = gr.Slider(label="ETA", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
55
- low_threshold = gr.Slider(label="Canny Low Threshold", minimum=1, maximum=255, value=100, step=1)
56
- high_threshold = gr.Slider(label="Canny High Threshold", minimum=1, maximum=255, value=200, step=1)
57
- submit = gr.Button("Generate")
58
- with gr.Column():
59
- output_image = gr.Gallery(label='Output', show_label=False, elem_id="gallery")
60
- 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)
61
-
62
- demo = block
63
- demo.launch()
 
1
+ # import torch
2
+ # import random
3
+ # import numpy as np
4
+ # import gradio as gr
5
+ # from pytorch_lightning import seed_everything
6
+ # from annotator.util import resize_image, HWC3
7
+ # from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
8
+
9
+ # # # Load the controlnet model
10
+ # # controlnet = ControlNetModel.from_pretrained("CompVis/controlnet")
11
+
12
+ # # # Load the pipeline
13
+ # # pipe = StableDiffusionControlNetPipeline.from_pretrained(
14
+ # # "CompVis/stable-diffusion-v1-4",
15
+ # # controlnet=controlnet
16
+ # # ).to("cuda")
17
+ # controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
18
+ # pipe = StableDiffusionControlNetPipeline.from_pretrained(
19
+ # "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
20
+ # )
21
+
22
+ # 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):
23
+ # with torch.no_grad():
24
+ # img = resize_image(HWC3(input_image), image_resolution)
25
+
26
+ # if seed == -1:
27
+ # seed = random.randint(0, 65535)
28
+ # seed_everything(seed)
29
+
30
+ # # Generate images using the pipeline
31
+ # generator = torch.Generator("cuda").manual_seed(seed)
32
+ # images = pipe(prompt=prompt + ', ' + a_prompt, num_inference_steps=ddim_steps, guidance_scale=scale, generator=generator, num_images_per_prompt=num_samples).images
33
+
34
+ # results = [np.array(image) for image in images]
35
+ # return results
36
+
37
+ # block = gr.Blocks().queue()
38
+ # with block:
39
+ # with gr.Row():
40
+ # gr.Markdown("## Scene Diffusion with ControlNet")
41
+ # with gr.Row():
42
+ # with gr.Column():
43
+ # input_image = gr.Image(label="Image")
44
+ # prompt = gr.Textbox(label="Prompt")
45
+ # a_prompt = gr.Textbox(label="Additional Prompt")
46
+ # n_prompt = gr.Textbox(label="Negative Prompt")
47
+ # num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
48
+ # image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
49
+ # ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
50
+ # guess_mode = gr.Checkbox(label='Guess Mode', value=False)
51
+ # strength = gr.Slider(label="Strength", minimum=0.0, maximum=1.0, value=0.5, step=0.1)
52
+ # scale = gr.Slider(label="Scale", minimum=0.1, maximum=30.0, value=10.0, step=0.1)
53
+ # seed = gr.Slider(label="Seed", minimum=0, maximum=10000, value=42, step=1)
54
+ # eta = gr.Slider(label="ETA", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
55
+ # low_threshold = gr.Slider(label="Canny Low Threshold", minimum=1, maximum=255, value=100, step=1)
56
+ # high_threshold = gr.Slider(label="Canny High Threshold", minimum=1, maximum=255, value=200, step=1)
57
+ # submit = gr.Button("Generate")
58
+ # with gr.Column():
59
+ # output_image = gr.Gallery(label='Output', show_label=False, elem_id="gallery")
60
+ # 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)
61
+
62
+ # demo = block
63
+ # demo.launch()
64
  import torch
65
+ from diffusers import StableDiffusionControlNetPipeline
66
+ from diffusers import ControlNetModel
67
  import gradio as gr
68
+ from PIL import Image
69
+ import numpy as np
 
70
 
71
+ # 初始化 ControlNet 模型和 Stable Diffusion Pipeline
72
+ controlnet = ControlNetModel.from_pretrained("controlnet/checkpoints/ControlNetModel")
73
+ pipeline = StableDiffusionControlNetPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", controlnet=controlnet).to("cuda")
74
 
75
+ # 定义图像生成函数
76
+ def generate_image(prompt: str, input_image: Image.Image):
77
+ # 可以在这里根据传入的图像做一些预处理(例如,使用控制网络或图像生成模型)
78
+
79
+ # 将输入图像转换为合适的格式
80
+ input_image = input_image.convert("RGB")
81
+
82
+ # 生成图像
83
+ result_image = pipeline(prompt=prompt, init_image=input_image, strength=0.75).images[0]
84
+ return result_image
85
+
86
+ # 创建 Gradio 界面
87
+ iface = gr.Interface(
88
+ fn=generate_image,
89
+ inputs=[
90
+ gr.Textbox(label="Enter a prompt", placeholder="e.g. a futuristic city at sunset"), # 提示框
91
+ gr.Image(label="Upload an Image", type="pil") # 图像上传框
92
+ ],
93
+ outputs=gr.Image(label="Generated Image"), # 输出生成的图像
94
+ live=True
95
  )
96
 
97
+ # 启动 Gradio 应用
98
+ iface.launch()