From 6278179075c21f0efcac41e010a171a6b8debe3f Mon Sep 17 00:00:00 2001 From: Batikan Iscan Date: Sun, 1 Dec 2024 18:21:11 -0500 Subject: [PATCH] adding new contribution for day 2 exercise --- ...2-exercise-ollama-website-summarizer.ipynb | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 week1/community-contributions/day2-exercise-ollama-website-summarizer.ipynb diff --git a/week1/community-contributions/day2-exercise-ollama-website-summarizer.ipynb b/week1/community-contributions/day2-exercise-ollama-website-summarizer.ipynb new file mode 100644 index 0000000..6d51069 --- /dev/null +++ b/week1/community-contributions/day2-exercise-ollama-website-summarizer.ipynb @@ -0,0 +1,97 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "8b1b8557-7551-4b34-97f0-1734505078c7", + "metadata": {}, + "source": [ + "# Hello everyone.\n", + "Here's my solution to the Day 2 Exercise, where I use Ollama (locally hosted) instead of OpenAI gpt-4o-mini to summarize a given website. This code is all in the same block for ease of running (we are all familiar with the process by this point, but guiding comments have been made). Furtnermore, I added a bit of user interactivity by asking the user to provide the website themselves instead of hardcoding a string in memory that the developer changes everytime. Enjoy and have fun, fellow programmers! \n", + "\\- Batikan Iscan" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18cd56af-aacd-4f15-9c8f-e6e141671d10", + "metadata": {}, + "outputs": [], + "source": [ + "# Imports\n", + "import requests\n", + "from bs4 import BeautifulSoup\n", + "from IPython.display import Markdown, display\n", + "import ollama\n", + "\n", + "# Constants\n", + "OLLAMA_API = \"http://localhost:11434/api/chat\"\n", + "HEADERS = {\"Content-Type\": \"application/json\"}\n", + "MODEL = \"llama3.2\"\n", + "\n", + "# Code\n", + "class Website:\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)\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)\n", + "\n", + "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", + "def summarize(url):\n", + " website = Website(url)\n", + " messages = [\n", + " {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", + " ]\n", + " response = ollama.chat(model=MODEL, messages=messages, stream=False)\n", + " # display(Markdown(response.choices[0].message.content))\n", + " display(Markdown(response.message.content))\n", + "\n", + "# User interaction\n", + "user_website = input(\"Enter a website: \")\n", + "summarize(user_website)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73991ad8-9c93-4c38-8daa-f93502b56740", + "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.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}