File size: 2,087 Bytes
5d59e52
 
 
 
 
 
8f82404
5d59e52
 
 
 
 
 
 
 
 
8f82404
5d59e52
 
 
191e6aa
5d59e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191e6aa
 
5d59e52
 
8f82404
5d59e52
 
 
 
 
 
 
 
 
8f82404
5d59e52
 
 
 
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
68
69
70
71
import gradio as gr
import qrcode
from PIL import Image
import io
import base64

def generate_qr_code(text: str, size: str = "medium", error_correction: str = "M") -> str:
    """
    Generate QR code from text/URL
    
    Args:
        text (str): Text or URL to encode
        size (str): Size of QR code (small, medium, large)
        error_correction (str): Error correction level (L, M, Q, H)
    
    Returns:
        str: Base64 data URL that can be displayed as QR code image
    """
    
    if not text.strip():
        return "Error: Input cannot be empty."
    
    size_map = {
        "small": 5,
        "medium": 10,
        "large": 15
    }
    
    error_map = {
        "L": qrcode.constants.ERROR_CORRECT_L,
        "M": qrcode.constants.ERROR_CORRECT_M,
        "Q": qrcode.constants.ERROR_CORRECT_Q,
        "H": qrcode.constants.ERROR_CORRECT_H
    }
    
    try:
        qr = qrcode.QRCode(
            version=1,
            error_correction=error_map[error_correction],
            box_size=size_map[size],
            border=4,
        )
        
        qr.add_data(text)
        qr.make(fit=True)
        
        img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
        
        buffer = io.BytesIO()
        img.save(buffer, format="PNG")
        img_base64 = base64.b64encode(buffer.getvalue()).decode()
        
        return f"data:image/png;base64,{img_base64}"
        
        
    except Exception as e:
        return f"Error: Failed to generate QR code: {str(e)}"

if __name__ == "__main__":
    demo = gr.Interface(
        fn=generate_qr_code,
        inputs=[
            gr.Textbox(placeholder="Enter URL or text to encode..."),
            gr.Dropdown(["small", "medium", "large"], value="medium", label="Size"),
            gr.Dropdown(["L", "M", "Q", "H"], value="M", label="Error Correction")
        ],
        outputs=gr.Textbox(label="QR Code Data URL"),
        title="QR Code Generator for Claude",
        description="Generate QR codes for Claude MCP integration"
    )
    demo.launch(mcp_server=True)