Skip to content

A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.

audit: fix music_explorer - add env comment, rewrite em dashes, fix wrong folder name, add docstrings

Tiioluwani6b8383138804
diff --git a/audio/music_explorer/.env.example b/audio/music_explorer/.env.example
index cda1e2f..c5773bf 100644
--- a/audio/music_explorer/.env.example
+++ b/audio/music_explorer/.env.example
@@ -1 +1,3 @@
+# Gemini API key used for audio understanding via Gemini 3 Flash.
+# Get one free at https://aistudio.google.com/app/apikey
GEMINI_API_KEY=your_gemini_api_key_here
diff --git a/audio/music_explorer/README.md b/audio/music_explorer/README.md
index 334877b..2c375ce 100644
--- a/audio/music_explorer/README.md
+++ b/audio/music_explorer/README.md
@@ -1,31 +1,31 @@
# 🎧 Music Explorer
-> **Powered by [Gemini 3 Flash](https://ai.google.dev/gemini-api/docs/audio) β€” ask anything about any audio file or YouTube video**
+> **Powered by [Gemini 3 Flash](https://ai.google.dev/gemini-api/docs/audio), ask anything about any audio file or YouTube video**
![Music Explorer Demo](assets/demo.gif)
## Overview
-Music Explorer lets you analyze any audio through natural language. Upload a file or paste a YouTube URL, and ask questions β€” transcribe lyrics, identify instruments, detect emotion, break down track structure, or describe anything you hear. The conversation is multi-turn, so you can keep asking follow-ups without re-uploading.
+Music Explorer lets you analyze any audio through natural language. Upload a file or paste a YouTube URL, and ask questions: transcribe lyrics, identify instruments, detect emotion, break down track structure, or describe anything you hear. The conversation is multi-turn, so you can keep asking follow-ups without re-uploading.
-Gemini 3 Flash handles the audio understanding entirely via the Gemini Files API β€” no local model, no GPU required.
+Gemini 3 Flash handles the audio understanding entirely via the Gemini Files API, no local model and no GPU required.
## Features
-- **Multi-turn chat** β€” ask follow-up questions about the same audio without re-uploading
-- **YouTube support** β€” paste any YouTube URL to download and analyze the audio automatically
-- **Five quick-analysis presets** β€” one click to run full description, transcription, emotion analysis, instrument identification, or track structure breakdown
-- **Timestamp-aware responses** β€” Gemini can reference specific moments in the audio
-- **Microphone input** β€” record directly in the browser for live audio analysis
-- **No GPU needed** β€” runs entirely through the Gemini API
+- **Multi-turn chat**: ask follow-up questions about the same audio without re-uploading
+- **YouTube support**: paste any YouTube URL to download and analyze the audio automatically
+- **Five quick-analysis presets**: one click to run full description, transcription, emotion analysis, instrument identification, or track structure breakdown
+- **Timestamp-aware responses**: Gemini can reference specific moments in the audio
+- **Microphone input**: record directly in the browser for live audio analysis
+- **No GPU needed**: runs entirely through the Gemini API
## What You Can Ask
-- "Describe this track β€” genre, tempo, key, instruments, and mood"
+- "Describe this track: genre, tempo, key, instruments, and mood"
- "Transcribe the lyrics with timestamps"
- "What emotion does this convey? Does it shift throughout?"
- "List every instrument and when each one enters"
-- "Break down the structure β€” intro, verse, chorus β€” with timestamps"
+- "Break down the structure (intro, verse, chorus) with timestamps"
- "What language is being spoken?"
- "Summarize what's being said in this podcast clip"
- "What chord progression is used in the chorus?"
@@ -65,7 +65,7 @@ winget install ffmpeg
```bash
git clone https://github.com/Sumanth077/Hands-On-AI-Engineering.git
-cd Hands-On-AI-Engineering/audio/audio_intelligence
+cd Hands-On-AI-Engineering/audio/music_explorer
```
### 2. Set Up Environment Variables
@@ -98,7 +98,7 @@ Navigate to `http://localhost:7860`.
1. **Upload an audio file** or paste a YouTube URL and click **Load YouTube Audio**
2. **Type a question** or click a **Quick Analysis** preset
-3. **Keep asking follow-ups** β€” the conversation is multi-turn, no need to re-upload
+3. **Keep asking follow-ups**: the conversation is multi-turn, no need to re-upload
## How It Works
@@ -121,10 +121,10 @@ Audio file / YouTube URL
## Project Structure
```text
-audio_intelligence/
+music_explorer/
β”œβ”€β”€ app.py # Gradio UI and Gemini inference logic
β”œβ”€β”€ assets/
-β”‚ └── demo.png # App screenshot
+β”‚ └── demo.gif # App demo
β”œβ”€β”€ .env.example # API key template
β”œβ”€β”€ pyproject.toml # Dependencies (uv)
└── README.md
diff --git a/audio/music_explorer/app.py b/audio/music_explorer/app.py
index 9907c3c..9a710e6 100644
--- a/audio/music_explorer/app.py
+++ b/audio/music_explorer/app.py
@@ -1,5 +1,5 @@
"""
-Music Explorer β€” Gemini-powered audio analysis
+Music Explorer: Gemini-powered audio analysis.
Ask anything about any audio file or YouTube video.
Run: python app.py
"""
@@ -27,6 +27,7 @@ _yt_cache: dict[str, tuple[str, str]] = {} # url β†’ (audio_path, title)
def _extract_video_id(url: str) -> str | None:
+ """Extract the 11-character video ID from a YouTube URL, or None if it doesn't match."""
patterns = [
r"(?:https?://)?(?:www\.)?youtube\.com/watch\?v=([^&=%?]{11})",
r"(?:https?://)?(?:www\.)?youtu\.be/([^&=%?]{11})",
@@ -40,6 +41,7 @@ def _extract_video_id(url: str) -> str | None:
def _youtube_embed(url: str) -> str:
+ """Build an HTML iframe embed for the given YouTube URL, or an empty string if invalid."""
vid = _extract_video_id(url)
if not vid:
return ""
@@ -53,6 +55,7 @@ def _youtube_embed(url: str) -> str:
def download_youtube(url: str, force: bool = False) -> tuple[str | None, str]:
+ """Download a YouTube video's audio as MP3, using a cached copy unless force is set."""
url = url.strip()
if not _extract_video_id(url):
return None, "❌ Invalid YouTube URL."
@@ -348,6 +351,7 @@ with gr.Blocks(title="Music Explorer") as demo:
# ── Event wiring ──────────────────────────────────────────────────────────
def load_yt(url):
+ """Download the YouTube URL's audio and return the audio path, status, and embed."""
if not url.strip():
return None, gr.update(value="❌ Enter a URL first.", visible=True), gr.update(visible=False)
path, msg = download_youtube(url.strip(), force=True)
@@ -365,6 +369,7 @@ with gr.Blocks(title="Music Explorer") as demo:
)
def clear_yt():
+ """Reset the YouTube URL field, audio input, status, and embed."""
return "", None, gr.update(value="", visible=False), gr.update(visible=False)
yt_clear_btn.click(fn=clear_yt, outputs=[yt_url, audio_input, yt_status, yt_embed])