CultriX commited on
Commit
327c8c5
·
verified ·
1 Parent(s): 3aa4e37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
4
  import gradio as gr
5
  import tempfile
6
  import numpy as np
7
- import shutil # For cleaning up temporary files
8
 
9
 
10
  # Function to generate a QR code
@@ -19,11 +19,11 @@ def generate_qr(data):
19
  qr.make(fit=True)
20
  img = qr.make_image(fill="black", back_color="white")
21
 
22
- # Save to a temporary file
23
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
24
  img.save(temp_file.name, format="PNG")
25
- temp_file.close() # Ensure file is written to disk
26
- return temp_file.name, img # Return the file path and the PIL image
27
 
28
 
29
  # Function to read a QR code
@@ -46,6 +46,7 @@ def create_gradio_interface():
46
  with gr.Blocks() as demo:
47
  gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
48
 
 
49
  with gr.Tab("Generate QR Code"):
50
  with gr.Row():
51
  data_input = gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode")
@@ -56,7 +57,10 @@ def create_gradio_interface():
56
  qr_file = gr.File(label="Download QR Code")
57
 
58
  def generate_qr_interface(data):
59
- qr_file_path, qr_image_pil = generate_qr(data)
 
 
 
60
  return qr_image_pil, qr_file_path
61
 
62
  generate_button.click(
@@ -65,6 +69,7 @@ def create_gradio_interface():
65
  outputs=[qr_image, qr_file],
66
  )
67
 
 
68
  with gr.Tab("Read QR Code"):
69
  with gr.Row():
70
  image_input = gr.Image(type="pil", label="Upload QR Code Image")
@@ -73,6 +78,8 @@ def create_gradio_interface():
73
  decoded_data = gr.Textbox(label="Decoded Data")
74
 
75
  def read_qr_interface(img):
 
 
76
  return read_qr(img)
77
 
78
  decode_button.click(
 
4
  import gradio as gr
5
  import tempfile
6
  import numpy as np
7
+ import os
8
 
9
 
10
  # Function to generate a QR code
 
19
  qr.make(fit=True)
20
  img = qr.make_image(fill="black", back_color="white")
21
 
22
+ # Save QR code image to a temporary file
23
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
24
  img.save(temp_file.name, format="PNG")
25
+ temp_file.close() # Ensure the file is saved
26
+ return temp_file.name # Return the file path
27
 
28
 
29
  # Function to read a QR code
 
46
  with gr.Blocks() as demo:
47
  gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
48
 
49
+ # Tab for generating QR codes
50
  with gr.Tab("Generate QR Code"):
51
  with gr.Row():
52
  data_input = gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode")
 
57
  qr_file = gr.File(label="Download QR Code")
58
 
59
  def generate_qr_interface(data):
60
+ if not data.strip():
61
+ raise ValueError("Input text cannot be empty!")
62
+ qr_file_path = generate_qr(data)
63
+ qr_image_pil = Image.open(qr_file_path)
64
  return qr_image_pil, qr_file_path
65
 
66
  generate_button.click(
 
69
  outputs=[qr_image, qr_file],
70
  )
71
 
72
+ # Tab for reading QR codes
73
  with gr.Tab("Read QR Code"):
74
  with gr.Row():
75
  image_input = gr.Image(type="pil", label="Upload QR Code Image")
 
78
  decoded_data = gr.Textbox(label="Decoded Data")
79
 
80
  def read_qr_interface(img):
81
+ if img is None:
82
+ raise ValueError("Please upload a valid QR code image!")
83
  return read_qr(img)
84
 
85
  decode_button.click(