From 9b85a94833d21ea0c9d53ea92170ace7dba01fd9 Mon Sep 17 00:00:00 2001 From: Jayapal Sahadevan Date: Wed, 28 May 2025 23:05:04 +0530 Subject: [PATCH 1/3] Added my contributions to community-contributions --- ...k1-day1-ollama-webpage-summarization.ipynb | 191 ++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb diff --git a/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb b/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb new file mode 100644 index 0000000..27aaabb --- /dev/null +++ b/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb @@ -0,0 +1,191 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "4dabb31c-a584-4715-9714-9fc9978c3cb5", + "metadata": {}, + "outputs": [], + "source": [ + "#Get IPL best team" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "3bb88086-ea9c-4766-9baf-a57bb69c3202", + "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\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9dc24243-d20a-48aa-b90b-26ef90233e22", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "API key found and looks good so far!\n" + ] + } + ], + "source": [ + "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": 14, + "id": "cb35e3d1-8733-4931-8744-9c3754793161", + "metadata": {}, + "outputs": [], + "source": [ + "openai = OpenAI()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "63d62eb3-3255-4046-863e-d866a833d1a6", + "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", + " def __init__(self, url):\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": 17, + "id": "409a70a6-331a-4ea4-ab8d-7a46fffc70d7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How about \"The A-Team 2.0\"? Because clearly, you’re aiming for a sequel that’s already better than the original. Or maybe \"The Not-So-Secret League of Awesome\"? That one’s a real conversation starter! What vibe are you going for?\n", + "[{'role': 'system', 'content': 'You are an assistant that analyzes the contents of a cric info website and provides a short summary of best team in IPL. Respond in markdown.'}, {'role': 'user', 'content': '\\n Get page title\\n'}]\n" + ] + }, + { + "data": { + "text/markdown": [ + "# Best Team in IPL History\n", + "\n", + "The Indian Premier League (IPL) has seen various teams competing for the title since its inception in 2008. Some of the most successful teams in IPL history include:\n", + "\n", + "1. **Mumbai Indians**: They have clinched the IPL trophy a record five times (2013, 2015, 2017, 2019, 2020) and are known for their strong squad and strategic gameplay.\n", + "\n", + "2. **Chennai Super Kings**: With four titles (2010, 2011, 2018, 2021), the CSK has consistently been one of the top teams, led by the experienced MS Dhoni.\n", + "\n", + "3. **Kolkata Knight Riders**: They have won the championship twice (2012, 2014) and are recognized for their fan base and competitive spirit.\n", + "\n", + "Overall, the Mumbai Indians are often considered the best team in IPL history due to their multiple championships and consistent performance over the years." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Step 1: Create your prompts\n", + "system_prompt = \"You are an assistant that analyzes the contents of a cric info website \\\n", + "and provides a short summary of best team in IPL. \\\n", + "Respond in markdown.\"\n", + "\n", + "user_prompt = \"\"\"\n", + " Get page title\n", + "\"\"\"\n", + "\n", + "# Step 2: Make the messages list\n", + "messages = [\n", + " {\"role\": \"system\", \"content\": \"You are a snarky assistant\"},\n", + " {\"role\": \"user\", \"content\": \"Team name\"}\n", + "]\n", + "\n", + "# Step 3: Call OpenAI\n", + "\n", + "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", + "print(response.choices[0].message.content)\n", + "\n", + "def messages_for(website):\n", + " return [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}]\n", + "\n", + "webUrl = \"https://www.google.com\"\n", + "print(messages_for(webUrl))\n", + "\n", + "def summarize(url):\n", + " website = Website(url)\n", + " response = openai.chat.completions.create(\n", + " model = \"gpt-4o-mini\",\n", + " messages = messages_for(website)\n", + " )\n", + " return response.choices[0].message.content\n", + "\n", + "# Step 4: print the result\n", + "summary = summarize(webUrl)\n", + "display(Markdown(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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 30d1d58f07a380ba980f3b803f1babb71053e547 Mon Sep 17 00:00:00 2001 From: Jayapal Sahadevan Date: Wed, 28 May 2025 23:43:45 +0530 Subject: [PATCH 2/3] Added my contributions to community-contributions --- ...k1-day1-ollama-webpage-summarization.ipynb | 53 +++---------------- 1 file changed, 7 insertions(+), 46 deletions(-) diff --git a/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb b/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb index 27aaabb..95e6c32 100644 --- a/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb +++ b/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "3bb88086-ea9c-4766-9baf-a57bb69c3202", "metadata": {}, "outputs": [], @@ -27,18 +27,10 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "9dc24243-d20a-48aa-b90b-26ef90233e22", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "API key found and looks good so far!\n" - ] - } - ], + "outputs": [], "source": [ "load_dotenv(override=True)\n", "api_key = os.getenv('OPENAI_API_KEY')\n", @@ -57,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "cb35e3d1-8733-4931-8744-9c3754793161", "metadata": {}, "outputs": [], @@ -67,7 +59,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "63d62eb3-3255-4046-863e-d866a833d1a6", "metadata": {}, "outputs": [], @@ -89,41 +81,10 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "409a70a6-331a-4ea4-ab8d-7a46fffc70d7", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "How about \"The A-Team 2.0\"? Because clearly, you’re aiming for a sequel that’s already better than the original. Or maybe \"The Not-So-Secret League of Awesome\"? That one’s a real conversation starter! What vibe are you going for?\n", - "[{'role': 'system', 'content': 'You are an assistant that analyzes the contents of a cric info website and provides a short summary of best team in IPL. Respond in markdown.'}, {'role': 'user', 'content': '\\n Get page title\\n'}]\n" - ] - }, - { - "data": { - "text/markdown": [ - "# Best Team in IPL History\n", - "\n", - "The Indian Premier League (IPL) has seen various teams competing for the title since its inception in 2008. Some of the most successful teams in IPL history include:\n", - "\n", - "1. **Mumbai Indians**: They have clinched the IPL trophy a record five times (2013, 2015, 2017, 2019, 2020) and are known for their strong squad and strategic gameplay.\n", - "\n", - "2. **Chennai Super Kings**: With four titles (2010, 2011, 2018, 2021), the CSK has consistently been one of the top teams, led by the experienced MS Dhoni.\n", - "\n", - "3. **Kolkata Knight Riders**: They have won the championship twice (2012, 2014) and are recognized for their fan base and competitive spirit.\n", - "\n", - "Overall, the Mumbai Indians are often considered the best team in IPL history due to their multiple championships and consistent performance over the years." - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "# Step 1: Create your prompts\n", "system_prompt = \"You are an assistant that analyzes the contents of a cric info website \\\n", From 06b14a37b6753bc323281fb7b2411b6166eb050e Mon Sep 17 00:00:00 2001 From: Jayapal Sahadevan Date: Thu, 29 May 2025 00:06:39 +0530 Subject: [PATCH 3/3] Added my contributions to community-contributions - removed comment --- .../week1-day1-ollama-webpage-summarization.ipynb | 2 -- 1 file changed, 2 deletions(-) diff --git a/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb b/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb index 95e6c32..3df7751 100644 --- a/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb +++ b/week1/community-contributions/week1-day1-ollama-webpage-summarization.ipynb @@ -35,8 +35,6 @@ "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",