diff --git a/scrape_amazon.py b/scrape_amazon.py index 64b3839..59b4aae 100644 --- a/scrape_amazon.py +++ b/scrape_amazon.py @@ -100,7 +100,7 @@ class AmazonScraper: print(review_data) print(product_info) print(product_info['images'], type(product_info['images'])) - self.images = eval(product_info['images']).keys() + self.images = eval(product_info['images']) print(self.images) diff --git a/webui.py b/webui.py index 4b90610..fff442a 100644 --- a/webui.py +++ b/webui.py @@ -1,25 +1,49 @@ import gradio as gr from scrape_amazon import AmazonScraper, AIInterface +import re + llms = ['meta-llama/Llama-2-70b-chat-hf', "mistralai/Mixtral-8x7B-Instruct-v0.1", "togethercomputer/LLaMA-2-7B-32K"] scraper = AmazonScraper() aii = AIInterface() +def replace_img_tag(html_text, img_dict): + # Regular expression pattern to find tags + img_tag_pattern = r'' + + # Function to replace tag + def replacer(match): + img_url = match.group(1) + srcset = ', '.join(f'{url} {w}w' for url, (w, h) in img_dict.items()) + replacement = f'''
+
+ Image from Amazon.com +
Image from Amazon.com
+
+
''' + return replacement + + # Use re.sub() to replace tags + new_html_text = re.sub(img_tag_pattern, replacer, html_text) + + return new_html_text + def write_article(url, ai_prompt): # Your logic to fetch HTML content from the URL # Replace this with your actual implementation text = scraper.get_product_info_and_reviews(url) - images = list(scraper.images)[0] - prompt_for_ai = "Write a summary of the following product and an overview of people's experiences based on the provided reviews of it as follows. Format it nicely and professionally in HTML:\n\n" + text - # prompt_for_ai = f"Write a summary of the following product and an overview of people's experiences based on the provided reviews of it as follows. Format it nicely and professionally in HTML. The title of this product should links to {url}. Also include this image {images} after the first or second paragraph as a link to {url} and
Image from Amazon.com
:\n\n" + text + image = list(scraper.images)[0] + # prompt_for_ai = "Write a summary of the following product and an overview of people's experiences based on the provided reviews of it as follows. Format it nicely and professionally in HTML:\n\n" + text + prompt_for_ai = f"Write an HTML code that includes summary of the following product and an overview of people's experiences based on the provided reviews of it as follows. Underneath the title in small letters write 'This page includes paid Amazon affiliate links' and Include a link to the product {url} at the very end. Also include this image {image} after the second paragraph. Format it nicely and professionally in HTML. :\n\n" + text ai_response = aii.ask_ai(prompt_for_ai, model=llms[1]) print(ai_response) html_content = ai_response - prompt_for_ai = f"Take the following HTML code and slightly modify it by converting the names of this product to links to {url}. Also include this image {images} after the first or second paragraph as a link to {url} and caption it with
Image from Amazon.com
. Return a nice and professional HTML code:\n" + ai_response - html_content = aii.ask_ai(prompt_for_ai, model=llms[1]) + # prompt_for_ai = f"Take the following HTML code and slightly modify it by converting the names of this product to links to {url}. Also include this image {image} after the first or second paragraph. Return a nice and professional HTML code:\n" + ai_response + # html_content = aii.ask_ai(prompt_for_ai, model=llms[1]) + html_content = replace_img_tag(html_content, scraper.images) print(html_content) return html_content