Spaces:
Running
Running
File size: 1,813 Bytes
3aa4e37 8b06480 2cad3b3 c54e6c8 352e30c 9f0ef40 c54e6c8 3aa4e37 2cad3b3 6084325 c54e6c8 37bb662 6084325 352e30c 3aa4e37 37bb662 3aa4e37 37bb662 3aa4e37 352e30c 37bb662 9e1cd24 3aa4e37 327c8c5 c54e6c8 37bb662 6084325 3aa4e37 37bb662 3aa4e37 550ae51 056d44f 3aa4e37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import qrcode
from PIL import Image
import gradio as gr
import io
import base64
# Function to generate a QR code and return base64 string
def generate_qr(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill="black", back_color="white")
# Encode the image as a base64 string
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
return f"data:image/png;base64,{img_base64}" # Return the base64 string
# Gradio Interface
def create_gradio_interface():
with gr.Blocks() as demo:
gr.Markdown("## QR Code Generator and Viewer")
# Input to generate QR code
with gr.Tab("Generate QR Code"):
with gr.Row():
data_input = gr.Textbox(placeholder="Enter text or URL to encode", label="Input Data")
generate_button = gr.Button("Generate QR Code")
qr_code_html = gr.HTML(label="Generated QR Code (Base64 Embedded)")
def generate_qr_interface(data):
if not data.strip():
raise ValueError("Input text cannot be empty!")
img_base64 = generate_qr(data)
# Wrap the base64 string in an <img> tag for display
html_content = f'<img src="{img_base64}" alt="QR Code" style="max-width:300px;">'
return html_content
generate_button.click(
generate_qr_interface,
inputs=data_input,
outputs=qr_code_html,
)
demo.launch(share=True)
# Run the Gradio interface
create_gradio_interface()
|