Update amazon_script.py
Browse files- amazon_script.py +49 -36
amazon_script.py
CHANGED
|
@@ -4,6 +4,7 @@ import json
|
|
| 4 |
from urllib.parse import urlparse
|
| 5 |
import asyncio
|
| 6 |
import edge_tts
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
# Load environment variables
|
|
@@ -18,27 +19,24 @@ def extract_asin_from_amazon_url(url):
|
|
| 18 |
return path_parts[i + 1]
|
| 19 |
return None
|
| 20 |
|
| 21 |
-
def get_product_details(asin):
|
| 22 |
-
"""Fetches product details using the
|
| 23 |
-
url =
|
| 24 |
-
|
| 25 |
-
headers = {
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
else:
|
| 32 |
-
print(f"Error: {response.status_code}, {response.text}")
|
| 33 |
-
return None
|
| 34 |
|
| 35 |
def save_product_data(product_data, file_path='data.json'):
|
| 36 |
"""Saves extracted product data to a JSON file."""
|
| 37 |
extracted_data = {
|
| 38 |
-
"title": product_data
|
| 39 |
-
"description": product_data
|
| 40 |
-
"price": product_data
|
| 41 |
-
"images": product_data
|
| 42 |
}
|
| 43 |
with open(file_path, 'w') as json_file:
|
| 44 |
json.dump(extracted_data, json_file, indent=4)
|
|
@@ -60,24 +58,39 @@ def load_data_from_json(file_path='data.json'):
|
|
| 60 |
return "", ""
|
| 61 |
|
| 62 |
def generate_ad_script(title, description, language):
|
| 63 |
-
"""Generates an ad script using the
|
| 64 |
-
|
| 65 |
-
prompt = f"""Create a short advertisement script for
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
return {"ad_script": "Error generating ad script"}
|
| 82 |
|
| 83 |
def save_ad_script(ad_script, file_path='ad_script.txt'):
|
|
@@ -94,4 +107,4 @@ async def list_voices():
|
|
| 94 |
async def text_to_speech(voice, text, output_file):
|
| 95 |
"""Converts text to speech using the selected voice."""
|
| 96 |
communicate = edge_tts.Communicate(text, voice)
|
| 97 |
-
await communicate.save(output_file)
|
|
|
|
| 4 |
from urllib.parse import urlparse
|
| 5 |
import asyncio
|
| 6 |
import edge_tts
|
| 7 |
+
from gradio_client import Client
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
# Load environment variables
|
|
|
|
| 19 |
return path_parts[i + 1]
|
| 20 |
return None
|
| 21 |
|
| 22 |
+
def get_product_details(asin, country="US"):
|
| 23 |
+
"""Fetches product details using the RapidAPI."""
|
| 24 |
+
url = os.getenv('RAPIDAPI_URL')
|
| 25 |
+
querystring = {"asin": asin, "country": country}
|
| 26 |
+
headers = {
|
| 27 |
+
"x-rapidapi-key": os.getenv('RAPIDAPI_KEY'),
|
| 28 |
+
"x-rapidapi-host": os.getenv('RAPIDAPI_HOST')
|
| 29 |
+
}
|
| 30 |
+
response = requests.get(url, headers=headers, params=querystring)
|
| 31 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def save_product_data(product_data, file_path='data.json'):
|
| 34 |
"""Saves extracted product data to a JSON file."""
|
| 35 |
extracted_data = {
|
| 36 |
+
"title": product_data["data"]["product_title"],
|
| 37 |
+
"description": product_data["data"]["product_description"],
|
| 38 |
+
"price": product_data["data"]["product_price"],
|
| 39 |
+
"images": product_data["data"]["product_photos"]
|
| 40 |
}
|
| 41 |
with open(file_path, 'w') as json_file:
|
| 42 |
json.dump(extracted_data, json_file, indent=4)
|
|
|
|
| 58 |
return "", ""
|
| 59 |
|
| 60 |
def generate_ad_script(title, description, language):
|
| 61 |
+
"""Generates an ad script using the Groq API."""
|
| 62 |
+
client = Client(os.getenv('GROQ_API_URL'))
|
| 63 |
+
prompt = f"""Create a short advertisement script for:
|
| 64 |
+
Title: {title}
|
| 65 |
+
Description: {description}
|
| 66 |
+
Language: {language}
|
| 67 |
+
Provide ONLY a JSON response in this exact format:
|
| 68 |
+
{{
|
| 69 |
+
"ad_script": "2 to 3 lines ad script here in the requested language"
|
| 70 |
+
}}"""
|
| 71 |
+
|
| 72 |
+
result = client.predict(
|
| 73 |
+
message=prompt,
|
| 74 |
+
request="llama3-70b-8192",
|
| 75 |
+
param_3=0.5,
|
| 76 |
+
param_4=8100,
|
| 77 |
+
param_5=0.5,
|
| 78 |
+
param_6=0,
|
| 79 |
+
api_name="/chat"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
start_idx = result.find('{')
|
| 84 |
+
end_idx = result.rfind('}') + 1
|
| 85 |
+
|
| 86 |
+
if start_idx != -1 and end_idx != -1:
|
| 87 |
+
json_str = result[start_idx:end_idx]
|
| 88 |
+
return json.loads(json_str)
|
| 89 |
+
else:
|
| 90 |
+
raise ValueError("No JSON object found in response")
|
| 91 |
+
except (json.JSONDecodeError, ValueError) as e:
|
| 92 |
+
print(f"Error processing API response: {e}")
|
| 93 |
+
print("Raw response:", result)
|
| 94 |
return {"ad_script": "Error generating ad script"}
|
| 95 |
|
| 96 |
def save_ad_script(ad_script, file_path='ad_script.txt'):
|
|
|
|
| 107 |
async def text_to_speech(voice, text, output_file):
|
| 108 |
"""Converts text to speech using the selected voice."""
|
| 109 |
communicate = edge_tts.Communicate(text, voice)
|
| 110 |
+
await communicate.save(output_file)
|