antfraia commited on
Commit
9e270ae
·
1 Parent(s): 1118153

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import openai
4
+
5
+ # Set up OpenAI API key
6
+ openai.api_key = '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'
7
+
8
+ def fetch_product_info_and_improve(asin, marketplace):
9
+ params = {
10
+ 'api_key': '5CCE0A4BA6C546C7987E63B21915AA99',
11
+ 'amazon_domain': marketplace,
12
+ 'asin': asin,
13
+ 'type': 'product'
14
+ }
15
+
16
+ api_result = requests.get('https://api.asindataapi.com/request', params)
17
+ data = api_result.json()
18
+
19
+ # Parsing the JSON to extract key details
20
+ product = data.get("product", {})
21
+ product_title = product.get("title", "N/A")
22
+ description = product.get("description", "N/A")
23
+ bullet_points = product.get("feature_bullets_flat", "N/A")
24
+
25
+ original_output = f"Product Title: {product_title}\nDescription: {description}\nBullet Points: {bullet_points}"
26
+
27
+ # Improve the content with OpenAI
28
+ prompt_text = (f"Based on the provided Amazon detail page content, please improve the following:\n"
29
+ f"(i) Product Title: {product_title}\n"
30
+ f"(ii) Product Description: {description}\n"
31
+ f"(iii) Bullet Points: {bullet_points}\n"
32
+ f"\nPlease provide a revised (i) title, (ii) description, and (iii) bullet points.")
33
+
34
+ try:
35
+ response = openai.Completion.create(
36
+ engine="davinci",
37
+ prompt=prompt_text,
38
+ max_tokens=300
39
+ )
40
+ improved_output = response.choices[0].text.strip()
41
+
42
+ except openai.error.OpenAiError:
43
+ improved_output = "Error occurred with OpenAI. Please try again later."
44
+
45
+ return original_output, improved_output
46
+
47
+ # Streamlit App Layout
48
+ st.title('ASIN Product Info Fetcher & Improver')
49
+
50
+ # User input
51
+ asin = st.text_input("Enter ASIN:")
52
+ marketplace = st.selectbox("Select Marketplace", ["amazon.co.uk", "amazon.de", "amazon.fr", "amazon.it", "amazon.es", "amazon.nl"])
53
+
54
+ if st.button('Fetch & Improve'):
55
+ original_output, improved_output = fetch_product_info_and_improve(asin, marketplace)
56
+
57
+ # Display results
58
+ st.subheader("Original Product Info")
59
+ st.write(original_output)
60
+
61
+ st.subheader("Improved Detail Page Content")
62
+ st.write(improved_output)