Spaces:
Running
Running
| 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) |