namelessai commited on
Commit
001415b
·
verified ·
1 Parent(s): bbf138c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import barcode
3
+ from barcode.writer import ImageWriter
4
+ from PIL import Image
5
+
6
+ def generate_barcode(text, barcode_type):
7
+ # Select the barcode class based on user choice
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
+ except KeyError:
17
+ return None
18
+
19
+ # Generate barcode and save to image in memory
20
+ try:
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(["EAN13", "Code128", "UPC", "ISBN13"], value="Code128", label="Barcode Type")
37
+ output = gr.Image(type="pil", label="Generated Barcode")
38
+ btn = gr.Button("Generate Barcode")
39
+ btn.click(generate_barcode, inputs=[text_input, barcode_type], outputs=output)
40
+
41
+ demo.launch()