John Yang commited on
Commit
7fb90e2
·
1 Parent(s): 730ca01

Update output format

Browse files
Files changed (1) hide show
  1. app.py +45 -42
app.py CHANGED
@@ -91,6 +91,36 @@ def bert_predict(obs, info, softmax=True):
91
  idx = outputs.logits[0].argmax(0).item()
92
  return valid_acts[idx]
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  def predict(obs, info):
96
  """
@@ -172,25 +202,7 @@ def run_episode(goal, env, verbose=True):
172
  page_type = Page.SEARCH
173
 
174
  elif action == 'click[buy now]':
175
- return_value = {}
176
- if env == 'amazon':
177
- return_value['Product URL'] = f"https://www.amazon.com/dp/{asin}"
178
- if len(clicked_options) > 0:
179
- return_value['Selected Options'] = ', '.join(list(clicked_options))
180
- if env == 'webshop':
181
- query_str = "+".join(search_terms.split())
182
- options_str = json.dumps(options)
183
- asin_url = (
184
- f'{WEBSHOP_URL}/item_page/{WEBSHOP_SESSION}/'
185
- f'{asin}/{query_str}/{page_num}/{options_str}'
186
- )
187
- return_value['Product URL'] = asin_url
188
- if env == 'ebay':
189
- asin_url = f"https://www.ebay.com/itm/{asin}"
190
- return_value['Product URL'] = asin_url
191
- if len(clicked_options) > 0:
192
- return_value['Selected Options'] = ', '.join(list(clicked_options))
193
- return return_value
194
 
195
  elif prev_page_type == Page.ITEM_PAGE:
196
  found = False
@@ -214,7 +226,7 @@ def run_episode(goal, env, verbose=True):
214
  if search_terms in search_results_cache:
215
  data = search_results_cache[search_terms]
216
  if verbose:
217
- print(f"Loading cached results page for\"{search_terms}\"")
218
  else:
219
  begin = time.time()
220
  if env == 'amazon':
@@ -273,37 +285,28 @@ def run_episode(goal, env, verbose=True):
273
  print("Extracting available actions took", end-begin, "seconds")
274
 
275
  if i == 50:
276
- return_value = {}
277
- if env == 'amazon':
278
- return_value['Product URL'] = f"https://www.amazon.com/dp/{asin}"
279
- if len(clicked_options) > 0:
280
- return_value['Selected Options'] = ', '.join(list(clicked_options))
281
- if env == 'webshop':
282
- query_str = "+".join(search_terms.split())
283
- options_str = json.dumps(options)
284
- asin_url = (
285
- f'{WEBSHOP_URL}/item_page/{WEBSHOP_SESSION}/'
286
- f'{asin}/{query_str}/{page_num}/{options_str}'
287
- )
288
- return_value['Product URL'] = asin_url
289
- if env == 'ebay':
290
- asin_url = f"https://www.ebay.com/itm/{asin}"
291
- return_value['Product URL'] = asin_url
292
- if len(clicked_options) > 0:
293
- return_value['Selected Options'] = ', '.join(list(clicked_options))
294
- return return_value
295
 
296
- gr.Interface(fn=run_episode,
 
297
  inputs=[
298
  gr.inputs.Textbox(lines=7, label="Input Text"),
299
  gr.inputs.Radio(['Amazon', 'eBay'], type="value", default="Amazon", label='Environment')
300
  ],
301
- outputs="text",
 
 
 
 
302
  examples=[
303
  ["I want to find a gold floor lamp with a glass shade and a nickel finish that i can use for my living room, and price lower than 270.00 dollars", "Amazon"],
304
  ["I need some cute heart-shaped glittery cupcake picks as a gift to bring to a baby shower", "Amazon"],
 
 
305
  ["I'm trying to find white bluetooth speakers that are not only water resistant but also come with stereo sound", "eBay"],
306
- ["find me the soy free 3.5 ounce 4-pack of dang thai rice chips, and make sure they are the aged cheddar flavor. i also need the ones in the resealable bags", "eBay"]
 
 
307
  ],
308
  title="WebShop",
309
  article="<p style='padding-top:15px;text-align:center;'>To learn more about this project, check out the <a href='https://webshop-pnlp.github.io/' target='_blank'>project page</a>!</p>",
 
91
  idx = outputs.logits[0].argmax(0).item()
92
  return valid_acts[idx]
93
 
94
+ def get_return_value(env, asin, options, search_terms, page_num, product):
95
+ asin_url = None
96
+
97
+ # Determine product URL + options based on environment
98
+ if env == 'webshop':
99
+ query_str = "+".join(search_terms.split())
100
+ options_str = json.dumps(options)
101
+ asin_url = (
102
+ f'{WEBSHOP_URL}/item_page/{WEBSHOP_SESSION}/'
103
+ f'{asin}/{query_str}/{page_num}/{options_str}'
104
+ )
105
+ else:
106
+ asin_url = f"https://www.ebay.com/itm/{asin}" if env == 'ebay' else \
107
+ f"https://www.amazon.com/dp/{asin}"
108
+
109
+ # Extract relevant fields for product
110
+ product_reduced = {k: v for k, v in product.items() if k in ["asin", "Title", "Description", "BulletPoints"]}
111
+ product_reduced["Description"] = product_reduced["Description"][:100] + "..."
112
+ product_reduced["Features"] = product_reduced.pop("BulletPoints")
113
+ product_reduced["Features"] = product_reduced["Features"][:100] + "..."
114
+
115
+ # Create HTML to show link to product
116
+ html = """<!DOCTYPE html><html><head><title>Chosen Product</title></head><body>"""
117
+ html += f"""Product Image:<img src="{product["MainImage"]}" height="50px" /><br>""" if len(product["MainImage"]) > 0 else ""
118
+ html += f"""Link to Product:
119
+ <a href="{asin_url}" style="color:blue;text-decoration:underline;" target="_blank">{asin_url}</a>
120
+ </body></html>"""
121
+
122
+ return product_reduced, options if len(options) > 0 else "None Selected", html
123
+
124
 
125
  def predict(obs, info):
126
  """
 
202
  page_type = Page.SEARCH
203
 
204
  elif action == 'click[buy now]':
205
+ return get_return_value(env, asin, options, search_terms, page_num, product_map[asin])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
  elif prev_page_type == Page.ITEM_PAGE:
208
  found = False
 
226
  if search_terms in search_results_cache:
227
  data = search_results_cache[search_terms]
228
  if verbose:
229
+ print(f"Loading cached results page for \"{search_terms}\"")
230
  else:
231
  begin = time.time()
232
  if env == 'amazon':
 
285
  print("Extracting available actions took", end-begin, "seconds")
286
 
287
  if i == 50:
288
+ return get_return_value(env, asin, options, search_terms, page_num, product_map[asin])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
+ gr.Interface(
291
+ fn=run_episode,
292
  inputs=[
293
  gr.inputs.Textbox(lines=7, label="Input Text"),
294
  gr.inputs.Radio(['Amazon', 'eBay'], type="value", default="Amazon", label='Environment')
295
  ],
296
+ outputs=[
297
+ gr.outputs.JSON(label="Selected Product"),
298
+ gr.outputs.JSON(label="Selected Options"),
299
+ gr.outputs.HTML()
300
+ ],
301
  examples=[
302
  ["I want to find a gold floor lamp with a glass shade and a nickel finish that i can use for my living room, and price lower than 270.00 dollars", "Amazon"],
303
  ["I need some cute heart-shaped glittery cupcake picks as a gift to bring to a baby shower", "Amazon"],
304
+ ["I want to buy ballet shoes which have rubber sole in grey suede color and a size of 6", "Amazon"],
305
+ ["I would like a 7 piece king comforter set decorated with flowers and is machine washable", "Amazon"],
306
  ["I'm trying to find white bluetooth speakers that are not only water resistant but also come with stereo sound", "eBay"],
307
+ ["find me the soy free 3.5 ounce 4-pack of dang thai rice chips, and make sure they are the aged cheddar flavor. i also need the ones in the resealable bags", "eBay"],
308
+ ["I am looking for a milk chocolate of 1 pound size in a single pack for valentine day", "eBay"],
309
+ ["I'm looking for a mini pc intel core desktop computer which supports with windows 11", "eBay"]
310
  ],
311
  title="WebShop",
312
  article="<p style='padding-top:15px;text-align:center;'>To learn more about this project, check out the <a href='https://webshop-pnlp.github.io/' target='_blank'>project page</a>!</p>",