Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,214 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import json
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from datetime import datetime
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
cart = []
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if quantity > p["stock"]:
|
| 22 |
-
return "Not enough stock available!"
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"Quantity": quantity,
|
| 30 |
-
"Unit": p["unit"],
|
| 31 |
-
"Price": p["price"],
|
| 32 |
-
"Total": total
|
| 33 |
-
})
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
def
|
| 41 |
if not cart:
|
| 42 |
-
return
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def generate_bill():
|
| 47 |
-
if not cart:
|
| 48 |
-
return "Cart is empty!"
|
| 49 |
-
|
| 50 |
-
df = pd.DataFrame(cart)
|
| 51 |
-
grand_total = df["Total"].sum()
|
| 52 |
-
|
| 53 |
-
bill_text = "🧾 Supermarket Bill\n"
|
| 54 |
-
bill_text += "---------------------------------\n"
|
| 55 |
|
| 56 |
for item in cart:
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
return
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("# 🛒 Smart Supermarket System")
|
| 68 |
|
| 69 |
-
|
| 70 |
-
choices=["Groceries", "Personal Care", "Cleaning"],
|
| 71 |
-
label="Select Category"
|
| 72 |
-
)
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
inputs=category,
|
| 89 |
-
outputs=product_list
|
| 90 |
-
)
|
| 91 |
|
| 92 |
add_btn.click(
|
| 93 |
add_to_cart,
|
| 94 |
-
inputs=[
|
| 95 |
-
outputs=
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
view_cart,
|
| 100 |
-
outputs=
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# ------------------------------
|
| 4 |
+
# PRODUCT DATABASE
|
| 5 |
+
# ------------------------------
|
| 6 |
+
|
| 7 |
+
products = [
|
| 8 |
+
{
|
| 9 |
+
"name": "Rice",
|
| 10 |
+
"brand": "India Gate",
|
| 11 |
+
"category": "Groceries",
|
| 12 |
+
"price": 60,
|
| 13 |
+
"unit": "kg",
|
| 14 |
+
"stock": 100,
|
| 15 |
+
"image": "https://images.unsplash.com/photo-1586201375761-83865001e31c"
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"name": "Milk",
|
| 19 |
+
"brand": "Heritage",
|
| 20 |
+
"category": "Groceries",
|
| 21 |
+
"price": 28,
|
| 22 |
+
"unit": "liter",
|
| 23 |
+
"stock": 50,
|
| 24 |
+
"image": "https://images.unsplash.com/photo-1582719478250-c89cae4dc85b"
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"name": "Bath Soap",
|
| 28 |
+
"brand": "Lux",
|
| 29 |
+
"category": "Personal Care",
|
| 30 |
+
"price": 35,
|
| 31 |
+
"unit": "piece",
|
| 32 |
+
"stock": 80,
|
| 33 |
+
"image": "https://images.unsplash.com/photo-1600857544200-b2f666a9a2ec"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"name": "Shampoo",
|
| 37 |
+
"brand": "Clinic Plus",
|
| 38 |
+
"category": "Personal Care",
|
| 39 |
+
"price": 120,
|
| 40 |
+
"unit": "bottle",
|
| 41 |
+
"stock": 40,
|
| 42 |
+
"image": "https://images.unsplash.com/photo-1620916566398-39f1143ab7be"
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"name": "Detergent Powder",
|
| 46 |
+
"brand": "Surf Excel",
|
| 47 |
+
"category": "Cleaning",
|
| 48 |
+
"price": 95,
|
| 49 |
+
"unit": "kg",
|
| 50 |
+
"stock": 60,
|
| 51 |
+
"image": "https://images.unsplash.com/photo-1615486511484-92e172cc4fe0"
|
| 52 |
+
}
|
| 53 |
+
]
|
| 54 |
|
| 55 |
cart = []
|
| 56 |
|
| 57 |
+
# ------------------------------
|
| 58 |
+
# AI RECOMMENDATION RULES
|
| 59 |
+
# ------------------------------
|
| 60 |
+
|
| 61 |
+
recommendation_rules = {
|
| 62 |
+
"Rice": ["Milk"],
|
| 63 |
+
"Milk": ["Rice"],
|
| 64 |
+
"Bath Soap": ["Shampoo"],
|
| 65 |
+
"Shampoo": ["Bath Soap"],
|
| 66 |
+
"Detergent Powder": ["Bath Soap"]
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# ------------------------------
|
| 70 |
+
# SEARCH FUNCTION
|
| 71 |
+
# ------------------------------
|
| 72 |
+
|
| 73 |
+
def search_products(query, category):
|
| 74 |
+
results = []
|
| 75 |
+
for product in products:
|
| 76 |
+
if (query.lower() in product["name"].lower()) and \
|
| 77 |
+
(category == "All" or product["category"] == category):
|
| 78 |
+
|
| 79 |
+
results.append(
|
| 80 |
+
f"""
|
| 81 |
+
### {product['name']} ({product['brand']})
|
| 82 |
+
Price: ₹{product['price']} per {product['unit']}
|
| 83 |
+
Stock: {product['stock']} available
|
| 84 |
+

|
| 85 |
+
"""
|
| 86 |
+
)
|
| 87 |
+
return "\n".join(results) if results else "No products found."
|
| 88 |
+
|
| 89 |
+
# ------------------------------
|
| 90 |
+
# ADD TO CART
|
| 91 |
+
# ------------------------------
|
| 92 |
+
|
| 93 |
+
def add_to_cart(product_name, quantity):
|
| 94 |
+
for product in products:
|
| 95 |
+
if product["name"] == product_name:
|
| 96 |
+
if quantity <= product["stock"]:
|
| 97 |
+
total = quantity * product["price"]
|
| 98 |
+
cart.append({
|
| 99 |
+
"Product": product_name,
|
| 100 |
+
"Quantity": quantity,
|
| 101 |
+
"Price": product["price"],
|
| 102 |
+
"Total": total
|
| 103 |
+
})
|
| 104 |
+
product["stock"] -= quantity
|
| 105 |
+
return f"Added {quantity} {product['unit']} of {product_name} to cart."
|
| 106 |
+
else:
|
| 107 |
+
return "Not enough stock available."
|
| 108 |
+
return "Product not found."
|
| 109 |
+
|
| 110 |
+
# ------------------------------
|
| 111 |
+
# VIEW CART
|
| 112 |
+
# ------------------------------
|
| 113 |
|
| 114 |
+
def view_cart():
|
| 115 |
+
if not cart:
|
| 116 |
+
return "Cart is empty."
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
bill = "🧾 BILL DETAILS\n\n"
|
| 119 |
+
grand_total = 0
|
| 120 |
|
| 121 |
+
for item in cart:
|
| 122 |
+
bill += f"{item['Product']} x {item['Quantity']} = ₹{item['Total']}\n"
|
| 123 |
+
grand_total += item["Total"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
bill += f"\nTotal Amount: ₹{grand_total}"
|
| 126 |
+
return bill
|
| 127 |
|
| 128 |
+
# ------------------------------
|
| 129 |
+
# AI RECOMMENDATION
|
| 130 |
+
# ------------------------------
|
| 131 |
|
| 132 |
+
def recommend_products():
|
| 133 |
if not cart:
|
| 134 |
+
return "Add items to cart to see recommendations."
|
| 135 |
|
| 136 |
+
recommended = set()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
for item in cart:
|
| 139 |
+
product_name = item["Product"]
|
| 140 |
+
if product_name in recommendation_rules:
|
| 141 |
+
for suggestion in recommendation_rules[product_name]:
|
| 142 |
+
recommended.add(suggestion)
|
| 143 |
|
| 144 |
+
suggestion_text = "🧠 Recommended Products:\n"
|
| 145 |
+
for product in products:
|
| 146 |
+
if product["name"] in recommended:
|
| 147 |
+
suggestion_text += f"- {product['name']} ({product['brand']}) | ₹{product['price']} per {product['unit']}\n"
|
| 148 |
|
| 149 |
+
return suggestion_text
|
| 150 |
|
| 151 |
+
# ------------------------------
|
| 152 |
+
# GRADIO UI
|
| 153 |
+
# ------------------------------
|
| 154 |
|
| 155 |
with gr.Blocks() as demo:
|
|
|
|
| 156 |
|
| 157 |
+
gr.Markdown("# 🛒 AI Supermarket App")
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
with gr.Row():
|
| 160 |
+
search_box = gr.Textbox(label="Search Product")
|
| 161 |
+
category_filter = gr.Dropdown(
|
| 162 |
+
choices=["All", "Groceries", "Personal Care", "Cleaning"],
|
| 163 |
+
value="All",
|
| 164 |
+
label="Category"
|
| 165 |
+
)
|
| 166 |
|
| 167 |
+
search_output = gr.Markdown()
|
| 168 |
|
| 169 |
+
search_btn = gr.Button("🔍 Search")
|
| 170 |
+
search_btn.click(
|
| 171 |
+
search_products,
|
| 172 |
+
inputs=[search_box, category_filter],
|
| 173 |
+
outputs=search_output
|
| 174 |
+
)
|
| 175 |
|
| 176 |
+
gr.Markdown("## 🛒 Add to Cart")
|
| 177 |
|
| 178 |
+
product_dropdown = gr.Dropdown(
|
| 179 |
+
choices=[p["name"] for p in products],
|
| 180 |
+
label="Select Product"
|
| 181 |
+
)
|
| 182 |
|
| 183 |
+
quantity_input = gr.Number(label="Quantity", value=1)
|
| 184 |
|
| 185 |
+
add_btn = gr.Button("➕ Add to Cart")
|
| 186 |
+
add_output = gr.Textbox()
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
add_btn.click(
|
| 189 |
add_to_cart,
|
| 190 |
+
inputs=[product_dropdown, quantity_input],
|
| 191 |
+
outputs=add_output
|
| 192 |
)
|
| 193 |
|
| 194 |
+
gr.Markdown("## 🧾 View Bill")
|
| 195 |
+
|
| 196 |
+
cart_btn = gr.Button("🧾 Generate Bill")
|
| 197 |
+
cart_output = gr.Textbox()
|
| 198 |
+
|
| 199 |
+
cart_btn.click(
|
| 200 |
view_cart,
|
| 201 |
+
outputs=cart_output
|
| 202 |
)
|
| 203 |
|
| 204 |
+
gr.Markdown("## 🧠 AI Recommendations")
|
| 205 |
+
|
| 206 |
+
recommend_btn = gr.Button("🧠 Get Recommendations")
|
| 207 |
+
recommend_output = gr.Textbox()
|
| 208 |
+
|
| 209 |
+
recommend_btn.click(
|
| 210 |
+
recommend_products,
|
| 211 |
+
outputs=recommend_output
|
| 212 |
)
|
| 213 |
|
| 214 |
demo.launch()
|