Spaces:
Running
Running
Update app.py
Browse files
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() # Close the file to flush contents to disk
|
| 25 |
-
return temp_file.name
|
| 26 |
|
| 27 |
# Function to read a QR code using OpenCV
|
| 28 |
def read_qr(img):
|
|
@@ -56,23 +56,49 @@ custom_css = """
|
|
| 56 |
</style>
|
| 57 |
"""
|
| 58 |
|
| 59 |
-
#
|
| 60 |
def create_gradio_interface():
|
| 61 |
# QR Code Generator Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
generate_interface = gr.Interface(
|
| 63 |
-
fn=
|
| 64 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
| 65 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 66 |
title="Generate QR Code",
|
| 67 |
description="Quickly create a QR code from any text or URL.",
|
| 68 |
theme="compact",
|
| 69 |
)
|
| 70 |
|
| 71 |
# QR Code Reader Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
read_interface = gr.Interface(
|
| 73 |
-
fn=
|
| 74 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
| 75 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 76 |
title="Read QR Code",
|
| 77 |
description="Upload an image with a QR code to decode the embedded data.",
|
| 78 |
theme="compact",
|
|
|
|
| 22 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 23 |
img.save(temp_file.name, format="PNG")
|
| 24 |
temp_file.close() # Close the file to flush contents to disk
|
| 25 |
+
return temp_file.name # Return the file path
|
| 26 |
|
| 27 |
# Function to read a QR code using OpenCV
|
| 28 |
def read_qr(img):
|
|
|
|
| 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",
|