import gradio as gr import qrcode from PIL import Image import uuid import os def generate_qr(data): if not data: raise gr.Error("Please enter text or URL!") qr = qrcode.QRCode(box_size=10, border=4) qr.add_data(data) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") img = img.convert("RGB") # Ensure PIL format return img def save_qr(data): if not data: raise gr.Error("Please enter text or URL!") # Generate QR qr = qrcode.QRCode(box_size=10, border=4) qr.add_data(data) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") img = img.convert("RGB") # Save with unique file name filename = f"qr_{uuid.uuid4().hex}.png" filepath = f"/tmp/{filename}" img.save(filepath) return filepath # Gradio automatically downloads this file with gr.Blocks() as demo: gr.Markdown("## 📱 QR Code Generator") text = gr.Textbox(label="Enter Text / URL / Image URL") with gr.Row(): generate = gr.Button("Generate QR Code") download = gr.Button("Download QR Code") output = gr.Image(label="QR Code") with gr.Accordion("Guideline", open=False): gr.Markdown(""" **How to use this app** 1. Enter Text or URL 2. Click **Generate QR Code** 3. Scan the QR code with your mobile 4. Or click **Download QR Code** to save it """) generate.click(fn=generate_qr, inputs=text, outputs=output) download.click(fn=save_qr, inputs=text, outputs=gr.File(label="Download File")) demo.launch()