Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import json
|
|
| 4 |
|
| 5 |
OXYLABS_ENDPOINT = 'https://realtime.oxylabs.io/v1/queries'
|
| 6 |
OXYLABS_AUTH = ('antonces', 'APIusertest23')
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def get_product_details(asin, domain):
|
| 9 |
payload = {
|
|
@@ -24,13 +26,27 @@ def get_reviews(asin, domain):
|
|
| 24 |
'parse': True
|
| 25 |
}
|
| 26 |
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def amazon_combined_interface(asin, domain):
|
| 30 |
product_details = get_product_details(asin, domain)
|
| 31 |
-
|
| 32 |
|
| 33 |
-
return product_details,
|
| 34 |
|
| 35 |
interface = gr.Interface(
|
| 36 |
fn=amazon_combined_interface,
|
|
@@ -40,7 +56,7 @@ interface = gr.Interface(
|
|
| 40 |
],
|
| 41 |
outputs=[
|
| 42 |
gr.Textbox(label="Product Details"),
|
| 43 |
-
gr.Textbox(label="
|
| 44 |
]
|
| 45 |
)
|
| 46 |
|
|
|
|
| 4 |
|
| 5 |
OXYLABS_ENDPOINT = 'https://realtime.oxylabs.io/v1/queries'
|
| 6 |
OXYLABS_AUTH = ('antonces', 'APIusertest23')
|
| 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 = {
|
|
|
|
| 26 |
'parse': True
|
| 27 |
}
|
| 28 |
response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
|
| 29 |
+
reviews = response.json()["results"][0]["content"]["reviews"]
|
| 30 |
+
review_texts = "\n\n".join([review["content"] for review in reviews])
|
| 31 |
+
|
| 32 |
+
# Summarize reviews using OpenAI
|
| 33 |
+
openai_payload = {
|
| 34 |
+
'prompt': f"Summarize the following reviews:\n\n{review_texts}",
|
| 35 |
+
'max_tokens': 150
|
| 36 |
+
}
|
| 37 |
+
headers = {
|
| 38 |
+
'Authorization': OPENAI_AUTH,
|
| 39 |
+
'Content-Type': 'application/json'
|
| 40 |
+
}
|
| 41 |
+
response = requests.post(OPENAI_ENDPOINT, headers=headers, json=openai_payload)
|
| 42 |
+
summary = response.json()["choices"][0]["text"].strip()
|
| 43 |
+
return summary
|
| 44 |
|
| 45 |
def amazon_combined_interface(asin, domain):
|
| 46 |
product_details = get_product_details(asin, domain)
|
| 47 |
+
review_summary = get_reviews(asin, domain)
|
| 48 |
|
| 49 |
+
return product_details, review_summary
|
| 50 |
|
| 51 |
interface = gr.Interface(
|
| 52 |
fn=amazon_combined_interface,
|
|
|
|
| 56 |
],
|
| 57 |
outputs=[
|
| 58 |
gr.Textbox(label="Product Details"),
|
| 59 |
+
gr.Textbox(label="Review Summary")
|
| 60 |
]
|
| 61 |
)
|
| 62 |
|