doreamong/YoutubeRAG
0
1---2title: YoutubeRAG3app_file: main.py4sdk: gradio5sdk_version: 5.46.06---7# YouTube RAG System8 9[](./LICENSE) [](https://python.org)10 11_Languages: English · [中文](./README.zh-CN.md)_12 13A **lightweight**, **modular** YouTube video Q&A tool with **persistent session management**. Supports automatic subtitle extraction or audio transcription to build a searchable knowledge base.14 15<p align="center">16 <img src="./images/Demo.png" alt="Demo Screenshot" width="720" />17 <br/>18 <em>Web Interface: Interactive Q&A with session management</em>19</p>20 21## ✨ New in v2.022 23- 🏗️ **Modular Architecture**: Clean, maintainable code structure24- 💾 **Session Persistence**: Save and reload RAG sessions25- 🌐 **Web Interface**: Beautiful Gradio-based UI26- 📱 **Simplified CLI**: Streamlined command-line experience27- 🔧 **Better Error Handling**: More robust and user-friendly28 29## Table of Contents30 31- [Features](#features)32- [Installation](#installation)33- [Quick Start](#quick-start)34- [Usage](#usage)35- [Project Structure](#project-structure)36- [License](#license)37 38## Features39 40### Core Features41- 🎥 **Smart Content Extraction**: Automatic YouTube subtitle extraction with audio transcription fallback42- 📝 **AI Summarization**: Generate concise video summaries using OpenAI models43- 🔍 **Vector Search**: Intelligent Q&A using RAG (Retrieval Augmented Generation)44- 💾 **Session Persistence**: Save and reload analysis sessions45- 🌐 **Multi-language Support**: Chinese and English interfaces46 47### Interface Options48- 🖥️ **Web Interface**: User-friendly Gradio web UI49- ⌨️ **Command Line**: Streamlined CLI for developers50- 🔄 **Session Management**: List, load, save, and delete sessions51 52### Technical Features53- 🏗️ **Modular Design**: Clean separation of concerns54- 🤖 **Multiple AI Models**: Support for GPT-3.5, GPT-4, GPT-4o series55- 📊 **Chunked Processing**: Handle long videos efficiently56- 🛡️ **Error Resilience**: Robust error handling and recovery57 58## Installation59 60### Prerequisites61- Python 3.12+62- OpenAI API key63 64### Install Dependencies65 66```bash67pip install -r requirements.txt68```69 70### Set Environment Variables71 72```bash73export OPENAI_API_KEY="your-openai-api-key"74```75 76## Quick Start77 78### 🌐 Web Interface (Recommended)79 80```bash81# Launch web interface (default mode)82python main.py83 84# Or explicitly specify UI mode85python main.py --ui86```87 88Open your browser and navigate to `http://localhost:7860`89 90### ⌨️ Command Line Interface91 92```bash93# Analyze a YouTube video94python main.py --url "https://www.youtube.com/watch?v=VIDEO_ID"95 96# Use different AI model97python main.py --url "https://www.youtube.com/watch?v=VIDEO_ID" --model gpt-498 99# Custom chunking parameters100python main.py --url "https://www.youtube.com/watch?v=VIDEO_ID" --chunk-size 1500 --chunk-overlap 50101```102 103## Usage104 105### Web Interface106 1071. **Start the application**: `python main.py`1082. **Enter API Key**: Provide your OpenAI API key1093. **Choose Action**:110 - **New Video**: Analyze a fresh YouTube video111 - **Load Session**: Continue with a previously saved session1124. **Ask Questions**: Interact with the AI about the video content113 114### Session Management115 116```bash117# List all saved sessions118python main.py --list-sessions119 120# Load a specific session121python main.py --load-session "session_name"122 123# Delete a session124python main.py --delete-session "session_name"125```126 127### Advanced Usage128 129#### Custom Model Configuration130 131```bash132# Use GPT-4 for better quality (slower, more expensive)133python main.py --url "VIDEO_URL" --model gpt-4134 135# Use GPT-4o-mini for faster processing136python main.py --url "VIDEO_URL" --model gpt-4o-mini137```138 139#### Session Management Commands (in Web/CLI interface)140 141- `sessions` - View all saved sessions142- `save as [name]` - Save current session with custom name143- `reset` - Restart the application144- `exit` - Quit the application145 146## Project Structure147 148```149YouTube RAG System/150├── main.py # Main entry point151├── requirements.txt # Python dependencies152├── README.md # This file153├── README.zh-CN.md # Chinese documentation154├── src/ # Source code155│ └── youtube_rag_system/ # Main package156│ ├── __init__.py # Package initialization157│ ├── core/ # Core functionality158│ │ ├── __init__.py159│ │ ├── rag_engine.py # Main RAG engine160│ │ ├── content_processor.py # Video processing161│ │ └── session_manager.py # Session persistence162│ ├── ui/ # User interfaces163│ │ ├── __init__.py164│ │ └── gradio_interface.py # Web interface165│ └── utils/ # Utility functions166│ ├── __init__.py167│ ├── validators.py # Input validation168│ └── file_utils.py # File operations169├── rag_sessions/ # Saved sessions (auto-created)170│ └── [session_id]/171│ ├── metadata.json # Session metadata172│ └── chroma_db/ # Vector database173└── images/ # Documentation images174 ├── Demo.png175 └── Demo2.png176```177 178## How It Works179 1801. **Content Acquisition**: 181 - First attempts to extract YouTube auto-generated subtitles182 - Falls back to audio download and Whisper transcription if needed183 1842. **AI Processing**:185 - Generates comprehensive video summary186 - Splits content into optimized chunks for vector search187 1883. **Knowledge Base Creation**:189 - Creates vector embeddings using OpenAI embeddings190 - Stores in ChromaDB for efficient retrieval191 1924. **Session Persistence**:193 - Saves all processed data to disk194 - Enables quick reload without reprocessing195 1965. **Interactive Q&A**:197 - Uses RAG to answer questions based on video content198 - Maintains context and provides relevant responses199 200## Command Line Options201 202```bash203python main.py [-h] [--ui] [--url URL] [--model MODEL] 204 [--chunk-size CHUNK_SIZE] [--chunk-overlap CHUNK_OVERLAP] 205 [--list-sessions] [--load-session SESSION] 206 [--delete-session SESSION]207 208YouTube RAG System - YouTube视频RAG问答工具209 210optional arguments:211 -h, --help show help message and exit212 --ui Launch Gradio web interface213 --url URL YouTube video URL for CLI mode214 --model MODEL OpenAI model name (default: gpt-3.5-turbo)215 --chunk-size SIZE Text chunk size (default: 1000)216 --chunk-overlap SIZE Text chunk overlap (default: 20)217 --list-sessions List all saved sessions218 --load-session NAME Load a saved session by name219 --delete-session NAME Delete a saved session by name220```221 222## Environment Variables223 224- `OPENAI_API_KEY`: Your OpenAI API key (required)225 226## Requirements227 228- Python 3.12+229- OpenAI API key230- Internet connection (for YouTube access and OpenAI API)231 232### Dependencies233 234See [requirements.txt](requirements.txt) for the complete list of Python packages.235 236## License237 238This project is licensed under the [MIT License](./LICENSE).239 240## Contributing241 242Contributions are welcome! Please feel free to submit a Pull Request.243 244## Support245 246If you encounter any issues or have questions, please open an issue on GitHub.247 