|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "c97ad592-c8be-4583-a19c-ac813e56f410", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Mac Users\n", |
| 9 | + "\n", |
| 10 | + "I find some challenges while setting up this in MAC silicon M1 chip. Execute below commands in MAC terminal.\n", |
| 11 | + "\n", |
| 12 | + "1. Download chromedriver.\n", |
| 13 | + "2. Unzip and add it to the path.\n", |
| 14 | + "3. Set Extended attributes." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "id": "b635b345-b000-48cc-8a7f-7df279a489a3", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "cd ~/Downloads\n", |
| 23 | + "wget https://storage.googleapis.com/chrome-for-testing-public/133.0.6943.126/mac-arm64/chromedriver-mac-arm64.zip\n", |
| 24 | + "unzip chromedriver-mac-arm64.zip\n", |
| 25 | + "sudo mv chromedriver-mac-arm64/chromedriver /usr/local/bin/\n", |
| 26 | + "chmod +x /usr/local/bin/chromedriver\n", |
| 27 | + "cd /usr/local/bin/\n", |
| 28 | + "xattr -d com.apple.quarantine chromedriver\n", |
| 29 | + "cd \n", |
| 30 | + "chromedriver --version" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "id": "17c7c79a-8ae0-4f5d-a7c8-c54aa7ba90fd", |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "!pip install selenium\n", |
| 41 | + "!pip install undetected-chromedriver\n", |
| 42 | + "!pip install beautifulsoup4" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "id": "c10bd630-2dfd-4572-8c21-2dc4c6a372ab", |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "from selenium import webdriver\n", |
| 53 | + "from selenium.webdriver.chrome.service import Service\n", |
| 54 | + "from selenium.webdriver.common.by import By\n", |
| 55 | + "from selenium.webdriver.chrome.options import Options\n", |
| 56 | + "from openai import OpenAI\n", |
| 57 | + "import os\n", |
| 58 | + "import requests\n", |
| 59 | + "from dotenv import load_dotenv\n", |
| 60 | + "from bs4 import BeautifulSoup\n", |
| 61 | + "from IPython.display import Markdown, display\n", |
| 62 | + "from openai import OpenAI" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "id": "6fb3641d-e9f8-4f5b-bb9d-ee0e971cccdb", |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "OLLAMA_API = \"http://localhost:11434/api/chat\"\n", |
| 73 | + "HEADERS = {\"Content-Type\": \"application/json\"}\n", |
| 74 | + "MODEL = \"llama3.2\"\n", |
| 75 | + "PATH_TO_CHROME_DRIVER = '/usr/local/bin/chromedriver'\n", |
| 76 | + "system_prompt = \"You are an assistant that analyzes the contents of a website \\\n", |
| 77 | + "and provides a short summary, ignoring text that might be navigation related. \\\n", |
| 78 | + "Respond in markdown. Highlight all the products this website offered and also find when website is created.\"\n" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "id": "5d57e958", |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [], |
| 87 | + "source": [ |
| 88 | + "class Website:\n", |
| 89 | + " url: str\n", |
| 90 | + " title: str\n", |
| 91 | + " text: str\n", |
| 92 | + "\n", |
| 93 | + " def __init__(self, url):\n", |
| 94 | + " self.url = url\n", |
| 95 | + "\n", |
| 96 | + " options = Options()\n", |
| 97 | + "\n", |
| 98 | + " options.add_argument(\"--no-sandbox\")\n", |
| 99 | + " options.add_argument(\"--disable-dev-shm-usage\")\n", |
| 100 | + "\n", |
| 101 | + " service = Service(PATH_TO_CHROME_DRIVER)\n", |
| 102 | + " driver = webdriver.Chrome(service=service, options=options)\n", |
| 103 | + " driver.get(url)\n", |
| 104 | + "\n", |
| 105 | + " # input(\"Please complete the verification in the browser and press Enter to continue...\")\n", |
| 106 | + " page_source = driver.page_source\n", |
| 107 | + " driver.quit()\n", |
| 108 | + "\n", |
| 109 | + " soup = BeautifulSoup(page_source, 'html.parser')\n", |
| 110 | + " self.title = soup.title.string if soup.title else \"No title found\"\n", |
| 111 | + " for irrelevant in soup([\"script\", \"style\", \"img\", \"input\"]):\n", |
| 112 | + " irrelevant.decompose()\n", |
| 113 | + " self.text = soup.get_text(separator=\"\\n\", strip=True)" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": null, |
| 119 | + "id": "56df8cd2-2707-43f6-a066-3367846929b3", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "def user_prompt_for(website):\n", |
| 124 | + " user_prompt = f\"You are looking at a website titled {website.title}\"\n", |
| 125 | + " user_prompt += \"\\nThe contents of this website is as follows; \\\n", |
| 126 | + "please provide a short summary of this website in markdown. \\\n", |
| 127 | + "If it includes news or announcements, then summarize these too.\\n\\n\"\n", |
| 128 | + " user_prompt += website.text\n", |
| 129 | + " return user_prompt\n", |
| 130 | + "\n", |
| 131 | + "\n", |
| 132 | + "\n", |
| 133 | + "def messages_for(website):\n", |
| 134 | + " return [\n", |
| 135 | + " {\"role\": \"system\", \"content\": system_prompt},\n", |
| 136 | + " {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", |
| 137 | + " ]\n", |
| 138 | + "\n", |
| 139 | + "\n", |
| 140 | + "def summarize(url):\n", |
| 141 | + " website = Website(url)\n", |
| 142 | + " ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n", |
| 143 | + " response = ollama_via_openai.chat.completions.create(\n", |
| 144 | + " model=MODEL,\n", |
| 145 | + " messages = messages_for(website)\n", |
| 146 | + " )\n", |
| 147 | + " return response.choices[0].message.content\n", |
| 148 | + "\n", |
| 149 | + "\n", |
| 150 | + "def display_summary(url):\n", |
| 151 | + " summary = summarize(url)\n", |
| 152 | + " display(Markdown(summary))" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "id": "f2eb9599", |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "display_summary(\"https://ae.almosafer.com\")" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "id": "31b66c0f-6b45-4986-b77c-758625945a91", |
| 169 | + "metadata": {}, |
| 170 | + "outputs": [], |
| 171 | + "source": [] |
| 172 | + } |
| 173 | + ], |
| 174 | + "metadata": { |
| 175 | + "kernelspec": { |
| 176 | + "display_name": "Python 3 (ipykernel)", |
| 177 | + "language": "python", |
| 178 | + "name": "python3" |
| 179 | + }, |
| 180 | + "language_info": { |
| 181 | + "codemirror_mode": { |
| 182 | + "name": "ipython", |
| 183 | + "version": 3 |
| 184 | + }, |
| 185 | + "file_extension": ".py", |
| 186 | + "mimetype": "text/x-python", |
| 187 | + "name": "python", |
| 188 | + "nbconvert_exporter": "python", |
| 189 | + "pygments_lexer": "ipython3", |
| 190 | + "version": "3.11.11" |
| 191 | + } |
| 192 | + }, |
| 193 | + "nbformat": 4, |
| 194 | + "nbformat_minor": 5 |
| 195 | +} |
0 commit comments