{ "cells": [ { "cell_type": "markdown", "id": "1e45263e", "metadata": {}, "source": [ "# Web Data Extraction and Summarization using openAI Latest model gpt-5-mini" ] }, { "cell_type": "markdown", "id": "df155151", "metadata": {}, "source": [ "#### Import Libraries" ] }, { "cell_type": "code", "execution_count": null, "id": "588f8e43", "metadata": {}, "outputs": [], "source": [ "import os\n", "import requests\n", "from dotenv import load_dotenv\n", "from bs4 import BeautifulSoup\n", "from IPython.display import Markdown, display\n", "from openai import OpenAI " ] }, { "cell_type": "markdown", "id": "b5925769", "metadata": {}, "source": [ "#### load api key" ] }, { "cell_type": "code", "execution_count": null, "id": "6cca85ec", "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "api_key = os.getenv('OPENAI_API_KEY')" ] }, { "cell_type": "markdown", "id": "56703f80", "metadata": {}, "source": [ "#### ScrapWebsite using BeautifulSoup" ] }, { "cell_type": "code", "execution_count": null, "id": "3d60c909", "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 ScrapeWebsite:\n", "\n", " def __init__(self, url):\n", " \"\"\" Scraping Website which provides title and content\"\"\"\n", " self.url = url\n", " response = requests.get(self.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": "markdown", "id": "a8b73c27", "metadata": {}, "source": [ "#### System Prompt" ] }, { "cell_type": "code", "execution_count": null, "id": "4a0c3bda", "metadata": {}, "outputs": [], "source": [ "system_prompt = \"You are an analyst that analyses the content of the website \\\n", " provides summary and ignore text related to navigation. Respond in markdown.\"" ] }, { "cell_type": "markdown", "id": "9117963b", "metadata": {}, "source": [ "#### User Prompt" ] }, { "cell_type": "code", "execution_count": null, "id": "ab164d55", "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; Please provide short summary in Markdown. Please include news and \\\n", " announcements\"\n", " user_prompt+=website.text\n", " return user_prompt" ] }, { "cell_type": "markdown", "id": "de7423fb", "metadata": {}, "source": [ "#### Format messages in openAI standard" ] }, { "cell_type": "code", "execution_count": null, "id": "47c82247", "metadata": {}, "outputs": [], "source": [ "def messages_for(website):\n", " return [\n", " {\"role\":\"system\", \"content\": system_prompt},\n", " {\"role\":\"user\", \"content\": user_prompt_for(website)}\n", " ]" ] }, { "cell_type": "markdown", "id": "6e9bb6e1", "metadata": {}, "source": [ "#### Summarise the content in website using openAI latest model gpt-5-mini" ] }, { "cell_type": "code", "execution_count": null, "id": "068d6bb2", "metadata": {}, "outputs": [], "source": [ "def summarise(url):\n", " website = ScrapeWebsite(url)\n", " openai = OpenAI()\n", " response = openai.chat.completions.create(model=\"gpt-5-mini\", messages=messages_for(website))\n", " return response.choices[0].message.content" ] }, { "cell_type": "markdown", "id": "7e6e9da6", "metadata": {}, "source": [ "#### Show summary as Markdown" ] }, { "cell_type": "code", "execution_count": null, "id": "cd86c2ca", "metadata": {}, "outputs": [], "source": [ "def display_summary(url):\n", " summary = summarise(url)\n", " display(Markdown(summary))" ] }, { "cell_type": "markdown", "id": "ed5e50d2", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "code", "execution_count": null, "id": "74a056b1", "metadata": {}, "outputs": [], "source": [ "display_summary(\"https://www.firstpost.com/world/united-states/\")" ] } ], "metadata": { "kernelspec": { "display_name": "llms", "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 }