Amaanali01 commited on
Commit
a9a8eff
·
verified ·
1 Parent(s): f887639

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -3
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import qrcode
3
  from PIL import Image
 
 
4
 
5
  def generate_qr(data):
6
  if not data:
@@ -11,15 +13,36 @@ def generate_qr(data):
11
  qr.make(fit=True)
12
  img = qr.make_image(fill_color="black", back_color="white")
13
 
14
- # Convert from qrcode image (PilImage) to normal PIL.Image
 
 
 
 
 
 
 
 
 
 
 
15
  img = img.convert("RGB")
16
- return img # <-- RETURN PIL IMAGE (✔ Works in Gradio 5)
 
 
 
 
 
 
17
 
18
  with gr.Blocks() as demo:
19
  gr.Markdown("## 📱 QR Code Generator")
20
 
21
  text = gr.Textbox(label="Enter Text / URL / Image URL")
22
- generate = gr.Button("Generate QR Code")
 
 
 
 
23
  output = gr.Image(label="QR Code")
24
 
25
  with gr.Accordion("Guideline", open=False):
@@ -28,8 +51,10 @@ with gr.Blocks() as demo:
28
  1. Enter Text or URL
29
  2. Click **Generate QR Code**
30
  3. Scan the QR code with your mobile
 
31
  """)
32
 
33
  generate.click(fn=generate_qr, inputs=text, outputs=output)
 
34
 
35
  demo.launch()
 
1
  import gradio as gr
2
  import qrcode
3
  from PIL import Image
4
+ import uuid
5
+ import os
6
 
7
  def generate_qr(data):
8
  if not data:
 
13
  qr.make(fit=True)
14
  img = qr.make_image(fill_color="black", back_color="white")
15
 
16
+ img = img.convert("RGB") # Ensure PIL format
17
+ return img
18
+
19
+ def save_qr(data):
20
+ if not data:
21
+ raise gr.Error("Please enter text or URL!")
22
+
23
+ # Generate QR
24
+ qr = qrcode.QRCode(box_size=10, border=4)
25
+ qr.add_data(data)
26
+ qr.make(fit=True)
27
+ img = qr.make_image(fill_color="black", back_color="white")
28
  img = img.convert("RGB")
29
+
30
+ # Save with unique file name
31
+ filename = f"qr_{uuid.uuid4().hex}.png"
32
+ filepath = f"/tmp/{filename}"
33
+ img.save(filepath)
34
+
35
+ return filepath # Gradio automatically downloads this file
36
 
37
  with gr.Blocks() as demo:
38
  gr.Markdown("## 📱 QR Code Generator")
39
 
40
  text = gr.Textbox(label="Enter Text / URL / Image URL")
41
+
42
+ with gr.Row():
43
+ generate = gr.Button("Generate QR Code")
44
+ download = gr.Button("Download QR Code")
45
+
46
  output = gr.Image(label="QR Code")
47
 
48
  with gr.Accordion("Guideline", open=False):
 
51
  1. Enter Text or URL
52
  2. Click **Generate QR Code**
53
  3. Scan the QR code with your mobile
54
+ 4. Or click **Download QR Code** to save it
55
  """)
56
 
57
  generate.click(fn=generate_qr, inputs=text, outputs=output)
58
+ download.click(fn=save_qr, inputs=text, outputs=gr.File(label="Download File"))
59
 
60
  demo.launch()