Add LLM product comparison using Selenium, OpenAI, and Ollama

This commit is contained in:
Ekta Shukla
2025-06-02 23:43:20 +05:30
parent 5782ca2b43
commit 65db703e2d

View File

@@ -0,0 +1,226 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "38795b24-9801-4cfb-a000-ccd7f41e6128",
"metadata": {},
"source": [
"\n",
"# 🧠 Multi-Product Competitor Intelligence Summarizer using Web Scraping + LLM\n",
"\n",
"This notebook scrapes product pages using `Selenium`, collects the product information, and summarizes key features and comparison insights using `Ollama (LLaMA3) and OpenAI`.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b87cadb-d513-4303-baee-a37b6f938e4d",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"\n",
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abdb8417-c5dc-44bc-9bee-2e059d162699",
"metadata": {},
"outputs": [],
"source": [
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
"\n",
"system_prompt = \"Summarize the following product information for comparison.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38245e18",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 📦 Install required packages (run once)\n",
"!pip install selenium bs4 requests\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88ae528b-aefe-4c64-b927-676e739194af",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4a831a5",
"metadata": {},
"outputs": [],
"source": [
"def summarize_with_openai(text, model=\"gpt-4o-mini\"):\n",
" response = openai.chat.completions.create(\n",
" model=model,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": text}\n",
" ],\n",
" temperature=0.7\n",
" )\n",
" return response.choices[0].message.content\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef65cd72",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# ⚙️ Selenium setup (headless)\n",
"from selenium import webdriver\n",
"from selenium.webdriver.chrome.options import Options\n",
"from selenium.webdriver.common.by import By\n",
"import time\n",
"\n",
"def scrape_text_from_url(url):\n",
" options = Options()\n",
" options.add_argument(\"--headless=new\")\n",
" driver = webdriver.Chrome(options=options)\n",
" driver.get(url)\n",
" time.sleep(3)\n",
" \n",
" # You can tune this selector depending on the site\n",
" body = driver.find_element(By.TAG_NAME, 'body')\n",
" text = body.text\n",
" driver.quit()\n",
" return text.strip()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36e19014",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 🧠 LLM Prompting using Ollama (local llama3)\n",
"import subprocess\n",
"\n",
"def summarize_with_ollama(text):\n",
" prompt = f\"Summarize the following product description:\\n\\n{text}\\n\\nSummary:\"\n",
" try:\n",
" print(\"inside ollama\")\n",
" result = subprocess.run(\n",
" [\"ollama\", \"run\", \"llama3.2\"],\n",
" input=prompt,\n",
" capture_output=True, text=True, check=True, encoding=\"utf-8\"\n",
" )\n",
" print(\"git result\")\n",
" return result.stdout.strip()\n",
" except subprocess.CalledProcessError as e:\n",
" return f\"Error running ollama: {e.stderr}\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e04cea6e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 🔁 Analyze multiple product URLs and summarize\n",
"product_urls = {\n",
" \"iPhone 15 Pro\": \"https://www.apple.com/in/iphone-15-pro/\",\n",
" \"Samsung S24 Ultra\": \"https://www.samsung.com/in/smartphones/galaxy-s24-ultra/\",\n",
"}\n",
"\n",
"product_texts = {}\n",
"\n",
"for name, url in product_urls.items():\n",
" print(f\"Scraping {name} ...\")\n",
" product_texts[name] = scrape_text_from_url(url)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ebd5a20",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# 📄 Display side-by-side summaries\n",
"for name, text in product_texts.items():\n",
" print(f\"\\n🔹 {name} Summary with Ollama:\")\n",
" print(summarize_with_ollama(text))\n",
"\n",
" print(f\"\\n🔹 {name} Summary with OpenAI GPT:\")\n",
" print(summarize_with_openai(text))\n",
" print(\"=\"*100)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "935e0081-ccf5-4d9a-a984-ee82c77c04a2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}