Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
import base64
|
4 |
import gradio as gr
|
5 |
|
@@ -9,49 +9,35 @@ def encode_file_to_base64(file_path):
|
|
9 |
encoded_data = base64.b64encode(f.read()).decode("utf-8")
|
10 |
return encoded_data
|
11 |
|
12 |
-
def
|
13 |
-
"""Generates a
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
error_correction=error_correction,
|
19 |
-
box_size=box_size,
|
20 |
-
border=border,
|
21 |
-
)
|
22 |
-
qr.add_data(final_url)
|
23 |
-
qr.make(fit=True)
|
24 |
-
img_qr = qr.make_image(fill_color=fill_color, back_color=back_color)
|
25 |
-
|
26 |
-
output_path = "qr_code.png"
|
27 |
-
img_qr.save(output_path)
|
28 |
return output_path
|
29 |
|
30 |
-
def
|
31 |
if input_type == "Text" and text:
|
32 |
encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8')
|
33 |
-
return
|
34 |
elif input_type == "File" and file:
|
35 |
encoded_file = encode_file_to_base64(file.name)
|
36 |
-
return
|
37 |
else:
|
38 |
return None
|
39 |
|
40 |
interface = gr.Interface(
|
41 |
-
fn=
|
42 |
inputs=[
|
43 |
gr.Radio(["Text", "File"], label="Input Type", value="Text"),
|
44 |
gr.Textbox(label="Enter Text"),
|
45 |
gr.File(label="Upload File"),
|
46 |
-
gr.
|
47 |
-
gr.Slider(1, 20, 1, label="Box Size", value=10),
|
48 |
-
gr.Slider(1, 10, 1, label="Border Size", value=4),
|
49 |
-
gr.ColorPicker(label="QR Fill Color", value="#000000"),
|
50 |
-
gr.ColorPicker(label="QR Background Color", value="#FFFFFF"),
|
51 |
],
|
52 |
-
outputs=gr.Image(type="filepath", label="Generated
|
53 |
-
description="Generate a
|
54 |
css="footer {visibility: hidden}"
|
55 |
)
|
56 |
|
57 |
-
interface.launch(share=True)
|
|
|
1 |
+
import barcode
|
2 |
+
from barcode.writer import ImageWriter
|
3 |
import base64
|
4 |
import gradio as gr
|
5 |
|
|
|
9 |
encoded_data = base64.b64encode(f.read()).decode("utf-8")
|
10 |
return encoded_data
|
11 |
|
12 |
+
def generate_barcode(data: str, barcode_type: str = "code128"):
|
13 |
+
"""Generates a barcode from given data."""
|
14 |
+
barcode_class = barcode.get_barcode_class(barcode_type)
|
15 |
+
barcode_instance = barcode_class(data, writer=ImageWriter())
|
16 |
+
output_path = "barcode.png"
|
17 |
+
barcode_instance.save(output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return output_path
|
19 |
|
20 |
+
def barcode_interface(input_type, text, file, barcode_type):
|
21 |
if input_type == "Text" and text:
|
22 |
encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8')
|
23 |
+
return generate_barcode(encoded_text, barcode_type)
|
24 |
elif input_type == "File" and file:
|
25 |
encoded_file = encode_file_to_base64(file.name)
|
26 |
+
return generate_barcode(encoded_file, barcode_type)
|
27 |
else:
|
28 |
return None
|
29 |
|
30 |
interface = gr.Interface(
|
31 |
+
fn=barcode_interface,
|
32 |
inputs=[
|
33 |
gr.Radio(["Text", "File"], label="Input Type", value="Text"),
|
34 |
gr.Textbox(label="Enter Text"),
|
35 |
gr.File(label="Upload File"),
|
36 |
+
gr.Dropdown(["code128", "ean13", "ean8"], label="Barcode Type", value="code128"),
|
|
|
|
|
|
|
|
|
37 |
],
|
38 |
+
outputs=gr.Image(type="filepath", label="Generated Barcode"),
|
39 |
+
description="Generate a Barcode from either text or any file.",
|
40 |
css="footer {visibility: hidden}"
|
41 |
)
|
42 |
|
43 |
+
interface.launch(share=True)
|