Saditiya commited on
Commit
2a35f5b
·
verified ·
1 Parent(s): b49262f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -1,51 +1,60 @@
1
  import gradio as gr
2
  import qrcode
3
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
- from diffusers.utils import load_image
5
- import torch
6
  from PIL import Image
7
- import io
 
8
 
9
- # Load ControlNet model khusus QR
10
  controlnet = ControlNetModel.from_pretrained(
11
- "monster-labs/controlnet-qr-code", torch_dtype=torch.float16
 
12
  )
13
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
14
- "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
 
 
 
 
15
  ).to("cuda")
16
 
17
- def generate_qr_with_logo(text, image):
18
- # 1. Buat QR code dasar
19
  qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
20
  qr.add_data(text)
21
  qr.make(fit=True)
22
- qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
23
 
24
- # 2. Resize agar sama dengan gambar input
25
- if image is not None:
26
- image = image.resize((512, 512))
27
- qr_img = qr_img.resize((512, 512))
 
28
 
29
- # 3. Gabungkan: ControlNet akan memandu agar QR mengikuti gambar dasar
30
- prompt = "artistic QR code blended with uploaded image, still scannable"
31
  result = pipe(
32
- prompt,
33
- image=qr_img,
34
  control_image=qr_img,
35
- num_inference_steps=30,
36
- guidance_scale=9.0
 
 
 
37
  ).images[0]
38
 
39
  return result
40
 
41
  with gr.Blocks() as demo:
42
- gr.Markdown("## Artistic QR Code Generator (seperti Starbucks QR)")
 
 
43
  with gr.Row():
44
- text = gr.Textbox(label="Teks/Link untuk QR code")
45
- img = gr.Image(label="Upload gambar dasar (logo/foto)", type="pil")
46
- btn = gr.Button("Generate")
47
- output = gr.Image(label="QR Code Hasil")
 
48
 
49
- btn.click(fn=generate_qr_with_logo, inputs=[text, img], outputs=output)
50
 
51
  demo.launch()
 
1
  import gradio as gr
2
  import qrcode
 
 
 
3
  from PIL import Image
4
+ import torch
5
+ from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel
6
 
7
+ # Load ControlNet QR model (publik)
8
  controlnet = ControlNetModel.from_pretrained(
9
+ "DionTimmer/controlnet_qrcode-control_v1p_sd15",
10
+ torch_dtype=torch.float16
11
  )
12
+
13
+ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
14
+ "runwayml/stable-diffusion-v1-5",
15
+ controlnet=controlnet,
16
+ safety_checker=None,
17
+ torch_dtype=torch.float16
18
  ).to("cuda")
19
 
20
+ def generate_qr_art(text, base_image):
21
+ # Generate QR dasar
22
  qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
23
  qr.add_data(text)
24
  qr.make(fit=True)
25
+ qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB").resize((512, 512))
26
 
27
+ # Gunakan gambar dasar jika ada
28
+ if base_image:
29
+ base = base_image.resize((512, 512))
30
+ else:
31
+ base = qr_img
32
 
33
+ # Generate artistic QR
 
34
  result = pipe(
35
+ prompt="artistic QR code blended with uploaded image, still scannable",
36
+ image=base,
37
  control_image=qr_img,
38
+ width=512,
39
+ height=512,
40
+ guidance_scale=7.5,
41
+ num_inference_steps=40,
42
+ controlnet_conditioning_scale=1.2
43
  ).images[0]
44
 
45
  return result
46
 
47
  with gr.Blocks() as demo:
48
+ gr.Markdown("## 🎨 Artistic QR Code Generator")
49
+ gr.Markdown("Masukkan teks/URL lalu upload logo atau gambar dasar → hasilnya QR artistik seperti contoh Starbucks.")
50
+
51
  with gr.Row():
52
+ text_input = gr.Textbox(label="Teks atau URL", placeholder="contoh: https://example.com")
53
+ img_input = gr.Image(label="Upload Gambar Dasar (opsional)", type="pil")
54
+
55
+ btn = gr.Button("🚀 Generate QR")
56
+ output = gr.Image(label="Hasil QR Artistik")
57
 
58
+ btn.click(generate_qr_art, inputs=[text_input, img_input], outputs=output)
59
 
60
  demo.launch()