Samuel Week 1 Task clear data

This commit is contained in:
samuelmurira
2025-10-24 11:24:06 +00:00
parent 5f1ccd48a2
commit 15be324bce

View File

@@ -0,0 +1,136 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-10-24T11:22:09.510611Z",
"start_time": "2025-10-24T11:21:52.159537Z"
}
},
"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. Australia Warns Citizens About Poisonous Alcohol in Kenya\n",
"2. Top 6 Best 5-Star Hotels in Nairobi, Kenya (2025)\n",
"3. Kenya Airways: 20 Facts About Africas Premier Airline You Need to Know\n",
"4. Sakaja Raises Nairobi Land Rates Effective January 2026\n",
"5. Motorists to Pay Ksh8 Per Kilometre to Use RironiMau Summit Expressway KeNHA\n",
"\n",
"=== AI NEWS ANALYSIS ===\n",
"Based on the provided trending news headlines, here are my observations:\n",
"\n",
"**Key Themes:**\n",
"\n",
"1. Economic Development and Infrastructure: Headlines related to land rates, transportation costs, and air travel pricing suggest a focus on economic growth and development.\n",
"2. Infrastructure Upgrades: News about motorist tolls and airline facts highlight efforts to modernize Kenya's infrastructure.\n",
"3. Social Services and Local Management: The mention of Nairobi land rates and housing policies indicate a focus on local governance and social services.\n",
"\n",
"**Summary (under 300 words):**\n",
"\n",
"Today, Kenyan news highlights significant developments in various sectors that impact the country's economy and citizens' daily lives. At the forefront is the introduction of higher land rates in Nairobi by Senator Jimi Sakaja, set to take effect January 2026. This decision aims to address housing shortages and generate revenue for local authorities.\n",
"\n",
"Additionally, motorist tolls have been introduced on the Rironi-Mau Summit Expressway, with motorists expected to pay Ksh8 per kilometre using this new route. These changes reflect Kenya's growing focus on infrastructure development, including upgraded road networks and air travel.\n",
"\n",
"While not necessarily breaking news, other headlines provide insight into Kenya Airways' strong positions in Africa's aviation industry, highlighting its role as a premier airline. With 20 facts outlined about the airline, it is clear that Kenya Airways remains an important player in regional and international air travel.\n",
"\n",
"In conclusion, today's trending Kenyan news emphasizes economic growth, infrastructure development, and local governance initiatives. These developments are likely to shape various facets of Kenyans' daily lives, from housing costs to transportation expenses and social services.\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
}