antfraia commited on
Commit
e1b1541
·
1 Parent(s): f77bc58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -42
app.py CHANGED
@@ -1,10 +1,9 @@
1
- import gradio as gr
2
- import requests
3
- from PIL import Image
4
- from io import BytesIO
5
 
 
6
  OXYLABS_ENDPOINT = 'https://realtime.oxylabs.io/v1/queries'
7
- OXYLABS_AUTH = ('user', 'pass1')
8
 
9
  def get_product_details(asin, domain):
10
  payload = {
@@ -12,63 +11,67 @@ def get_product_details(asin, domain):
12
  'domain': domain,
13
  'query': asin,
14
  'parse': True,
15
- 'context': [{'key': 'autoselect_variant', 'value': True}]
 
 
 
 
16
  }
17
-
18
  response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
19
  product_data = response.json()
20
 
21
- content = product_data.get('results', [{}])[0].get('content', {})
22
- ads = content.get('ads', [{}])[0]
 
 
 
23
 
24
- title = ads.get('title', "")
25
- product_name = title.split(" ")[0] # Taking the first word as product name
26
- bullet_points = title # Assuming bullet points are the title (adjust if needed)
27
- image_url = ads.get('images', [""])[0]
28
-
29
  return title, product_name, bullet_points, image_url
30
 
 
31
  def get_reviews(asin, domain):
32
  payload = {
33
  'source': 'amazon_reviews',
34
  'domain': domain,
35
  'query': asin,
36
- 'parse': True
37
  }
38
-
39
  response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
40
  review_data = response.json()
41
 
42
- reviews = review_data.get('results', [{}])[0].get('content', {}).get('reviews', [])
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- return reviews
 
45
 
46
- def fetch_image_from_url(url):
47
- response = requests.get(url)
48
- return Image.open(BytesIO(response.content))
49
 
50
  def amazon_combined_interface(asin, domain):
51
  title, product_name, bullet_points, image_url = get_product_details(asin, domain)
52
  reviews = get_reviews(asin, domain)
53
 
54
- reviews_summary = "\n\n".join([f"Title: {review['title']}\nAuthor: {review['author']}\nRating: {review['rating']}\nContent: {review['content']}" for review in reviews])
55
- image = fetch_image_from_url(image_url)
56
-
57
- return title, product_name, bullet_points, image, reviews_summary
58
-
59
- interface = gr.Interface(
60
- fn=amazon_combined_interface,
61
- inputs=[
62
- gr.Textbox(label="Enter the ASIN"),
63
- gr.Dropdown(choices=["com", "uk", "de", "fr", "es", "it", "nl"], label="Select Marketplace")
64
- ],
65
- outputs=[
66
- gr.Textbox(label="Title"),
67
- gr.Textbox(label="Product Name"),
68
- gr.Textbox(label="Bullet Points"),
69
- gr.Image(label="Product Image", type="pil"),
70
- gr.Textbox(label="Customer Reviews")
71
- ]
72
- )
73
-
74
- interface.launch()
 
1
+ # Your OpenAI API key
2
+ OPENAI_API_KEY = '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'
 
 
3
 
4
+ # Constants for Oxylabs
5
  OXYLABS_ENDPOINT = 'https://realtime.oxylabs.io/v1/queries'
6
+ OXYLABS_AUTH = ('antonces', 'APIusertest23') # Replace 'user' and 'pass1' with your credentials
7
 
8
  def get_product_details(asin, domain):
9
  payload = {
 
11
  'domain': domain,
12
  'query': asin,
13
  'parse': True,
14
+ 'context': [{'key': 'autoselect_variant', 'value': True}],
15
+
16
+
17
+
18
+
19
  }
 
20
  response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
21
  product_data = response.json()
22
 
23
+ # Extract relevant data. This might need changes based on the exact structure of the response.
24
+ title = product_data['results'][0]['title']
25
+ product_name = product_data['results'][0]['product_name']
26
+ bullet_points = product_data['results'][0]['bullet_points']
27
+ image_url = product_data['results'][0]['images'][0]
28
 
 
 
 
 
 
29
  return title, product_name, bullet_points, image_url
30
 
31
+
32
  def get_reviews(asin, domain):
33
  payload = {
34
  'source': 'amazon_reviews',
35
  'domain': domain,
36
  'query': asin,
37
+ 'parse': True,
38
  }
 
39
  response = requests.post(OXYLABS_ENDPOINT, auth=OXYLABS_AUTH, json=payload)
40
  review_data = response.json()
41
 
42
+ # Assuming 'reviews' is a key in the returned JSON. This might need changes based on the exact structure of the response.
43
+ return review_data['reviews']
44
+
45
+ def get_reviews_summary(reviews):
46
+ headers = {
47
+ 'Authorization': f'Bearer {OPENAI_API_KEY}',
48
+ 'Content-Type': 'application/json',
49
+ }
50
+
51
+ data = {
52
+ 'prompt': f"Summarize the following customer reviews in bullet points:\n\n{reviews}",
53
+ 'max_tokens': 150
54
+ }
55
 
56
+ response = requests.post('https://api.openai.com/v1/engines/davinci/completions', headers=headers, json=data)
57
+ summary = response.json()['choices'][0]['text'].strip()
58
 
59
+ return summary
 
 
60
 
61
  def amazon_combined_interface(asin, domain):
62
  title, product_name, bullet_points, image_url = get_product_details(asin, domain)
63
  reviews = get_reviews(asin, domain)
64
 
65
+
66
+
67
+
68
+
69
+
70
+
71
+ # Summarize reviews using OpenAI
72
+ summarized_reviews = get_reviews_summary(reviews)
73
+
74
+ return title, product_name, '\n'.join(bullet_points), image_url, summarized_reviews
75
+
76
+ # Gradio interface setup
77
+ iface = gr.Interface(