CultriX commited on
Commit
9e1cd24
·
verified ·
1 Parent(s): 784d954

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -22,7 +22,7 @@ def generate_qr(data):
22
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
23
  img.save(temp_file.name, format="PNG")
24
  temp_file.close() # Ensure file is written to disk
25
- return temp_file.name, img # Return the file path and the PIL image
26
 
27
 
28
  # Function to read a QR code
@@ -40,57 +40,62 @@ def read_qr(img):
40
  return data if data else "No QR code found."
41
 
42
 
43
- # Gradio Interface
44
- def create_gradio_interface():
45
- # QR Code Generator with Display and Downloadable Link
46
- def generate_qr_interface(data):
47
- qr_file, qr_image = generate_qr(data)
48
- return qr_image, qr_file # Show image and provide download link
 
 
 
 
 
49
 
50
- # QR Code Reader with Copy-to-Clipboard Button
51
- def read_qr_interface(img):
52
- decoded_data = read_qr(img)
53
- return decoded_data # Return decoded text
54
 
 
 
 
55
  # QR Code Generator Tab
56
  generate_interface = gr.Interface(
57
  fn=generate_qr_interface,
58
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
59
  outputs=[
60
- gr.Image(label="Generated QR Code"), # Display the QR code image
61
- gr.File(label="Download QR Code"), # Downloadable link for the QR code file
62
  ],
63
  title="Generate QR Code",
64
  description="Quickly create a QR code from any text or URL.",
65
  )
66
 
67
  # QR Code Reader Tab
68
- read_interface = gr.Interface(
69
- fn=read_qr_interface,
70
- inputs=gr.Image(type="pil", label="Upload QR Code Image"),
71
- outputs=gr.Textbox(label="Decoded Data"),
72
- title="Read QR Code",
73
- description="Upload an image with a QR code to decode the embedded data.",
74
- )
75
-
76
- # Clipboard Functionality for "Read QR Code" Tab
 
 
 
 
77
  with gr.Blocks() as demo:
78
- gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
79
  with gr.Tab("Generate QR Code"):
80
  generate_interface.render()
81
  with gr.Tab("Read QR Code"):
82
- with gr.Row():
83
- qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
84
- copy_button = gr.Button("Copy to Clipboard")
85
  read_interface.render()
86
- copy_button.click(
87
- lambda text: gr.Textbox.update(value="Copied to Clipboard!"),
88
- qr_text,
89
- qr_text,
90
- )
91
 
 
92
  demo.launch(share=True)
93
 
94
 
95
- # Run the Gradio interface
96
  create_gradio_interface()
 
22
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
23
  img.save(temp_file.name, format="PNG")
24
  temp_file.close() # Ensure file is written to disk
25
+ return temp_file.name, img # Return the file path and the image
26
 
27
 
28
  # Function to read a QR code
 
40
  return data if data else "No QR code found."
41
 
42
 
43
+ # Function for generating QR codes with both image and download link
44
+ def generate_qr_interface(data):
45
+ qr_file, qr_image = generate_qr(data)
46
+ return qr_image, qr_file # Return the image and file for download
47
+
48
+
49
+ # Function for reading QR codes with decoded text and clipboard update
50
+ def read_qr_interface(img):
51
+ decoded_data = read_qr(img)
52
+ return decoded_data # Return the decoded text
53
+
54
 
55
+ # Clipboard copy callback
56
+ def copy_to_clipboard(decoded_text):
57
+ # Gradio does not support direct clipboard copy; simulate success message
58
+ return f"Copied: {decoded_text}" if decoded_text else "Nothing to copy!"
59
 
60
+
61
+ # Create Gradio Interface
62
+ def create_gradio_interface():
63
  # QR Code Generator Tab
64
  generate_interface = gr.Interface(
65
  fn=generate_qr_interface,
66
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
67
  outputs=[
68
+ gr.Image(label="Generated QR Code"), # Display the image
69
+ gr.File(label="Download QR Code"), # Provide a download link
70
  ],
71
  title="Generate QR Code",
72
  description="Quickly create a QR code from any text or URL.",
73
  )
74
 
75
  # QR Code Reader Tab
76
+ with gr.Blocks() as read_interface:
77
+ gr.Markdown("### Read QR Code")
78
+ with gr.Row():
79
+ qr_input = gr.Image(type="pil", label="Upload QR Code Image")
80
+ qr_text = gr.Textbox(label="Decoded Data", interactive=False)
81
+ copy_button = gr.Button("Copy to Clipboard")
82
+ copy_feedback = gr.Textbox(label="Clipboard Status", interactive=False)
83
+
84
+ # Define interactions
85
+ qr_input.change(read_qr_interface, inputs=qr_input, outputs=qr_text)
86
+ copy_button.click(copy_to_clipboard, inputs=qr_text, outputs=copy_feedback)
87
+
88
+ # Main interface with tabs
89
  with gr.Blocks() as demo:
90
+ gr.Markdown("# QR Code Tool: Generate and Decode with Ease")
91
  with gr.Tab("Generate QR Code"):
92
  generate_interface.render()
93
  with gr.Tab("Read QR Code"):
 
 
 
94
  read_interface.render()
 
 
 
 
 
95
 
96
+ # Launch the interface
97
  demo.launch(share=True)
98
 
99
 
100
+ # Run the Gradio app
101
  create_gradio_interface()