Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
-
from PIL import Image, ImageDraw
|
5 |
|
6 |
BASE_DIR = "data"
|
7 |
-
ASSETS_DIR = "assets"
|
8 |
-
STORE_MAP_FILE = os.path.join(ASSETS_DIR, "store_map_clinicplus.png")
|
9 |
-
|
10 |
-
|
11 |
-
# Approximate mapping from Aisle number to bounding box on the store map
|
12 |
-
AISLE_TO_BOX = {
|
13 |
-
# Example: '14': (x, y, width, height) - you can later load this from config
|
14 |
-
"14": (220, 150, 40, 120),
|
15 |
-
"15": (270, 150, 40, 120),
|
16 |
-
"16": (320, 150, 40, 120),
|
17 |
-
# Add more as needed...
|
18 |
-
}
|
19 |
-
|
20 |
|
|
|
21 |
def get_subfolders(path):
|
22 |
try:
|
23 |
return sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))])
|
@@ -25,7 +12,7 @@ def get_subfolders(path):
|
|
25 |
print(f"β Error reading subfolders from {path}: {e}")
|
26 |
return []
|
27 |
|
28 |
-
|
29 |
def get_csv_files(path):
|
30 |
try:
|
31 |
return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")])
|
@@ -33,7 +20,7 @@ def get_csv_files(path):
|
|
33 |
print(f"β Error reading CSVs from {path}: {e}")
|
34 |
return []
|
35 |
|
36 |
-
|
37 |
def get_quantities_from_csv(path):
|
38 |
try:
|
39 |
df = pd.read_csv(path)
|
@@ -44,12 +31,11 @@ def get_quantities_from_csv(path):
|
|
44 |
print(f"β Error loading CSV: {e}")
|
45 |
return gr.update(choices=[], visible=False), {}
|
46 |
|
47 |
-
|
48 |
def display_quantity_info(quantity, data_dict):
|
49 |
try:
|
50 |
df = pd.DataFrame(data_dict)
|
51 |
row = df[df["Quantity"] == quantity].iloc[0]
|
52 |
-
|
53 |
if str(row["In Stock"]).strip().lower() == "yes":
|
54 |
msg = (
|
55 |
f"β
{quantity} is available!\n"
|
@@ -57,6 +43,7 @@ def display_quantity_info(quantity, data_dict):
|
|
57 |
f"β’ Aisle: {row['Aisle']}\n"
|
58 |
f"β’ Price: βΉ{row['Price']}"
|
59 |
)
|
|
|
60 |
if "Offer" in row and pd.notna(row["Offer"]) and row["Offer"].strip():
|
61 |
msg += f"\nβ’ π Offer: {row['Offer']}"
|
62 |
return msg
|
@@ -65,26 +52,7 @@ def display_quantity_info(quantity, data_dict):
|
|
65 |
except Exception as e:
|
66 |
return f"β οΈ Error: {e}"
|
67 |
|
68 |
-
|
69 |
-
def generate_map_with_highlight(quantity, data_dict):
|
70 |
-
try:
|
71 |
-
df = pd.DataFrame(data_dict)
|
72 |
-
row = df[df["Quantity"] == quantity].iloc[0]
|
73 |
-
aisle = str(row.get("Aisle", "")).strip()
|
74 |
-
|
75 |
-
image = Image.open(STORE_MAP_FILE).convert("RGBA")
|
76 |
-
draw = ImageDraw.Draw(image)
|
77 |
-
|
78 |
-
if aisle in AISLE_TO_BOX:
|
79 |
-
x, y, w, h = AISLE_TO_BOX[aisle]
|
80 |
-
draw.rectangle([x, y, x + w, y + h], outline="red", width=5)
|
81 |
-
|
82 |
-
return image
|
83 |
-
except Exception as e:
|
84 |
-
print(f"β οΈ Map render error: {e}")
|
85 |
-
return Image.open(STORE_MAP_FILE)
|
86 |
-
|
87 |
-
|
88 |
def suggest_items(query):
|
89 |
query = query.lower()
|
90 |
if "gift" in query and "500" in query:
|
@@ -103,28 +71,19 @@ def suggest_items(query):
|
|
103 |
)
|
104 |
return "π€· Sorry, no smart suggestions found. Try asking: 'Gift items under 500' or 'Shampoo for dry hair'"
|
105 |
|
106 |
-
|
107 |
def reset_all():
|
108 |
return (
|
109 |
-
None,
|
110 |
-
|
111 |
-
None, # city - Dropdown
|
112 |
-
None, # store - Dropdown
|
113 |
-
None, # category - Dropdown
|
114 |
-
None, # product - Dropdown
|
115 |
-
None, # brand - Dropdown
|
116 |
-
gr.update(choices=[], visible=False), # quantity - Dropdown
|
117 |
-
"", # result - Textbox
|
118 |
-
{}, # data_state - State (MUST be a raw dict or string, not gr.update)
|
119 |
-
Image.open(STORE_MAP_FILE) # store_map - Image (if using type="pil")
|
120 |
)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
with gr.Blocks(title="RetailGenie") as demo:
|
125 |
gr.Markdown("# π§ββοΈ RetailGenie β In-Store Smart Assistant")
|
126 |
|
127 |
with gr.Tabs():
|
|
|
128 |
with gr.TabItem("π§ Navigator"):
|
129 |
with gr.Row():
|
130 |
country = gr.Dropdown(label="π Country", choices=get_subfolders(BASE_DIR), value=None)
|
@@ -137,23 +96,58 @@ with gr.Blocks(title="RetailGenie") as demo:
|
|
137 |
quantity = gr.Dropdown(label="π’ Quantity", visible=False)
|
138 |
|
139 |
result = gr.Textbox(label="π Product Info", lines=5)
|
140 |
-
|
141 |
-
data_state = gr.State(value={})
|
142 |
reset_btn = gr.Button("π Reset All")
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
brand.change(
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
quantity.change(display_quantity_info, inputs=[quantity, data_state], outputs=result)
|
154 |
-
|
155 |
-
reset_btn.click(reset_all, inputs=[], outputs=[country, state, city, store, category, product, brand, quantity, result, data_state, store_map])
|
156 |
|
|
|
157 |
with gr.TabItem("π Smart Suggestions"):
|
158 |
gr.Markdown("### π€ Ask RetailGenie for Recommendations")
|
159 |
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 |
|
5 |
BASE_DIR = "data"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Get subfolders
|
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 |
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 |
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 |
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)
|
38 |
row = df[df["Quantity"] == quantity].iloc[0]
|
|
|
39 |
if str(row["In Stock"]).strip().lower() == "yes":
|
40 |
msg = (
|
41 |
f"β
{quantity} is available!\n"
|
|
|
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 |
except Exception as e:
|
53 |
return f"β οΈ Error: {e}"
|
54 |
|
55 |
+
# Smart Suggestions (hardcoded logic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def suggest_items(query):
|
57 |
query = query.lower()
|
58 |
if "gift" in query and "500" in query:
|
|
|
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 |
+
# Interface
|
|
|
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), value=None)
|
|
|
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 chain logic
|
103 |
+
country.change(
|
104 |
+
lambda c: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)) if c else [], value=None, interactive=True),
|
105 |
+
inputs=country,
|
106 |
+
outputs=state
|
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 |
+
category.change(
|
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 |
+
product.change(
|
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 |
+
brand.change(
|
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 |
quantity.change(display_quantity_info, inputs=[quantity, data_state], outputs=result)
|
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)
|