File size: 4,447 Bytes
cb78aa4
 
b177e30
ec88b7b
b177e30
ffbb953
 
e99ee78
 
ec88b7b
 
e99ee78
 
 
 
ffbb953
a52cd57
b177e30
 
 
bff859d
ec88b7b
 
 
 
 
 
 
 
 
 
 
 
 
 
e99ee78
ec88b7b
e99ee78
 
ec88b7b
e99ee78
 
ec88b7b
e99ee78
ec88b7b
 
 
b177e30
ec88b7b
e99ee78
 
 
 
 
 
 
 
b177e30
e99ee78
ec88b7b
 
bff859d
e7f4e08
b177e30
e99ee78
bff859d
e99ee78
 
 
e7f4e08
b177e30
e99ee78
 
 
bff859d
e99ee78
 
bff859d
e99ee78
bff859d
ec88b7b
 
 
e99ee78
ec88b7b
e99ee78
 
ec88b7b
e99ee78
ec88b7b
e99ee78
ec88b7b
e99ee78
ec88b7b
 
b177e30
 
 
bff859d
a4b156f
bff859d
a4b156f
77d3c2f
e99ee78
 
ec88b7b
 
e99ee78
ec88b7b
 
 
 
 
 
 
 
 
 
 
e99ee78
ec88b7b
e99ee78
ec88b7b
 
 
 
e99ee78
ec88b7b
 
e99ee78
ec88b7b
 
 
 
 
77d3c2f
ec88b7b
77d3c2f
ec88b7b
 
19f9f2e
ec88b7b
 
 
 
 
77d3c2f
e99ee78
77d3c2f
ec88b7b
e99ee78
ec88b7b
 
77d3c2f
b177e30
1
2
3
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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)