Spaces:
Sleeping
Sleeping
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() | |