alfredplpl commited on
Commit
431f639
1 Parent(s): 7dfd19e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -16
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # Thank AK. https://huggingface.co/spaces/akhaliq/cool-japan-diffusion-2-1-0/blob/main/app.py
2
- from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, EulerAncestralDiscreteScheduler
3
  from transformers import CLIPFeatureExtractor
4
  import gradio as gr
5
  import torch
@@ -27,6 +27,10 @@ pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(
27
  )
28
  pipe_i2i.enable_xformers_memory_efficient_attention()
29
 
 
 
 
 
30
  if torch.cuda.is_available():
31
  pipe = pipe.to("cuda")
32
  pipe_i2i = pipe_i2i.to("cuda")
@@ -45,27 +49,36 @@ def inference(prompt, guidance, steps, image_size="Square", seed=0, img=None, st
45
  if(image_size=="Portrait"):
46
  height=1024
47
  width=768
 
48
  #pipe.enable_attention_slicing()
49
  elif(image_size=="Landscape"):
50
  height=768
51
  width=1024
 
52
  #pipe.enable_attention_slicing()
53
  elif(image_size=="Highreso."):
54
  height=1024
55
- width=1024
 
 
 
 
 
 
56
  #pipe.enable_attention_slicing()
57
  else:
58
  height=768
59
  width=768
 
60
  #pipe.enable_attention_slicing()
61
 
62
  print(prompt,neg_prompt)
63
 
64
  try:
65
  if img is not None:
66
- return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
67
  else:
68
- return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator), None
69
  except Exception as e:
70
  return None, error_str(e)
71
  def auto_prompt_correction(prompt_ui,neg_prompt_ui,cool_japan_type_ui,disable_auto_prompt_correction):
@@ -123,22 +136,65 @@ def auto_prompt_correction(prompt_ui,neg_prompt_ui,cool_japan_type_ui,disable_au
123
 
124
  return prompt,neg_prompt
125
 
126
- def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator):
127
- result = pipe(
128
- prompt,
129
- negative_prompt = neg_prompt,
130
- num_inference_steps = int(steps),
131
- guidance_scale = guidance,
132
- width = width,
133
- height = height,
134
- generator = generator)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  return result.images[0]
137
 
138
- def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
139
  ratio = min(height / img.height, width / img.width)
140
  img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
141
- result = pipe_i2i(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  prompt,
143
  negative_prompt = neg_prompt,
144
  init_image = img,
@@ -148,6 +204,7 @@ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height
148
  #width = width,
149
  #height = height,
150
  generator = generator)
 
151
 
152
  return result.images[0]
153
 
@@ -199,7 +256,7 @@ with gr.Blocks(css=css) as demo:
199
  neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
200
  disable_auto_prompt_correction = gr.Checkbox(label="Disable auto prompt corretion.")
201
  with gr.Row():
202
- image_size=gr.Radio(["Portrait","Landscape","Square","Highreso."])
203
  image_size.show_label=False
204
  image_size.value="Square"
205
 
 
1
  # Thank AK. https://huggingface.co/spaces/akhaliq/cool-japan-diffusion-2-1-0/blob/main/app.py
2
+ from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, EulerAncestralDiscreteScheduler,StableDiffusionLatentUpscalePipeline
3
  from transformers import CLIPFeatureExtractor
4
  import gradio as gr
5
  import torch
 
27
  )
28
  pipe_i2i.enable_xformers_memory_efficient_attention()
29
 
30
+ upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained("stabilityai/sd-x2-latent-upscaler", torch_dtype=torch.float16)
31
+ upscaler.enable_xformers_memory_efficient_attention()
32
+ upscaler.to("cuda")
33
+
34
  if torch.cuda.is_available():
35
  pipe = pipe.to("cuda")
36
  pipe_i2i = pipe_i2i.to("cuda")
 
49
  if(image_size=="Portrait"):
50
  height=1024
51
  width=768
52
+ superreso=False
53
  #pipe.enable_attention_slicing()
54
  elif(image_size=="Landscape"):
55
  height=768
56
  width=1024
57
+ superreso=False
58
  #pipe.enable_attention_slicing()
59
  elif(image_size=="Highreso."):
60
  height=1024
61
+ width=1024
62
+ superreso=False
63
+ #pipe.enable_attention_slicing()
64
+ elif(image_size=="Superreso."):
65
+ height=1024
66
+ width=1024
67
+ superreso=True
68
  #pipe.enable_attention_slicing()
69
  else:
70
  height=768
71
  width=768
72
+ superreso=False
73
  #pipe.enable_attention_slicing()
74
 
75
  print(prompt,neg_prompt)
76
 
77
  try:
78
  if img is not None:
79
+ return img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator,superreso), None
80
  else:
81
+ return txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,superreso), None
82
  except Exception as e:
83
  return None, error_str(e)
84
  def auto_prompt_correction(prompt_ui,neg_prompt_ui,cool_japan_type_ui,disable_auto_prompt_correction):
 
136
 
137
  return prompt,neg_prompt
138
 
139
+ def txt_to_img(prompt, neg_prompt, guidance, steps, width, height, generator,superreso=False):
140
+ if(superreso):
141
+ low_res_latents = pipe(
142
+ prompt,
143
+ negative_prompt = neg_prompt,
144
+ num_inference_steps = int(steps),
145
+ guidance_scale = guidance,
146
+ width = width,
147
+ height = height,
148
+ output_type="latent",
149
+ generator = generator)
150
+ low_res_latents = pipeline(prompt, generator=generator, output_type="latent").images
151
+
152
+ result = upscaler(
153
+ prompt=prompt,
154
+ negative_prompt = neg_prompt,
155
+ image=low_res_latents,
156
+ num_inference_steps=20,
157
+ guidance_scale=guidance,
158
+ generator=generator,
159
+ )
160
+ else:
161
+ result = pipe(
162
+ prompt,
163
+ negative_prompt = neg_prompt,
164
+ num_inference_steps = int(steps),
165
+ guidance_scale = guidance,
166
+ width = width,
167
+ height = height,
168
+ generator = generator)
169
 
170
  return result.images[0]
171
 
172
+ def img_to_img(prompt, neg_prompt, img, strength, guidance, steps, width, height, generator,superreso=False):
173
  ratio = min(height / img.height, width / img.width)
174
  img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
175
+ if(superreso):
176
+ low_res_latents = pipe_i2i(
177
+ prompt,
178
+ negative_prompt = neg_prompt,
179
+ init_image = img,
180
+ num_inference_steps = int(steps),
181
+ strength = strength,
182
+ guidance_scale = guidance,
183
+ #width = width,
184
+ #height = height,
185
+ output_type="latent",
186
+ generator = generator)
187
+
188
+ result = upscaler(
189
+ prompt=prompt,
190
+ negative_prompt = neg_prompt,
191
+ image=low_res_latents,
192
+ num_inference_steps=20,
193
+ guidance_scale=guidance,
194
+ generator=generator,
195
+ )
196
+ else:
197
+ result = pipe_i2i(
198
  prompt,
199
  negative_prompt = neg_prompt,
200
  init_image = img,
 
204
  #width = width,
205
  #height = height,
206
  generator = generator)
207
+
208
 
209
  return result.images[0]
210
 
 
256
  neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
257
  disable_auto_prompt_correction = gr.Checkbox(label="Disable auto prompt corretion.")
258
  with gr.Row():
259
+ image_size=gr.Radio(["Portrait","Landscape","Square","Highreso.","Superreso."])
260
  image_size.show_label=False
261
  image_size.value="Square"
262