guptavishal79 commited on
Commit
2c591bd
·
1 Parent(s): 3c750c3

support adds

Browse files
Files changed (2) hide show
  1. app.py +33 -10
  2. gemini_api.py +66 -0
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from datetime import datetime, timedelta
3
 
4
- from gemini_api import model_api, sentiment, category, ord_num, NO_ORDER, food_return, cloth_return
5
 
6
  cust_qry_resp = {"senti":"", "cat":"", "num":""}
7
 
@@ -10,11 +10,13 @@ with gr.Blocks(title="Customer Support Assistant",
10
  analytics_enabled=False) as app:
11
  # States for triggering events
12
  # Order Num, Sentiment, Category, User Input
13
- state_order_num = gr.State([NO_ORDER, None, None, None])
14
  # Category, User Input
15
  state_ret_pol_cat = gr.State([None, None])
 
 
16
 
17
- gr.Markdown("Customer Support Assistant")
18
  # Inputs from user
19
  with gr.Row():
20
  cust_qry = gr.Textbox(lines=5, type="text", label="Customer Query")
@@ -31,19 +33,25 @@ with gr.Blocks(title="Customer Support Assistant",
31
  senti = model_api(user_input, sentiment)
32
  cat = model_api(user_input, category)
33
  num = model_api(user_input, ord_num)
 
34
  # Output response
35
  gr.Textbox(lines=1, type="text", label="Customer Sentiment", value=senti)
36
  gr.Textbox(lines=1, type="text", label="Order Category", value=cat)
37
  gr.Textbox(lines=1, type="text", label="Order Number", value=num)
 
38
 
39
  # Decision Rules
40
- if num != NO_ORDER:
41
- btn_ord_det = gr.Button("Fetch Order Details")
42
- btn_ord_det.click(lambda x: [num, senti, cat, user_input], state_order_num, state_order_num)
43
  # Order Details
44
- if senti in ["NEGATIVE", "MIXED"] and num == NO_ORDER:
45
- with gr.Row():
46
- gr.Textbox(lines=1, type="text", label="Next Step", value="Ask Order Number")
 
 
 
 
 
 
 
47
 
48
  @gr.render(inputs=state_order_num)
49
  def fetch_order_det(ip_arr):
@@ -52,9 +60,10 @@ with gr.Blocks(title="Customer Support Assistant",
52
  ord_senti = ip_arr[1]
53
  ord_cat = ip_arr[2]
54
  usr_ip = ip_arr[3]
 
55
  if ord_num != NO_ORDER:
56
  pur_dt = datetime.now() + timedelta(days=-2)
57
- ord_det = f"Order Number: {ord_num}\nPurchase Date: {pur_dt}"
58
  gr.Textbox(lines=1, type="text", label="Order Details", value=ord_det)
59
  if ord_senti in ["NEGATIVE", "MIXED"] and ord_num != NO_ORDER:
60
  with gr.Row():
@@ -76,6 +85,20 @@ with gr.Blocks(title="Customer Support Assistant",
76
  gr.Textbox(lines=1, type="text", label="Return Response", value=resp)
77
  with gr.Column(scale=1):
78
  gr.Textbox(lines=1, type="text", label="Return Policy Used", value=policy)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
 
81
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from datetime import datetime, timedelta
3
 
4
+ from gemini_api import model_api, sentiment, category, ord_num, NO_ORDER, NO_ITEM, food_return, cloth_return, item_identy, item_match
5
 
6
  cust_qry_resp = {"senti":"", "cat":"", "num":""}
7
 
 
10
  analytics_enabled=False) as app:
11
  # States for triggering events
12
  # Order Num, Sentiment, Category, User Input
13
+ state_order_num = gr.State([NO_ORDER, None, None, None, None])
14
  # Category, User Input
15
  state_ret_pol_cat = gr.State([None, None])
16
+ # Item, Category
17
+ state_match_item = gr.State([None, None])
18
 
19
+ gr.Markdown("# Customer Support Assistant")
20
  # Inputs from user
21
  with gr.Row():
22
  cust_qry = gr.Textbox(lines=5, type="text", label="Customer Query")
 
33
  senti = model_api(user_input, sentiment)
34
  cat = model_api(user_input, category)
35
  num = model_api(user_input, ord_num)
36
+ item = model_api(user_input, item_identy)
37
  # Output response
38
  gr.Textbox(lines=1, type="text", label="Customer Sentiment", value=senti)
39
  gr.Textbox(lines=1, type="text", label="Order Category", value=cat)
40
  gr.Textbox(lines=1, type="text", label="Order Number", value=num)
41
+ gr.Textbox(lines=1, type="text", label="Order Item", value=item)
42
 
43
  # Decision Rules
 
 
 
44
  # Order Details
45
+ if senti in ["NEGATIVE", "MIXED"]:
46
+ if num != NO_ORDER:
47
+ btn_ord_det = gr.Button("Fetch Order Details")
48
+ btn_ord_det.click(lambda x: [num, senti, cat, user_input, item], state_order_num, state_order_num)
49
+ else:
50
+ with gr.Row():
51
+ gr.Textbox(lines=1, type="text", label="Next Step", value="Ask Order Number")
52
+ else:
53
+ btn_item_match = gr.Button("Fetch Matching Items from Adds Store")
54
+ btn_item_match.click(lambda x: [item, cat], state_match_item, state_match_item)
55
 
56
  @gr.render(inputs=state_order_num)
57
  def fetch_order_det(ip_arr):
 
60
  ord_senti = ip_arr[1]
61
  ord_cat = ip_arr[2]
62
  usr_ip = ip_arr[3]
63
+ ord_item = ip_arr[4]
64
  if ord_num != NO_ORDER:
65
  pur_dt = datetime.now() + timedelta(days=-2)
66
+ ord_det = f"Order Number: {ord_num}\nPurchase Date: {pur_dt}\nItem Ordered: {ord_item}"
67
  gr.Textbox(lines=1, type="text", label="Order Details", value=ord_det)
68
  if ord_senti in ["NEGATIVE", "MIXED"] and ord_num != NO_ORDER:
69
  with gr.Row():
 
85
  gr.Textbox(lines=1, type="text", label="Return Response", value=resp)
86
  with gr.Column(scale=1):
87
  gr.Textbox(lines=1, type="text", label="Return Policy Used", value=policy)
88
+
89
+ @gr.render(inputs=state_match_item)
90
+ def fetch_ret_response(ip_arr):
91
+ print("Return Matching Items")
92
+ ord_item = ip_arr[0]
93
+ ord_cat = ip_arr[1]
94
+ if ord_item is not None:
95
+ ret_item, ret_theme = item_match(ord_item)
96
+ with gr.Row():
97
+ gr.Textbox(lines=1, type="text", label="Nearest Matching Item from Adds Store", value=ret_item)
98
+ gr.Textbox(lines=1, type="text", label="Add Theme", value=ret_theme)
99
+ with gr.Row():
100
+ age_cat = gr.Radio(["less than 13 years", "14 to 30 years", "30 to 50 years", "more than 50 years"])
101
+ btn_gen_add = gr.Button("Generate Add")
102
 
103
 
104
  if __name__ == "__main__":
gemini_api.py CHANGED
@@ -34,7 +34,19 @@ model = genai.GenerativeModel(
34
  safety_settings=safety_settings,
35
  generation_config=generation_config,
36
  )
 
37
  NO_ORDER = "Order Number not provided."
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def model_api(input, prompt_type):
40
  return prompt_type(input)
@@ -101,6 +113,33 @@ def ord_num(input):
101
  else:
102
  return ret_num
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  def food_return(input):
105
  policy = """
106
  For food items, never refund or accept return.
@@ -164,4 +203,31 @@ def cloth_return(input):
164
  print(response.text)
165
  full_resp = f"{response.text}\n**For return to be processed, please ensure all tags are intact. Delivery agent will inspect the item and then only process.**"
166
  return full_resp, policy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
 
34
  safety_settings=safety_settings,
35
  generation_config=generation_config,
36
  )
37
+
38
  NO_ORDER = "Order Number not provided."
39
+ NO_ITEM = "No item present."
40
+
41
+ adds_store = {
42
+ "pasta": "buy 1 get 1 free on penne",
43
+ "chocolate": "50 percent off on premium dark chocolate",
44
+ "biryani": "100 rupees off on all biryani orders",
45
+ "shoe": "get 1 pair socks free",
46
+ "skirt": "new exclusive summer collection",
47
+ "socks": "buy 2 get 3 free",
48
+ "saree": "10 percent off on silk collection"
49
+ }
50
 
51
  def model_api(input, prompt_type):
52
  return prompt_type(input)
 
113
  else:
114
  return ret_num
115
 
116
+ def item_identy(input):
117
+ chat_session = model.start_chat(
118
+ history=[
119
+ {
120
+ "role": "user",
121
+ "parts": [
122
+ "Identify the item being discussed.\nQuery: The shoes I ordered are small. Size is not what was in the catalog. Order number is 11223\nOutput:{\"item\": \"shoe\"}",
123
+ "Identify the item being discussed.\nQuery: The pizza had strange smell. Order number is 11223\nOutput:{\"item\": \"pizza\"}",
124
+ ],
125
+ },
126
+ ]
127
+ )
128
+
129
+ response = chat_session.send_message(f"Query: {input}\nOutput:")
130
+ print(response.text)
131
+ ret_item = None
132
+ try:
133
+ obj = json.loads(response.text)
134
+ ret_item = obj.get('item', NO_ITEM)
135
+ except ValueError:
136
+ print("exception parsing item.")
137
+
138
+ if ret_item is None:
139
+ return NO_ITEM
140
+ else:
141
+ return ret_item
142
+
143
  def food_return(input):
144
  policy = """
145
  For food items, never refund or accept return.
 
203
  print(response.text)
204
  full_resp = f"{response.text}\n**For return to be processed, please ensure all tags are intact. Delivery agent will inspect the item and then only process.**"
205
  return full_resp, policy
206
+
207
+ def item_match(ip_item):
208
+ food_items = ["pasta", "chocolate", "biryani"]
209
+ cloth_items = [ "shoe", "skirt", "socks", "saree"]
210
+
211
+ ex1 = f'Get nearest matching item. Item list is {food_items}.\nQuery: Find item matching pizza.\nOutput:{{"item": "pasta"}}'
212
+ ex2 = f'Get nearest matching item. Item list is {cloth_items}.\nQuery: Find item matching shoe.\nOutput:{{"item": "shoe"}}'
213
+ ex3 = f'Get nearest matching item. Item list is {food_items}.\nQuery: Find item matching cake.\nOutput:{{"item": "chocolate"}}'
214
+ ex4 = f'Get nearest matching item. Item list is {cloth_items}.\nQuery: Find item matching dress.\nOutput:{{"item": "skirt"}}'
215
+
216
+ chat_session = model.start_chat(
217
+ history=[
218
+ {
219
+ "role": "user",
220
+ "parts": [
221
+ ex1, ex2, ex3, ex4
222
+ ],
223
+ },
224
+ ]
225
+ )
226
+
227
+ response = chat_session.send_message(f"Query: Find item matching {ip_item}.\nOutput:")
228
+ print(response.text)
229
+ obj = json.loads(response.text)
230
+ ret_item = obj['item']
231
+ theme = adds_store.get(ret_item, "5 percent discount to all members. Become a member")
232
+ return ret_item, theme
233