Parishri07 commited on
Commit
b17b983
Β·
verified Β·
1 Parent(s): 042e0e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -28
app.py CHANGED
@@ -4,23 +4,25 @@ import pandas as pd
4
 
5
  BASE_DIR = "data"
6
 
 
7
  def get_subfolders(path):
8
- print(f"πŸ“‚ Reading subfolders from: {path}")
9
  try:
10
- folders = sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))])
11
- print(f"βœ… Found: {folders}")
12
- return folders
13
  except Exception as e:
14
- print(f"❌ Error reading {path}: {e}")
15
  return []
16
 
17
-
18
  def get_csv_files(path):
 
19
  try:
20
  return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")])
21
- except FileNotFoundError:
 
22
  return []
23
 
 
24
  def get_quantities_from_csv(path):
25
  try:
26
  df = pd.read_csv(path)
@@ -30,6 +32,7 @@ def get_quantities_from_csv(path):
30
  except Exception as e:
31
  return gr.update(choices=[], visible=False), {}
32
 
 
33
  def display_quantity_info(quantity, data_dict):
34
  try:
35
  df = pd.DataFrame(data_dict)
@@ -46,40 +49,52 @@ def display_quantity_info(quantity, data_dict):
46
  except Exception as e:
47
  return f"⚠️ Error processing quantity info: {e}"
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  with gr.Blocks(title="RetailGenie Navigator") as demo:
50
  gr.Markdown("## 🧞 RetailGenie – Multi-Store Smart Inventory Assistant")
51
 
52
  with gr.Row():
53
  country = gr.Dropdown(label="🌍 Country", choices=get_subfolders(BASE_DIR), value=None)
54
- state = gr.Dropdown(label="πŸ™οΈ State")
55
- city = gr.Dropdown(label="🏘️ City")
56
- store = gr.Dropdown(label="πŸͺ Store")
57
- category = gr.Dropdown(label="πŸ›οΈ Category")
58
- product = gr.Dropdown(label="πŸ“¦ Product")
59
- brand = gr.Dropdown(label="🏷️ Brand")
60
  quantity = gr.Dropdown(label="πŸ”’ Quantity", visible=False)
61
 
62
  result = gr.Textbox(label="πŸ” Product Info", lines=5)
63
  data_state = gr.State()
64
 
65
- # Logic chaining
 
 
66
  country.change(
67
- lambda c: (
68
- print(f"πŸ”½ Selected country: {c}"),
69
- gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)), value=None)
70
- )[1],
71
- inputs=country,
72
- outputs=state
73
- )
74
 
75
  state.change(
76
- lambda c, s: (
77
- print(f"πŸ”½ Selected state: {s} under country: {c}"),
78
- gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)), value=None)
79
- )[1],
80
- inputs=[country, state],
81
- outputs=city
82
- )
83
 
84
  city.change(
85
  lambda c, s, ci: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci)), value=None),
@@ -119,4 +134,10 @@ with gr.Blocks(title="RetailGenie Navigator") as demo:
119
  outputs=result
120
  )
121
 
 
 
 
 
 
 
122
  demo.launch()
 
4
 
5
  BASE_DIR = "data"
6
 
7
+ # Helper: Get subfolders at a given path
8
  def get_subfolders(path):
9
+ print(f"πŸ“ Reading subfolders from: {path}")
10
  try:
11
+ return sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))])
 
 
12
  except Exception as e:
13
+ print(f"❌ Error: {e}")
14
  return []
15
 
16
+ # Helper: Get all .csv filenames (without .csv)
17
  def get_csv_files(path):
18
+ print(f"πŸ“„ Reading CSV files from: {path}")
19
  try:
20
  return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")])
21
+ except Exception as e:
22
+ print(f"❌ Error: {e}")
23
  return []
24
 
25
+ # Read CSV for brand and return available quantity options
26
  def get_quantities_from_csv(path):
27
  try:
28
  df = pd.read_csv(path)
 
32
  except Exception as e:
33
  return gr.update(choices=[], visible=False), {}
34
 
35
+ # Show info based on selected quantity
36
  def display_quantity_info(quantity, data_dict):
37
  try:
38
  df = pd.DataFrame(data_dict)
 
49
  except Exception as e:
50
  return f"⚠️ Error processing quantity info: {e}"
51
 
52
+ # Reset all fields
53
+ def reset_all():
54
+ return (
55
+ None, # country
56
+ None, # state
57
+ None, # city
58
+ None, # store
59
+ None, # category
60
+ None, # product
61
+ None, # brand
62
+ gr.update(choices=[], visible=False), # quantity
63
+ "", # result
64
+ {} # data state
65
+ )
66
+
67
+ # Interface
68
  with gr.Blocks(title="RetailGenie Navigator") as demo:
69
  gr.Markdown("## 🧞 RetailGenie – Multi-Store Smart Inventory Assistant")
70
 
71
  with gr.Row():
72
  country = gr.Dropdown(label="🌍 Country", choices=get_subfolders(BASE_DIR), value=None)
73
+ state = gr.Dropdown(label="πŸ™οΈ State", value=None)
74
+ city = gr.Dropdown(label="🏘️ City", value=None)
75
+ store = gr.Dropdown(label="πŸͺ Store", value=None)
76
+ category = gr.Dropdown(label="πŸ›οΈ Category", value=None)
77
+ product = gr.Dropdown(label="πŸ“¦ Product", value=None)
78
+ brand = gr.Dropdown(label="🏷️ Brand", value=None)
79
  quantity = gr.Dropdown(label="πŸ”’ Quantity", visible=False)
80
 
81
  result = gr.Textbox(label="πŸ” Product Info", lines=5)
82
  data_state = gr.State()
83
 
84
+ reset_btn = gr.Button("πŸ”„ Reset All")
85
+
86
+ # Logic: Chain each dropdown to the next
87
  country.change(
88
+ lambda c: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)), value=None),
89
+ inputs=country,
90
+ outputs=state
91
+ )
 
 
 
92
 
93
  state.change(
94
+ lambda c, s: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)), value=None),
95
+ inputs=[country, state],
96
+ outputs=city
97
+ )
 
 
 
98
 
99
  city.change(
100
  lambda c, s, ci: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci)), value=None),
 
134
  outputs=result
135
  )
136
 
137
+ reset_btn.click(
138
+ reset_all,
139
+ inputs=[],
140
+ outputs=[country, state, city, store, category, product, brand, quantity, result, data_state]
141
+ )
142
+
143
  demo.launch()