RetailGenie / app.py
Parishri07's picture
Update app.py
0807a82 verified
raw
history blame
3.97 kB
import gradio as gr
import os
import pandas as pd
BASE_DIR = "data"
def get_subfolders(path):
print(f"πŸ“‚ Reading subfolders from: {path}")
try:
folders = sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))])
print(f"βœ… Found: {folders}")
return folders
except Exception as e:
print(f"❌ Error reading {path}: {e}")
return []
def get_csv_files(path):
try:
return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")])
except FileNotFoundError:
return []
def get_quantities_from_csv(path):
try:
df = pd.read_csv(path)
if df.empty or "Quantity" not in df.columns:
return gr.update(choices=[], visible=False), {}
return gr.update(choices=df["Quantity"].dropna().tolist(), visible=True), df.to_dict()
except Exception as e:
return gr.update(choices=[], visible=False), {}
def display_quantity_info(quantity, data_dict):
try:
df = pd.DataFrame(data_dict)
row = df[df["Quantity"] == quantity].iloc[0]
if str(row["In Stock"]).strip().lower() == "yes":
return (
f"βœ… {quantity} is available!\n"
f"β€’ Floor: {row['Floor']}\n"
f"β€’ Aisle: {row['Aisle']}\n"
f"β€’ Price: β‚Ή{row['Price']}"
)
else:
return f"❌ Sorry, {quantity} is currently not in stock."
except Exception as e:
return f"⚠️ Error processing quantity info: {e}"
with gr.Blocks(title="RetailGenie Navigator") as demo:
gr.Markdown("## 🧞 RetailGenie – Multi-Store Smart Inventory Assistant")
with gr.Row():
country = gr.Dropdown(label="🌍 Country", choices=get_subfolders(BASE_DIR))
state = gr.Dropdown(label="πŸ™οΈ State")
city = gr.Dropdown(label="🏘️ City")
store = gr.Dropdown(label="πŸͺ Store")
category = gr.Dropdown(label="πŸ›οΈ Category")
product = gr.Dropdown(label="πŸ“¦ Product")
brand = gr.Dropdown(label="🏷️ Brand")
quantity = gr.Dropdown(label="πŸ”’ Quantity", visible=False)
result = gr.Textbox(label="πŸ” Product Info", lines=5)
data_state = gr.State()
# Logic chaining
country.change(
lambda c: (
print(f"πŸ”½ Selected country: {c}"),
gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)), value=None)
)[1],
inputs=country,
outputs=state
)
state.change(
lambda c, s: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)), value=None),
inputs=[country, state],
outputs=city
)
city.change(
lambda c, s, ci: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci)), value=None),
inputs=[country, state, city],
outputs=store
)
store.change(
lambda c, s, ci, st: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci, st)), value=None),
inputs=[country, state, city, store],
outputs=category
)
category.change(
lambda c, s, ci, st, cat: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci, st, cat)), value=None),
inputs=[country, state, city, store, category],
outputs=product
)
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),
inputs=[country, state, city, store, category, product],
outputs=brand
)
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")
),
inputs=[country, state, city, store, category, product, brand],
outputs=[quantity, data_state]
)
quantity.change(
display_quantity_info,
inputs=[quantity, data_state],
outputs=result
)
demo.launch()