ZahirJS commited on
Commit
5d59e52
·
verified ·
1 Parent(s): 8641353

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import qrcode
3
+ from PIL import Image
4
+ import io
5
+ import base64
6
+
7
+ def generate_qr_code(text: str, size: str = "medium", error_correction: str = "M") -> dict:
8
+ """
9
+ Generate QR code from text/URL
10
+
11
+ Args:
12
+ text (str): Text or URL to encode
13
+ size (str): Size of QR code (small, medium, large)
14
+ error_correction (str): Error correction level (L, M, Q, H)
15
+
16
+ Returns:
17
+ dict: QR code information with qr_url field containing displayable image
18
+ """
19
+
20
+ if not text.strip():
21
+ return {"error": "Input cannot be empty."}
22
+
23
+ size_map = {
24
+ "small": 5,
25
+ "medium": 10,
26
+ "large": 15
27
+ }
28
+
29
+ error_map = {
30
+ "L": qrcode.constants.ERROR_CORRECT_L,
31
+ "M": qrcode.constants.ERROR_CORRECT_M,
32
+ "Q": qrcode.constants.ERROR_CORRECT_Q,
33
+ "H": qrcode.constants.ERROR_CORRECT_H
34
+ }
35
+
36
+ try:
37
+ qr = qrcode.QRCode(
38
+ version=1,
39
+ error_correction=error_map[error_correction],
40
+ box_size=size_map[size],
41
+ border=4,
42
+ )
43
+
44
+ qr.add_data(text)
45
+ qr.make(fit=True)
46
+
47
+ img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
48
+
49
+ buffer = io.BytesIO()
50
+ img.save(buffer, format="PNG")
51
+ img_base64 = base64.b64encode(buffer.getvalue()).decode()
52
+
53
+ return {
54
+ "qr_code_image": f"data:image/png;base64,{img_base64}",
55
+ "info": f"QR code generated for: {text}"
56
+ }
57
+
58
+ except Exception as e:
59
+ return {"error": f"Failed to generate QR code: {str(e)}"}
60
+
61
+ if __name__ == "__main__":
62
+ demo = gr.Interface(
63
+ fn=generate_qr_code,
64
+ inputs=[
65
+ gr.Textbox(placeholder="Enter URL or text to encode..."),
66
+ gr.Dropdown(["small", "medium", "large"], value="medium", label="Size"),
67
+ gr.Dropdown(["L", "M", "Q", "H"], value="M", label="Error Correction")
68
+ ],
69
+ outputs=gr.JSON(),
70
+ title="QR Code Generator for Claude",
71
+ description="Generate QR codes for Claude MCP integration"
72
+ )
73
+ demo.launch(mcp_server=True)