Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# -----------------------------
|
| 4 |
+
# Module 1: Data Structures
|
| 5 |
+
# -----------------------------
|
| 6 |
+
|
| 7 |
+
Inventory = [
|
| 8 |
+
(101, "Hammer", 120),
|
| 9 |
+
(102, "Screwdriver Set", 350),
|
| 10 |
+
(103, "Drill Machine", 2500),
|
| 11 |
+
(104, "Pliers", 180),
|
| 12 |
+
(105, "Wrench", 220),
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
# Convert to a lookup dictionary for fast access
|
| 16 |
+
inventory_lookup = {item[0]: {"name": item[1], "price": item[2]} for item in Inventory}
|
| 17 |
+
|
| 18 |
+
# Cart is a list of item IDs
|
| 19 |
+
My_Cart = []
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# -----------------------------
|
| 23 |
+
# Module 2: Business Logic
|
| 24 |
+
# -----------------------------
|
| 25 |
+
|
| 26 |
+
def add_to_cart(item_id):
|
| 27 |
+
if item_id in inventory_lookup:
|
| 28 |
+
My_Cart.append(item_id)
|
| 29 |
+
return f"Added: {inventory_lookup[item_id]['name']}"
|
| 30 |
+
return "Invalid item ID."
|
| 31 |
+
|
| 32 |
+
def remove_from_cart(item_id):
|
| 33 |
+
if item_id in My_Cart:
|
| 34 |
+
My_Cart.remove(item_id)
|
| 35 |
+
return f"Removed: {inventory_lookup[item_id]['name']}"
|
| 36 |
+
return "Item not in cart."
|
| 37 |
+
|
| 38 |
+
def get_cart_contents():
|
| 39 |
+
if not My_Cart:
|
| 40 |
+
return "Cart is empty."
|
| 41 |
+
lines = []
|
| 42 |
+
for item_id in My_Cart:
|
| 43 |
+
item = inventory_lookup[item_id]
|
| 44 |
+
lines.append(f"{item_id} - {item['name']} - ₹{item['price']}")
|
| 45 |
+
return "\n".join(lines)
|
| 46 |
+
|
| 47 |
+
def calculate_total():
|
| 48 |
+
total = sum(inventory_lookup[item_id]["price"] for item_id in My_Cart)
|
| 49 |
+
return f"Total Amount: ₹{total}"
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# Module 3: Gradio Callbacks
|
| 54 |
+
# -----------------------------
|
| 55 |
+
|
| 56 |
+
def handle_add(selected_id):
|
| 57 |
+
msg = add_to_cart(selected_id)
|
| 58 |
+
return get_cart_contents(), msg
|
| 59 |
+
|
| 60 |
+
def handle_remove(selected_id):
|
| 61 |
+
msg = remove_from_cart(selected_id)
|
| 62 |
+
return get_cart_contents(), msg
|
| 63 |
+
|
| 64 |
+
def handle_total():
|
| 65 |
+
return calculate_total()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# -----------------------------
|
| 69 |
+
# Module 4: Gradio Interface
|
| 70 |
+
# -----------------------------
|
| 71 |
+
|
| 72 |
+
with gr.Blocks(title="Hardware Store Billing System") as demo:
|
| 73 |
+
|
| 74 |
+
gr.Markdown("## 🧰 Hardware Store Billing System")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
add_dropdown = gr.Dropdown(
|
| 78 |
+
choices=[item[0] for item in Inventory],
|
| 79 |
+
label="Select Item to Add (by ID)"
|
| 80 |
+
)
|
| 81 |
+
add_button = gr.Button("Add Tool")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
remove_dropdown = gr.Dropdown(
|
| 85 |
+
choices=[item[0] for item in Inventory],
|
| 86 |
+
label="Select Item to Remove (by ID)"
|
| 87 |
+
)
|
| 88 |
+
remove_button = gr.Button("Remove Tool")
|
| 89 |
+
|
| 90 |
+
cart_display = gr.Textbox(label="Cart Contents", lines=8)
|
| 91 |
+
message_display = gr.Textbox(label="Status Message")
|
| 92 |
+
|
| 93 |
+
total_button = gr.Button("Calculate Total")
|
| 94 |
+
total_output = gr.Textbox(label="Total Amount")
|
| 95 |
+
|
| 96 |
+
# Bind callbacks
|
| 97 |
+
add_button.click(handle_add, inputs=add_dropdown, outputs=[cart_display, message_display])
|
| 98 |
+
remove_button.click(handle_remove, inputs=remove_dropdown, outputs=[cart_display, message_display])
|
| 99 |
+
total_button.click(handle_total, outputs=total_output)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|