File size: 1,412 Bytes
2a35f5b
c4e4c36
2a35f5b
c4e4c36
 
18ff94e
c4e4c36
 
 
 
18ff94e
2a35f5b
c4e4c36
18ff94e
2a35f5b
c4e4c36
2a35f5b
 
 
c4e4c36
 
 
 
 
 
 
 
 
 
 
 
 
 
4a2b93f
 
c4e4c36
4a2b93f
c4e4c36
 
 
 
 
 
 
2a35f5b
c4e4c36
2a35f5b
4a2b93f
c4e4c36
 
 
 
 
 
 
 
4a2b93f
c4e4c36
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
import gradio as gr
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel
from diffusers.utils import load_image
import qrcode

# Set device CPU karena tidak ada GPU
device = "cpu"

# Load ControlNet model
controlnet = ControlNetModel.from_pretrained(
    "DionTimmer/controlnet_qrcode-control_v1p_sd15",
    torch_dtype=torch.float32
)

# Load pipeline
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    controlnet=controlnet,
    torch_dtype=torch.float32,
    safety_checker=None
).to(device)


def generate_qr(prompt, url_text):
    # 1. Buat QR dasar dari teks/URL
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=4,
    )
    qr.add_data(url_text)
    qr.make(fit=True)

    qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB")

    # 2. Ubah ke gaya artistik dengan ControlNet
    image = pipe(
        prompt,
        image=qr_img,
        num_inference_steps=20,
        guidance_scale=7.5
    ).images[0]

    return image


demo = gr.Interface(
    fn=generate_qr,
    inputs=[
        gr.Textbox(label="Prompt / Deskripsi Gaya Gambar"),
        gr.Textbox(label="Teks atau URL untuk QR"),
    ],
    outputs=gr.Image(label="QR Code Artistik")
)

if __name__ == "__main__":
    demo.launch()