Parishri07 commited on
Commit
6264a0c
Β·
verified Β·
1 Parent(s): e406115

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -16,24 +16,30 @@ def get_csv_files(path):
16
  except FileNotFoundError:
17
  return []
18
 
19
- def load_csv_info(path):
20
  try:
21
  df = pd.read_csv(path)
22
- if df.empty:
23
- return "⚠️ No data found in the selected brand file."
24
- row = df.iloc[0]
 
 
 
 
 
 
 
25
  if str(row["In Stock"]).strip().lower() == "yes":
26
  return (
27
- f"βœ… {os.path.basename(path).replace('.csv', '')} is in stock.\n"
28
- f"- Quantity: {row['Quantity']}\n"
29
- f"- Floor: {row['Floor']}\n"
30
- f"- Aisle: {row['Aisle']}\n"
31
- f"- Price: β‚Ή{row['Price']}"
32
  )
33
  else:
34
- return f"❌ {os.path.basename(path).replace('.csv', '')} is currently out of stock."
35
  except Exception as e:
36
- return f"⚠️ Error reading CSV: {e}"
37
 
38
  with gr.Blocks(title="RetailGenie Navigator") as demo:
39
  gr.Markdown("## 🧞 RetailGenie – Multi-Store Smart Inventory Assistant")
@@ -46,6 +52,7 @@ with gr.Blocks(title="RetailGenie Navigator") as demo:
46
  category = gr.Dropdown(label="πŸ›οΈ Category")
47
  product = gr.Dropdown(label="πŸ“¦ Product")
48
  brand = gr.Dropdown(label="🏷️ Brand")
 
49
 
50
  result = gr.Textbox(label="πŸ” Product Info", lines=5)
51
 
@@ -68,11 +75,19 @@ with gr.Blocks(title="RetailGenie Navigator") as demo:
68
  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)), value=None),
69
  inputs=[country, state, city, store, category, product], outputs=brand)
70
 
 
71
  brand.change(
72
- lambda c, s, ci, st, cat, prod, b: load_csv_info(
73
  os.path.join(BASE_DIR, c, s, ci, st, cat, prod, f"{b}.csv")
74
  ),
75
  inputs=[country, state, city, store, category, product, brand],
 
 
 
 
 
 
 
76
  outputs=result
77
  )
78
 
 
16
  except FileNotFoundError:
17
  return []
18
 
19
+ def get_quantities_from_csv(path):
20
  try:
21
  df = pd.read_csv(path)
22
+ if df.empty or "Quantity" not in df.columns:
23
+ return gr.update(choices=[], visible=False), gr.State({})
24
+ return gr.update(choices=df["Quantity"].dropna().tolist(), visible=True), df.to_dict()
25
+ except Exception as e:
26
+ return gr.update(choices=[], visible=False), gr.State({})
27
+
28
+ def display_quantity_info(quantity, data_dict):
29
+ try:
30
+ df = pd.DataFrame(data_dict)
31
+ row = df[df["Quantity"] == quantity].iloc[0]
32
  if str(row["In Stock"]).strip().lower() == "yes":
33
  return (
34
+ f"βœ… {quantity} is available!\n"
35
+ f"β€’ Floor: {row['Floor']}\n"
36
+ f"β€’ Aisle: {row['Aisle']}\n"
37
+ f"β€’ Price: β‚Ή{row['Price']}"
 
38
  )
39
  else:
40
+ return f"❌ Sorry, {quantity} is currently not in stock."
41
  except Exception as e:
42
+ return f"⚠️ Error processing quantity info: {e}"
43
 
44
  with gr.Blocks(title="RetailGenie Navigator") as demo:
45
  gr.Markdown("## 🧞 RetailGenie – Multi-Store Smart Inventory Assistant")
 
52
  category = gr.Dropdown(label="πŸ›οΈ Category")
53
  product = gr.Dropdown(label="πŸ“¦ Product")
54
  brand = gr.Dropdown(label="🏷️ Brand")
55
+ quantity = gr.Dropdown(label="πŸ”’ Quantity", visible=False)
56
 
57
  result = gr.Textbox(label="πŸ” Product Info", lines=5)
58
 
 
75
  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)), value=None),
76
  inputs=[country, state, city, store, category, product], outputs=brand)
77
 
78
+ # When brand is selected β†’ get quantities + dataframe state
79
  brand.change(
80
+ lambda c, s, ci, st, cat, prod, b: get_quantities_from_csv(
81
  os.path.join(BASE_DIR, c, s, ci, st, cat, prod, f"{b}.csv")
82
  ),
83
  inputs=[country, state, city, store, category, product, brand],
84
+ outputs=[quantity, gr.State()]
85
+ )
86
+
87
+ # When quantity is selected β†’ lookup and display info
88
+ quantity.change(
89
+ display_quantity_info,
90
+ inputs=[quantity, gr.State()],
91
  outputs=result
92
  )
93