SevaBotAI / app.py
charantejapolavarapu's picture
Update app.py
e99ee78 verified
import gradio as gr
# -----------------------------
# PRODUCT DATABASE
# -----------------------------
products = [
{"id": 1, "name": "Apple", "price": 120, "category": "Fruits", "best": True},
{"id": 2, "name": "Banana", "price": 50, "category": "Fruits", "best": False},
{"id": 3, "name": "Milk 1L", "price": 60, "category": "Dairy", "best": True},
{"id": 4, "name": "Bread", "price": 40, "category": "Dairy", "best": False},
{"id": 5, "name": "Rice 1kg", "price": 70, "category": "Grains", "best": False},
{"id": 6, "name": "Cooking Oil", "price": 140, "category": "Essentials", "best": True},
{"id": 7, "name": "Potato Chips", "price": 20, "category": "Snacks", "best": True},
{"id": 8, "name": "Sugar", "price": 45, "category": "Essentials", "best": False},
]
# -----------------------------
# FUNCTIONS
# -----------------------------
def filter_products(search, category, sort_option):
filtered = products
if search:
filtered = [p for p in filtered if search.lower() in p["name"].lower()]
if category != "All":
filtered = [p for p in filtered if p["category"] == category]
if sort_option == "Low to High":
filtered = sorted(filtered, key=lambda x: x["price"])
elif sort_option == "High to Low":
filtered = sorted(filtered, key=lambda x: -x["price"])
text = ""
for p in filtered:
star = " ⭐ Best Seller" if p["best"] else ""
text += f"{p['name']}{star} - ₹{p['price']}\n"
if not text:
text = "No products found."
return text
def add_to_cart(product_name, cart):
for p in products:
if p["name"] == product_name:
found = False
for item in cart:
if item["name"] == product_name:
item["qty"] += 1
found = True
break
if not found:
cart.append({"name": p["name"], "price": p["price"], "qty": 1})
break
return cart, generate_bill(cart), recommend(cart)
def generate_bill(cart):
if not cart:
return "Cart is empty"
total = 0
text = "🧾 BILL SUMMARY\n"
text += "----------------------\n"
for item in cart:
subtotal = item["price"] * item["qty"]
total += subtotal
text += f"{item['name']} x{item['qty']} = ₹{subtotal}\n"
text += "----------------------\n"
text += f"TOTAL: ₹{total}"
return text
def recommend(cart):
if not cart:
return "Add products to see recommendations."
categories = [p["category"] for p in products if any(c["name"] == p["name"] for c in cart)]
suggestions = [p for p in products if p["category"] in categories]
text = "🧠 Recommended:\n"
for s in suggestions[:3]:
text += f"{s['name']} - ₹{s['price']}\n"
return text
# -----------------------------
# UI
# -----------------------------
with gr.Blocks() as demo:
cart_state = gr.State([])
gr.Markdown("# 🛒 Smart Grocery Delivery App")
gr.Markdown("### Startup-Level Grocery Ordering System")
with gr.Row():
search_box = gr.Textbox(label="🔍 Search")
category_dropdown = gr.Dropdown(
["All", "Fruits", "Dairy", "Grains", "Essentials", "Snacks"],
value="All",
label="🏪 Category"
)
sort_dropdown = gr.Dropdown(
["None", "Low to High", "High to Low"],
value="None",
label="📊 Sort"
)
filter_btn = gr.Button("Apply")
product_box = gr.Textbox(label="📦 Products", lines=10)
filter_btn.click(
filter_products,
inputs=[search_box, category_dropdown, sort_dropdown],
outputs=product_box
)
gr.Markdown("## 🛒 Add to Cart")
product_selector = gr.Dropdown(
[p["name"] for p in products],
label="Select Product"
)
add_btn = gr.Button("Add to Cart")
bill_box = gr.Textbox(label="🧾 Bill", lines=10)
rec_box = gr.Textbox(label="🧠 AI Recommendation", lines=5)
add_btn.click(
add_to_cart,
inputs=[product_selector, cart_state],
outputs=[cart_state, bill_box, rec_box]
)
gr.Markdown("## 💳 UPI QR Payment")
gr.Image(
"https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=upi://pay?pa=demo@upi",
height=200
)
demo.launch(server_name="0.0.0.0", server_port=7860)