Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,3 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
-
|
5 |
-
OXYLABS_ENDPOINT = 'https://realtime.oxylabs.io/v1/queries'
|
6 |
-
OXYLABS_AUTH = ('user', 'pass1')
|
7 |
-
OPENAI_ENDPOINT = 'https://api.openai.com/v1/engines/davinci/completions'
|
8 |
-
OPENAI_AUTH = 'Bearer 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'
|
9 |
-
|
10 |
def get_product_details(asin, domain):
|
11 |
payload = {
|
12 |
'source': 'amazon_product',
|
@@ -16,19 +7,24 @@ def get_product_details(asin, domain):
|
|
16 |
'context': [{'key': 'autoselect_variant', 'value': True}]
|
17 |
}
|
18 |
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
19 |
-
data = response.json()
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def get_reviews(asin, domain):
|
34 |
payload = {
|
@@ -38,38 +34,23 @@ def get_reviews(asin, domain):
|
|
38 |
'parse': True
|
39 |
}
|
40 |
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
41 |
-
|
42 |
-
review_texts = "\n\n".join([review["content"] for review in reviews])
|
43 |
-
|
44 |
-
# Summarize reviews using OpenAI
|
45 |
-
openai_payload = {
|
46 |
-
'prompt': f"Summarize the following reviews:\n\n{review_texts}",
|
47 |
-
'max_tokens': 150
|
48 |
-
}
|
49 |
-
headers = {
|
50 |
-
'Authorization': OPENAI_AUTH,
|
51 |
-
'Content-Type': 'application/json'
|
52 |
-
}
|
53 |
-
response = requests.post(OPENAI_ENDPOINT, headers=headers, json=openai_payload)
|
54 |
-
summary = response.json()["choices"][0]["text"].strip()
|
55 |
-
return summary
|
56 |
-
|
57 |
-
def amazon_combined_interface(asin, domain):
|
58 |
-
product_details = get_product_details(asin, domain)
|
59 |
-
review_summary = get_reviews(asin, domain)
|
60 |
-
|
61 |
-
return product_details, review_summary
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
gr.Dropdown(choices=["com", "uk", "de", "fr", "es", "it", "nl"], label="Select Marketplace")
|
68 |
-
],
|
69 |
-
outputs=[
|
70 |
-
gr.Textbox(label="Product Details"),
|
71 |
-
gr.Textbox(label="Review Summary")
|
72 |
-
]
|
73 |
-
)
|
74 |
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def get_product_details(asin, domain):
|
2 |
payload = {
|
3 |
'source': 'amazon_product',
|
|
|
7 |
'context': [{'key': 'autoselect_variant', 'value': True}]
|
8 |
}
|
9 |
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
10 |
+
data = response.json()
|
11 |
|
12 |
+
# Check if 'results' and 'content' keys are present in the data
|
13 |
+
if 'results' in data and len(data['results']) > 0 and 'content' in data['results'][0]:
|
14 |
+
content = data['results'][0]['content']
|
15 |
+
|
16 |
+
product_info = {
|
17 |
+
"asin": content.get("asin", "N/A"),
|
18 |
+
"title": content.get("title", "N/A"),
|
19 |
+
"brand": content.get("brand", "N/A"),
|
20 |
+
"price": content.get("price", "N/A"),
|
21 |
+
"rating": content.get("rating", "N/A"),
|
22 |
+
"description": content.get("description", "N/A")
|
23 |
+
}
|
24 |
+
|
25 |
+
return json.dumps(product_info, indent=4)
|
26 |
+
else:
|
27 |
+
return "Failed to retrieve product details."
|
28 |
|
29 |
def get_reviews(asin, domain):
|
30 |
payload = {
|
|
|
34 |
'parse': True
|
35 |
}
|
36 |
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
37 |
+
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Check if 'results', 'content', and 'reviews' keys are present in the data
|
40 |
+
if 'results' in data and len(data['results']) > 0 and 'content' in data['results'][0] and 'reviews' in data['results'][0]['content']:
|
41 |
+
reviews = data['results'][0]['content']['reviews']
|
42 |
+
review_texts = "\n\n".join([review["content"] for review in reviews])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
openai_payload = {
|
45 |
+
'prompt': f"Summarize the following reviews:\n\n{review_texts}",
|
46 |
+
'max_tokens': 150
|
47 |
+
}
|
48 |
+
headers = {
|
49 |
+
'Authorization': OPENAI_AUTH,
|
50 |
+
'Content-Type': 'application/json'
|
51 |
+
}
|
52 |
+
response = requests.post(OPENAI_ENDPOINT, headers=headers, json=openai_payload)
|
53 |
+
summary = response.json()["choices"][0]["text"].strip()
|
54 |
+
return summary
|
55 |
+
else:
|
56 |
+
return "Failed to retrieve reviews."
|