Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,52 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# Load
|
5 |
df = pd.read_csv("inventory.csv")
|
6 |
|
7 |
-
#
|
8 |
categories = sorted(df['Category'].dropna().unique())
|
9 |
|
10 |
-
#
|
11 |
-
def
|
12 |
-
return "Please select a product category:", gr.Dropdown(choices=categories, label="Product Category"), None
|
13 |
-
|
14 |
-
# Step 2: Based on category, ask for product
|
15 |
-
def ask_product(category):
|
16 |
products = df[df['Category'] == category]['Product Name'].unique().tolist()
|
17 |
-
return (
|
18 |
-
f"Now select a product from {category}:",
|
19 |
-
gr.update(choices=products, visible=True), # β
Fix applied here
|
20 |
-
category
|
21 |
-
)
|
22 |
|
23 |
-
#
|
24 |
-
def
|
25 |
match = df[(df['Category'] == category) & (df['Product Name'] == product)]
|
26 |
-
|
27 |
if not match.empty:
|
28 |
row = match.iloc[0]
|
29 |
-
if row["In Stock"].strip().lower() == "yes":
|
30 |
-
return f"β
|
31 |
else:
|
32 |
-
return f"β Sorry,
|
33 |
else:
|
34 |
-
return "Product not found."
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
gr.
|
39 |
-
gr.Markdown("Start by selecting a product category:")
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
cat_out = gr.Textbox(label="Message")
|
46 |
-
cat_dropdown = gr.Dropdown(choices=categories, visible=False)
|
47 |
-
|
48 |
-
# Step 2
|
49 |
-
prod_out = gr.Textbox(label="Message")
|
50 |
-
prod_dropdown = gr.Dropdown(visible=False)
|
51 |
-
|
52 |
-
# Step 3
|
53 |
-
final_out = gr.Textbox(label="Result")
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
demo.launch()
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# Load data
|
5 |
df = pd.read_csv("inventory.csv")
|
6 |
|
7 |
+
# Get category list
|
8 |
categories = sorted(df['Category'].dropna().unique())
|
9 |
|
10 |
+
# Product selector from category
|
11 |
+
def get_products(category):
|
|
|
|
|
|
|
|
|
12 |
products = df[df['Category'] == category]['Product Name'].unique().tolist()
|
13 |
+
return gr.update(choices=products, visible=True), category
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Final result message
|
16 |
+
def get_product_info(product, category):
|
17 |
match = df[(df['Category'] == category) & (df['Product Name'] == product)]
|
|
|
18 |
if not match.empty:
|
19 |
row = match.iloc[0]
|
20 |
+
if str(row["In Stock"]).strip().lower() == "yes":
|
21 |
+
return f"β
{product} is available in Aisle {row['Aisle']}. We have {row['Quantity']} in stock, and it's priced at βΉ{row['Price (rupees)']}."
|
22 |
else:
|
23 |
+
return f"β Sorry, {product} is currently not available in stock."
|
24 |
else:
|
25 |
+
return "β οΈ Product not found."
|
26 |
|
27 |
+
# Clear function
|
28 |
+
def reset():
|
29 |
+
return gr.update(value=None), gr.update(choices=[], visible=False), "", ""
|
|
|
30 |
|
31 |
+
# Gradio App
|
32 |
+
with gr.Blocks(title="RetailGenie") as demo:
|
33 |
+
gr.Markdown("## π§ RetailGenie β Your Smart In-Store Assistant")
|
34 |
+
gr.Markdown("### π Welcome! Let me help you quickly find products in your store.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
with gr.Row():
|
37 |
+
with gr.Column():
|
38 |
+
category_input = gr.Dropdown(label="ποΈ Choose Product Category", choices=categories)
|
39 |
+
product_input = gr.Dropdown(label="π¦ Choose a Product", visible=False)
|
40 |
+
clear_btn = gr.Button("π Start Over")
|
41 |
|
42 |
+
with gr.Column():
|
43 |
+
message = gr.Textbox(label="π¬ Assistant Response", interactive=False)
|
44 |
+
|
45 |
+
# Logic chaining
|
46 |
+
state_category = gr.State()
|
47 |
|
48 |
+
category_input.change(get_products, inputs=category_input, outputs=[product_input, state_category])
|
49 |
+
product_input.change(get_product_info, inputs=[product_input, state_category], outputs=message)
|
50 |
+
clear_btn.click(reset, outputs=[category_input, product_input, message, state_category])
|
51 |
|
52 |
demo.launch()
|