From 3eb27a8289ae3b9d66faa4f95532501f99d27350 Mon Sep 17 00:00:00 2001 From: sruthianem89 Date: Wed, 25 Jun 2025 10:59:30 -0400 Subject: [PATCH 1/2] Added my contributions to community-contributions --- .../ollama_website_summarizer.py | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 community-contributions/ollama_website_summarizer.py diff --git a/community-contributions/ollama_website_summarizer.py b/community-contributions/ollama_website_summarizer.py new file mode 100644 index 0000000..d2750f7 --- /dev/null +++ b/community-contributions/ollama_website_summarizer.py @@ -0,0 +1,84 @@ +""" +Project: Web Content Summarizer using Ollama's llama3.2 model +- Developed a Python tool to extract and summarize website content using Ollama's llama3.2 model and BeautifulSoup. +- Implemented secure API integration and HTTP requests with custom headers to mimic browser behavior. +""" + +import os +import requests +from bs4 import BeautifulSoup +import ollama + +# Constants + +OLLAMA_API = "http://localhost:11434/api/chat" +HEADERS = {"Content-Type": "application/json"} +MODEL = "llama3.2" + +# Define the Website class to fetch and parse website content +class Website: + def __init__(self, url): + """ + Initialize a Website object by fetching and parsing the given URL. + Uses BeautifulSoup to extract the title and text content of the page. + """ + self.url = url + response = requests.get(url, headers=HEADERS) + soup = BeautifulSoup(response.content, 'html.parser') + + # Extract the title of the website + self.title = soup.title.string if soup.title else "No title found" + + # Remove irrelevant elements like scripts, styles, images, and inputs + for irrelevant in soup.body(["script", "style", "img", "input"]): + irrelevant.decompose() + + # Extract the main text content of the website + self.text = soup.body.get_text(separator="\n", strip=True) + +# Define the system prompt for the OpenAI model +system_prompt = ( + "You are an assistant that analyzes the contents of a website " + "and provides a short summary, ignoring text that might be navigation related. " + "Respond in markdown." +) + +# Function to generate the user prompt based on the website content +def user_prompt_for(website): + """ + Generate a user prompt for the llama3.2 model based on the website's title and content. + """ + user_prompt = f"You are looking at a website titled {website.title}" + user_prompt += "\nThe contents of this website is as follows; summarize these.\n\n" + user_prompt += website.text + return user_prompt + +# Function to create the messages list for the OpenAI API +def messages_for(website): + """ + Create a list of messages for the ollama, including the system and user prompts. + """ + return [ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt_for(website)} + ] + +# Function to summarize the content of a given URL +def summarize(url): + """ + Summarize the content of the given URL using the OpenAI API. + """ + # Create a Website object to fetch and parse the URL + website = Website(url) + + # Call the llama3.2 using ollama with the generated messages + response = ollama.chat( + model= MODEL, + messages=messages_for(website) + ) + + # Return the summary generated by ollama + print(response.message.content) + +# Example usage: Summarize the content of a specific URL +summarize("https://sruthianem.com") \ No newline at end of file From 68b64e9ae82d48c7a5c3e9ac775f8f873da82b55 Mon Sep 17 00:00:00 2001 From: sruthianem89 Date: Wed, 25 Jun 2025 11:07:17 -0400 Subject: [PATCH 2/2] ollama_website_summarizer --- .../sruthi-day1-ollama_website_summarizer.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename community-contributions/ollama_website_summarizer.py => week1/community-contributions/sruthi-day1-ollama_website_summarizer.py (100%) diff --git a/community-contributions/ollama_website_summarizer.py b/week1/community-contributions/sruthi-day1-ollama_website_summarizer.py similarity index 100% rename from community-contributions/ollama_website_summarizer.py rename to week1/community-contributions/sruthi-day1-ollama_website_summarizer.py