Spaces:
Runtime error
Runtime error
add code for scraper
Browse files- amazon_oxy.py +43 -0
- app.py +17 -5
amazon_oxy.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
username = os.environ.get('USERNAME')
|
9 |
+
passwd = os.environ.get('PASSWORD')
|
10 |
+
def scrape_amazon(search_query, n =10):
|
11 |
+
# Structure payload.
|
12 |
+
payload = {
|
13 |
+
'source': 'amazon_search',
|
14 |
+
'domain': 'com',
|
15 |
+
'query': f'{search_query}',
|
16 |
+
'start_page': 1,
|
17 |
+
'pages': 1,
|
18 |
+
'parse': True,
|
19 |
+
# 'context': [
|
20 |
+
# {'key': 'category_id', 'value': 16391693031}
|
21 |
+
# ],
|
22 |
+
}
|
23 |
+
|
24 |
+
# Get response.
|
25 |
+
response = requests.request(
|
26 |
+
'POST',
|
27 |
+
'https://realtime.oxylabs.io/v1/queries',
|
28 |
+
auth=(username, passwd),
|
29 |
+
json=payload,
|
30 |
+
)
|
31 |
+
result = json.loads(response.content)
|
32 |
+
final_result = {}
|
33 |
+
i=0
|
34 |
+
# Parse result.
|
35 |
+
for item in result['results'][0]['content']['results']['organic']:
|
36 |
+
if i<n:
|
37 |
+
final_result[item['url_image']]= (item['price'],item['url'])
|
38 |
+
i+=1
|
39 |
+
else:
|
40 |
+
break
|
41 |
+
|
42 |
+
return final_result
|
43 |
+
print(scrape_amazon("yellow top"))
|
app.py
CHANGED
@@ -24,6 +24,17 @@ from preprocess.humanparsing.run_parsing import Parsing
|
|
24 |
from preprocess.openpose.run_openpose import OpenPose
|
25 |
from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
|
26 |
from torchvision.transforms.functional import to_pil_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
def pil_to_binary_mask(pil_image, threshold=0):
|
@@ -280,11 +291,11 @@ with image_blocks as demo:
|
|
280 |
garm_img = gr.Image(label="Garment", sources='upload', type="pil")
|
281 |
with gr.Row(elem_id="prompt-container"):
|
282 |
with gr.Row():
|
283 |
-
prompt = gr.Textbox(placeholder="Description of garment ex)
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
with gr.Column():
|
289 |
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
290 |
masked_img = gr.Image(label="Masked image output", elem_id="masked-img",show_share_button=False)
|
@@ -303,6 +314,7 @@ with image_blocks as demo:
|
|
303 |
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
304 |
|
305 |
|
|
|
306 |
|
307 |
try_button.click(fn=start_tryon, inputs=[imgs, garm_img, prompt, is_checked,is_checked_crop, denoise_steps, seed], outputs=[image_out,masked_img], api_name='tryon')
|
308 |
|
|
|
24 |
from preprocess.openpose.run_openpose import OpenPose
|
25 |
from detectron2.data.detection_utils import convert_PIL_to_numpy,_apply_exif_orientation
|
26 |
from torchvision.transforms.functional import to_pil_image
|
27 |
+
import amazon_oxy
|
28 |
+
|
29 |
+
def fetch_products(query):
|
30 |
+
result= amazon_oxy.scrape_amazon(query)
|
31 |
+
values = list(result.values())
|
32 |
+
prices = []
|
33 |
+
urls = []
|
34 |
+
for price, url in values:
|
35 |
+
prices.append(price)
|
36 |
+
urls.append(url)
|
37 |
+
return [list(result.keys()), prices, urls]
|
38 |
|
39 |
|
40 |
def pil_to_binary_mask(pil_image, threshold=0):
|
|
|
291 |
garm_img = gr.Image(label="Garment", sources='upload', type="pil")
|
292 |
with gr.Row(elem_id="prompt-container"):
|
293 |
with gr.Row():
|
294 |
+
prompt = gr.Textbox(placeholder="Description of garment ex) Yellow Top", show_label=False, elem_id="prompt")
|
295 |
+
fetch_button = gr.Button("Fetch Products")
|
296 |
+
image_gallery = gr.Gallery(label="Fetched Images", input=garm_img).style(grid=[3], height="200px")
|
297 |
+
price_display = gr.Textbox(label="Price of Selected Image", interactive=False)
|
298 |
+
url_display = gr.Markdown()
|
299 |
with gr.Column():
|
300 |
# image_out = gr.Image(label="Output", elem_id="output-img", height=400)
|
301 |
masked_img = gr.Image(label="Masked image output", elem_id="masked-img",show_share_button=False)
|
|
|
314 |
seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
|
315 |
|
316 |
|
317 |
+
fetch_button.click(fn=fetch_products, inputs=prompt, outputs=[image_gallery, price_display, url_display])
|
318 |
|
319 |
try_button.click(fn=start_tryon, inputs=[imgs, garm_img, prompt, is_checked,is_checked_crop, denoise_steps, seed], outputs=[image_out,masked_img], api_name='tryon')
|
320 |
|