Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import qrcode | |
| from io import BytesIO | |
| from PIL import Image | |
| import requests | |
| # Streamlit app | |
| st.title("QR Code Generator") | |
| st.subheader("Generate a QR Code for Text, URLs, or Uploaded Files") | |
| # Choice: Text/URL or File Upload | |
| option = st.radio("Choose an option:", ["Enter Text/URL", "Upload a File"]) | |
| if option == "Enter Text/URL": | |
| user_input = st.text_input("Enter the text or URL for which you want to generate a QR Code:") | |
| if st.button("Generate QR Code"): | |
| if user_input: | |
| # Generate QR code | |
| qr = qrcode.QRCode( | |
| version=1, | |
| error_correction=qrcode.constants.ERROR_CORRECT_L, | |
| box_size=10, | |
| border=4, | |
| ) | |
| qr.add_data(user_input) | |
| qr.make(fit=True) | |
| # Create an image from the QR Code instance | |
| img = qr.make_image(fill_color="black", back_color="white") | |
| # Convert the image to BytesIO for display and download | |
| buffer = BytesIO() | |
| img.save(buffer, format="PNG") | |
| buffer.seek(0) | |
| # Display the QR code | |
| st.image(buffer, caption="Your QR Code", use_column_width=True) | |
| # Provide a download button | |
| st.download_button( | |
| label="Download QR Code", | |
| data=buffer, | |
| file_name="qrcode.png", | |
| mime="image/png" | |
| ) | |
| else: | |
| st.error("Please enter some text or URL to generate the QR Code.") | |
| elif option == "Upload a File": | |
| uploaded_file = st.file_uploader("Upload a file to generate a QR Code link:", type=None) | |
| if uploaded_file: | |
| if st.button("Upload and Generate QR Code"): | |
| # Upload file to File.io | |
| response = requests.post( | |
| "https://file.io/", | |
| files={"file": uploaded_file} | |
| ) | |
| if response.status_code == 200: | |
| file_url = response.json().get("link", None) | |
| if file_url: | |
| st.success("File uploaded successfully!") | |
| # Generate QR code for the file link | |
| qr = qrcode.QRCode( | |
| version=1, | |
| error_correction=qrcode.constants.ERROR_CORRECT_L, | |
| box_size=10, | |
| border=4, | |
| ) | |
| qr.add_data(file_url) | |
| qr.make(fit=True) | |
| # Create an image from the QR Code instance | |
| img = qr.make_image(fill_color="black", back_color="white") | |
| # Convert the image to BytesIO for display and download | |
| buffer = BytesIO() | |
| img.save(buffer, format="PNG") | |
| buffer.seek(0) | |
| # Display the QR code | |
| st.image(buffer, caption="Your QR Code", use_column_width=True) | |
| # Provide a download button for the QR code | |
| st.download_button( | |
| label="Download QR Code", | |
| data=buffer, | |
| file_name="qrcode.png", | |
| mime="image/png" | |
| ) | |
| # Display the file link | |
| st.markdown(f"[Click here to download your uploaded file]({file_url})") | |
| else: | |
| st.error("Failed to retrieve file link from File.io.") | |
| else: | |
| st.error("File upload failed. Please try again.") | |