Parishri07 commited on
Commit
b89610e
Β·
verified Β·
1 Parent(s): 584533a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -4,25 +4,23 @@ import pandas as pd
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)
@@ -30,9 +28,10 @@ def get_quantities_from_csv(path):
30
  return gr.update(choices=[], visible=False), {}
31
  return gr.update(choices=df["Quantity"].dropna().tolist(), visible=True), df.to_dict()
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)
@@ -47,45 +46,38 @@ def display_quantity_info(quantity, data_dict):
47
  else:
48
  return f"❌ Sorry, {quantity} is currently not in stock."
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", choices=[], visible=True, interactive=False)
74
- city = gr.Dropdown(label="🏘️ City", choices=[], visible=True, interactive=False)
75
- store = gr.Dropdown(label="πŸͺ Store", choices=[], visible=True, interactive=False)
76
- category = gr.Dropdown(label="πŸ›οΈ Category", choices=[], visible=True, interactive=False)
77
- product = gr.Dropdown(label="πŸ“¦ Product", choices=[], visible=True, interactive=False)
78
- brand = gr.Dropdown(label="🏷️ Brand", choices=[], visible=True, interactive=False)
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,interactive=True),
89
  inputs=country,
90
  outputs=state
91
  )
 
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))])
11
  except Exception as e:
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")])
19
  except Exception as e:
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)
 
28
  return gr.update(choices=[], visible=False), {}
29
  return gr.update(choices=df["Quantity"].dropna().tolist(), visible=True), df.to_dict()
30
  except Exception as e:
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)
 
46
  else:
47
  return f"❌ Sorry, {quantity} is currently not in stock."
48
  except Exception as e:
49
+ return f"⚠️ Error: {e}"
50
 
51
+ # Reset all
52
  def reset_all():
53
  return (
54
+ None, None, None, None, None, None, None,
55
+ gr.update(choices=[], visible=False),
56
+ "", {}
 
 
 
 
 
 
 
57
  )
58
 
59
+ # UI
60
  with gr.Blocks(title="RetailGenie Navigator") as demo:
61
  gr.Markdown("## 🧞 RetailGenie – Multi-Store Smart Inventory Assistant")
62
 
63
  with gr.Row():
64
+ country = gr.Dropdown(label="🌍 Country", choices=get_subfolders(BASE_DIR), value=None)
65
+ state = gr.Dropdown(label="πŸ™οΈ State", choices=[], visible=True, interactive=False)
66
+ city = gr.Dropdown(label="🏘️ City", choices=[], visible=True, interactive=False)
67
+ store = gr.Dropdown(label="πŸͺ Store", choices=[], visible=True, interactive=False)
68
+ category = gr.Dropdown(label="πŸ›οΈ Category", choices=[], visible=True, interactive=False)
69
+ product = gr.Dropdown(label="πŸ“¦ Product", choices=[], visible=True, interactive=False)
70
+ brand = gr.Dropdown(label="🏷️ Brand", choices=[], visible=True, interactive=False)
71
+ quantity = gr.Dropdown(label="πŸ”’ Quantity", visible=False)
72
 
73
  result = gr.Textbox(label="πŸ” Product Info", lines=5)
74
  data_state = gr.State()
75
 
76
  reset_btn = gr.Button("πŸ”„ Reset All")
77
 
78
+ # Logic chaining
79
  country.change(
80
+ lambda c: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)), value=None, interactive=True),
81
  inputs=country,
82
  outputs=state
83
  )