Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,164 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import pandas as pd
|
4 |
-
from PIL import Image
|
5 |
-
|
6 |
-
# ----- Config -----
|
7 |
-
BASE_DIR = "data"
|
8 |
-
ASSETS_DIR = "assets"
|
9 |
-
DEFAULT_MAP = os.path.join(ASSETS_DIR, "default_map.jpg")
|
10 |
-
PRODUCT_IMAGE_MAP = {
|
11 |
-
"shampoo": os.path.join(ASSETS_DIR, "shampoo_map.jpg"),
|
12 |
-
"curd": os.path.join(ASSETS_DIR, "curd_map.jpg"),
|
13 |
-
"ghee": os.path.join(ASSETS_DIR, "ghee_map.jpg")
|
14 |
-
}
|
15 |
-
|
16 |
-
# ----- Utilities -----
|
17 |
-
def get_subfolders(path):
|
18 |
-
try:
|
19 |
-
return sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))])
|
20 |
-
except Exception as e:
|
21 |
-
print(f"❌ Error reading subfolders from {path}: {e}")
|
22 |
-
return []
|
23 |
-
|
24 |
-
def get_csv_files(path):
|
25 |
-
try:
|
26 |
-
return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")])
|
27 |
-
except Exception as e:
|
28 |
-
print(f"❌ Error reading CSVs from {path}: {e}")
|
29 |
-
return []
|
30 |
-
|
31 |
-
def get_quantities_from_csv(path):
|
32 |
-
try:
|
33 |
-
df = pd.read_csv(path)
|
34 |
-
if df.empty or "Quantity" not in df.columns:
|
35 |
-
return gr.update(choices=[], visible=False), {}
|
36 |
-
return gr.update(choices=df["Quantity"].dropna().tolist(), visible=True), df.to_dict()
|
37 |
-
except Exception as e:
|
38 |
-
print(f"❌ Error loading CSV: {e}")
|
39 |
-
return gr.update(choices=[], visible=False), {}
|
40 |
-
|
41 |
-
def display_quantity_info(quantity, data_dict):
|
42 |
-
try:
|
43 |
-
df = pd.DataFrame(data_dict)
|
44 |
-
row = df[df["Quantity"] == quantity].iloc[0]
|
45 |
-
if str(row["In Stock"]).strip().lower() == "yes":
|
46 |
-
msg = (
|
47 |
-
f"✅ {quantity} is available!\n"
|
48 |
-
f"• Floor: {row['Floor']}\n"
|
49 |
-
f"• Aisle: {row['Aisle']}\n"
|
50 |
-
f"• Price: ₹{row['Price']}"
|
51 |
-
)
|
52 |
-
if "Offer" in row and pd.notna(row["Offer"]) and row["Offer"].strip():
|
53 |
-
msg += f"\n• 🎉 Offer: {row['Offer']}"
|
54 |
-
return msg
|
55 |
-
else:
|
56 |
-
return f"❌ Sorry, {quantity} is currently not in stock."
|
57 |
-
except Exception as e:
|
58 |
-
return f"⚠ Error: {e}"
|
59 |
-
|
60 |
-
# Smart Suggestions Function
|
61 |
-
def suggest_items(query):
|
62 |
-
query = query.lower()
|
63 |
-
if "gift" in query and "500" in query:
|
64 |
-
return (
|
65 |
-
"🎁 Gift Suggestions under ₹500:\n"
|
66 |
-
"1. Bath & Body Gift Set - ₹499\n"
|
67 |
-
"2. Mini Perfume Pack - ₹349\n"
|
68 |
-
"3. Skin Care Hamper - ₹399\n"
|
69 |
-
"4. Chocolates Gift Box - ₹299"
|
70 |
-
)
|
71 |
-
if "shampoo" in query and "dry" in query:
|
72 |
-
return (
|
73 |
-
"🧴 Shampoos for Dry Hair:\n"
|
74 |
-
"1. Dove 500 ml - ₹325\n"
|
75 |
-
"2. Clinic Plus 500 ml - ₹680"
|
76 |
-
)
|
77 |
-
return "🤷 Sorry, no smart suggestions found. Try asking: 'Gift items under 500' or 'Shampoo for dry hair'"
|
78 |
-
|
79 |
-
def reset_all():
|
80 |
-
return (
|
81 |
-
None, None, None, None, None, None, None,
|
82 |
-
gr.update(choices=[], visible=False), # quantity dropdown reset
|
83 |
-
"", # result textbox reset
|
84 |
-
{} # data_state (gr.State) reset with raw dict
|
85 |
-
)
|
86 |
-
|
87 |
-
# ----- UI -----
|
88 |
-
with gr.Blocks(title="RetailGenie") as demo:
|
89 |
-
gr.Markdown("# 🧞♂ RetailGenie – In-Store Smart Assistant")
|
90 |
-
|
91 |
-
with gr.Tabs():
|
92 |
-
with gr.TabItem("🧭 Navigator"):
|
93 |
-
with gr.Row():
|
94 |
-
country = gr.Dropdown(label="🌍 Country", choices=get_subfolders(BASE_DIR), interactive=True)
|
95 |
-
state = gr.Dropdown(label="🏙 State", choices=[], interactive=True)
|
96 |
-
city = gr.Dropdown(label="🏘 City", choices=[], interactive=True)
|
97 |
-
store = gr.Dropdown(label="🏪 Store", choices=[], interactive=True)
|
98 |
-
category = gr.Dropdown(label="🛍 Category", choices=[], interactive=True)
|
99 |
-
product = gr.Dropdown(label="📦 Product", choices=[], interactive=True)
|
100 |
-
brand = gr.Dropdown(label="🏷 Brand", choices=[], interactive=True)
|
101 |
-
quantity = gr.Dropdown(label="🔢 Quantity", visible=False, interactive=True)
|
102 |
-
|
103 |
-
result = gr.Textbox(label="🔍 Product Info", lines=5)
|
104 |
-
data_state = gr.State()
|
105 |
-
reset_btn = gr.Button("🔄 Reset All")
|
106 |
-
|
107 |
-
# Dropdown chaining
|
108 |
-
country.change(
|
109 |
-
lambda c: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)) if c else [], value=None),
|
110 |
-
inputs=country, outputs=state
|
111 |
-
)
|
112 |
-
state.change(
|
113 |
-
lambda c, s: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)) if c and s else [], value=None),
|
114 |
-
inputs=[country, state], outputs=city
|
115 |
-
)
|
116 |
-
city.change(
|
117 |
-
lambda c, s, ci: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci)) if c and s and ci else [], value=None),
|
118 |
-
inputs=[country, state, city], outputs=store
|
119 |
-
)
|
120 |
-
store.change(
|
121 |
-
lambda c, s, ci, st: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci, st)) if all([c, s, ci, st]) else [], value=None, interactive=True),
|
122 |
-
inputs=[country, state, city, store], outputs=category
|
123 |
-
)
|
124 |
-
category.change(
|
125 |
-
lambda c, s, ci, st, cat: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci, st, cat)) if all([c, s, ci, st, cat]) else [], value=None),
|
126 |
-
inputs=[country, state, city, store, category], outputs=product
|
127 |
-
)
|
128 |
-
product.change(
|
129 |
-
lambda c, s, ci, st, cat, prod: gr.update(choices=get_csv_files(os.path.join(BASE_DIR, c, s, ci, st, cat, prod)) if all([c, s, ci, st, cat, prod]) else [], value=None),
|
130 |
-
inputs=[country, state, city, store, category, product], outputs=brand
|
131 |
-
)
|
132 |
-
brand.change(
|
133 |
-
lambda c, s, ci, st, cat, prod, b: get_quantities_from_csv(os.path.join(BASE_DIR, c, s, ci, st, cat, prod, f"{b}.csv")) if all([c, s, ci, st, cat, prod, b]) else (gr.update(choices=[], visible=False), {}),
|
134 |
-
inputs=[country, state, city, store, category, product, brand],
|
135 |
-
outputs=[quantity, data_state]
|
136 |
-
)
|
137 |
-
quantity.change(
|
138 |
-
display_quantity_info,
|
139 |
-
inputs=[quantity, data_state],
|
140 |
-
outputs=result
|
141 |
-
)
|
142 |
-
reset_btn.click(
|
143 |
-
reset_all,
|
144 |
-
inputs=[],
|
145 |
-
outputs=[
|
146 |
-
country, state, city, store,
|
147 |
-
category, product, brand, quantity,
|
148 |
-
result, data_state
|
149 |
-
]
|
150 |
-
)
|
151 |
-
|
152 |
-
with gr.TabItem("🎁 Smart Suggestions"):
|
153 |
-
gr.Markdown("### 🤖 Ask RetailGenie for Recommendations")
|
154 |
-
suggestion_input = gr.Textbox(label="Ask something like:", placeholder="Gift items under 500", lines=1)
|
155 |
-
suggest_btn = gr.Button("💡 Get Suggestions")
|
156 |
-
suggestions_output = gr.Textbox(label="📝 Suggestions", lines=10)
|
157 |
-
|
158 |
-
suggest_btn.click(
|
159 |
-
suggest_items,
|
160 |
-
inputs=suggestion_input,
|
161 |
-
outputs=suggestions_output
|
162 |
-
)
|
163 |
|
|
|
164 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from ui.layout import build_ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
demo = build_ui()
|
5 |
demo.launch()
|