Files
LLM_Engineering_OLD/week1/community-contributions/week1_assignments/text_summary_ollama.ipynb
KiranAyyagari ccf5cdb0fe Refactor and relocate text summarizer notebooks
Deleted the old kiran-text-summarizer-gpt5mini notebook and added refactored versions for OpenAI GPT-5-mini and Ollama models under week1_assignments. Introduced a reusable scrape_website.py module for web scraping logic. Updated 'day2 EXERCISE.ipynb' to set execution counts and include output for code cells, improving reproducibility and clarity.
2025-08-10 14:38:52 +05:30

187 lines
6.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI \n",
"from scrape_website import ScrapeWebsite"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29ddd15d-a3c5-4f4e-a678-873f56162724",
"metadata": {},
"outputs": [],
"source": [
"# Constants\n",
"MODEL = \"llama3.2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42c8a8c2",
"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": "code",
"execution_count": null,
"id": "51e86dd1",
"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": "code",
"execution_count": null,
"id": "b69d7238",
"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": "code",
"execution_count": null,
"id": "a56e99ea",
"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",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b4061d0",
"metadata": {},
"outputs": [],
"source": [
"def summarise(url):\n",
" website = ScrapeWebsite(url, headers)\n",
" ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
" response = ollama_via_openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages_for(website)\n",
" )\n",
"\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65f96545",
"metadata": {},
"outputs": [],
"source": [
"def display_summary(url):\n",
" summary = summarise(url)\n",
" display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23057e00-b6fc-4678-93a9-6b31cb704bff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generative AI has numerous business applications across various industries. Here are some examples:\n",
"\n",
"1. **Marketing and Advertising**: Generative AI can create personalized product recommendations, generate targeted advertisements, and develop new marketing campaigns.\n",
"2. **Content Creation**: AI-powered tools can assist in content creation, such as writing articles, generating social media posts, and creating videos, podcasts, and music.\n",
"3. **Product Design and Development**: Generative AI can aid in designing products, such as 3D modeling, prototyping, and testing product feasibility.\n",
"4. **Customer Service Chatbots**: AI-powered chatbots can provide personalized customer service, answering common queries, and helping resolve issues faster.\n",
"5. **Language Translation**: Generative AI can translate languages in real-time, enabling businesses to communicate with global customers more effectively.\n",
"6. **Data Analysis and Visualization**: AI can analyze large datasets, identify patterns, and create insights, making it easier for businesses to make informed decisions.\n",
"7. **Cybersecurity Threat Detection**: Generative AI-powered systems can detect and respond to cyber threats more efficiently, reducing the risk of data breaches and attacks.\n",
"8. **Supply Chain Optimization**: AI can optimize supply chain operations, predict demand, and identify opportunities for improvement, leading to increased efficiency and reduced costs.\n",
"9. **Network Security**: Generative AI can analyze network traffic patterns, detect anomalies, and prevent cyber-attacks.\n",
"10. **Finance and Banking**: AI-powered systems can detect financial fraud, predict customer creditworthiness, and generate credit reports.\n",
"\n",
"**Industry-specific applications:**\n",
"\n",
"1. **Healthcare**: AI can help with medical diagnosis, patient data analysis, and personalized medicine.\n",
"2. **Manufacturing**: Generative AI can create optimized production schedules, predict equipment failures, and improve product quality.\n",
"3. **Education**: AI-powered tools can develop personalized learning plans, automate grading, and provide educational resources.\n",
"4. **Real Estate**: AI can help with property valuations, identify market trends, and analyze potential clients' needs.\n",
"\n",
"**Business benefits:**\n",
"\n",
"1. **Increased efficiency**: Automating mundane tasks frees up human resources for more strategic work.\n",
"2. **Improved accuracy**: Generative AI reduces the likelihood of human error in decision-making and task execution.\n",
"3. **Enhanced customer experience**: Personalized experiences are created through data-driven insights.\n",
"4. **Competitive advantage**: Companies using AI can differentiate themselves from competitors by offering innovative services and products.\n",
"\n",
"As Generative AI continues to evolve, we can expect even more exciting applications across various industries, leading to increased efficiency, accuracy, and improved competitiveness for businesses worldwide.\n"
]
}
],
"source": [
"display_summary(\"https://www.firstpost.com/world/united-states/\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6de38216-6d1c-48c4-877b-86d403f4e0f8",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}