kadirnar commited on
Commit
2204ef0
1 Parent(s): 83675c6

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +162 -0
  2. requirements.txt +5 -0
  3. utils/image2image.py +36 -0
  4. utils/text2image.py +33 -0
app.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.image2image import stable_diffusion_img2img
2
+ from utils.text2image import stable_diffusion_text2img
3
+
4
+ import gradio as gr
5
+
6
+ stable_model_list = [
7
+ "runwayml/stable-diffusion-v1-5",
8
+ "stabilityai/stable-diffusion-2",
9
+ "stabilityai/stable-diffusion-2-base",
10
+ "stabilityai/stable-diffusion-2-1",
11
+ "stabilityai/stable-diffusion-2-1-base"
12
+ ]
13
+ stable_prompt_list = [
14
+ "a photo of a man.",
15
+ "a photo of a girl."
16
+ ]
17
+
18
+ stable_negative_prompt_list = [
19
+ "bad, ugly",
20
+ "deformed"
21
+ ]
22
+ app = gr.Blocks()
23
+ with app:
24
+ gr.Markdown("# **<h2 align='center'>Stable Diffusion WebUI<h2>**")
25
+ gr.Markdown(
26
+ """
27
+ <h5 style='text-align: center'>
28
+ Follow me for more!
29
+ <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a>
30
+ </h5>
31
+ """
32
+ )
33
+ with gr.Row():
34
+ with gr.Column():
35
+ with gr.Tab('Text2Image'):
36
+ text2image_model_id = gr.Dropdown(
37
+ choices=stable_model_list,
38
+ value=stable_model_list[0],
39
+ label='Text-Image Model Id'
40
+ )
41
+
42
+ text2image_prompt = gr.Textbox(
43
+ lines=1,
44
+ value=stable_prompt_list[0],
45
+ label='Prompt'
46
+ )
47
+
48
+ text2image_negative_prompt = gr.Textbox(
49
+ lines=1,
50
+ value=stable_negative_prompt_list[0],
51
+ label='Negative Prompt'
52
+ )
53
+
54
+ with gr.Accordion("Advanced Options", open=False):
55
+ text2image_guidance_scale = gr.Slider(
56
+ minimum=0.1,
57
+ maximum=15,
58
+ step=0.1,
59
+ value=7.5,
60
+ label='Guidance Scale'
61
+ )
62
+
63
+ text2image_num_inference_step = gr.Slider(
64
+ minimum=1,
65
+ maximum=100,
66
+ step=1,
67
+ value=50,
68
+ label='Num Inference Step'
69
+ )
70
+
71
+ text2image_height = gr.Slider(
72
+ minimum=128,
73
+ maximum=1280,
74
+ step=32,
75
+ value=512,
76
+ label='Tile Height'
77
+ )
78
+
79
+ text2image_width = gr.Slider(
80
+ minimum=128,
81
+ maximum=1280,
82
+ step=32,
83
+ value=768,
84
+ label='Tile Height'
85
+ )
86
+
87
+ text2image_predict = gr.Button(value='Generator')
88
+
89
+
90
+ with gr.Tab('Image2Image'):
91
+ image2image2_image_file = gr.Image(label='Image')
92
+
93
+ image2image_model_id = gr.Dropdown(
94
+ choices=stable_model_list,
95
+ value=stable_model_list[0],
96
+ label='Image-Image Model Id'
97
+ )
98
+
99
+ image2image_prompt = gr.Textbox(
100
+ lines=1,
101
+ value=stable_prompt_list[0],
102
+ label='Prompt'
103
+ )
104
+
105
+ image2image_negative_prompt = gr.Textbox(
106
+ lines=1,
107
+ value=stable_negative_prompt_list[0],
108
+ label='Negative Prompt'
109
+ )
110
+
111
+ with gr.Accordion("Advanced Options", open=False):
112
+ image2image_guidance_scale = gr.Slider(
113
+ minimum=0.1,
114
+ maximum=15,
115
+ step=0.1,
116
+ value=7.5,
117
+ label='Guidance Scale'
118
+ )
119
+
120
+ image2image_num_inference_step = gr.Slider(
121
+ minimum=1,
122
+ maximum=100,
123
+ step=1,
124
+ value=50,
125
+ label='Num Inference Step'
126
+ )
127
+
128
+ image2image_predict = gr.Button(value='Generator')
129
+
130
+
131
+ with gr.Tab('Generator'):
132
+ with gr.Column():
133
+ output_image = gr.Image(label='Image')
134
+
135
+ text2image_predict.click(
136
+ fn = stable_diffusion_text2img,
137
+ inputs = [
138
+ text2image_model_id,
139
+ text2image_prompt,
140
+ text2image_negative_prompt,
141
+ text2image_guidance_scale,
142
+ text2image_num_inference_step,
143
+ text2image_height,
144
+ text2image_width,
145
+ ],
146
+ outputs = [output_image],
147
+ )
148
+
149
+ image2image_predict.click(
150
+ fn = stable_diffusion_img2img,
151
+ inputs = [
152
+ image2image2_image_file,
153
+ image2image_model_id,
154
+ image2image_prompt,
155
+ image2image_negative_prompt,
156
+ image2image_guidance_scale,
157
+ image2image_num_inference_step,
158
+ ],
159
+ outputs = [output_image],
160
+ )
161
+
162
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers
2
+ bitsandbytes==0.35.0
3
+ xformers
4
+ controlnet_aux
5
+ diffusers
utils/image2image.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionImg2ImgPipeline, DDIMScheduler
2
+ from IPython.display import display
3
+ from PIL import Image
4
+ import torch
5
+
6
+ def stable_diffusion_img2img(
7
+ model_path:str,
8
+ image_path:str,
9
+ prompt:str,
10
+ negative_prompt:str,
11
+ num_samples:int,
12
+ guidance_scale:int,
13
+ num_inference_step:int,
14
+ ):
15
+
16
+ image = Image.open(image_path)
17
+
18
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
19
+ model_path,
20
+ safety_checker=None,
21
+ torch_dtype=torch.float16
22
+ )
23
+ pipe.to("cuda")
24
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
25
+ pipe.enable_xformers_memory_efficient_attention()
26
+
27
+ output = pipe(
28
+ prompt = prompt,
29
+ image = image,
30
+ negative_prompt = negative_prompt,
31
+ num_images_per_prompt = num_samples,
32
+ num_inference_steps = num_inference_step,
33
+ guidance_scale = guidance_scale,
34
+ ).images
35
+
36
+ return output
utils/text2image.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline, DDIMScheduler
2
+ import torch
3
+
4
+ def stable_diffusion_text2img(
5
+ model_path:str,
6
+ prompt:str,
7
+ negative_prompt:str,
8
+ guidance_scale:int,
9
+ num_inference_step:int,
10
+ height:int,
11
+ width:int,
12
+ ):
13
+
14
+ pipe = StableDiffusionPipeline.from_pretrained(
15
+ model_path,
16
+ safety_checker=None,
17
+ torch_dtype=torch.float16
18
+ ).to("cuda")
19
+
20
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
21
+ pipe.enable_xformers_memory_efficient_attention()
22
+
23
+ images = pipe(
24
+ prompt,
25
+ height=height,
26
+ width=width,
27
+ negative_prompt=negative_prompt,
28
+ num_images_per_prompt=1,
29
+ num_inference_steps=num_inference_step,
30
+ guidance_scale=guidance_scale,
31
+ ).images
32
+
33
+ return images