Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import pandas as pd
|
|
|
4 |
|
|
|
5 |
BASE_DIR = "data"
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def get_subfolders(path):
|
9 |
try:
|
10 |
return sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))])
|
@@ -12,7 +21,6 @@ def get_subfolders(path):
|
|
12 |
print(f"β Error reading subfolders from {path}: {e}")
|
13 |
return []
|
14 |
|
15 |
-
# Get CSV files
|
16 |
def get_csv_files(path):
|
17 |
try:
|
18 |
return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")])
|
@@ -20,7 +28,6 @@ def get_csv_files(path):
|
|
20 |
print(f"β Error reading CSVs from {path}: {e}")
|
21 |
return []
|
22 |
|
23 |
-
# Load quantities from brand CSV
|
24 |
def get_quantities_from_csv(path):
|
25 |
try:
|
26 |
df = pd.read_csv(path)
|
@@ -31,7 +38,6 @@ def get_quantities_from_csv(path):
|
|
31 |
print(f"β Error loading CSV: {e}")
|
32 |
return gr.update(choices=[], visible=False), {}
|
33 |
|
34 |
-
# Show result based on quantity
|
35 |
def display_quantity_info(quantity, data_dict):
|
36 |
try:
|
37 |
df = pd.DataFrame(data_dict)
|
@@ -43,7 +49,6 @@ def display_quantity_info(quantity, data_dict):
|
|
43 |
f"β’ Aisle: {row['Aisle']}\n"
|
44 |
f"β’ Price: βΉ{row['Price']}"
|
45 |
)
|
46 |
-
# Show only Offer field if it exists and is not empty
|
47 |
if "Offer" in row and pd.notna(row["Offer"]) and row["Offer"].strip():
|
48 |
msg += f"\nβ’ π Offer: {row['Offer']}"
|
49 |
return msg
|
@@ -52,102 +57,53 @@ def display_quantity_info(quantity, data_dict):
|
|
52 |
except Exception as e:
|
53 |
return f"β οΈ Error: {e}"
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return (
|
60 |
-
"π Gift Suggestions under βΉ500:\n"
|
61 |
-
"1. Bath & Body Gift Set - βΉ499\n"
|
62 |
-
"2. Mini Perfume Pack - βΉ349\n"
|
63 |
-
"3. Skin Care Hamper - βΉ399\n"
|
64 |
-
"4. Chocolates Gift Box - βΉ299"
|
65 |
-
)
|
66 |
-
if "shampoo" in query and "dry" in query:
|
67 |
-
return (
|
68 |
-
"π§΄ Shampoos for Dry Hair:\n"
|
69 |
-
"1. Dove 500 ml - βΉ325\n"
|
70 |
-
"2. Clinic Plus 500 ml - βΉ680"
|
71 |
-
)
|
72 |
-
return "π€· Sorry, no smart suggestions found. Try asking: 'Gift items under 500' or 'Shampoo for dry hair'"
|
73 |
-
|
74 |
-
# Reset logic
|
75 |
def reset_all():
|
76 |
return (
|
77 |
None, None, None, None, None, None, None,
|
78 |
-
gr.update(choices=[], visible=False), "", {}
|
79 |
)
|
80 |
|
81 |
-
#
|
82 |
with gr.Blocks(title="RetailGenie") as demo:
|
83 |
gr.Markdown("# π§ββοΈ RetailGenie β In-Store Smart Assistant")
|
84 |
|
85 |
with gr.Tabs():
|
86 |
-
# π§ Navigator Tab
|
87 |
with gr.TabItem("π§ Navigator"):
|
88 |
with gr.Row():
|
89 |
-
country = gr.Dropdown(label="π Country", choices=get_subfolders(BASE_DIR),
|
90 |
-
state = gr.Dropdown(label="ποΈ State", choices=[], interactive=
|
91 |
-
city = gr.Dropdown(label="ποΈ City", choices=[], interactive=
|
92 |
-
store = gr.Dropdown(label="πͺ Store", choices=[], interactive=
|
93 |
-
category = gr.Dropdown(label="ποΈ Category", choices=[], interactive=
|
94 |
-
product = gr.Dropdown(label="π¦ Product", choices=[], interactive=
|
95 |
-
brand = gr.Dropdown(label="π·οΈ Brand", choices=[], interactive=
|
96 |
-
quantity = gr.Dropdown(label="π’ Quantity", visible=False)
|
97 |
|
98 |
result = gr.Textbox(label="π Product Info", lines=5)
|
99 |
data_state = gr.State()
|
|
|
100 |
reset_btn = gr.Button("π Reset All")
|
101 |
|
102 |
-
# Dropdown
|
103 |
-
country.change(
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
)
|
108 |
-
|
109 |
-
state.change(
|
110 |
-
lambda c, s: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)) if c and s else [], value=None, interactive=True),
|
111 |
-
inputs=[country, state],
|
112 |
-
outputs=city
|
113 |
-
)
|
114 |
-
|
115 |
-
city.change(
|
116 |
-
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, interactive=True),
|
117 |
-
inputs=[country, state, city],
|
118 |
-
outputs=store
|
119 |
-
)
|
120 |
-
|
121 |
-
store.change(
|
122 |
-
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),
|
123 |
-
inputs=[country, state, city, store],
|
124 |
-
outputs=category
|
125 |
-
)
|
126 |
|
127 |
-
|
128 |
-
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, interactive=True),
|
129 |
-
inputs=[country, state, city, store, category],
|
130 |
-
outputs=product
|
131 |
-
)
|
132 |
|
133 |
-
|
134 |
-
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, interactive=True),
|
135 |
-
inputs=[country, state, city, store, category, product],
|
136 |
-
outputs=brand
|
137 |
-
)
|
138 |
|
139 |
-
|
140 |
-
lambda c, s, ci, st, cat, prod, b: get_quantities_from_csv(
|
141 |
-
os.path.join(BASE_DIR, c, s, ci, st, cat, prod, f"{b}.csv")
|
142 |
-
) if all([c, s, ci, st, cat, prod, b]) else (gr.update(choices=[], visible=False), {}),
|
143 |
-
inputs=[country, state, city, store, category, product, brand],
|
144 |
-
outputs=[quantity, data_state]
|
145 |
-
)
|
146 |
|
147 |
-
|
148 |
-
reset_btn.click(reset_all, inputs=[], outputs=[country, state, city, store, category, product, brand, quantity, result, data_state])
|
149 |
|
150 |
-
# π Suggestions Tab
|
151 |
with gr.TabItem("π Smart Suggestions"):
|
152 |
gr.Markdown("### π€ Ask RetailGenie for Recommendations")
|
153 |
suggestion_input = gr.Textbox(label="Ask something like:", placeholder="Gift items under 500", lines=1)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
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.png")
|
10 |
+
PRODUCT_IMAGE_MAP = {
|
11 |
+
"shampoo": os.path.join(ASSETS_DIR, "shampoo_map.png"),
|
12 |
+
"curd": os.path.join(ASSETS_DIR, "curd_map.png"),
|
13 |
+
"ghee": os.path.join(ASSETS_DIR, "ghee_map.png")
|
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))])
|
|
|
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")])
|
|
|
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)
|
|
|
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)
|
|
|
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
|
|
|
57 |
except Exception as e:
|
58 |
return f"β οΈ Error: {e}"
|
59 |
|
60 |
+
def get_product_image(product_name):
|
61 |
+
lower_name = product_name.strip().lower()
|
62 |
+
return Image.open(PRODUCT_IMAGE_MAP.get(lower_name, DEFAULT_MAP))
|
63 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
def reset_all():
|
65 |
return (
|
66 |
None, None, None, None, None, None, None,
|
67 |
+
gr.update(choices=[], visible=False), "", {}, Image.open(DEFAULT_MAP)
|
68 |
)
|
69 |
|
70 |
+
# ----- UI -----
|
71 |
with gr.Blocks(title="RetailGenie") as demo:
|
72 |
gr.Markdown("# π§ββοΈ RetailGenie β In-Store Smart Assistant")
|
73 |
|
74 |
with gr.Tabs():
|
|
|
75 |
with gr.TabItem("π§ Navigator"):
|
76 |
with gr.Row():
|
77 |
+
country = gr.Dropdown(label="π Country", choices=get_subfolders(BASE_DIR), interactive=True)
|
78 |
+
state = gr.Dropdown(label="ποΈ State", choices=[], interactive=True)
|
79 |
+
city = gr.Dropdown(label="ποΈ City", choices=[], interactive=True)
|
80 |
+
store = gr.Dropdown(label="πͺ Store", choices=[], interactive=True)
|
81 |
+
category = gr.Dropdown(label="ποΈ Category", choices=[], interactive=True)
|
82 |
+
product = gr.Dropdown(label="π¦ Product", choices=[], interactive=True)
|
83 |
+
brand = gr.Dropdown(label="π·οΈ Brand", choices=[], interactive=True)
|
84 |
+
quantity = gr.Dropdown(label="π’ Quantity", visible=False, interactive=True)
|
85 |
|
86 |
result = gr.Textbox(label="π Product Info", lines=5)
|
87 |
data_state = gr.State()
|
88 |
+
store_map = gr.Image(value=Image.open(DEFAULT_MAP), type="pil", label="πΊοΈ Store Map")
|
89 |
reset_btn = gr.Button("π Reset All")
|
90 |
|
91 |
+
# Dropdown chaining
|
92 |
+
country.change(lambda c: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)) if c else [], value=None), inputs=country, outputs=state)
|
93 |
+
state.change(lambda c, s: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)) if c and s else [], value=None), inputs=[country, state], outputs=city)
|
94 |
+
city.change(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), inputs=[country, state, city], outputs=store)
|
95 |
+
store.change(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), inputs=[country, state, city, store], outputs=category)
|
96 |
+
category.change(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), inputs=[country, state, city, store, category], outputs=product)
|
97 |
+
product.change(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), inputs=[country, state, city, store, category, product], outputs=brand)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
brand.change(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), {}), inputs=[country, state, city, store, category, product, brand], outputs=[quantity, data_state])
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
quantity.change(display_quantity_info, inputs=[quantity, data_state], outputs=result)
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
product.change(fn=get_product_image, inputs=product, outputs=store_map)
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
reset_btn.click(reset_all, inputs=[], outputs=[country, state, city, store, category, product, brand, quantity, result, data_state, store_map])
|
|
|
106 |
|
|
|
107 |
with gr.TabItem("π Smart Suggestions"):
|
108 |
gr.Markdown("### π€ Ask RetailGenie for Recommendations")
|
109 |
suggestion_input = gr.Textbox(label="Ask something like:", placeholder="Gift items under 500", lines=1)
|