Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,75 @@
|
|
| 1 |
-
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# Oxylabs credentials
|
| 7 |
-
oxylabs_user = 'antonces'
|
| 8 |
-
oxylabs_pass = 'APIusertest23'
|
| 9 |
-
|
| 10 |
-
# OpenAI API Key
|
| 11 |
-
openai.api_key = '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'
|
| 12 |
-
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def get_product_details(asin, domain):
|
| 16 |
payload = {
|
|
|
|
| 17 |
'domain': domain,
|
| 18 |
'query': asin,
|
| 19 |
'parse': True,
|
|
|
|
| 20 |
}
|
| 21 |
-
response = requests.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
)
|
| 54 |
-
return response.choices[0].text.strip()
|
| 55 |
|
| 56 |
def amazon_combined_interface(asin, domain):
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
return product_asin, title, brand, price, rating, description, summarized_reviews
|
| 62 |
|
| 63 |
-
|
| 64 |
fn=amazon_combined_interface,
|
| 65 |
inputs=[
|
| 66 |
gr.Textbox(label="Enter the ASIN"),
|
| 67 |
gr.Dropdown(choices=["com", "uk", "de", "fr", "es", "it", "nl"], label="Select Marketplace")
|
| 68 |
],
|
| 69 |
outputs=[
|
| 70 |
-
gr.Textbox(label="Product
|
| 71 |
-
gr.Textbox(label="
|
| 72 |
-
gr.Textbox(label="Brand"),
|
| 73 |
-
gr.Textbox(label="Price"),
|
| 74 |
-
gr.Textbox(label="Rating"),
|
| 75 |
-
gr.Textbox(label="Description"),
|
| 76 |
-
gr.Textbox(label="Summarized Reviews")
|
| 77 |
]
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 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',
|
| 13 |
'domain': domain,
|
| 14 |
'query': asin,
|
| 15 |
'parse': True,
|
| 16 |
+
'context': [{'key': 'autoselect_variant', 'value': True}]
|
| 17 |
}
|
| 18 |
+
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
| 19 |
+
data = response.json()["results"][0]["content"]
|
| 20 |
+
|
| 21 |
+
# Isolate information referring to the input ASIN
|
| 22 |
+
product_info = {
|
| 23 |
+
"asin": data.get("asin", "N/A"),
|
| 24 |
+
"title": data.get("title", "N/A"),
|
| 25 |
+
"brand": data.get("brand", "N/A"),
|
| 26 |
+
"price": data.get("price", "N/A"),
|
| 27 |
+
"rating": data.get("rating", "N/A"),
|
| 28 |
+
"description": data.get("description", "N/A")
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
return json.dumps(product_info, indent=4)
|
| 32 |
|
| 33 |
def get_reviews(asin, domain):
|
| 34 |
payload = {
|
| 35 |
'source': 'amazon_reviews',
|
| 36 |
'domain': domain,
|
| 37 |
'query': asin,
|
| 38 |
+
'parse': True
|
| 39 |
}
|
| 40 |
+
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
| 41 |
+
reviews = response.json()["results"][0]["content"]["reviews"]
|
| 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 |
+
interface = gr.Interface(
|
| 64 |
fn=amazon_combined_interface,
|
| 65 |
inputs=[
|
| 66 |
gr.Textbox(label="Enter the ASIN"),
|
| 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 |
+
interface.launch()
|