File size: 3,970 Bytes
943ca21
e406115
 
a8db628
e406115
a85dd4d
e406115
0807a82
e406115
0807a82
 
 
 
 
e406115
a8db628
0807a82
e406115
 
 
 
 
a85dd4d
6264a0c
e406115
 
6264a0c
29f9a6c
6264a0c
 
29f9a6c
6264a0c
 
 
 
 
20ff6eb
e406115
6264a0c
 
 
 
e406115
a85dd4d
6264a0c
e406115
6264a0c
a85dd4d
e406115
 
a8db628
20ff6eb
e406115
 
 
 
 
 
 
6264a0c
a8db628
e406115
29f9a6c
20ff6eb
 
29f9a6c
0807a82
 
 
 
 
 
 
e406115
132b70c
 
 
 
 
e406115
29f9a6c
 
 
 
 
e406115
29f9a6c
 
 
 
 
e406115
29f9a6c
 
 
 
 
e406115
29f9a6c
 
 
 
 
a8db628
e406115
6264a0c
e406115
 
 
29f9a6c
6264a0c
 
 
 
29f9a6c
e406115
 
a8db628
943ca21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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()