Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# Launch the app
|
| 70 |
-
interface.launch()
|
| 71 |
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
|
|
|
| 3 |
|
| 4 |
+
# Sample JSON data of dishes
|
| 5 |
+
dishes = [
|
| 6 |
+
{
|
| 7 |
+
"dish_id": 1,
|
| 8 |
+
"name": "Spaghetti Bolognese",
|
| 9 |
+
"description": "Classic Italian pasta with a rich, meaty tomato sauce.",
|
| 10 |
+
"price": 12.99
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"dish_id": 2,
|
| 14 |
+
"name": "Margherita Pizza",
|
| 15 |
+
"description": "Simple and delicious pizza with fresh mozzarella, basil, and tomato sauce.",
|
| 16 |
+
"price": 9.99
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"dish_id": 3,
|
| 20 |
+
"name": "Caesar Salad",
|
| 21 |
+
"description": "Crisp romaine lettuce, parmesan, croutons, and Caesar dressing.",
|
| 22 |
+
"price": 8.99
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"dish_id": 4,
|
| 26 |
+
"name": "Chicken Alfredo",
|
| 27 |
+
"description": "Grilled chicken breast served with creamy Alfredo sauce over fettuccine.",
|
| 28 |
+
"price": 14.99
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"dish_id": 5,
|
| 32 |
+
"name": "Tiramisu",
|
| 33 |
+
"description": "Classic Italian dessert with layers of coffee-soaked ladyfingers and mascarpone cream.",
|
| 34 |
+
"price": 5.99
|
| 35 |
+
}
|
| 36 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Function to generate an invoice based on selected dishes
|
| 39 |
+
def generate_invoice(selected_dishes):
|
| 40 |
+
total_price = sum(dish["price"] for dish in selected_dishes)
|
| 41 |
+
invoice_text = "----- Invoice -----\n"
|
| 42 |
+
for dish in selected_dishes:
|
| 43 |
+
invoice_text += f"{dish['name']}: ${dish['price']}\n"
|
| 44 |
+
invoice_text += f"Total: ${total_price:.2f}\n"
|
| 45 |
+
invoice_text += "\nThank you for dining with us! Have a great day!"
|
| 46 |
+
return invoice_text
|
| 47 |
|
| 48 |
# Gradio Interface
|
| 49 |
+
def invoice_interface(selected_dishes_ids):
|
| 50 |
+
selected_dishes = [dishes[i] for i in selected_dishes_ids]
|
| 51 |
+
return generate_invoice(selected_dishes)
|
| 52 |
|
| 53 |
+
# Create Gradio interface
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("### Select Dishes for Your Invoice")
|
| 56 |
+
|
| 57 |
+
dish_choices = [gr.Checkbox(label=dish['name'], value=False) for dish in dishes]
|
| 58 |
+
|
| 59 |
+
btn = gr.Button("Generate Invoice")
|
| 60 |
+
|
| 61 |
+
output = gr.Textbox(label="Invoice")
|
| 62 |
+
|
| 63 |
+
btn.click(invoice_interface, inputs=dish_choices, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
demo.launch()
|