Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +78 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import qrcode
|
| 3 |
+
from qrcode.image.pil import PilImage
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Function to convert image to base64
|
| 11 |
+
def get_image_as_base64(image: Image):
|
| 12 |
+
buffer = io.BytesIO()
|
| 13 |
+
image.save(buffer, format="PNG")
|
| 14 |
+
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
| 15 |
+
return image_base64
|
| 16 |
+
|
| 17 |
+
def get_url_filename(url):
|
| 18 |
+
parsed_uri = urlparse(url)
|
| 19 |
+
domain = '{uri.netloc}'.format(uri=parsed_uri)
|
| 20 |
+
main_domain = domain.split('.')
|
| 21 |
+
main_domain = main_domain[1] if main_domain[0] == 'www' else main_domain[0]
|
| 22 |
+
path = parsed_uri.path.strip('/').replace('/', '_')
|
| 23 |
+
return f"{main_domain}_{path}" if path else main_domain
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Streamlit app title
|
| 29 |
+
st.title("QR Code Generator")
|
| 30 |
+
|
| 31 |
+
# QR code content options
|
| 32 |
+
qr_content_options = ["URL", "Text"]
|
| 33 |
+
# qr_content_options = ["URL", "Text", "Contact Information"]
|
| 34 |
+
qr_content_type = st.selectbox("Select QR content type", qr_content_options)
|
| 35 |
+
|
| 36 |
+
if qr_content_type == "Contact Information":
|
| 37 |
+
first_name = st.text_input("First Name")
|
| 38 |
+
last_name = st.text_input("Last Name")
|
| 39 |
+
phone = st.text_input("Phone Number")
|
| 40 |
+
email = st.text_input("Email Address")
|
| 41 |
+
content = f"BEGIN:VCARD\nVERSION:3.0\nN:{last_name};{first_name}\nFN:{first_name} {last_name}\nTEL;TYPE=CELL:{phone}\nEMAIL:{email}\nEND:VCARD"
|
| 42 |
+
else:
|
| 43 |
+
content = st.text_area("Enter your content (one per line for multiple QR codes)", height=150)
|
| 44 |
+
|
| 45 |
+
if st.button("Generate QR Code"):
|
| 46 |
+
if content:
|
| 47 |
+
contents = content.split("\n")
|
| 48 |
+
|
| 49 |
+
for i, c in enumerate(contents):
|
| 50 |
+
if c.strip():
|
| 51 |
+
# Generate QR code
|
| 52 |
+
qr = qrcode.QRCode(
|
| 53 |
+
version=1,
|
| 54 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
| 55 |
+
box_size=10,
|
| 56 |
+
border=4
|
| 57 |
+
)
|
| 58 |
+
qr.add_data(c)
|
| 59 |
+
qr.make(fit=True)
|
| 60 |
+
|
| 61 |
+
img = qr.make_image(fill_color="black", back_color="white", image_factory=PilImage)
|
| 62 |
+
|
| 63 |
+
# Convert PilImage to bytes-like object
|
| 64 |
+
buffer = io.BytesIO()
|
| 65 |
+
img.save(buffer, format="PNG")
|
| 66 |
+
img_bytes = buffer.getvalue()
|
| 67 |
+
|
| 68 |
+
img_base64 = get_image_as_base64(img)
|
| 69 |
+
|
| 70 |
+
st.markdown(f"##### {c}")
|
| 71 |
+
st.image(img_bytes, caption=f"QR code for {c}", use_column_width=True)
|
| 72 |
+
file_name = get_url_filename(c) if qr_content_type == "URL" else f"QR_{i}"
|
| 73 |
+
st.markdown(f'<a href="data:image/png;base64,{img_base64}" download="{file_name}.png" style="display:inline-block;background-color:#4CAF50;border:none;color:white;padding:8px 16px;text-align:center;text-decoration:none;font-size:16px;margin:4px 2px;cursor:pointer;">Download QR code</a>', unsafe_allow_html=True)
|
| 74 |
+
else:
|
| 75 |
+
st.error("Please enter content for the QR code.")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
streamlit
|
| 3 |
+
qrcode
|
| 4 |
+
pillow
|