{ "cells": [ { "cell_type": "markdown", "id": "d12b9c22", "metadata": {}, "source": [ "# Song Lyrics → One-Sentence Summary\n", "Get the lyrics of a song and summarize its main idea in about one sentence.\n", "\n", "## Setup\n", "Import required libraries: environment vars, display helper, OpenAI client, BeautifulSoup, and requests." ] }, { "cell_type": "code", "execution_count": null, "id": "d94bbd61", "metadata": {}, "outputs": [], "source": [ "import os\n", "from IPython.display import Markdown, display\n", "from openai import OpenAI\n", "from bs4 import BeautifulSoup\n", "import requests" ] }, { "cell_type": "markdown", "id": "92dc1bde", "metadata": {}, "source": [ "## Function: Get Lyrics from Genius\n", "Fetch and extract the lyrics from a Genius.com song page using BeautifulSoup." ] }, { "cell_type": "code", "execution_count": null, "id": "2b43fa98", "metadata": {}, "outputs": [], "source": [ "def get_lyrics_from_genius(url: str) -> str:\n", " \"\"\"\n", " Extracts song lyrics from a Genius.com song URL using BeautifulSoup.\n", " Example URL: https://genius.com/Ed-sheeran-shape-of-you-lyrics\n", " \"\"\"\n", " # Standard headers to fetch a website\n", " 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", " }\n", "\n", " response = requests.get(url, headers=headers)\n", " response.raise_for_status() # raises error if page not found\n", "\n", " soup = BeautifulSoup(response.text, \"html.parser\")\n", "\n", " # Genius stores lyrics inside