Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,55 +4,64 @@ import requests
|
|
| 4 |
# Your OpenAI API key
|
| 5 |
OPENAI_API_KEY = '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'
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
| 8 |
def get_product_details(asin, domain):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
'
|
| 12 |
-
'
|
| 13 |
-
'
|
| 14 |
-
|
| 15 |
-
'New 2020 release',
|
| 16 |
-
'1,023 Pieces',
|
| 17 |
-
],
|
| 18 |
-
'images': ['https://m.media-amazon.com/images/I/51-e5UA3mEL._AC_SR160,160_.jpg']
|
| 19 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Mockup function for getting reviews
|
| 22 |
def get_reviews(asin, domain):
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def get_reviews_summary(reviews):
|
| 27 |
headers = {
|
| 28 |
'Authorization': f'Bearer {OPENAI_API_KEY}',
|
| 29 |
'Content-Type': 'application/json',
|
| 30 |
}
|
| 31 |
-
|
| 32 |
data = {
|
| 33 |
'prompt': f"Summarize the following customer reviews in bullet points:\n\n{reviews}",
|
| 34 |
'max_tokens': 150
|
| 35 |
}
|
| 36 |
-
|
| 37 |
response = requests.post('https://api.openai.com/v1/engines/davinci/completions', headers=headers, json=data)
|
| 38 |
summary = response.json()['choices'][0]['text'].strip()
|
| 39 |
|
| 40 |
return summary
|
| 41 |
|
| 42 |
def amazon_combined_interface(asin, domain):
|
| 43 |
-
|
| 44 |
reviews = get_reviews(asin, domain)
|
| 45 |
|
| 46 |
-
# Extract required fields from the product details
|
| 47 |
-
title = product_details['title']
|
| 48 |
-
product_name = product_details['product_name']
|
| 49 |
-
bullet_points = '\n'.join(product_details['bullet_points'])
|
| 50 |
-
image_url = product_details['images'][0]
|
| 51 |
-
|
| 52 |
# Summarize reviews using OpenAI
|
| 53 |
summarized_reviews = get_reviews_summary(reviews)
|
| 54 |
|
| 55 |
-
return title, product_name, bullet_points, image_url, summarized_reviews
|
| 56 |
|
| 57 |
# Gradio interface setup
|
| 58 |
iface = gr.Interface(
|
|
|
|
| 4 |
# Your OpenAI API key
|
| 5 |
OPENAI_API_KEY = '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'
|
| 6 |
|
| 7 |
+
# Constants for Oxylabs
|
| 8 |
+
OXYLABS_ENDPOINT = 'https://realtime.oxylabs.io/v1/queries'
|
| 9 |
+
OXYLABS_AUTH = ('antonces', 'APIusertest23') # Replace 'user' and 'pass1' with your credentials
|
| 10 |
+
|
| 11 |
def get_product_details(asin, domain):
|
| 12 |
+
payload = {
|
| 13 |
+
'source': 'amazon_product',
|
| 14 |
+
'domain': domain,
|
| 15 |
+
'query': asin,
|
| 16 |
+
'parse': True,
|
| 17 |
+
'context': [{'key': 'autoselect_variant', 'value': True}],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
+
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
| 20 |
+
product_data = response.json()
|
| 21 |
+
|
| 22 |
+
# Extract relevant data. This might need changes based on the exact structure of the response.
|
| 23 |
+
title = product_data['results'][0]['title']
|
| 24 |
+
product_name = product_data['results'][0]['product_name']
|
| 25 |
+
bullet_points = product_data['results'][0]['bullet_points']
|
| 26 |
+
image_url = product_data['results'][0]['images'][0]
|
| 27 |
+
|
| 28 |
+
return title, product_name, bullet_points, image_url
|
| 29 |
|
|
|
|
| 30 |
def get_reviews(asin, domain):
|
| 31 |
+
payload = {
|
| 32 |
+
'source': 'amazon_reviews',
|
| 33 |
+
'domain': domain,
|
| 34 |
+
'query': asin,
|
| 35 |
+
'parse': True,
|
| 36 |
+
}
|
| 37 |
+
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
| 38 |
+
review_data = response.json()
|
| 39 |
+
|
| 40 |
+
# Assuming 'reviews' is a key in the returned JSON. This might need changes based on the exact structure of the response.
|
| 41 |
+
return review_data['reviews']
|
| 42 |
|
| 43 |
def get_reviews_summary(reviews):
|
| 44 |
headers = {
|
| 45 |
'Authorization': f'Bearer {OPENAI_API_KEY}',
|
| 46 |
'Content-Type': 'application/json',
|
| 47 |
}
|
|
|
|
| 48 |
data = {
|
| 49 |
'prompt': f"Summarize the following customer reviews in bullet points:\n\n{reviews}",
|
| 50 |
'max_tokens': 150
|
| 51 |
}
|
|
|
|
| 52 |
response = requests.post('https://api.openai.com/v1/engines/davinci/completions', headers=headers, json=data)
|
| 53 |
summary = response.json()['choices'][0]['text'].strip()
|
| 54 |
|
| 55 |
return summary
|
| 56 |
|
| 57 |
def amazon_combined_interface(asin, domain):
|
| 58 |
+
title, product_name, bullet_points, image_url = get_product_details(asin, domain)
|
| 59 |
reviews = get_reviews(asin, domain)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Summarize reviews using OpenAI
|
| 62 |
summarized_reviews = get_reviews_summary(reviews)
|
| 63 |
|
| 64 |
+
return title, product_name, '\n'.join(bullet_points), image_url, summarized_reviews
|
| 65 |
|
| 66 |
# Gradio interface setup
|
| 67 |
iface = gr.Interface(
|