AP123 multimodalart HF staff commited on
Commit
9ad92f4
1 Parent(s): fa07c02

Update app.py (#2)

Browse files

- Update app.py (552ecdb2f129af4928bd10439ea41eec2bb9a52f)


Co-authored-by: Apolinário from multimodal AI art <[email protected]>

Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -4,8 +4,9 @@ import gradio as gr
4
  from PIL import Image
5
  from diffusers import (
6
  DiffusionPipeline,
7
- StableDiffusionControlNetImg2ImgPipeline,
8
  ControlNetModel,
 
9
  DPMSolverMultistepScheduler, # <-- Added import
10
  EulerDiscreteScheduler # <-- Added import
11
  )
@@ -13,12 +14,16 @@ from diffusers import (
13
  # Initialize both pipelines
14
  init_pipe = DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V2.0", torch_dtype=torch.float16).to("cuda")
15
  controlnet = ControlNetModel.from_pretrained("monster-labs/control_v1p_sd15_qrcode_monster", torch_dtype=torch.float16)
16
- main_pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
17
  "SG161222/Realistic_Vision_V2.0",
18
  controlnet=controlnet,
19
  safety_checker=None,
20
  torch_dtype=torch.float16,
21
  ).to("cuda")
 
 
 
 
22
 
23
  # Sampler map
24
  SAMPLER_MAP = {
@@ -26,6 +31,22 @@ SAMPLER_MAP = {
26
  "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
27
  }
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # Inference function
30
  def inference(
31
  control_image: Image.Image,
@@ -33,49 +54,46 @@ def inference(
33
  negative_prompt: str,
34
  guidance_scale: float = 8.0,
35
  controlnet_conditioning_scale: float = 1,
36
- strength: float = 0.9,
37
  seed: int = -1,
38
  sampler = "DPM++ Karras SDE",
 
39
  ):
40
  if prompt is None or prompt == "":
41
  raise gr.Error("Prompt is required")
42
 
43
  # Generate the initial image
44
- init_image = init_pipe(prompt).images[0]
45
 
46
  # Rest of your existing code
47
- control_image = control_image.resize((512, 512))
48
  main_pipe.scheduler = SAMPLER_MAP[sampler](main_pipe.scheduler.config)
49
  generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
50
 
51
  out = main_pipe(
52
  prompt=prompt,
53
  negative_prompt=negative_prompt,
54
- image=init_image,
55
- control_image=control_image,
56
- guidance_scale=guidance_scale,
57
- controlnet_conditioning_scale=controlnet_conditioning_scale,
58
  generator=generator,
59
- strength=strength,
60
  num_inference_steps=30,
61
- )
62
- return out.images[0]
 
 
63
 
64
  with gr.Blocks() as app:
65
  gr.Markdown(
66
  '''
67
- <center>
68
-
69
- <span style="color:blue; font-size:24px;">Illusion Diffusion 🌀</span>
70
- <span style="color:black; font-size:16px;">Generate stunning illusion artwork with Stable Diffusion</span>
71
- <span style="color:black; font-size:10px;">A space by AP [Follow me on Twitter](https://twitter.com/angrypenguinPNG)</span>
72
-
73
  </center>
74
 
75
- <p style="text-align:center;">
76
- <span style="color:black; font-size:10px;">This project works by using the QR Control Net by Monster Labs: [Monster Labs QR Control Net](https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster).
77
- Given a prompt, we generate an init image and pass that alongside the control illusion to create a stunning illusion! Credit to : MrUgleh (https://twitter.com/MrUgleh) for discovering the workflow :)</span>
78
- </p>
79
 
80
  '''
81
  )
@@ -83,13 +101,14 @@ with gr.Blocks() as app:
83
  with gr.Row():
84
  with gr.Column():
85
  control_image = gr.Image(label="Input Illusion", type="pil")
 
 
86
  prompt = gr.Textbox(label="Prompt")
87
  negative_prompt = gr.Textbox(label="Negative Prompt", value="ugly, disfigured, low quality, blurry, nsfw")
88
  with gr.Accordion(label="Advanced Options", open=False):
89
- controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=5.0, step=0.01, value=1.1, label="Controlnet Conditioning Scale")
90
- strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength")
91
  guidance_scale = gr.Slider(minimum=0.0, maximum=50.0, step=0.25, value=7.5, label="Guidance Scale")
92
- sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE")
93
  seed = gr.Slider(minimum=-1, maximum=9999999999, step=1, value=2313123, label="Seed", randomize=True)
94
  run_btn = gr.Button("Run")
95
  with gr.Column():
@@ -97,11 +116,11 @@ with gr.Blocks() as app:
97
 
98
  run_btn.click(
99
  inference,
100
- inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, strength, seed, sampler],
101
  outputs=[result_image]
102
  )
103
 
104
  app.queue(max_size=20)
105
 
106
  if __name__ == "__main__":
107
- app.launch(debug=True)
 
4
  from PIL import Image
5
  from diffusers import (
6
  DiffusionPipeline,
7
+ StableDiffusionControlNetPipeline,
8
  ControlNetModel,
9
+ StableDiffusionLatentUpscalePipeline,
10
  DPMSolverMultistepScheduler, # <-- Added import
11
  EulerDiscreteScheduler # <-- Added import
12
  )
 
14
  # Initialize both pipelines
15
  init_pipe = DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V2.0", torch_dtype=torch.float16).to("cuda")
16
  controlnet = ControlNetModel.from_pretrained("monster-labs/control_v1p_sd15_qrcode_monster", torch_dtype=torch.float16)
17
+ main_pipe = StableDiffusionControlNetPipeline.from_pretrained(
18
  "SG161222/Realistic_Vision_V2.0",
19
  controlnet=controlnet,
20
  safety_checker=None,
21
  torch_dtype=torch.float16,
22
  ).to("cuda")
23
+ model_id = "stabilityai/sd-x2-latent-upscaler"
24
+ upscaler = StableDiffusionLatentUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
25
+ upscaler.to("cuda")
26
+
27
 
28
  # Sampler map
29
  SAMPLER_MAP = {
 
31
  "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
32
  }
33
 
34
+ def center_crop_resize(img, output_size=(512, 512)):
35
+ width, height = img.size
36
+
37
+ # Calculate dimensions to crop to the center
38
+ new_dimension = min(width, height)
39
+ left = (width - new_dimension)/2
40
+ top = (height - new_dimension)/2
41
+ right = (width + new_dimension)/2
42
+ bottom = (height + new_dimension)/2
43
+
44
+ # Crop and resize
45
+ img = img.crop((left, top, right, bottom))
46
+ img = img.resize(output_size)
47
+
48
+ return img
49
+
50
  # Inference function
51
  def inference(
52
  control_image: Image.Image,
 
54
  negative_prompt: str,
55
  guidance_scale: float = 8.0,
56
  controlnet_conditioning_scale: float = 1,
 
57
  seed: int = -1,
58
  sampler = "DPM++ Karras SDE",
59
+ progress = gr.Progress(track_tqdm=True)
60
  ):
61
  if prompt is None or prompt == "":
62
  raise gr.Error("Prompt is required")
63
 
64
  # Generate the initial image
65
+ #init_image = init_pipe(prompt).images[0]
66
 
67
  # Rest of your existing code
68
+ control_image = center_crop_resize(control_image)
69
  main_pipe.scheduler = SAMPLER_MAP[sampler](main_pipe.scheduler.config)
70
  generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
71
 
72
  out = main_pipe(
73
  prompt=prompt,
74
  negative_prompt=negative_prompt,
75
+ image=control_image,
76
+ #control_image=control_image,
77
+ guidance_scale=float(guidance_scale),
78
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
79
  generator=generator,
80
+ #strength=strength,
81
  num_inference_steps=30,
82
+ #output_type="latent"
83
+ ).images[0]
84
+
85
+ return out
86
 
87
  with gr.Blocks() as app:
88
  gr.Markdown(
89
  '''
90
+ <center><h1>Illusion Diffusion 🌀</h1></span>
91
+ <span font-size:16px;">Generate stunning illusion artwork with Stable Diffusion</span>
92
+ <span font-size:10px;">A space by AP [Follow me on Twitter](https://twitter.com/angrypenguinPNG)</span>
 
 
 
93
  </center>
94
 
95
+ This project works by using the QR Control Net by Monster Labs: [Monster Labs QR Control Net](https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster).
96
+ Given a prompt and your pattern, we use a QR code conditioned controlnet to create a stunning illusion! Credit to: MrUgleh (https://twitter.com/MrUgleh) for discovering the workflow :)
 
 
97
 
98
  '''
99
  )
 
101
  with gr.Row():
102
  with gr.Column():
103
  control_image = gr.Image(label="Input Illusion", type="pil")
104
+ controlnet_conditioning_scale = gr.Slider(minimum=0.0, maximum=5.0, step=0.01, value=0.8, label="Illusion strength", info="ControlNet conditioning scale")
105
+ gr.Examples(examples=["checkers.png", "pattern.png", "spiral.jpeg"], inputs=control_image)
106
  prompt = gr.Textbox(label="Prompt")
107
  negative_prompt = gr.Textbox(label="Negative Prompt", value="ugly, disfigured, low quality, blurry, nsfw")
108
  with gr.Accordion(label="Advanced Options", open=False):
109
+ #strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength")
 
110
  guidance_scale = gr.Slider(minimum=0.0, maximum=50.0, step=0.25, value=7.5, label="Guidance Scale")
111
+ sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="Euler")
112
  seed = gr.Slider(minimum=-1, maximum=9999999999, step=1, value=2313123, label="Seed", randomize=True)
113
  run_btn = gr.Button("Run")
114
  with gr.Column():
 
116
 
117
  run_btn.click(
118
  inference,
119
+ inputs=[control_image, prompt, negative_prompt, guidance_scale, controlnet_conditioning_scale, seed, sampler],
120
  outputs=[result_image]
121
  )
122
 
123
  app.queue(max_size=20)
124
 
125
  if __name__ == "__main__":
126
+ app.launch()