Parishri07 commited on
Commit
8b47930
Β·
verified Β·
1 Parent(s): b2108fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -81
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
- # 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,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
- # Smart Suggestions (hardcoded logic)
56
- def suggest_items(query):
57
- query = query.lower()
58
- if "gift" in query and "500" in query:
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
- # 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)
90
- state = gr.Dropdown(label="πŸ™οΈ State", choices=[], interactive=False)
91
- city = gr.Dropdown(label="🏘️ City", choices=[], interactive=False)
92
- store = gr.Dropdown(label="πŸͺ Store", choices=[], interactive=False)
93
- category = gr.Dropdown(label="πŸ›οΈ Category", choices=[], interactive=False)
94
- product = gr.Dropdown(label="πŸ“¦ Product", choices=[], interactive=False)
95
- brand = gr.Dropdown(label="🏷️ Brand", choices=[], interactive=False)
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)
 
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)