Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +101 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import qrcode
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# Streamlit app
|
| 8 |
+
st.title("QR Code Generator")
|
| 9 |
+
st.subheader("Generate a QR Code for Text, URLs, or Uploaded Files")
|
| 10 |
+
|
| 11 |
+
# Choice: Text/URL or File Upload
|
| 12 |
+
option = st.radio("Choose an option:", ["Enter Text/URL", "Upload a File"])
|
| 13 |
+
|
| 14 |
+
if option == "Enter Text/URL":
|
| 15 |
+
user_input = st.text_input("Enter the text or URL for which you want to generate a QR Code:")
|
| 16 |
+
|
| 17 |
+
if st.button("Generate QR Code"):
|
| 18 |
+
if user_input:
|
| 19 |
+
# Generate QR code
|
| 20 |
+
qr = qrcode.QRCode(
|
| 21 |
+
version=1,
|
| 22 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 23 |
+
box_size=10,
|
| 24 |
+
border=4,
|
| 25 |
+
)
|
| 26 |
+
qr.add_data(user_input)
|
| 27 |
+
qr.make(fit=True)
|
| 28 |
+
|
| 29 |
+
# Create an image from the QR Code instance
|
| 30 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
| 31 |
+
|
| 32 |
+
# Convert the image to BytesIO for display and download
|
| 33 |
+
buffer = BytesIO()
|
| 34 |
+
img.save(buffer, format="PNG")
|
| 35 |
+
buffer.seek(0)
|
| 36 |
+
|
| 37 |
+
# Display the QR code
|
| 38 |
+
st.image(buffer, caption="Your QR Code", use_column_width=True)
|
| 39 |
+
|
| 40 |
+
# Provide a download button
|
| 41 |
+
st.download_button(
|
| 42 |
+
label="Download QR Code",
|
| 43 |
+
data=buffer,
|
| 44 |
+
file_name="qrcode.png",
|
| 45 |
+
mime="image/png"
|
| 46 |
+
)
|
| 47 |
+
else:
|
| 48 |
+
st.error("Please enter some text or URL to generate the QR Code.")
|
| 49 |
+
|
| 50 |
+
elif option == "Upload a File":
|
| 51 |
+
uploaded_file = st.file_uploader("Upload a file to generate a QR Code link:", type=None)
|
| 52 |
+
|
| 53 |
+
if uploaded_file:
|
| 54 |
+
if st.button("Upload and Generate QR Code"):
|
| 55 |
+
# Upload file to File.io
|
| 56 |
+
response = requests.post(
|
| 57 |
+
"https://file.io/",
|
| 58 |
+
files={"file": uploaded_file}
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if response.status_code == 200:
|
| 62 |
+
file_url = response.json().get("link", None)
|
| 63 |
+
|
| 64 |
+
if file_url:
|
| 65 |
+
st.success("File uploaded successfully!")
|
| 66 |
+
|
| 67 |
+
# Generate QR code for the file link
|
| 68 |
+
qr = qrcode.QRCode(
|
| 69 |
+
version=1,
|
| 70 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 71 |
+
box_size=10,
|
| 72 |
+
border=4,
|
| 73 |
+
)
|
| 74 |
+
qr.add_data(file_url)
|
| 75 |
+
qr.make(fit=True)
|
| 76 |
+
|
| 77 |
+
# Create an image from the QR Code instance
|
| 78 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
| 79 |
+
|
| 80 |
+
# Convert the image to BytesIO for display and download
|
| 81 |
+
buffer = BytesIO()
|
| 82 |
+
img.save(buffer, format="PNG")
|
| 83 |
+
buffer.seek(0)
|
| 84 |
+
|
| 85 |
+
# Display the QR code
|
| 86 |
+
st.image(buffer, caption="Your QR Code", use_column_width=True)
|
| 87 |
+
|
| 88 |
+
# Provide a download button for the QR code
|
| 89 |
+
st.download_button(
|
| 90 |
+
label="Download QR Code",
|
| 91 |
+
data=buffer,
|
| 92 |
+
file_name="qrcode.png",
|
| 93 |
+
mime="image/png"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Display the file link
|
| 97 |
+
st.markdown(f"[Click here to download your uploaded file]({file_url})")
|
| 98 |
+
else:
|
| 99 |
+
st.error("Failed to retrieve file link from File.io.")
|
| 100 |
+
else:
|
| 101 |
+
st.error("File upload failed. Please try again.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
qrcode[pil]
|
| 3 |
+
Pillow
|