Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import barcode
|
| 3 |
-
from barcode.writer import
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
BARCODE_CLASSES = {
|
| 9 |
"EAN13": barcode.get_barcode_class('ean13'),
|
| 10 |
"Code128": barcode.get_barcode_class('code128'),
|
| 11 |
"UPC": barcode.get_barcode_class('upc'),
|
| 12 |
"ISBN13": barcode.get_barcode_class('isbn13'),
|
| 13 |
}
|
|
|
|
| 14 |
try:
|
| 15 |
bc_class = BARCODE_CLASSES[barcode_type]
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
barcode_img = bc_class(text, writer=ImageWriter())
|
| 22 |
-
# Save to a BytesIO object instead of disk
|
| 23 |
-
from io import BytesIO
|
| 24 |
-
buffer = BytesIO()
|
| 25 |
-
barcode_img.write(buffer)
|
| 26 |
-
buffer.seek(0)
|
| 27 |
-
img = Image.open(buffer)
|
| 28 |
-
return img
|
| 29 |
except Exception as e:
|
| 30 |
-
return None
|
| 31 |
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
gr.Markdown("# Barcode Generator")
|
| 34 |
with gr.Row():
|
| 35 |
text_input = gr.Textbox(label="Text to encode")
|
| 36 |
-
barcode_type = gr.Dropdown(
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
btn = gr.Button("Generate Barcode")
|
| 39 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import barcode
|
| 3 |
+
from barcode.writer import SVGWriter
|
| 4 |
+
from io import BytesIO
|
| 5 |
|
| 6 |
+
# Barcode explanations
|
| 7 |
+
BARCODE_EXPLANATIONS = {
|
| 8 |
+
"EAN13": (
|
| 9 |
+
"EAN-13 (International Article Number) is a 13-digit barcode widely used for retail products globally. "
|
| 10 |
+
"It encodes a GS1 prefix (country or organization), manufacturer code, product code, and a check digit. "
|
| 11 |
+
"EAN-13 is compatible with UPC systems and is essential for international product identification."
|
| 12 |
+
),
|
| 13 |
+
"Code128": (
|
| 14 |
+
"Code 128 is a high-density, variable-length barcode that can encode all ASCII characters (letters, numbers, symbols). "
|
| 15 |
+
"It is commonly used in logistics and shipping due to its ability to store complex data in a compact form and includes a checksum for error detection."
|
| 16 |
+
),
|
| 17 |
+
"UPC": (
|
| 18 |
+
"UPC (Universal Product Code) is a 12-digit barcode used mainly in North America for retail product identification. "
|
| 19 |
+
"It consists of a manufacturer code, item number, and a check digit. UPCs streamline inventory and checkout processes in stores."
|
| 20 |
+
),
|
| 21 |
+
"ISBN13": (
|
| 22 |
+
"ISBN-13 is a 13-digit identifier for books, encoded as an EAN-13 barcode. "
|
| 23 |
+
"It uniquely identifies books internationally and is essential for publishers, libraries, and retailers."
|
| 24 |
+
),
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def generate_barcode_svg(text, barcode_type):
|
| 28 |
BARCODE_CLASSES = {
|
| 29 |
"EAN13": barcode.get_barcode_class('ean13'),
|
| 30 |
"Code128": barcode.get_barcode_class('code128'),
|
| 31 |
"UPC": barcode.get_barcode_class('upc'),
|
| 32 |
"ISBN13": barcode.get_barcode_class('isbn13'),
|
| 33 |
}
|
| 34 |
+
explanation = BARCODE_EXPLANATIONS.get(barcode_type, "No explanation available.")
|
| 35 |
try:
|
| 36 |
bc_class = BARCODE_CLASSES[barcode_type]
|
| 37 |
+
svg_buffer = BytesIO()
|
| 38 |
+
bc = bc_class(text, writer=SVGWriter())
|
| 39 |
+
bc.write(svg_buffer)
|
| 40 |
+
svg_data = svg_buffer.getvalue().decode("utf-8")
|
| 41 |
+
return explanation, svg_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
+
return f"Error: {str(e)}", None
|
| 44 |
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("# Barcode Generator (SVG Output)")
|
| 47 |
with gr.Row():
|
| 48 |
text_input = gr.Textbox(label="Text to encode")
|
| 49 |
+
barcode_type = gr.Dropdown(
|
| 50 |
+
["EAN13", "Code128", "UPC", "ISBN13"],
|
| 51 |
+
value="Code128",
|
| 52 |
+
label="Barcode Type"
|
| 53 |
+
)
|
| 54 |
+
explanation_box = gr.Markdown(label="Barcode Explanation")
|
| 55 |
+
svg_output = gr.HTML(label="Generated Barcode (SVG)")
|
| 56 |
btn = gr.Button("Generate Barcode")
|
| 57 |
+
btn.click(
|
| 58 |
+
generate_barcode_svg,
|
| 59 |
+
inputs=[text_input, barcode_type],
|
| 60 |
+
outputs=[explanation_box, svg_output]
|
| 61 |
+
)
|
| 62 |
|
| 63 |
demo.launch()
|