Spaces:
Running
Running
import gradio as gr | |
import qrcode | |
import random | |
import os | |
from datetime import datetime | |
from PIL import Image | |
# QR ์ฝ๋ ์์ฑ์ ์ํ ๋ฉ์ธ ํจ์ | |
def create_qr( | |
content, | |
qr_type, | |
fill_color, | |
back_color, | |
box_size, | |
border_size, | |
error_correction | |
): | |
# QR ์ฝ๋ ๋ฐ์ดํฐ ํฌ๋งทํ | |
formatted_data = format_data(content, qr_type) | |
# ์๋ฌ ์์ ๋ ๋ฒจ ์ค์ | |
error_levels = { | |
"Low (7%)": qrcode.constants.ERROR_CORRECT_L, | |
"Medium (15%)": qrcode.constants.ERROR_CORRECT_M, | |
"Quartile (25%)": qrcode.constants.ERROR_CORRECT_Q, | |
"High (30%)": qrcode.constants.ERROR_CORRECT_H | |
} | |
# QR ์ฝ๋ ์์ฑ | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=error_levels[error_correction], | |
box_size=box_size, | |
border=border_size, | |
) | |
qr.add_data(formatted_data) | |
qr.make(fit=True) | |
# QR ์ด๋ฏธ์ง ์์ฑ | |
qr_img = qr.make_image(fill_color=fill_color, back_color=back_color) | |
# ํ์ผ ์ ์ฅ | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
random_id = random.randint(1000, 9999) | |
filename = f"qrfile/qr_{timestamp}_{random_id}.png" | |
# ๋๋ ํ ๋ฆฌ ํ์ธ ๋ฐ ์์ฑ | |
os.makedirs("qrfile", exist_ok=True) | |
# ์ด๋ฏธ์ง ์ ์ฅ | |
qr_img.save(filename) | |
cleanup_old_files("qrfile/", max_files=100) | |
return filename, formatted_data | |
# ๋ฐ์ดํฐ ํฌ๋งทํ ํจ์ | |
def format_data(content, qr_type): | |
if not content: | |
return "" | |
format_rules = { | |
"URL": lambda x: f"https://{x}" if not x.startswith(('http://', 'https://')) else x, | |
"Email": lambda x: f"mailto:{x}", | |
"Phone": lambda x: f"tel:{x}", | |
"SMS": lambda x: f"sms:{x}", | |
"WhatsApp": lambda x: f"whatsapp://send?text={x}", | |
"Location": lambda x: f"geo:{x}", | |
"Wi-Fi": lambda x: f"WIFI:S:{x};;", | |
"Text": lambda x: x, | |
"vCard": lambda x: f"BEGIN:VCARD\nVERSION:3.0\n{x}\nEND:VCARD" | |
} | |
return format_rules[qr_type](content.strip()) | |
# ํ์ผ ์ ๋ฆฌ ํจ์ | |
def cleanup_old_files(directory, max_files): | |
files = [f for f in os.listdir(directory) if f.endswith('.png')] | |
if len(files) > max_files: | |
files.sort(key=lambda x: os.path.getctime(os.path.join(directory, x))) | |
for f in files[:-max_files]: | |
try: | |
os.remove(os.path.join(directory, f)) | |
except: | |
continue | |
def get_example_placeholder(qr_type): | |
examples = { | |
"URL": "example.com or https://example.com", | |
"Email": "[email protected]", | |
"Phone": "+1234567890", | |
"SMS": "+1234567890", | |
"WhatsApp": "Hello World!", | |
"Location": "37.7749,-122.4194", | |
"Wi-Fi": "MyWiFiNetwork", | |
"Text": "Any text you want to encode", | |
"vCard": "FN:John Doe\nTEL:+1234567890\nEMAIL:[email protected]" | |
} | |
return examples.get(qr_type, "Enter your content here...") | |
def format_example_text(qr_type): | |
examples = { | |
"URL": "โข Direct URL: https://example.com\nโข Without https: example.com", | |
"Email": "โข Basic: [email protected]\nโข With subject: [email protected]?subject=Hello", | |
"Phone": "โข International: +1234567890\nโข Local: 01012345678", | |
"SMS": "โข Basic: +1234567890\nโข With message: +1234567890?body=Hello", | |
"WhatsApp": "โข Message: Hello World!\nโข With number: +1234567890:Hello", | |
"Location": "โข Coordinates: 37.7749,-122.4194\nโข With zoom: 37.7749,-122.4194,15z", | |
"Wi-Fi": "โข Network name only: MyWiFiNetwork\nโข With password: WIFI:S:MyNetwork;P:password;;", | |
"Text": "โข Simple text: Hello World!\nโข Multiple lines: Line 1\\nLine 2", | |
"vCard": "โข Basic:\nFN:John Doe\nTEL:+1234567890\nEMAIL:[email protected]\nโข Extended:\nFN:John Doe\nTEL:+1234567890\nEMAIL:[email protected]\nADR:;;123 Street;City;State;12345;Country" | |
} | |
return examples.get(qr_type, "Enter your content here...") | |
# Gradio ์ธํฐํ์ด์ค ์์ฑ | |
def create_interface(): | |
# ํ ๋ง ์ค์ | |
theme = gr.themes.Soft( | |
primary_hue="blue", | |
secondary_hue="indigo", | |
).set( | |
body_background_fill="*neutral_50", | |
block_background_fill="*neutral_100", | |
button_primary_background_fill="*primary_500", | |
) | |
# ์ธํฐํ์ด์ค ๊ตฌ์ฑ | |
with gr.Blocks(theme=theme, title="QR Canvas") as demo: | |
# Inside create_interface() function, fixing the indentation: | |
gr.Markdown( | |
""" | |
# ๐ฏ QR CANVAS | |
Create customized QR codes for various purposes with professional styling options. | |
""" | |
) | |
# Fixed indentation to match the surrounding block | |
gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fginipick-QR-Canvas.hf.space"> | |
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fginipick-QR-Canvas.hf.space&countColor=%23263759" /> | |
</a>""") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
# ์ ๋ ฅ ์น์ | |
qr_type = gr.Dropdown( | |
choices=[ | |
"URL", | |
"Email", | |
"Phone", | |
"SMS", | |
"WhatsApp", | |
"Location", | |
"Wi-Fi", | |
"Text", | |
"vCard" | |
], | |
value="URL", | |
label="QR Code Type" | |
) | |
content = gr.Textbox( | |
label="Content", | |
placeholder="Enter your content here...", | |
lines=3 | |
) | |
example_format = gr.Textbox( | |
value=format_example_text("URL"), | |
label="Format Examples", | |
interactive=False, | |
lines=6 | |
) | |
with gr.Row(): | |
fill_color = gr.ColorPicker( | |
label="QR Code Color", | |
value="#000000" | |
) | |
back_color = gr.ColorPicker( | |
label="Background Color", | |
value="#FFFFFF" | |
) | |
with gr.Row(): | |
box_size = gr.Slider( | |
minimum=1, | |
maximum=20, | |
value=10, | |
step=1, | |
label="QR Code Size" | |
) | |
border_size = gr.Slider( | |
minimum=0, | |
maximum=10, | |
value=4, | |
step=1, | |
label="Border Size" | |
) | |
error_correction = gr.Dropdown( | |
choices=[ | |
"Low (7%)", | |
"Medium (15%)", | |
"Quartile (25%)", | |
"High (30%)" | |
], | |
value="Medium (15%)", | |
label="Error Correction Level" | |
) | |
generate_btn = gr.Button( | |
"Generate QR Code", | |
variant="primary" | |
) | |
with gr.Column(scale=1): | |
# ์ถ๋ ฅ ์น์ | |
output_image = gr.Image( | |
label="Generated QR Code", | |
type="filepath" | |
) | |
output_data = gr.Textbox( | |
label="Formatted Data", | |
interactive=False | |
) | |
def update_example(qr_type): | |
return format_example_text(qr_type) | |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ | |
qr_type.change( | |
fn=update_example, | |
inputs=[qr_type], | |
outputs=example_format | |
) | |
generate_btn.click( | |
fn=create_qr, | |
inputs=[ | |
content, | |
qr_type, | |
fill_color, | |
back_color, | |
box_size, | |
border_size, | |
error_correction | |
], | |
outputs=[output_image, output_data] | |
) | |
# ์ฌ์ฉ ์๋ด | |
gr.Markdown( | |
""" | |
### ๐ Instructions | |
1. Select the QR code type from the dropdown menu | |
2. Enter your content following the format examples shown | |
3. Customize the appearance using the color pickers and sliders | |
4. Click 'Generate QR Code' to create your custom QR code | |
### ๐ก Tips | |
- Use higher error correction levels for better scan reliability | |
- Ensure sufficient contrast between QR code and background colors | |
- Keep the content concise for better readability | |
- Follow the format examples for best results | |
""" | |
) | |
return demo | |
if __name__ == "__main__": | |
try: | |
# ์ถ๋ ฅ ๋๋ ํ ๋ฆฌ ์์ฑ | |
os.makedirs("qrfile", exist_ok=True) | |
# ์ธํฐํ์ด์ค ์์ฑ ๋ฐ ์คํ | |
demo = create_interface() | |
demo.launch( | |
server_name="0.0.0.0", # ๋ชจ๋ IP์์ ์ ๊ทผ ๊ฐ๋ฅ | |
server_port=7860, # ํฌํธ ์ง์ | |
share=True, # ๊ณต์ ๋งํฌ ์์ฑ | |
debug=True # ๋๋ฒ๊ทธ ๋ชจ๋ ํ์ฑํ | |
) | |
except Exception as e: | |
print(f"Error starting the application: {e}") |