192 lines
5.0 KiB
Plaintext
192 lines
5.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "786b2ed1-f82e-4ca4-8113-c4515b36e970",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Day 2 Exercise | Website Summarizer with Llama 3.2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b88bf233-29e0-4c01-a4da-8a16896a95e3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import requests\n",
|
|
"from bs4 import BeautifulSoup\n",
|
|
"from IPython.display import Markdown, display"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f66f620e-ebf6-45d3-a710-2bb931cac841",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 1. Scraping info from website:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4e300303-02ac-4d60-9c8c-044a4627be9e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"headers = {\n",
|
|
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
|
|
"}\n",
|
|
"\n",
|
|
"class Website:\n",
|
|
"\n",
|
|
" def __init__(self, url):\n",
|
|
" \"\"\"\n",
|
|
" Create this Website object from the given url using the BeautifulSoup library\n",
|
|
" \"\"\"\n",
|
|
" self.url = url\n",
|
|
" response = requests.get(url, headers=headers)\n",
|
|
" soup = BeautifulSoup(response.content, 'html.parser')\n",
|
|
" self.title = soup.title.string if soup.title else \"No title found\"\n",
|
|
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
|
|
" irrelevant.decompose()\n",
|
|
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "137714b9-24eb-4541-8f24-507dbcd09279",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"ed = Website(\"https://edwarddonner.com\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "77ba1b4b-fc4c-4e3c-bef7-c4d4281d8263",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 2. Ollama configuration:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "97811fcb-1ceb-49a8-bfb9-2e610605c406",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
|
|
"HEADERS = {\"Content-Type\": \"application/json\"}\n",
|
|
"MODEL = \"llama3.2\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "392326b8-ad0f-4bc9-b055-6220f8bcc57c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def user_prompt_for(website):\n",
|
|
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
|
|
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
|
|
"please provide a short summary of this website in markdown. \\\n",
|
|
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
|
|
" user_prompt += website.text\n",
|
|
" return user_prompt\n",
|
|
"\n",
|
|
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
|
|
"and provides a short summary, ignoring text that might be navigation related. \\\n",
|
|
"Respond in markdown.\"\n",
|
|
"user_prompt = user_prompt_for(ed)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8caa94ff-5ace-4f9b-b2f0-beb6ff550636",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"messages = [\n",
|
|
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
|
" {\"role\": \"user\", \"content\": user_prompt}\n",
|
|
"]\n",
|
|
"\n",
|
|
"payload = {\n",
|
|
" \"model\": MODEL,\n",
|
|
" \"messages\": messages,\n",
|
|
" \"stream\": False\n",
|
|
"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f5f856bc-0437-4607-9204-5390d2dfd8db",
|
|
"metadata": {},
|
|
"source": [
|
|
"### 3. Get & display summary:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a7fd6f93-92ae-419f-b8b6-ee8214e0d93f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n",
|
|
"summary = response.json()['message']['content']"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "78e4a433-b974-463f-82d0-b4696c63e0ab",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def display_summary(summary_text: str):\n",
|
|
" cleaned = summary_text.encode('utf-8').decode('unicode_escape')\n",
|
|
" cleaned = cleaned.strip()\n",
|
|
" display(Markdown(cleaned))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dc408f1d-fe26-4bd6-859f-d18118f74ca6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"display_summary(summary)"
|
|
]
|
|
}
|
|
],
|
|
"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.13"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|