Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import pandas as pd | |
BASE_DIR = "data" | |
# Get subfolders | |
def get_subfolders(path): | |
try: | |
return sorted([f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]) | |
except Exception as e: | |
print(f"β Error reading subfolders from {path}: {e}") | |
return [] | |
# Get CSV files | |
def get_csv_files(path): | |
try: | |
return sorted([f[:-4] for f in os.listdir(path) if f.endswith(".csv")]) | |
except Exception as e: | |
print(f"β Error reading CSVs from {path}: {e}") | |
return [] | |
# Load quantities from brand CSV | |
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: | |
print(f"β Error loading CSV: {e}") | |
return gr.update(choices=[], visible=False), {} | |
# Show result based on quantity | |
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": | |
msg = ( | |
f"β {quantity} is available!\n" | |
f"β’ Floor: {row['Floor']}\n" | |
f"β’ Aisle: {row['Aisle']}\n" | |
f"β’ Price: βΉ{row['Price']}" | |
) | |
# Show only Offer field if it exists and is not empty | |
if "Offer" in row and pd.notna(row["Offer"]) and row["Offer"].strip(): | |
msg += f"\nβ’ π Offer: {row['Offer']}" | |
return msg | |
else: | |
return f"β Sorry, {quantity} is currently not in stock." | |
except Exception as e: | |
return f"β οΈ Error: {e}" | |
# Smart Suggestions (hardcoded logic) | |
def suggest_items(query): | |
query = query.lower() | |
if "gift" in query and "500" in query: | |
return ( | |
"π Gift Suggestions under βΉ500:\n" | |
"1. Bath & Body Gift Set - βΉ499\n" | |
"2. Mini Perfume Pack - βΉ349\n" | |
"3. Skin Care Hamper - βΉ399\n" | |
"4. Chocolates Gift Box - βΉ299" | |
) | |
if "shampoo" in query and "dry" in query: | |
return ( | |
"π§΄ Shampoos for Dry Hair:\n" | |
"1. Dove 500 ml - βΉ325\n" | |
"2. Clinic Plus 500 ml - βΉ680" | |
) | |
return "π€· Sorry, no smart suggestions found. Try asking: 'Gift items under 500' or 'Shampoo for dry hair'" | |
# Reset logic | |
def reset_all(): | |
return ( | |
None, None, None, None, None, None, None, | |
gr.update(choices=[], visible=False), "", {} | |
) | |
# Interface | |
with gr.Blocks(title="RetailGenie") as demo: | |
gr.Markdown("# π§ββοΈ RetailGenie β In-Store Smart Assistant") | |
with gr.Tabs(): | |
# π§ Navigator Tab | |
with gr.TabItem("π§ Navigator"): | |
with gr.Row(): | |
country = gr.Dropdown(label="π Country", choices=get_subfolders(BASE_DIR), value=None) | |
state = gr.Dropdown(label="ποΈ State", choices=[], interactive=False) | |
city = gr.Dropdown(label="ποΈ City", choices=[], interactive=False) | |
store = gr.Dropdown(label="πͺ Store", choices=[], interactive=False) | |
category = gr.Dropdown(label="ποΈ Category", choices=[], interactive=False) | |
product = gr.Dropdown(label="π¦ Product", choices=[], interactive=False) | |
brand = gr.Dropdown(label="π·οΈ Brand", choices=[], interactive=False) | |
quantity = gr.Dropdown(label="π’ Quantity", visible=False) | |
result = gr.Textbox(label="π Product Info", lines=5) | |
data_state = gr.State() | |
reset_btn = gr.Button("π Reset All") | |
# Dropdown chain logic | |
country.change( | |
lambda c: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c)) if c else [], value=None, interactive=True), | |
inputs=country, | |
outputs=state | |
) | |
state.change( | |
lambda c, s: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s)) if c and s else [], value=None, interactive=True), | |
inputs=[country, state], | |
outputs=city | |
) | |
city.change( | |
lambda c, s, ci: gr.update(choices=get_subfolders(os.path.join(BASE_DIR, c, s, ci)) if c and s and ci else [], value=None, interactive=True), | |
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)) if all([c, s, ci, st]) else [], value=None, interactive=True), | |
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)) if all([c, s, ci, st, cat]) else [], value=None, interactive=True), | |
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)) if all([c, s, ci, st, cat, prod]) else [], value=None, interactive=True), | |
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") | |
) if all([c, s, ci, st, cat, prod, b]) else (gr.update(choices=[], visible=False), {}), | |
inputs=[country, state, city, store, category, product, brand], | |
outputs=[quantity, data_state] | |
) | |
quantity.change(display_quantity_info, inputs=[quantity, data_state], outputs=result) | |
reset_btn.click(reset_all, inputs=[], outputs=[country, state, city, store, category, product, brand, quantity, result, data_state]) | |
# π Suggestions Tab | |
with gr.TabItem("π Smart Suggestions"): | |
gr.Markdown("### π€ Ask RetailGenie for Recommendations") | |
suggestion_input = gr.Textbox(label="Ask something like:", placeholder="Gift items under 500", lines=1) | |
suggest_btn = gr.Button("π‘ Get Suggestions") | |
suggestions_output = gr.Textbox(label="π Suggestions", lines=10) | |
suggest_btn.click(suggest_items, inputs=suggestion_input, outputs=suggestions_output) | |
demo.launch() | |