A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.
audit: fix hacker_news_newsletter_agent - rewrite em dashes, fix cd path typo, add docstrings, replace print with logging, improve README
Tiioluwani41614895087d
diff --git a/ai_agents/hacker_news_newsletter_agent/README.md b/ai_agents/hacker_news_newsletter_agent/README.mdindex eab1cc6..44ab168 100644--- a/ai_agents/hacker_news_newsletter_agent/README.md+++ b/ai_agents/hacker_news_newsletter_agent/README.md@@ -17,16 +17,16 @@ Hacker News Newsletter Agent is a Streamlit app that automates the full newslett- Generates a structured HTML newsletter with story summaries and key insights using Gemma 4- Live newsletter preview rendered in the Streamlit UI before sending- Delivers the newsletter via Gmail SMTP with SSL encryption-- Single-page Streamlit UI — enter your email and click generate+- Single-page Streamlit UI: enter your email and click generate## Tech Stack| Layer | Technology ||---|---|| LLM | Gemma 4 (`gemma-4-31b-it`) via Google AI Studio (`google-genai`) |-| News Fetching | Hacker News Algolia API — free, no API key needed |+| News Fetching | Hacker News Algolia API (free, no API key needed) || Content Extraction | `trafilatura` |-| Email Delivery | `smtplib` — Gmail SMTP on port 465 (SSL) |+| Email Delivery | `smtplib` via Gmail SMTP on port 465 (SSL) || UI | Streamlit |## Prerequisites@@ -41,7 +41,7 @@ Hacker News Newsletter Agent is a Streamlit app that automates the full newslett```bashgit clone https://github.com/Sumanth077/Hands-On-AI-Engineering.git-cd Hands-On-AI-Engineering/ai_agents/hacker_news_newsletter_agentc+cd Hands-On-AI-Engineering/ai_agents/hacker_news_newsletter_agent```**2. Create and activate a virtual environment**@@ -72,25 +72,13 @@ cp .env.example .envOpen `.env` and fill in your credentials (see [Environment Variables](#environment-variables) below).-## Usage--```bash-streamlit run app.py-```--1. Enter your email address in the input field-2. Click **Generate Newsletter**-3. Wait while the app fetches stories, scrapes content, and generates the newsletter (~30–60 seconds)-4. Review the rendered HTML preview-5. Click **Send Newsletter** to deliver it to your inbox-## Environment Variables| Variable | Description ||---|---|| `GOOGLE_API_KEY` | API key from [aistudio.google.com](https://aistudio.google.com/apikey) || `GMAIL_ADDRESS` | The Gmail address the newsletter is sent from |-| `GMAIL_APP_PASSWORD` | 16-character App Password from [myaccount.google.com/apppasswords](https://myaccount.google.com/apppasswords) — remove spaces |+| `GMAIL_APP_PASSWORD` | 16-character App Password from [myaccount.google.com/apppasswords](https://myaccount.google.com/apppasswords); remove spaces when copying |> **Note:** Gmail App Passwords require 2-Step Verification to be enabled. Do not use your regular Gmail password.@@ -100,6 +88,18 @@ [email protected]GMAIL_APP_PASSWORD=your16charapppassword```+## Usage++```bash+streamlit run app.py+```++1. Enter your email address in the input field+2. Click **Generate Newsletter**+3. Wait while the app fetches stories, scrapes content, and generates the newsletter (~30–60 seconds)+4. Review the rendered HTML preview+5. Click **Send Newsletter** to deliver it to your inbox+## Project Structure```text@@ -111,3 +111,7 @@ hacker-news-newsletter-agent/└── assets/└── demo.png # Demo screenshot```++---++[⬆ Back to Top](#hacker-news-newsletter-agent)diff --git a/ai_agents/hacker_news_newsletter_agent/app.py b/ai_agents/hacker_news_newsletter_agent/app.pyindex 6faa2f6..563b43e 100644--- a/ai_agents/hacker_news_newsletter_agent/app.py+++ b/ai_agents/hacker_news_newsletter_agent/app.py@@ -1,3 +1,4 @@+"""Streamlit app for the Hacker News Newsletter Agent: fetches top HN stories, generates an HTML newsletter with Gemma 4, and delivers it via Gmail SMTP."""import osimport re@@ -51,6 +52,7 @@ for key, val in {def extract_html(raw: str) -> str:+ """Strip chain-of-thought tags and return the first complete HTML document found in the LLM output."""cleaned = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL)m = re.search(r"<!DOCTYPE html>.*?</html>", cleaned, re.DOTALL | re.IGNORECASE)if m:diff --git a/ai_agents/hacker_news_newsletter_agent/tools.py b/ai_agents/hacker_news_newsletter_agent/tools.pyindex 6dd2c60..4833f4e 100644--- a/ai_agents/hacker_news_newsletter_agent/tools.py+++ b/ai_agents/hacker_news_newsletter_agent/tools.py@@ -1,3 +1,4 @@+import loggingimport osimport reimport smtplib@@ -10,8 +11,11 @@ import trafilaturafrom google import genaifrom google.genai import types+logger = logging.getLogger(__name__)+def fetch_top_stories(count: int = 10) -> list[dict]:+ """Fetch the most recent tech stories from Hacker News via the Algolia Search API and return them as a list of dicts."""url = "https://hn.algolia.com/api/v1/search_by_date?query=tech&tags=story&hitsPerPage=10"response = requests.get(url,@@ -32,11 +36,12 @@ def fetch_top_stories(count: int = 10) -> list[dict]:"comments": hit.get("num_comments", 0),})- print(f"[fetch_top_stories] Algolia returned {len(stories)} stories")+ logger.info("Algolia returned %d stories", len(stories))return storiesdef extract_content(url: str) -> str:+ """Download and extract plain-text article content from a URL using Trafilatura, truncated to 1800 characters."""if not url or "news.ycombinator.com" in url:return f"[Full content unavailable — read at: {url}]"try:@@ -56,6 +61,7 @@ def extract_content(url: str) -> str:def generate_newsletter(stories: list[dict]) -> str:+ """Send the list of HN stories to Gemma 4 and return a raw HTML newsletter document."""date_str = datetime.now().strftime("%B %d, %Y")stories_text = ""@@ -154,6 +160,7 @@ STORIES DATA:def send_email(newsletter: str, recipient: str) -> str:+ """Send the HTML newsletter to the recipient via Gmail SMTP and return a status message string."""gmail_address = os.getenv("GMAIL_ADDRESS")gmail_password = os.getenv("GMAIL_APP_PASSWORD")