Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,71 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
# Function to generate a
|
| 4 |
-
def generate_invoice(customer_name, items,
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
Restaurant
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
inputs=[
|
| 33 |
gr.Textbox(label="Customer Name"),
|
| 34 |
-
gr.
|
| 35 |
-
gr.
|
| 36 |
-
],
|
| 37 |
-
outputs="
|
| 38 |
-
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
# Launch the app
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fpdf import FPDF
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
|
| 5 |
+
# Function to generate a themed invoice
|
| 6 |
+
def generate_invoice(customer_name, items, logo_path):
|
| 7 |
+
# Create a PDF document
|
| 8 |
+
pdf = FPDF()
|
| 9 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 10 |
+
pdf.add_page()
|
| 11 |
+
|
| 12 |
+
# Title and Basic Styling
|
| 13 |
+
pdf.set_font("Arial", 'B', 16)
|
| 14 |
+
pdf.cell(200, 10, txt="Restaurant Invoice", ln=True, align='C')
|
| 15 |
+
|
| 16 |
+
# Add logo
|
| 17 |
+
if logo_path:
|
| 18 |
+
pdf.image(logo_path, x=10, y=20, w=30) # Adjust the size and position of the logo
|
| 19 |
+
|
| 20 |
+
# Customer Details
|
| 21 |
+
pdf.ln(30) # Add space after title
|
| 22 |
+
pdf.set_font("Arial", size=12)
|
| 23 |
+
pdf.cell(100, 10, f"Customer: {customer_name}", ln=True)
|
| 24 |
+
|
| 25 |
+
# Itemized list
|
| 26 |
+
pdf.cell(100, 10, "Item", ln=True, border=1, align="C")
|
| 27 |
+
pdf.cell(30, 10, "Price", ln=True, border=1, align="C")
|
| 28 |
+
total_price = 0
|
| 29 |
+
|
| 30 |
+
for item, price in items.items():
|
| 31 |
+
pdf.cell(100, 10, item, ln=True, border=1)
|
| 32 |
+
pdf.cell(30, 10, f"${price:.2f}", ln=True, border=1, align="R")
|
| 33 |
+
total_price += price
|
| 34 |
+
|
| 35 |
+
# Add Total
|
| 36 |
+
pdf.ln(10)
|
| 37 |
+
pdf.cell(100, 10, "Total", ln=True, align="C", border=1)
|
| 38 |
+
pdf.cell(30, 10, f"${total_price:.2f}", ln=True, border=1, align="R")
|
| 39 |
+
|
| 40 |
+
# Wish Message
|
| 41 |
+
pdf.ln(20)
|
| 42 |
+
pdf.set_font("Arial", 'I', 12)
|
| 43 |
+
pdf.multi_cell(0, 10, "Thank you for choosing us! We hope to serve you again soon.")
|
| 44 |
|
| 45 |
+
# Save PDF to a file
|
| 46 |
+
pdf_output = "/tmp/invoice.pdf"
|
| 47 |
+
pdf.output(pdf_output)
|
| 48 |
+
return pdf_output
|
| 49 |
+
|
| 50 |
|
| 51 |
+
# Gradio Interface
|
| 52 |
+
def invoice_interface(customer_name, items, logo_path):
|
| 53 |
+
return generate_invoice(customer_name, items, logo_path)
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# Example Gradio Interface
|
| 57 |
+
interface = gr.Interface(
|
| 58 |
+
fn=invoice_interface,
|
| 59 |
inputs=[
|
| 60 |
gr.Textbox(label="Customer Name"),
|
| 61 |
+
gr.JSON(label="Items (JSON format like {'item1': 10.5, 'item2': 7.25})"),
|
| 62 |
+
gr.Image(label="Upload Logo", type="filepath")
|
| 63 |
+
],
|
| 64 |
+
outputs=gr.File(label="Download Invoice"),
|
| 65 |
+
title="Customized Restaurant Invoice Generator",
|
| 66 |
+
description="Generate an invoice with your restaurant's custom theme, logo, and message!"
|
| 67 |
)
|
| 68 |
|
| 69 |
# Launch the app
|
| 70 |
+
interface.launch()
|
|
|
|
| 71 |
|