Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
|
| 8 |
|
| 9 |
-
# Load ControlNet model
|
| 10 |
controlnet = ControlNetModel.from_pretrained(
|
| 11 |
-
"
|
|
|
|
| 12 |
)
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
).to("cuda")
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
#
|
| 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 |
-
#
|
| 25 |
-
if
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
prompt = "artistic QR code blended with uploaded image, still scannable"
|
| 31 |
result = pipe(
|
| 32 |
-
prompt,
|
| 33 |
-
image=
|
| 34 |
control_image=qr_img,
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
).images[0]
|
| 38 |
|
| 39 |
return result
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown("## Artistic QR Code Generator
|
|
|
|
|
|
|
| 43 |
with gr.Row():
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
btn.click(
|
| 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()
|