CultriX commited on
Commit
9f6b576
·
verified ·
1 Parent(s): 44c3663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -56
app.py CHANGED
@@ -36,85 +36,58 @@ def read_qr(img):
36
  # Initialize OpenCV QR code detector
37
  detector = cv2.QRCodeDetector()
38
  data, _, _ = detector.detectAndDecode(img)
39
-
40
  return data if data else "No QR code found."
41
 
42
- # Custom CSS styling as HTML for dark mode
43
- custom_css = """
44
- <style>
45
- body {background-color: #1e1e2f; font-family: 'Arial', sans-serif; color: #e0e0e0;}
46
- .gradio-container {max-width: 700px; margin: auto; padding: 25px; background-color: #2c2c3e;
47
- border-radius: 15px; box-shadow: 0 8px 15px rgba(0, 0, 0, 0.3);}
48
- h1, h2 {text-align: center; color: #ffffff; font-weight: bold; margin-bottom: 15px;}
49
- .gr-button {background-color: #4a4a6a; color: #ffffff; padding: 10px 25px; border-radius: 8px;
50
- font-size: 16px; border: none; transition: 0.3s;}
51
- .gr-button:hover {background-color: #6a6a8a; transform: scale(1.05);}
52
- input, textarea, .gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 10px;
53
- padding: 10px; font-size: 14px; color: #e0e0e0;}
54
- .gr-box:hover, input:hover, textarea:hover {border-color: #777;}
55
- .tab {margin-top: 20px;}
56
- </style>
57
- """
58
-
59
- # Add download and copy-to-clipboard functionality
60
  def create_gradio_interface():
61
- # QR Code Generator Interface
62
  def generate_qr_with_download(data):
63
- qr_file = generate_qr(data)
64
- return qr_file, f"Download your QR code: [Download QR Code]({qr_file})"
 
 
 
 
 
65
 
 
66
  generate_interface = gr.Interface(
67
  fn=generate_qr_with_download,
68
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
69
- outputs=[
70
- gr.Image(label="Generated QR Code"),
71
- gr.HTML(label="Download")
72
- ],
73
  title="Generate QR Code",
74
  description="Quickly create a QR code from any text or URL.",
75
- theme="compact",
76
  )
77
 
78
- # QR Code Reader Interface
79
- def read_qr_with_copy(img):
80
- decoded_data = read_qr(img)
81
- clipboard_script = f"""
82
- <script>
83
- function copyToClipboard(text) {{
84
- navigator.clipboard.writeText(text).then(function() {{
85
- alert("Copied to clipboard: " + text);
86
- }}, function(err) {{
87
- alert("Failed to copy: ", err);
88
- }});
89
- }}
90
- </script>
91
- <button onclick="copyToClipboard('{decoded_data}')">Copy to Clipboard</button>
92
- """
93
- return decoded_data, clipboard_script
94
-
95
  read_interface = gr.Interface(
96
  fn=read_qr_with_copy,
97
  inputs=gr.Image(type="pil", label="Upload QR Code Image"),
98
  outputs=[
99
- gr.Textbox(label="Decoded Data"),
100
- gr.HTML(label="Copy to Clipboard")
101
  ],
102
  title="Read QR Code",
103
  description="Upload an image with a QR code to decode the embedded data.",
104
- theme="compact",
105
  )
106
 
107
- # Combine interfaces into a single tabbed layout
108
- interface = gr.TabbedInterface(
109
- [generate_interface, read_interface],
110
- ["Generate QR Code", "Read QR Code"]
111
- )
112
 
113
- # Launch interface with custom HTML for CSS styling
114
  with gr.Blocks() as demo:
115
- gr.HTML(custom_css) # Embed the custom CSS
116
- gr.Markdown("# QR Code Tool\nEasily generate and decode QR codes.")
117
- interface.render()
 
 
 
 
 
 
 
118
 
119
  demo.launch(share=True)
120
 
 
36
  # Initialize OpenCV QR code detector
37
  detector = cv2.QRCodeDetector()
38
  data, _, _ = detector.detectAndDecode(img)
 
39
  return data if data else "No QR code found."
40
 
41
+ # Gradio interface with functional buttons
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def create_gradio_interface():
43
+ # QR Code Generator with Downloadable File
44
  def generate_qr_with_download(data):
45
+ qr_file = generate_qr(data) # Generate QR code file
46
+ return qr_file # Return file directly as downloadable output
47
+
48
+ # QR Code Reader with Copy-to-Clipboard functionality
49
+ def read_qr_with_copy(img):
50
+ decoded_data = read_qr(img) # Decode QR code
51
+ return decoded_data, decoded_data # Return decoded text for display and copy
52
 
53
+ # Generator Interface
54
  generate_interface = gr.Interface(
55
  fn=generate_qr_with_download,
56
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
57
+ outputs=gr.File(label="Download Generated QR Code"), # File download
 
 
 
58
  title="Generate QR Code",
59
  description="Quickly create a QR code from any text or URL.",
 
60
  )
61
 
62
+ # Reader Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  read_interface = gr.Interface(
64
  fn=read_qr_with_copy,
65
  inputs=gr.Image(type="pil", label="Upload QR Code Image"),
66
  outputs=[
67
+ gr.Textbox(label="Decoded Data"), # Decoded QR code text
68
+ gr.Textbox(visible=False), # Invisible output for clipboard content
69
  ],
70
  title="Read QR Code",
71
  description="Upload an image with a QR code to decode the embedded data.",
72
+ live=False,
73
  )
74
 
75
+ # Add clipboard copy functionality using Gradio button callbacks
76
+ def on_copy(decoded_text):
77
+ return gr.Textbox.update(value=decoded_text) # Update clipboard content
 
 
78
 
79
+ # Custom UI with clipboard copy button
80
  with gr.Blocks() as demo:
81
+ gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
82
+ with gr.Tab("Generate QR Code"):
83
+ generate_interface.render()
84
+ with gr.Tab("Read QR Code"):
85
+ with gr.Row():
86
+ qr_text = gr.Textbox(label="Decoded Data")
87
+ qr_clipboard = gr.Textbox(visible=False) # Clipboard value
88
+ copy_button = gr.Button("Copy to Clipboard")
89
+ read_interface.render()
90
+ copy_button.click(on_copy, qr_text, qr_clipboard)
91
 
92
  demo.launch(share=True)
93