antfraia commited on
Commit
2e2ac19
·
1 Parent(s): 5660b20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -52
app.py CHANGED
@@ -1,80 +1,75 @@
1
-
2
- import requests
3
  import gradio as gr
4
- import openai
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.request(
22
- 'POST',
23
- 'https://realtime.oxylabs.io/v1/queries',
24
- auth=(oxylabs_user, oxylabs_pass),
25
- json=payload,
26
- )
27
- data = response.json()
28
- return data['asin'], data['title'], data['brand'], data['price'], data['rating'], data['description']
 
 
 
 
 
 
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.request(
38
- 'POST',
39
- 'https://realtime.oxylabs.io/v1/queries',
40
- auth=(oxylabs_user, oxylabs_pass),
41
- json=payload,
42
- )
43
- data = response.json()
44
- reviews = ' '.join([rev['content'] for rev in data['reviews']])
45
- return reviews
46
-
47
- def summarize_reviews(reviews):
48
- prompt = "Summarize the following customer reviews through 5 bullet points:\n" + reviews
49
- response = openai.Completion.create(
50
- engine="text-davinci-003",
51
- prompt=prompt,
52
- max_tokens=1000,
53
- )
54
- return response.choices[0].text.strip()
55
 
56
  def amazon_combined_interface(asin, domain):
57
- product_asin, title, brand, price, rating, description = get_product_details(asin, domain)
58
- reviews = get_reviews(asin, domain)
59
- summarized_reviews = summarize_reviews(reviews)
60
-
61
- return product_asin, title, brand, price, rating, description, summarized_reviews
62
 
63
- iface = 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 ASIN"),
71
- gr.Textbox(label="Title"),
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
- iface.launch()
 
 
 
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()