Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import qrcode
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
|
| 8 |
-
qr.add_data(
|
| 9 |
qr.make(fit=True)
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
bg = Image.open(bg_file).convert("RGBA")
|
| 16 |
-
bg = bg.resize(qr_img.size)
|
| 17 |
-
qr_img = Image.alpha_composite(bg, qr_img)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
return
|
| 29 |
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
-
gr.Markdown("##
|
| 32 |
-
|
| 33 |
with gr.Row():
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
bg_file = gr.File(label="Upload Background (PNG/JPG)", type="filepath")
|
| 39 |
-
|
| 40 |
-
generate_btn = gr.Button("Generate QR")
|
| 41 |
-
output_img = gr.Image(label="Hasil QR Code")
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
demo.launch()
|
|
|
|
| 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()
|
|
|