Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,278 +1,2 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import qrcode
|
3 |
-
import random
|
4 |
import os
|
5 |
-
|
6 |
-
from PIL import Image
|
7 |
-
|
8 |
-
# QR μ½λ μμ±μ μν λ©μΈ ν¨μ
|
9 |
-
def create_qr(
|
10 |
-
content,
|
11 |
-
qr_type,
|
12 |
-
fill_color,
|
13 |
-
back_color,
|
14 |
-
box_size,
|
15 |
-
border_size,
|
16 |
-
error_correction
|
17 |
-
):
|
18 |
-
# QR μ½λ λ°μ΄ν° ν¬λ§·ν
|
19 |
-
formatted_data = format_data(content, qr_type)
|
20 |
-
|
21 |
-
# μλ¬ μμ λ 벨 μ€μ
|
22 |
-
error_levels = {
|
23 |
-
"Low (7%)": qrcode.constants.ERROR_CORRECT_L,
|
24 |
-
"Medium (15%)": qrcode.constants.ERROR_CORRECT_M,
|
25 |
-
"Quartile (25%)": qrcode.constants.ERROR_CORRECT_Q,
|
26 |
-
"High (30%)": qrcode.constants.ERROR_CORRECT_H
|
27 |
-
}
|
28 |
-
|
29 |
-
# QR μ½λ μμ±
|
30 |
-
qr = qrcode.QRCode(
|
31 |
-
version=1,
|
32 |
-
error_correction=error_levels[error_correction],
|
33 |
-
box_size=box_size,
|
34 |
-
border=border_size,
|
35 |
-
)
|
36 |
-
|
37 |
-
qr.add_data(formatted_data)
|
38 |
-
qr.make(fit=True)
|
39 |
-
|
40 |
-
# QR μ΄λ―Έμ§ μμ±
|
41 |
-
qr_img = qr.make_image(fill_color=fill_color, back_color=back_color)
|
42 |
-
|
43 |
-
# νμΌ μ μ₯
|
44 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
45 |
-
random_id = random.randint(1000, 9999)
|
46 |
-
filename = f"qrfile/qr_{timestamp}_{random_id}.png"
|
47 |
-
|
48 |
-
# λλ ν 리 νμΈ λ° μμ±
|
49 |
-
os.makedirs("qrfile", exist_ok=True)
|
50 |
-
|
51 |
-
# μ΄λ―Έμ§ μ μ₯
|
52 |
-
qr_img.save(filename)
|
53 |
-
cleanup_old_files("qrfile/", max_files=100)
|
54 |
-
|
55 |
-
return filename, formatted_data
|
56 |
-
|
57 |
-
# λ°μ΄ν° ν¬λ§·ν
ν¨μ
|
58 |
-
def format_data(content, qr_type):
|
59 |
-
if not content:
|
60 |
-
return ""
|
61 |
-
|
62 |
-
format_rules = {
|
63 |
-
"URL": lambda x: f"https://{x}" if not x.startswith(('http://', 'https://')) else x,
|
64 |
-
"Email": lambda x: f"mailto:{x}",
|
65 |
-
"Phone": lambda x: f"tel:{x}",
|
66 |
-
"SMS": lambda x: f"sms:{x}",
|
67 |
-
"WhatsApp": lambda x: f"whatsapp://send?text={x}",
|
68 |
-
"Location": lambda x: f"geo:{x}",
|
69 |
-
"Wi-Fi": lambda x: f"WIFI:S:{x};;",
|
70 |
-
"Text": lambda x: x,
|
71 |
-
"vCard": lambda x: f"BEGIN:VCARD\nVERSION:3.0\n{x}\nEND:VCARD"
|
72 |
-
}
|
73 |
-
|
74 |
-
return format_rules[qr_type](content.strip())
|
75 |
-
|
76 |
-
# νμΌ μ 리 ν¨μ
|
77 |
-
def cleanup_old_files(directory, max_files):
|
78 |
-
files = [f for f in os.listdir(directory) if f.endswith('.png')]
|
79 |
-
if len(files) > max_files:
|
80 |
-
files.sort(key=lambda x: os.path.getctime(os.path.join(directory, x)))
|
81 |
-
for f in files[:-max_files]:
|
82 |
-
try:
|
83 |
-
os.remove(os.path.join(directory, f))
|
84 |
-
except:
|
85 |
-
continue
|
86 |
-
|
87 |
-
def get_example_placeholder(qr_type):
|
88 |
-
examples = {
|
89 |
-
"URL": "example.com or https://example.com",
|
90 |
-
"Email": "example@email.com",
|
91 |
-
"Phone": "+1234567890",
|
92 |
-
"SMS": "+1234567890",
|
93 |
-
"WhatsApp": "Hello World!",
|
94 |
-
"Location": "37.7749,-122.4194",
|
95 |
-
"Wi-Fi": "MyWiFiNetwork",
|
96 |
-
"Text": "Any text you want to encode",
|
97 |
-
"vCard": "FN:John Doe\nTEL:+1234567890\nEMAIL:john@example.com"
|
98 |
-
}
|
99 |
-
return examples.get(qr_type, "Enter your content here...")
|
100 |
-
|
101 |
-
def format_example_text(qr_type):
|
102 |
-
examples = {
|
103 |
-
"URL": "β’ Direct URL: https://example.com\nβ’ Without https: example.com",
|
104 |
-
"Email": "β’ Basic: example@email.com\nβ’ With subject: example@email.com?subject=Hello",
|
105 |
-
"Phone": "β’ International: +1234567890\nβ’ Local: 01012345678",
|
106 |
-
"SMS": "β’ Basic: +1234567890\nβ’ With message: +1234567890?body=Hello",
|
107 |
-
"WhatsApp": "β’ Message: Hello World!\nβ’ With number: +1234567890:Hello",
|
108 |
-
"Location": "β’ Coordinates: 37.7749,-122.4194\nβ’ With zoom: 37.7749,-122.4194,15z",
|
109 |
-
"Wi-Fi": "β’ Network name only: MyWiFiNetwork\nβ’ With password: WIFI:S:MyNetwork;P:password;;",
|
110 |
-
"Text": "β’ Simple text: Hello World!\nβ’ Multiple lines: Line 1\\nLine 2",
|
111 |
-
"vCard": "β’ Basic:\nFN:John Doe\nTEL:+1234567890\nEMAIL:john@example.com\nβ’ Extended:\nFN:John Doe\nTEL:+1234567890\nEMAIL:john@example.com\nADR:;;123 Street;City;State;12345;Country"
|
112 |
-
}
|
113 |
-
return examples.get(qr_type, "Enter your content here...")
|
114 |
-
|
115 |
-
# Gradio μΈν°νμ΄μ€ μμ±
|
116 |
-
def create_interface():
|
117 |
-
# ν
λ§ μ€μ
|
118 |
-
theme = gr.themes.Soft(
|
119 |
-
primary_hue="blue",
|
120 |
-
secondary_hue="indigo",
|
121 |
-
).set(
|
122 |
-
body_background_fill="*neutral_50",
|
123 |
-
block_background_fill="*neutral_100",
|
124 |
-
button_primary_background_fill="*primary_500",
|
125 |
-
)
|
126 |
-
|
127 |
-
# μΈν°νμ΄μ€ ꡬμ±
|
128 |
-
with gr.Blocks(theme=theme, title="QR Canvas") as demo:
|
129 |
-
gr.Markdown(
|
130 |
-
"""
|
131 |
-
# π― Advanced QR Code Generator
|
132 |
-
Create customized QR codes for various purposes with professional styling options.
|
133 |
-
"""
|
134 |
-
)
|
135 |
-
|
136 |
-
with gr.Row():
|
137 |
-
with gr.Column(scale=2):
|
138 |
-
# μ
λ ₯ μΉμ
|
139 |
-
qr_type = gr.Dropdown(
|
140 |
-
choices=[
|
141 |
-
"URL",
|
142 |
-
"Email",
|
143 |
-
"Phone",
|
144 |
-
"SMS",
|
145 |
-
"WhatsApp",
|
146 |
-
"Location",
|
147 |
-
"Wi-Fi",
|
148 |
-
"Text",
|
149 |
-
"vCard"
|
150 |
-
],
|
151 |
-
value="URL",
|
152 |
-
label="QR Code Type"
|
153 |
-
)
|
154 |
-
|
155 |
-
content = gr.Textbox(
|
156 |
-
label="Content",
|
157 |
-
placeholder="Enter your content here...",
|
158 |
-
lines=3
|
159 |
-
)
|
160 |
-
|
161 |
-
example_format = gr.Textbox(
|
162 |
-
value=format_example_text("URL"),
|
163 |
-
label="Format Examples",
|
164 |
-
interactive=False,
|
165 |
-
lines=6
|
166 |
-
)
|
167 |
-
|
168 |
-
with gr.Row():
|
169 |
-
fill_color = gr.ColorPicker(
|
170 |
-
label="QR Code Color",
|
171 |
-
value="#000000"
|
172 |
-
)
|
173 |
-
back_color = gr.ColorPicker(
|
174 |
-
label="Background Color",
|
175 |
-
value="#FFFFFF"
|
176 |
-
)
|
177 |
-
|
178 |
-
with gr.Row():
|
179 |
-
box_size = gr.Slider(
|
180 |
-
minimum=1,
|
181 |
-
maximum=20,
|
182 |
-
value=10,
|
183 |
-
step=1,
|
184 |
-
label="QR Code Size"
|
185 |
-
)
|
186 |
-
border_size = gr.Slider(
|
187 |
-
minimum=0,
|
188 |
-
maximum=10,
|
189 |
-
value=4,
|
190 |
-
step=1,
|
191 |
-
label="Border Size"
|
192 |
-
)
|
193 |
-
|
194 |
-
error_correction = gr.Dropdown(
|
195 |
-
choices=[
|
196 |
-
"Low (7%)",
|
197 |
-
"Medium (15%)",
|
198 |
-
"Quartile (25%)",
|
199 |
-
"High (30%)"
|
200 |
-
],
|
201 |
-
value="Medium (15%)",
|
202 |
-
label="Error Correction Level"
|
203 |
-
)
|
204 |
-
|
205 |
-
generate_btn = gr.Button(
|
206 |
-
"Generate QR Code",
|
207 |
-
variant="primary"
|
208 |
-
)
|
209 |
-
|
210 |
-
with gr.Column(scale=1):
|
211 |
-
# μΆλ ₯ μΉμ
|
212 |
-
output_image = gr.Image(
|
213 |
-
label="Generated QR Code",
|
214 |
-
type="filepath"
|
215 |
-
)
|
216 |
-
output_data = gr.Textbox(
|
217 |
-
label="Formatted Data",
|
218 |
-
interactive=False
|
219 |
-
)
|
220 |
-
|
221 |
-
def update_example(qr_type):
|
222 |
-
return format_example_text(qr_type)
|
223 |
-
|
224 |
-
# μ΄λ²€νΈ νΈλ€λ¬
|
225 |
-
qr_type.change(
|
226 |
-
fn=update_example,
|
227 |
-
inputs=[qr_type],
|
228 |
-
outputs=example_format
|
229 |
-
)
|
230 |
-
|
231 |
-
generate_btn.click(
|
232 |
-
fn=create_qr,
|
233 |
-
inputs=[
|
234 |
-
content,
|
235 |
-
qr_type,
|
236 |
-
fill_color,
|
237 |
-
back_color,
|
238 |
-
box_size,
|
239 |
-
border_size,
|
240 |
-
error_correction
|
241 |
-
],
|
242 |
-
outputs=[output_image, output_data]
|
243 |
-
)
|
244 |
-
|
245 |
-
# μ¬μ© μλ΄
|
246 |
-
gr.Markdown(
|
247 |
-
"""
|
248 |
-
### π Instructions
|
249 |
-
1. Select the QR code type from the dropdown menu
|
250 |
-
2. Enter your content following the format examples shown
|
251 |
-
3. Customize the appearance using the color pickers and sliders
|
252 |
-
4. Click 'Generate QR Code' to create your custom QR code
|
253 |
-
|
254 |
-
### π‘ Tips
|
255 |
-
- Use higher error correction levels for better scan reliability
|
256 |
-
- Ensure sufficient contrast between QR code and background colors
|
257 |
-
- Keep the content concise for better readability
|
258 |
-
- Follow the format examples for best results
|
259 |
-
"""
|
260 |
-
)
|
261 |
-
|
262 |
-
return demo
|
263 |
-
|
264 |
-
if __name__ == "__main__":
|
265 |
-
try:
|
266 |
-
# μΆλ ₯ λλ ν 리 μμ±
|
267 |
-
os.makedirs("qrfile", exist_ok=True)
|
268 |
-
|
269 |
-
# μΈν°νμ΄μ€ μμ± λ° μ€ν
|
270 |
-
demo = create_interface()
|
271 |
-
demo.launch(
|
272 |
-
server_name="0.0.0.0", # λͺ¨λ IPμμ μ κ·Ό κ°λ₯
|
273 |
-
server_port=7860, # ν¬νΈ μ§μ
|
274 |
-
share=True, # 곡μ λ§ν¬ μμ±
|
275 |
-
debug=True # λλ²κ·Έ λͺ¨λ νμ±ν
|
276 |
-
)
|
277 |
-
except Exception as e:
|
278 |
-
print(f"Error starting the application: {e}")
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|