Parishri07 commited on
Commit
20ff6eb
Β·
verified Β·
1 Parent(s): 17f2637

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -45
app.py CHANGED
@@ -1,66 +1,52 @@
1
  import pandas as pd
2
  import gradio as gr
3
 
4
- # Load inventory
5
  df = pd.read_csv("inventory.csv")
6
 
7
- # Extract category list
8
  categories = sorted(df['Category'].dropna().unique())
9
 
10
- # Step 1: Ask for category
11
- def ask_category():
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
- # Step 3: Show product info
24
- def show_result(product, category):
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"βœ… Your **{product}** is in **{row['Aisle']}**. Quantity available: **{row['Quantity']}**, Price: β‚Ή{row['Price (rupees)']}."
31
  else:
32
- return f"❌ Sorry, **{product}** is currently **not available** in stock."
33
  else:
34
- return "Product not found."
35
 
36
- # Gradio interface with state
37
- with gr.Blocks(title="🧞 RetailGenie Guided Assistant") as demo:
38
- gr.Markdown("## πŸ›οΈ RetailGenie – Smart In-Store Assistant")
39
- gr.Markdown("Start by selecting a product category:")
40
 
41
- state_category = gr.State()
42
-
43
- # Step 1
44
- cat_btn = gr.Button("Select Category")
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
- # Step 1 callback
56
- def show_cat_options():
57
- return "Please choose a product category:", gr.update(visible=True), None
58
- cat_btn.click(show_cat_options, outputs=[cat_out, cat_dropdown, state_category])
 
59
 
60
- # Step 2 callback
61
- cat_dropdown.change(ask_product, inputs=cat_dropdown, outputs=[prod_out, prod_dropdown, state_category])
 
 
 
62
 
63
- # Step 3 callback
64
- prod_dropdown.change(show_result, inputs=[prod_dropdown, state_category], outputs=final_out)
 
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()