Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import qrcode
|
| 2 |
+
from pyzbar.pyzbar import decode
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Function to generate a QR code
|
| 9 |
+
def generate_qr(data):
|
| 10 |
+
qr = qrcode.QRCode(
|
| 11 |
+
version=1,
|
| 12 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 13 |
+
box_size=10,
|
| 14 |
+
border=4,
|
| 15 |
+
)
|
| 16 |
+
qr.add_data(data)
|
| 17 |
+
qr.make(fit=True)
|
| 18 |
+
img = qr.make_image(fill="black", back_color="white")
|
| 19 |
+
|
| 20 |
+
# Save to a temporary file and return the file path
|
| 21 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 22 |
+
img.save(temp_file.name, format="PNG")
|
| 23 |
+
temp_file.close() # Close the file to flush contents to disk
|
| 24 |
+
return temp_file.name
|
| 25 |
+
|
| 26 |
+
# Function to read a QR code from an uploaded image
|
| 27 |
+
def read_qr(img):
|
| 28 |
+
decoded_data = decode(img)
|
| 29 |
+
if decoded_data:
|
| 30 |
+
return decoded_data[0].data.decode("utf-8")
|
| 31 |
+
else:
|
| 32 |
+
return "No QR code found."
|
| 33 |
+
|
| 34 |
+
# Custom CSS styling as HTML for dark mode
|
| 35 |
+
custom_css = """
|
| 36 |
+
<style>
|
| 37 |
+
body {background-color: #1e1e2f; font-family: Arial, sans-serif; color: #e0e0e0;}
|
| 38 |
+
.gradio-container {max-width: 600px; margin: auto; padding: 20px; background-color: #2c2c3e; border-radius: 10px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);}
|
| 39 |
+
h1, h2 {text-align: center; color: #f0f0f0; font-weight: 600;}
|
| 40 |
+
.gr-button {background-color: #3a3a4f; color: #e0e0e0; padding: 10px 20px; border-radius: 5px; font-size: 15px; border: none;}
|
| 41 |
+
.gr-button:hover {background-color: #505068;}
|
| 42 |
+
input, textarea, .gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 5px; padding: 10px; font-size: 14px; color: #e0e0e0;}
|
| 43 |
+
.gr-box:hover, input:hover, textarea:hover {border-color: #777;}
|
| 44 |
+
</style>
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
# Gradio interface for generating and reading QR codes
|
| 48 |
+
def create_gradio_interface():
|
| 49 |
+
# QR Code Generator Interface
|
| 50 |
+
generate_interface = gr.Interface(
|
| 51 |
+
fn=generate_qr,
|
| 52 |
+
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
| 53 |
+
outputs=gr.Image(label="Generated QR Code"),
|
| 54 |
+
title="Generate QR Code",
|
| 55 |
+
description="Quickly create a QR code from any text or URL.",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# QR Code Reader Interface
|
| 59 |
+
read_interface = gr.Interface(
|
| 60 |
+
fn=read_qr,
|
| 61 |
+
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
| 62 |
+
outputs=gr.Textbox(label="Decoded Data"),
|
| 63 |
+
title="Read QR Code",
|
| 64 |
+
description="Upload an image with a QR code to decode the embedded data.",
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Combine interfaces into a single tabbed layout
|
| 68 |
+
interface = gr.TabbedInterface(
|
| 69 |
+
[generate_interface, read_interface],
|
| 70 |
+
["Generate QR Code", "Read QR Code"]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Launch interface with custom HTML for CSS styling
|
| 74 |
+
with gr.Blocks() as demo:
|
| 75 |
+
gr.HTML(custom_css) # Embed the custom CSS
|
| 76 |
+
interface.render()
|
| 77 |
+
|
| 78 |
+
demo.launch(share=True)
|
| 79 |
+
|
| 80 |
+
# Run the Gradio interface
|
| 81 |
+
create_gradio_interface()
|