Parishri07 commited on
Commit
a85dd4d
·
verified ·
1 Parent(s): 84124fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -35
app.py CHANGED
@@ -1,64 +1,95 @@
1
  import gradio as gr
 
 
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
25
 
 
 
 
 
 
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
-
30
- for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
33
  stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
  ):
37
- token = message.choices[0].delta.content
38
-
39
  response += token
40
  yield response
41
 
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
  respond,
 
 
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import re
4
  from huggingface_hub import InferenceClient
5
 
6
+ # Load inventory dataset
7
+ df = pd.read_csv("inventory.csv")
8
+
9
+ # Hugging Face LLM client
10
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
 
12
+ # 🧞 RetailGenie logic
13
+ def retailgenie_query(user_input: str) -> str:
14
+ user_input = user_input.lower()
15
 
16
+ if "where can i find" in user_input:
17
+ product = user_input.split("find")[-1].strip()
18
+ match = df[df['Product Name'].str.lower().str.contains(product)]
19
+ if not match.empty:
20
+ return f"{product.title()} is in {match.iloc[0]['Aisle']}."
21
+ else:
22
+ return "Sorry, I couldn't find that product."
23
+
24
+ elif "in stock" in user_input:
25
+ product = re.sub(r"(is|in stock|\?)", "", user_input).strip()
26
+ match = df[df['Product Name'].str.lower().str.contains(product)]
27
+ if not match.empty:
28
+ return f"{product.title()} is {'available' if match.iloc[0]['In Stock'].lower() == 'yes' else 'out of stock'}."
29
+ else:
30
+ return "Product not found."
31
+
32
+ elif "suggest" in user_input and "under" in user_input:
33
+ category = user_input.split("suggest a")[-1].split("under")[0].strip()
34
+ try:
35
+ price = int(re.findall(r"\d+", user_input)[-1])
36
+ except:
37
+ return "Please enter a valid price."
38
+ matches = df[(df['Category'].str.lower().str.contains(category)) & (df['Price (rupees)'] <= price)]
39
+ if not matches.empty:
40
+ return "Here are some options: " + ", ".join(matches['Product Name'].values[:3])
41
+ else:
42
+ return "No items found in that range."
43
+
44
+ elif "deal" in user_input or "deals" in user_input:
45
+ electronics = df[df['Category'].str.lower() == "electronics"]
46
+ deals = electronics[electronics['Price (rupees)'] < 5000]
47
+ if not deals.empty:
48
+ return "Deals: " + ", ".join(deals['Product Name'].values[:3])
49
+ else:
50
+ return "No current deals found in electronics."
51
+
52
+ return None # Not a RetailGenie-style query
53
 
54
+ # 🔁 Unified response function
55
+ def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
56
+ # First try product-aware logic
57
+ rg_reply = retailgenie_query(message)
58
+ if rg_reply:
59
+ yield rg_reply
60
+ return
61
 
62
+ # Else use LLM
63
+ messages = [{"role": "system", "content": system_message}]
64
+ for user, assistant in history:
65
+ if user: messages.append({"role": "user", "content": user})
66
+ if assistant: messages.append({"role": "assistant", "content": assistant})
67
  messages.append({"role": "user", "content": message})
68
 
69
  response = ""
70
+ for msg in client.chat_completion(
 
71
  messages,
72
  max_tokens=max_tokens,
73
  stream=True,
74
  temperature=temperature,
75
  top_p=top_p,
76
  ):
77
+ token = msg.choices[0].delta.content
 
78
  response += token
79
  yield response
80
 
81
+ # 🧠 Gradio Chat UI
 
 
 
82
  demo = gr.ChatInterface(
83
  respond,
84
+ title="🧞 RetailGenie",
85
+ description="Ask me where to find products, check availability, get price-based suggestions, or talk to an AI.",
86
  additional_inputs=[
87
+ gr.Textbox(value="You are a helpful retail assistant.", label="System message"),
88
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
89
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
90
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
91
+ ]
 
 
 
 
 
 
92
  )
93
 
 
94
  if __name__ == "__main__":
95
  demo.launch()