From 5f1ccd48a2ff0b818419144549b5e8d159e53c15 Mon Sep 17 00:00:00 2001 From: samuelmurira Date: Fri, 24 Oct 2025 11:21:37 +0000 Subject: [PATCH] Samuel Week 1 Task --- ...1 Task: Daily Kenyan News Summarizer.ipynb | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 week1/solutions/Week 1 Task: Daily Kenyan News Summarizer.ipynb diff --git a/week1/solutions/Week 1 Task: Daily Kenyan News Summarizer.ipynb b/week1/solutions/Week 1 Task: Daily Kenyan News Summarizer.ipynb new file mode 100644 index 0000000..d52cde7 --- /dev/null +++ b/week1/solutions/Week 1 Task: Daily Kenyan News Summarizer.ipynb @@ -0,0 +1,126 @@ +{ + "cells": [ + { + "cell_type": "code", + "id": "initial_id", + "metadata": { + "collapsed": true, + "ExecuteTime": { + "end_time": "2025-10-24T10:16:48.726481Z", + "start_time": "2025-10-24T10:16:24.411125Z" + } + }, + "source": [ + "import requests\n", + "from bs4 import BeautifulSoup\n", + "from openai import OpenAI\n", + "\n", + "# Initialize the OpenAI client for Ollama\n", + "openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n", + "\n", + "# Step 1: Fetch and parse trending news from The Star (Kenya)\n", + "def fetch_trending_news():\n", + " url = \"https://thestar.co.ke/\"\n", + " try:\n", + " response = requests.get(url)\n", + " response.raise_for_status() # Check for request errors\n", + "\n", + " soup = BeautifulSoup(response.text, 'html.parser')\n", + " news_list = []\n", + "\n", + " # Look for headlines - SELECTORS MAY NEED ADJUSTMENT\n", + " # Try to find common headline elements (h1, h2, h3, h4) with relevant classes\n", + " headlines = soup.find_all(['h1', 'h2', 'h3', 'h4'], class_=lambda x: x != None)\n", + "\n", + " for headline in headlines[:10]: # Get first 10 headlines\n", + " headline_text = headline.get_text().strip()\n", + " if headline_text and len(headline_text) > 20: # Filter out short text\n", + " news_list.append(headline_text)\n", + "\n", + " return news_list[:5] # Return top 5 headlines\n", + " except Exception as e:\n", + " print(f\"Error fetching news: {e}\")\n", + " return [\"Failed to fetch trending news.\"]\n", + "\n", + "# Step 2: Create your prompts using real news data\n", + "trending_news = fetch_trending_news()\n", + "news_text = \"\\n\".join(trending_news)\n", + "\n", + "system_prompt = \"You are a news analyst specializing in Kenyan current affairs.\"\n", + "user_prompt = f\"\"\"\n", + "Based on the following trending news headlines from Kenya for today, provide a brief analysis of the main news topics:\n", + "\n", + "{news_text}\n", + "\n", + "Please identify 2-3 key themes and write a short summary (less than 300 words) about what's currently trending in Kenyan news.\n", + "\"\"\"\n", + "\n", + "# Step 3: Make the messages list\n", + "messages = [\n", + " {\"role\": \"system\", \"content\": system_prompt},\n", + " {\"role\": \"user\", \"content\": user_prompt}\n", + "]\n", + "\n", + "# Step 4: Call Ollama\n", + "try:\n", + " response = openai.chat.completions.create(model=\"llama3.2\", messages=messages)\n", + " # Step 5: Print the result\n", + " print(\"=== TRENDING KENYAN NEWS ANALYSIS ===\")\n", + " print(\"\\nToday's key headlines:\")\n", + " for i, headline in enumerate(trending_news, 1):\n", + " print(f\"{i}. {headline}\")\n", + " print(\"\\n=== AI NEWS ANALYSIS ===\")\n", + " print(response.choices[0].message.content)\n", + "except Exception as e:\n", + " print(f\"Error in AI analysis: {e}\")" + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=== TRENDING KENYAN NEWS ANALYSIS ===\n", + "\n", + "Today's key headlines:\n", + "1. Kenya Airways: 20 Facts About Africa’s Premier Airline You Need to Know\n", + "2. Sakaja Raises Nairobi Land Rates Effective January 2026\n", + "3. Motorists to Pay Ksh8 Per Kilometre to Use Rironi–Mau Summit Expressway – KeNHA\n", + "4. JSC Shortlists 100 Candidates for High Court Judge Positions\n", + "5. How to Check Nairobi Water Bill Online and Via SMS\n", + "\n", + "=== AI NEWS ANALYSIS ===\n", + "After analyzing the trending news headlines from Kenya, I have identified three key themes:\n", + "\n", + "1. **Infrastructure Development**: The first two headlines pertain to infrastructure development projects in Kenya. The article on Kenya Airways highlights the airline's importance in connecting Africa, while the story about Nairobi land rates raises questions about affordability and impact on developers. Meanwhile, motorists are set to face a new fee structure when using the Rironi-Mau Summit Expressway.\n", + "2. **E-Governance**: Two headlines touch upon e-governance initiatives. The article explaining how to check Nairobi Water bill online and via SMS demonstrates efforts to make public services more accessible to citizens through digital channels. This theme suggests that the Kenyan government is placing a growing emphasis on modernizing its administrative processes.\n", + "3. **Justice Sector Reform**: The final headline highlights an important development in the justice sector, with JSC shortlisting 100 candidates for High Court judge positions. This signifies a critical phase in Kenya's ongoing efforts to strengthen its judicial system and ensure effective justice delivery.\n", + "\n", + "In summary, current news trends in Kenya revolve around strategic developments, digital access, and institutional reform. These interconnected stories underscore ongoing efforts by the Kenyan government to foster growth through modern infrastructure projects while promoting online services and enhancing effectiveness within the judiciary.\n" + ] + } + ], + "execution_count": 1 + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}