aisuko commited on
Commit
6d75b72
1 Parent(s): d1f7f05

Init commit

Browse files

Signed-off-by: Aisuko <urakiny@gmail.com>

Files changed (2) hide show
  1. app.py +217 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import qrcode
5
+
6
+
7
+ from diffusers import (
8
+ StableDiffusionControlNetImg2ImgPipeline,
9
+ ControlNetModel,
10
+ DDIMScheduler,
11
+ DPMSolverMultistepScheduler,
12
+ DEISMultistepScheduler,
13
+ HeunDiscreteScheduler,
14
+ EulerDiscreteScheduler,
15
+ )
16
+
17
+ controlnet = ControlNetModel.from_pretrained(
18
+ "DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
19
+ )
20
+
21
+ pipe= StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
22
+ "runwayml/stable-diffusion-v1-5",
23
+ controlnet=controlnet,
24
+ use_safetensors=True,
25
+ torch_dtype=torch.float16,
26
+ ).to("cuda")
27
+
28
+
29
+ SAMPLER_MAP={
30
+ "DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
31
+ "DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True),
32
+ "Heun": lambda config: HeunDiscreteScheduler.from_config(config),
33
+ "Euler": lambda config: EulerDiscreteScheduler.from_config(config),
34
+ "DDIM": lambda config: DDIMScheduler.from_config(config),
35
+ "DEIS": lambda config: DEISMultistepScheduler.from_config(config),
36
+ }
37
+
38
+
39
+ def inference(
40
+ qr_code_content: str,
41
+ prompt: str,
42
+ negative_prompt: str,
43
+ guidance_scale: float = 10.0,
44
+ controlnet_conditioning_scale: float = 2.0,
45
+ strength: float = 0.8,
46
+ seed: int = -1,
47
+ init_image: Image.Image | None = None,
48
+ qrcode_image: Image.Image | None = None,
49
+ sampler = "DPM++ Karras SDE",
50
+ ):
51
+ if prompt is None or prompt == "":
52
+ raise gr.Error("Prompt is required")
53
+
54
+ if qrcode_image is None and qr_code_content == "":
55
+ raise gr.Error("QR Code Image or QR Code Content is required")
56
+
57
+ pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
58
+
59
+ generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
60
+
61
+ if qr_code_content != "" or qrcode_image.size == (1, 1):
62
+ qr = qrcode.QRCode(
63
+ version=1,
64
+ error_correction=qrcode.constants.ERROR_CORRECT_H,
65
+ box_size=10,
66
+ border=4,
67
+ )
68
+ qr.add_data(qr_code_content)
69
+ qr.make(fit=True)
70
+
71
+ qrcode_image = qr.make_image(fill_color="black", back_color="white")
72
+ qrcode_image = qrcode_image.resize((768, 768))
73
+ else:
74
+ qrcode_image = qrcode_image.resize((768, 768))
75
+
76
+ # hack due to gradio examples
77
+ init_image = qrcode_image
78
+
79
+ out = pipe(
80
+ prompt=prompt,
81
+ negative_prompt=negative_prompt,
82
+ image=init_image,
83
+ control_image=qrcode_image, # type: ignore
84
+ width=768, # type: ignore
85
+ height=768, # type: ignore
86
+ guidance_scale=float(guidance_scale),
87
+ controlnet_conditioning_scale=float(controlnet_conditioning_scale), # type: ignore
88
+ generator=generator,
89
+ strength=float(strength),
90
+ num_inference_steps=40,
91
+ )
92
+ return out.images[0] # type: ignore
93
+
94
+ def inference_ui_demo():
95
+ return None
96
+
97
+ # https://www.kaggle.com/code/aisuko/text-to-image-qr-code-generator/notebook
98
+ # image=inference(qr_code_content="https://www.kaggle.com/aisuko",
99
+ # prompt="A sky view of a colorful lakes and rivers flowing through the mountains",
100
+ # negative_prompt="ugly, disfigured, low quality, blurry, nsfw",
101
+ # guidance_scale=7.5,
102
+ # controlnet_conditioning_scale=1.3,
103
+ # strength=0.9,
104
+ # seed=5392011833,
105
+ # init_image=None,
106
+ # qrcode_image=None,
107
+ # sampler="DPM++ Karras SDE")
108
+
109
+ with gr.Blocks() as blocks:
110
+ gr.Markdown(
111
+ """
112
+ # QR Code Image to Image UI Demo
113
+
114
+ This code cannot be runable because of the low resource. So, it is aimed to show the the componnets of the UI only.
115
+
116
+ If you want to run the Code, please go to Kaggle <a href="https://www.kaggle.com/code/aisuko/text-to-image-qr-code-generator/notebook" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
117
+ """
118
+ )
119
+
120
+ with gr.Row():
121
+ with gr.Column():
122
+ qrcode_content=gr.Textbox(
123
+ label="QR Code Content",
124
+ info="QR Code Content or URL",
125
+ value="",
126
+ )
127
+ with gr.Accordion(label="QR Code Image (Optional)", open=False):
128
+ qr_code_image=gr.Image(
129
+ label="QR Code Image (Optional). Leave blank to automatically generate QR Code",
130
+ type="pil",
131
+ )
132
+
133
+ prompt=gr.Textbox(
134
+ label="Prompt",
135
+ info="Prompt that guides the generation towards",
136
+ )
137
+
138
+ negative_prompt=gr.Textbox(
139
+ label="Negative Prompt",
140
+ value="ugly, disfigured, low quality, blurry, nsfw",
141
+ )
142
+
143
+ use_qr_code_as_init_image=gr.Checkbox(
144
+ label="Use QR code as init image",
145
+ value=True,
146
+ interactive=False,
147
+ info="Whether init image should be QR code. Unclick to pass init image or generate init image with Stable Diffusion 1.5"
148
+ )
149
+
150
+ with gr.Accordion(label="Init Image (Optional)", open=False) as init_image_acc:
151
+ init_image=gr.Image(
152
+ label="Init Image (Optional). Leave blank to generate image with SD 1.5",
153
+ type="pil",
154
+ )
155
+
156
+ with gr.Accordion(
157
+ label="Params: The generated QR Code functionality is largely influenced by the parameters detailed below",
158
+ open=True,):
159
+
160
+ controlnet_conditioning_scale=gr.Slider(
161
+ minimum=0.0,
162
+ maximum=5.0,
163
+ step=0.1,
164
+ value=1.1,
165
+ label="Controlnet Conditioning Scale",
166
+ )
167
+ strength=gr.Slider(
168
+ minimum=0.0,
169
+ maximum=1.0,
170
+ step=0.1,
171
+ value=0.9,
172
+ label="Strength",
173
+ )
174
+ guidance_scale=gr.Slider(
175
+ minimum=0.0,
176
+ maximum=10.0,
177
+ step=0.1,
178
+ value=7.5,
179
+ label="Guidance Scale",
180
+ )
181
+ sampler=gr.Dropdown(
182
+ choices=list(SAMPLER_MAP.keys()),
183
+ value="DPM++ Karras SDE",
184
+ label="Sampler"
185
+ )
186
+ seed=gr.Slider(
187
+ minimum=-1,
188
+ maximum=9999999999,
189
+ step=1,
190
+ value=2313123,
191
+ label="Seed",
192
+ randomize=True,
193
+ )
194
+ with gr.Row():
195
+ btn=gr.Button("Run")
196
+ with gr.Column():
197
+ result_image=gr.Image(label="Result Image")
198
+
199
+ btn.click(
200
+ inference_ui_demo,
201
+ inputs=[
202
+ qrcode_content,
203
+ prompt,
204
+ negative_prompt,
205
+ guidance_scale,
206
+ controlnet_conditioning_scale,
207
+ strength,
208
+ seed,
209
+ init_image,
210
+ qr_code_image,
211
+ sampler,
212
+ ],
213
+ outputs=[result_image],
214
+ )
215
+
216
+ blocks.queue(concurrency_count=1, max_size=2)
217
+ blocks.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ diffusers==0.23.1
2
+ torch==2.1.1
3
+ gradio==4.7.1
4
+ Pillow==10.1.0
5
+ qrcode==7.4.2