lsb commited on
Commit
6b394d7
1 Parent(s): ee53163
Files changed (3) hide show
  1. Atkinson-Hyperlegible-Bold-102.otf +0 -0
  2. app.py +116 -0
  3. requirements.txt +5 -0
Atkinson-Hyperlegible-Bold-102.otf ADDED
Binary file (36.6 kB). View file
 
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import torch
4
+ from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageEnhance
5
+ from quanto import qfloat8, quantize, freeze
6
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
7
+ from diffusers.utils import make_image_grid
8
+
9
+ atkbold = ImageFont.truetype("Atkinson-Hyperlegible-Bold-102.otf",50)
10
+
11
+ default_width = 1024
12
+ default_height = 768
13
+ default_timesteps = 8
14
+
15
+ def mask_image_factory(mask_text="ASK FOR\nA SNACK", width=default_width, height=default_height):
16
+ img = Image.new("L", (width, height), (0,))
17
+ draw = ImageDraw.Draw(img)
18
+ draw.multiline_text(
19
+ xy=(0,0),
20
+ text=mask_text,
21
+ fill=(255,),
22
+ font=atkbold,
23
+ align="center",
24
+ spacing=0,
25
+ )
26
+ cropped = img.crop(img.getbbox())
27
+ # Calculate aspect ratios
28
+ image_aspect_ratio = width / height
29
+ cropped_aspect_ratio = cropped.size[0] / cropped.size[1]
30
+
31
+ # Determine which dimension of cropped.size is larger
32
+ if cropped_aspect_ratio > image_aspect_ratio:
33
+ # Calculate new dimensions for padding
34
+ new_width = int(cropped.size[1] * image_aspect_ratio)
35
+ new_height = cropped.size[1]
36
+ else:
37
+ new_width = cropped.size[0]
38
+ new_height = int(cropped.size[0] / image_aspect_ratio)
39
+
40
+ # Pad the image to the desired aspect ratio
41
+ padded = ImageOps.pad(cropped, (new_width, new_height))
42
+
43
+ resized = padded.resize((width, height), resample=Image.Resampling.LANCZOS)
44
+ return resized
45
+
46
+ preferred_device = "cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")
47
+ preferred_device = "cpu"
48
+ preferred_dtype = torch.float32
49
+
50
+ controlnet = ControlNetModel.from_pretrained(
51
+ "monster-labs/control_v1p_sd15_qrcode_monster",
52
+ # "monster-labs/control_v1p_sdxl_qrcode_monster",
53
+ subfolder="v2",
54
+ torch_dtype=preferred_dtype,
55
+ #torch_dtype=unet_preferred_dtype
56
+ ).to(preferred_device)
57
+
58
+ #quantize(controlnet, weights=qfloat8)
59
+ #freeze(controlnet)
60
+
61
+ ctlpipe = StableDiffusionControlNetPipeline.from_pretrained(
62
+ "SimianLuo/LCM_Dreamshaper_v7",
63
+ controlnet=controlnet,
64
+ torch_dtype=preferred_dtype,
65
+ safety_checker=None,
66
+ ).to(preferred_device)
67
+
68
+ #quantize(ctlpipe.unet, weights=qfloat8)
69
+ #freeze(ctlpipe.unet)
70
+ #quantize(ctlpipe.text_encoder, weights=qfloat8)
71
+ #freeze(ctlpipe.text_encoder)
72
+
73
+ def app(prompt, negative_prompt, mask_text, num_inference_steps, controlnet_conditioning_scale, width, height, seed, count):
74
+ all_images = [ctlpipe(
75
+ prompt=prompt,
76
+ negative_prompt=negative_prompt,
77
+ image=mask_image_factory(mask_text=mask_text, width=width, height=height),
78
+ num_inference_steps=int(num_inference_steps),
79
+ guidance_scale=8.0,
80
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale),
81
+ generator=torch.manual_seed(int(seed + i)),
82
+ height=height,
83
+ width=width,
84
+ ).images[0] for i in range(count)]
85
+ if count == 1:
86
+ cols = 1
87
+ rows = 1
88
+ elif count == 2:
89
+ cols = 1
90
+ rows = 2
91
+ else:
92
+ cols = 2 if count % 2 == 0 else 1
93
+ rows = count // cols
94
+ return make_image_grid(all_images, cols=cols, rows=rows)
95
+
96
+
97
+ app("corgis running in the park", "ugly, wrong", "ASK FOR\nA SNACK", 1, 1.0, default_height, default_width, 42, 1)
98
+
99
+ iface = gr.Interface(
100
+ app,
101
+ [
102
+ gr.Textbox(label="Prompt", value="corgis running in a flower-filled meadow at golden hour"),
103
+ gr.Textbox(label="Negative Prompt", value="ugly, wrong"),
104
+ gr.Textbox(label="Mask Text", value="ASK FOR\nA SNACK"),
105
+ gr.Number(label="Number of Inference Steps", value=default_timesteps, minimum=1, maximum=50, step=1),
106
+ gr.Slider(label="ControlNet Conditioning Scale", value=0.5, minimum=-1.0, maximum=2.0, step=0.01),
107
+ gr.Number(label="Width", value=default_width, minimum=256, maximum=2048, precision=0),
108
+ gr.Number(label="Height", value=default_height, minimum=256, maximum=2048, precision=0),
109
+ gr.Number(label="Random Number Seed", value=42, minimum=0, maximum=2**32-1, precision=0),
110
+ gr.Radio(label="Number of Images to Generate with Subsequent Consecutive Seeds", choices=[1, 2, 4, 6, 10], value=2),
111
+ ],
112
+ "image",
113
+ )
114
+
115
+ iface.launch()
116
+
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ diffusers
3
+ accelerate
4
+ optimum
5
+ quanto