File size: 1,615 Bytes
a7dd399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
import gradio as gr
import qrcode
from PIL import Image
import io

def generate_qrcode(text):
    """

    Generate a QR code image from input text

    """
    # Create QR code instance
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    
    # Add data to the QR code
    qr.add_data(text)
    qr.make(fit=True)
    
    # Create an image from the QR code
    img = qr.make_image(fill_color="black", back_color="white")
    
    # Convert to PIL Image
    pil_img = img.get_image()
    
    return pil_img

# Create Gradio interface
with gr.Blocks() as app:
    gr.Markdown("## 🧾 QR Code Generator")
    gr.Markdown("Enter text or a URL to generate a QR code")
    
    with gr.Row():
        text_input = gr.Textbox(
            label="Input Text/URL",
            placeholder="Enter text or URL here...",
            max_lines=1
        )
        
    with gr.Row():
        generate_btn = gr.Button("Generate QR Code")
    
    with gr.Row():
        image_output = gr.Image(label="Generated QR Code")
    
    # Set up event handler
    generate_btn.click(
        fn=generate_qrcode,
        inputs=text_input,
        outputs=image_output
    )
    
    # Example inputs
    gr.Examples(
        examples=[
            "https://www.example.com",
            "Hello World!",
            "mailto:[email protected]",
            "WIFI:S:MyNetwork;T:WPA;P:MyPassword;;"
        ],
        inputs=text_input
    )

# Launch the app
app.launch()