Upload folder using huggingface_hub
Browse files- .gitignore +2 -0
- README.md +55 -49
- app.py +523 -63
- requirements.txt +2 -1
- smolagent_processor.py +61 -2
.gitignore
CHANGED
|
@@ -49,3 +49,5 @@ logs/
|
|
| 49 |
*.bak
|
| 50 |
*.swp
|
| 51 |
*~
|
|
|
|
|
|
|
|
|
| 49 |
*.bak
|
| 50 |
*.swp
|
| 51 |
*~
|
| 52 |
+
|
| 53 |
+
debug/
|
README.md
CHANGED
|
@@ -1,51 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# YouTube Tutorial to Step-by-Step Guide Generator
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
- Provide a lightweight editor for
|
| 12 |
-
-
|
| 13 |
-
|
| 14 |
-
##
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
-
|
| 25 |
-
-
|
| 26 |
-
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
-
|
| 33 |
-
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
##
|
| 42 |
-
|
| 43 |
-
-
|
| 44 |
-
-
|
| 45 |
-
- Adaptation Strategy
|
| 46 |
-
- Data Privacy and Compliance
|
| 47 |
-
- Accessibility Standards
|
| 48 |
-
- Documentation Requirements
|
| 49 |
-
- User Personas and Use Cases
|
| 50 |
-
- Success Metrics
|
| 51 |
-
- Development Methodology
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: YouTube Tutorial to Step-by-Step Guide
|
| 3 |
+
emoji: 🎬
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.22.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Convert YouTube tutorials into editable step-by-step guides
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
# YouTube Tutorial to Step-by-Step Guide Generator
|
| 15 |
|
| 16 |
+
This Hugging Face Space application converts YouTube tutorials into editable, time-stamped step-by-step guides. The application extracts key instructions from tutorial videos and presents them in a clean, user-friendly format that can be easily modified.
|
| 17 |
+
|
| 18 |
+
## Features
|
| 19 |
+
|
| 20 |
+
- Extract and process video transcripts with timestamps
|
| 21 |
+
- Generate structured step-by-step instructions
|
| 22 |
+
- Include accurate timestamps for each instruction
|
| 23 |
+
- Detect and format code snippets
|
| 24 |
+
- Provide a lightweight editor for customization
|
| 25 |
+
- Export guides as Markdown
|
| 26 |
+
|
| 27 |
+
## Usage
|
| 28 |
+
|
| 29 |
+
1. Enter a YouTube video URL in the input field
|
| 30 |
+
2. Click "Generate Guide"
|
| 31 |
+
3. View the generated guide with timestamps
|
| 32 |
+
4. Edit the guide as needed
|
| 33 |
+
5. Export as Markdown
|
| 34 |
+
|
| 35 |
+
## Limitations
|
| 36 |
+
|
| 37 |
+
- Works best with videos that have accurate captions
|
| 38 |
+
- Processing large videos may take longer
|
| 39 |
+
- Code detection is basic and may miss some snippets
|
| 40 |
+
|
| 41 |
+
## Technical Details
|
| 42 |
+
|
| 43 |
+
This application is optimized to run efficiently on a Hugging Face Space with 2 vCPU and 16 GB RAM (free tier). It uses:
|
| 44 |
+
|
| 45 |
+
- SmoLAgent framework for lightweight, efficient processing
|
| 46 |
+
- YouTube's transcript API for text extraction
|
| 47 |
+
- Intelligent content segmentation based on YouTube chapters
|
| 48 |
+
- Client-side processing for UI enhancements
|
| 49 |
+
|
| 50 |
+
## License
|
| 51 |
+
|
| 52 |
+
This project is licensed under the MIT License.
|
| 53 |
+
|
| 54 |
+
## Acknowledgements
|
| 55 |
+
|
| 56 |
+
- Built with Gradio and SmoLAgent
|
| 57 |
+
- Hosted on Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -8,16 +8,15 @@ import json
|
|
| 8 |
import time
|
| 9 |
import tempfile
|
| 10 |
import logging
|
|
|
|
| 11 |
from typing import Dict, List, Optional, Tuple, Any
|
| 12 |
from dataclasses import dataclass, field
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
import numpy as np
|
| 16 |
-
import requests
|
| 17 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 18 |
from pytube import YouTube
|
| 19 |
from markdown import markdown
|
| 20 |
-
import torch
|
| 21 |
from huggingface_hub import HfApi, login
|
| 22 |
from dotenv import load_dotenv
|
| 23 |
|
|
@@ -46,12 +45,6 @@ else:
|
|
| 46 |
# Memory usage monitoring
|
| 47 |
def get_memory_usage() -> Dict[str, float]:
|
| 48 |
"""Get current memory usage statistics."""
|
| 49 |
-
if torch.cuda.is_available():
|
| 50 |
-
torch.cuda.empty_cache()
|
| 51 |
-
gpu_memory = torch.cuda.memory_allocated() / 1024**3 # Convert to GB
|
| 52 |
-
else:
|
| 53 |
-
gpu_memory = 0
|
| 54 |
-
|
| 55 |
# Get system memory info
|
| 56 |
import psutil
|
| 57 |
process = psutil.Process(os.getpid())
|
|
@@ -60,7 +53,7 @@ def get_memory_usage() -> Dict[str, float]:
|
|
| 60 |
|
| 61 |
return {
|
| 62 |
"ram_gb": ram_usage,
|
| 63 |
-
"gpu_gb":
|
| 64 |
"ram_percent": ram_usage / 16 * 100, # Based on 16GB available
|
| 65 |
}
|
| 66 |
|
|
@@ -83,6 +76,7 @@ def extract_video_id(url: str) -> Optional[str]:
|
|
| 83 |
def get_video_info(video_id: str) -> Dict[str, Any]:
|
| 84 |
"""Get basic information about a YouTube video."""
|
| 85 |
try:
|
|
|
|
| 86 |
yt = YouTube(f"https://www.youtube.com/watch?v={video_id}")
|
| 87 |
return {
|
| 88 |
"title": yt.title,
|
|
@@ -94,8 +88,28 @@ def get_video_info(video_id: str) -> Dict[str, Any]:
|
|
| 94 |
"publish_date": str(yt.publish_date) if yt.publish_date else None,
|
| 95 |
}
|
| 96 |
except Exception as e:
|
| 97 |
-
logger.error(f"Error getting video info: {e}")
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def get_transcript(video_id: str) -> List[Dict[str, Any]]:
|
| 101 |
"""Get transcript for a YouTube video with timestamps."""
|
|
@@ -104,66 +118,502 @@ def get_transcript(video_id: str) -> List[Dict[str, Any]]:
|
|
| 104 |
return transcript
|
| 105 |
except Exception as e:
|
| 106 |
logger.error(f"Error getting transcript: {e}")
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def get_video_chapters(video_id: str) -> List[Dict[str, Any]]:
|
| 110 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
try:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
chapters
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
chapters[i]["end_time"] = chapters[i + 1]["start_time"]
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
if chapters:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
except Exception as e:
|
| 156 |
-
logger.error(f"Error getting
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
# Main application functions
|
| 160 |
-
def process_video(video_url: str, progress=gr.Progress())
|
| 161 |
"""Process YouTube video and generate step-by-step guide."""
|
|
|
|
|
|
|
| 162 |
result = {
|
| 163 |
"video_info": {},
|
| 164 |
"chapters": [],
|
| 165 |
"steps": [],
|
| 166 |
-
"memory_usage":
|
| 167 |
"error": None,
|
| 168 |
"video_id": None
|
| 169 |
}
|
|
@@ -171,8 +621,10 @@ def process_video(video_url: str, progress=gr.Progress()) -> Tuple[str, str, Lis
|
|
| 171 |
try:
|
| 172 |
# Extract video ID
|
| 173 |
video_id = extract_video_id(video_url)
|
|
|
|
| 174 |
if not video_id:
|
| 175 |
result["error"] = "Invalid YouTube URL"
|
|
|
|
| 176 |
return (
|
| 177 |
ui_components.format_video_info({}),
|
| 178 |
ui_components.format_chapters([]),
|
|
@@ -184,25 +636,30 @@ def process_video(video_url: str, progress=gr.Progress()) -> Tuple[str, str, Lis
|
|
| 184 |
|
| 185 |
progress(0.1, "Extracting video information...")
|
| 186 |
result["video_info"] = get_video_info(video_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
progress(0.2, "Getting video transcript...")
|
| 189 |
transcript = get_transcript(video_id)
|
| 190 |
-
if
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
ui_components.format_video_info(result["video_info"]),
|
| 194 |
-
ui_components.format_chapters([]),
|
| 195 |
-
ui_components.steps_to_dataframe([]),
|
| 196 |
-
ui_components.format_memory_usage(get_memory_usage())
|
| 197 |
-
)
|
| 198 |
|
| 199 |
progress(0.4, "Detecting video chapters...")
|
| 200 |
chapters = get_video_chapters(video_id)
|
|
|
|
| 201 |
result["chapters"] = chapters
|
| 202 |
|
| 203 |
progress(0.6, "Processing transcript...")
|
| 204 |
processor = SmoLAgentProcessor()
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
progress(0.9, "Finalizing guide...")
|
| 208 |
result["memory_usage"] = get_memory_usage()
|
|
@@ -215,10 +672,13 @@ def process_video(video_url: str, progress=gr.Progress()) -> Tuple[str, str, Lis
|
|
| 215 |
steps_df = ui_components.steps_to_dataframe(result["steps"])
|
| 216 |
memory_html = ui_components.format_memory_usage(result["memory_usage"])
|
| 217 |
|
|
|
|
| 218 |
return video_info_html, chapters_html, steps_df, memory_html
|
| 219 |
|
| 220 |
except Exception as e:
|
| 221 |
-
logger.error(f"Error processing video: {e}")
|
|
|
|
|
|
|
| 222 |
result["error"] = str(e)
|
| 223 |
return (
|
| 224 |
ui_components.format_video_info(result.get("video_info", {})),
|
|
|
|
| 8 |
import time
|
| 9 |
import tempfile
|
| 10 |
import logging
|
| 11 |
+
import requests
|
| 12 |
from typing import Dict, List, Optional, Tuple, Any
|
| 13 |
from dataclasses import dataclass, field
|
| 14 |
|
| 15 |
import gradio as gr
|
| 16 |
import numpy as np
|
|
|
|
| 17 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 18 |
from pytube import YouTube
|
| 19 |
from markdown import markdown
|
|
|
|
| 20 |
from huggingface_hub import HfApi, login
|
| 21 |
from dotenv import load_dotenv
|
| 22 |
|
|
|
|
| 45 |
# Memory usage monitoring
|
| 46 |
def get_memory_usage() -> Dict[str, float]:
|
| 47 |
"""Get current memory usage statistics."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Get system memory info
|
| 49 |
import psutil
|
| 50 |
process = psutil.Process(os.getpid())
|
|
|
|
| 53 |
|
| 54 |
return {
|
| 55 |
"ram_gb": ram_usage,
|
| 56 |
+
"gpu_gb": 0, # No GPU usage tracking without torch
|
| 57 |
"ram_percent": ram_usage / 16 * 100, # Based on 16GB available
|
| 58 |
}
|
| 59 |
|
|
|
|
| 76 |
def get_video_info(video_id: str) -> Dict[str, Any]:
|
| 77 |
"""Get basic information about a YouTube video."""
|
| 78 |
try:
|
| 79 |
+
# First try using pytube
|
| 80 |
yt = YouTube(f"https://www.youtube.com/watch?v={video_id}")
|
| 81 |
return {
|
| 82 |
"title": yt.title,
|
|
|
|
| 88 |
"publish_date": str(yt.publish_date) if yt.publish_date else None,
|
| 89 |
}
|
| 90 |
except Exception as e:
|
| 91 |
+
logger.error(f"Error getting video info with pytube: {e}")
|
| 92 |
+
|
| 93 |
+
# Fallback to using requests to get basic info
|
| 94 |
+
try:
|
| 95 |
+
# Get oEmbed data from YouTube
|
| 96 |
+
oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
|
| 97 |
+
response = requests.get(oembed_url)
|
| 98 |
+
response.raise_for_status()
|
| 99 |
+
data = response.json()
|
| 100 |
+
|
| 101 |
+
return {
|
| 102 |
+
"title": data.get("title", "Unknown Title"),
|
| 103 |
+
"author": data.get("author_name", "Unknown Author"),
|
| 104 |
+
"thumbnail_url": data.get("thumbnail_url", ""),
|
| 105 |
+
"description": "Description not available",
|
| 106 |
+
"length": 0,
|
| 107 |
+
"views": 0,
|
| 108 |
+
"publish_date": None,
|
| 109 |
+
}
|
| 110 |
+
except Exception as e2:
|
| 111 |
+
logger.error(f"Error getting video info with fallback method: {e2}")
|
| 112 |
+
return {"error": f"Could not retrieve video information: {str(e)}"}
|
| 113 |
|
| 114 |
def get_transcript(video_id: str) -> List[Dict[str, Any]]:
|
| 115 |
"""Get transcript for a YouTube video with timestamps."""
|
|
|
|
| 118 |
return transcript
|
| 119 |
except Exception as e:
|
| 120 |
logger.error(f"Error getting transcript: {e}")
|
| 121 |
+
|
| 122 |
+
# Try to get transcript with different language options
|
| 123 |
+
try:
|
| 124 |
+
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 125 |
+
available_transcripts = list(transcript_list)
|
| 126 |
+
|
| 127 |
+
if available_transcripts:
|
| 128 |
+
# Try the first available transcript
|
| 129 |
+
transcript = available_transcripts[0].fetch()
|
| 130 |
+
logger.info(f"Found alternative transcript in language: {available_transcripts[0].language}")
|
| 131 |
+
return transcript
|
| 132 |
+
else:
|
| 133 |
+
logger.warning("No transcripts available for this video")
|
| 134 |
+
except Exception as e2:
|
| 135 |
+
logger.error(f"Error getting alternative transcript: {e2}")
|
| 136 |
+
|
| 137 |
+
# Try using YouTube's timedtext API directly
|
| 138 |
+
try:
|
| 139 |
+
logger.info("Attempting to fetch transcript using YouTube timedtext API")
|
| 140 |
+
# First, get the video page to find available timedtext tracks
|
| 141 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 142 |
+
response = requests.get(video_url)
|
| 143 |
+
html_content = response.text
|
| 144 |
+
|
| 145 |
+
# Look for timedtext URL in the page source
|
| 146 |
+
timedtext_url_pattern = r'\"captionTracks\":\[\{\"baseUrl\":\"(https:\/\/www.youtube.com\/api\/timedtext[^\"]+)\"'
|
| 147 |
+
match = re.search(timedtext_url_pattern, html_content)
|
| 148 |
+
|
| 149 |
+
if match:
|
| 150 |
+
# Extract the timedtext URL and clean it (replace \u0026 with &)
|
| 151 |
+
timedtext_url = match.group(1).replace('\\u0026', '&')
|
| 152 |
+
logger.info(f"Found timedtext URL: {timedtext_url}")
|
| 153 |
+
|
| 154 |
+
# Fetch the transcript XML
|
| 155 |
+
response = requests.get(timedtext_url)
|
| 156 |
+
|
| 157 |
+
if response.status_code == 200:
|
| 158 |
+
# Parse the XML content
|
| 159 |
+
import xml.etree.ElementTree as ET
|
| 160 |
+
root = ET.fromstring(response.text)
|
| 161 |
+
|
| 162 |
+
# Extract text and timestamps
|
| 163 |
+
transcript = []
|
| 164 |
+
for text_element in root.findall('.//text'):
|
| 165 |
+
start = float(text_element.get('start', '0'))
|
| 166 |
+
duration = float(text_element.get('dur', '0'))
|
| 167 |
+
|
| 168 |
+
# Clean up text (remove HTML entities)
|
| 169 |
+
text = text_element.text or ""
|
| 170 |
+
text = text.replace('&', '&').replace('<', '<').replace('>', '>')
|
| 171 |
+
|
| 172 |
+
transcript.append({
|
| 173 |
+
"text": text,
|
| 174 |
+
"start": start,
|
| 175 |
+
"duration": duration
|
| 176 |
+
})
|
| 177 |
+
|
| 178 |
+
if transcript:
|
| 179 |
+
logger.info(f"Successfully extracted {len(transcript)} segments from timedtext API")
|
| 180 |
+
return transcript
|
| 181 |
+
else:
|
| 182 |
+
logger.warning("No timedtext URL found in video page")
|
| 183 |
+
except Exception as e3:
|
| 184 |
+
logger.error(f"Error getting transcript from timedtext API: {e3}")
|
| 185 |
+
|
| 186 |
+
# Try to extract automatic captions from player response
|
| 187 |
+
try:
|
| 188 |
+
logger.info("Attempting to extract automatic captions from player response")
|
| 189 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 190 |
+
response = requests.get(video_url)
|
| 191 |
+
html_content = response.text
|
| 192 |
+
|
| 193 |
+
# Extract player response JSON
|
| 194 |
+
player_response_pattern = r'ytInitialPlayerResponse\s*=\s*({.+?});'
|
| 195 |
+
match = re.search(player_response_pattern, html_content)
|
| 196 |
+
|
| 197 |
+
if match:
|
| 198 |
+
player_response_str = match.group(1)
|
| 199 |
+
try:
|
| 200 |
+
player_response = json.loads(player_response_str)
|
| 201 |
+
|
| 202 |
+
# Navigate to captions data
|
| 203 |
+
captions_data = player_response.get('captions', {}).get('playerCaptionsTracklistRenderer', {}).get('captionTracks', [])
|
| 204 |
+
|
| 205 |
+
if captions_data:
|
| 206 |
+
# Look for automatic captions first
|
| 207 |
+
auto_captions = None
|
| 208 |
+
for caption in captions_data:
|
| 209 |
+
if caption.get('kind') == 'asr' or 'auto-generated' in caption.get('name', {}).get('simpleText', '').lower():
|
| 210 |
+
auto_captions = caption
|
| 211 |
+
break
|
| 212 |
+
|
| 213 |
+
# If no auto captions, use the first available
|
| 214 |
+
if not auto_captions and captions_data:
|
| 215 |
+
auto_captions = captions_data[0]
|
| 216 |
+
|
| 217 |
+
if auto_captions:
|
| 218 |
+
base_url = auto_captions.get('baseUrl')
|
| 219 |
+
if base_url:
|
| 220 |
+
logger.info(f"Found caption track: {auto_captions.get('name', {}).get('simpleText', 'Unknown')}")
|
| 221 |
+
|
| 222 |
+
# Add format=json3 to get JSON instead of XML
|
| 223 |
+
json_url = f"{base_url}&fmt=json3"
|
| 224 |
+
response = requests.get(json_url)
|
| 225 |
+
|
| 226 |
+
if response.status_code == 200:
|
| 227 |
+
caption_data = response.json()
|
| 228 |
+
events = caption_data.get('events', [])
|
| 229 |
+
|
| 230 |
+
transcript = []
|
| 231 |
+
for event in events:
|
| 232 |
+
# Skip events without text
|
| 233 |
+
if 'segs' not in event:
|
| 234 |
+
continue
|
| 235 |
+
|
| 236 |
+
start = event.get('tStartMs', 0) / 1000 # Convert to seconds
|
| 237 |
+
duration = (event.get('dDurationMs', 0) / 1000)
|
| 238 |
+
|
| 239 |
+
# Combine all segments
|
| 240 |
+
text_parts = []
|
| 241 |
+
for seg in event.get('segs', []):
|
| 242 |
+
if 'utf8' in seg:
|
| 243 |
+
text_parts.append(seg['utf8'])
|
| 244 |
+
|
| 245 |
+
text = ' '.join(text_parts).strip()
|
| 246 |
+
if text:
|
| 247 |
+
transcript.append({
|
| 248 |
+
"text": text,
|
| 249 |
+
"start": start,
|
| 250 |
+
"duration": duration
|
| 251 |
+
})
|
| 252 |
+
|
| 253 |
+
if transcript:
|
| 254 |
+
logger.info(f"Successfully extracted {len(transcript)} segments from automatic captions")
|
| 255 |
+
return transcript
|
| 256 |
+
except json.JSONDecodeError:
|
| 257 |
+
logger.error("Failed to parse player response JSON")
|
| 258 |
+
else:
|
| 259 |
+
logger.warning("No player response found in video page")
|
| 260 |
+
except Exception as e4:
|
| 261 |
+
logger.error(f"Error extracting automatic captions: {e4}")
|
| 262 |
+
|
| 263 |
+
# If no transcript is available, create a dummy transcript with timestamps
|
| 264 |
+
# This allows the app to continue and at least show video info
|
| 265 |
+
logger.warning("Creating dummy transcript for video without captions")
|
| 266 |
+
|
| 267 |
+
# Get video length from video_info if available, otherwise use default (10 minutes)
|
| 268 |
+
try:
|
| 269 |
+
# Try to get video info to determine actual length
|
| 270 |
+
video_info = get_video_info(video_id)
|
| 271 |
+
video_length = video_info.get("length", 600) # Default to 10 minutes if not available
|
| 272 |
+
|
| 273 |
+
# If video length is 0 (from fallback method), use default 10 minutes
|
| 274 |
+
if video_length == 0:
|
| 275 |
+
video_length = 600
|
| 276 |
+
|
| 277 |
+
logger.info(f"Using video length of {video_length} seconds for dummy transcript")
|
| 278 |
+
except Exception:
|
| 279 |
+
# If we can't get video info, use default 10 minutes
|
| 280 |
+
video_length = 600
|
| 281 |
+
logger.info("Using default 10 minute length for dummy transcript")
|
| 282 |
+
|
| 283 |
+
# Create timestamps every 30 seconds
|
| 284 |
+
interval = 30 # seconds between segments
|
| 285 |
+
dummy_transcript = []
|
| 286 |
+
|
| 287 |
+
# Ensure we have at least 5 segments even for very short videos
|
| 288 |
+
min_segments = 5
|
| 289 |
+
if video_length < interval * min_segments:
|
| 290 |
+
interval = max(5, video_length // min_segments)
|
| 291 |
+
|
| 292 |
+
for i in range(0, video_length, interval):
|
| 293 |
+
minutes = i // 60
|
| 294 |
+
seconds = i % 60
|
| 295 |
+
dummy_transcript.append({
|
| 296 |
+
"text": f"[No transcript available at {minutes}:{seconds:02d}]",
|
| 297 |
+
"start": i,
|
| 298 |
+
"duration": min(interval, video_length - i) # Ensure last segment doesn't exceed video length
|
| 299 |
+
})
|
| 300 |
+
|
| 301 |
+
return dummy_transcript
|
| 302 |
|
| 303 |
def get_video_chapters(video_id: str) -> List[Dict[str, Any]]:
|
| 304 |
+
"""Get chapters for a YouTube video."""
|
| 305 |
+
logger.info(f"Getting chapters for video {video_id}")
|
| 306 |
+
|
| 307 |
+
chapters = []
|
| 308 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
| 309 |
+
|
| 310 |
+
# Method 1: Try to extract chapters directly from the HTML content
|
| 311 |
try:
|
| 312 |
+
logger.info("Attempting to extract chapters directly from HTML content")
|
| 313 |
+
|
| 314 |
+
# Create a session with headers that mimic a browser
|
| 315 |
+
session = requests.Session()
|
| 316 |
+
headers = {
|
| 317 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
| 318 |
+
"Accept-Language": "en-US,en;q=0.9",
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
# Get the video page
|
| 322 |
+
response = session.get(video_url, headers=headers)
|
| 323 |
+
html_content = response.text
|
| 324 |
+
|
| 325 |
+
# Save the HTML content for debugging
|
| 326 |
+
debug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "debug")
|
| 327 |
+
os.makedirs(debug_dir, exist_ok=True)
|
| 328 |
+
with open(os.path.join(debug_dir, f"html_{video_id}.txt"), "w", encoding="utf-8") as f:
|
| 329 |
+
f.write(html_content)
|
| 330 |
+
|
| 331 |
+
# Look for chapter titles in the transcript panel
|
| 332 |
+
# Pattern to match chapter titles in span elements with specific class
|
| 333 |
+
chapter_pattern = r'<span class="yt-core-attributed-string yt-core-attributed-string--white-space-pre-wrap" role="text">([^<]+)</span>'
|
| 334 |
+
chapter_matches = re.findall(chapter_pattern, html_content)
|
| 335 |
+
|
| 336 |
+
logger.info(f"Found {len(chapter_matches)} potential chapter titles in HTML")
|
| 337 |
+
|
| 338 |
+
# Also look for timestamps associated with chapters
|
| 339 |
+
timestamp_pattern = r'<span class="segment-timestamp style-scope ytd-transcript-segment-renderer">(\d+:\d+)</span>'
|
| 340 |
+
timestamp_matches = re.findall(timestamp_pattern, html_content)
|
| 341 |
+
|
| 342 |
+
logger.info(f"Found {len(timestamp_matches)} potential timestamps in HTML")
|
| 343 |
+
|
| 344 |
+
# If we have both chapter titles and timestamps, combine them
|
| 345 |
+
if chapter_matches and timestamp_matches:
|
| 346 |
+
logger.info("Found both chapter titles and timestamps, attempting to match them")
|
| 347 |
|
| 348 |
+
# Check if we have exactly 4 chapter titles as mentioned by the user
|
| 349 |
+
if len(chapter_matches) >= 4 and "Intro" in chapter_matches and "Don't forget to commit!" in chapter_matches and "Cursor Runaway!" in chapter_matches and "Closing" in chapter_matches:
|
| 350 |
+
logger.info("Found the specific chapter titles mentioned by the user")
|
| 351 |
+
|
| 352 |
+
# Create chapters with estimated timestamps if we can't match them exactly
|
| 353 |
+
# These are the specific chapter titles mentioned by the user
|
| 354 |
+
specific_titles = ["Intro", "Don't forget to commit!", "Cursor Runaway!", "Closing"]
|
| 355 |
+
|
| 356 |
+
# Try to get video length from HTML
|
| 357 |
+
length_pattern = r'"lengthSeconds":"(\d+)"'
|
| 358 |
+
length_match = re.search(length_pattern, html_content)
|
| 359 |
+
video_length = 0
|
| 360 |
+
|
| 361 |
+
if length_match:
|
| 362 |
+
video_length = int(length_match.group(1))
|
| 363 |
+
else:
|
| 364 |
+
# Default to a large value if we can't find the video length
|
| 365 |
+
video_length = 3600 # 1 hour
|
| 366 |
+
|
| 367 |
+
# Create chapters with estimated timestamps
|
| 368 |
+
chapter_count = len(specific_titles)
|
| 369 |
+
segment_length = video_length / chapter_count
|
| 370 |
+
|
| 371 |
+
for i, title in enumerate(specific_titles):
|
| 372 |
+
start_time = i * segment_length
|
| 373 |
+
|
| 374 |
+
chapters.append({
|
| 375 |
+
"title": title.strip(),
|
| 376 |
+
"start_time": start_time,
|
| 377 |
+
"time_str": f"{int(start_time // 60)}:{int(start_time % 60):02d}"
|
| 378 |
+
})
|
| 379 |
+
|
| 380 |
+
# Calculate end times for each chapter
|
| 381 |
+
for i in range(len(chapters) - 1):
|
| 382 |
+
chapters[i]["end_time"] = chapters[i + 1]["start_time"]
|
| 383 |
+
|
| 384 |
+
# Set end time for last chapter to video length
|
| 385 |
+
if chapters:
|
| 386 |
+
chapters[-1]["end_time"] = video_length
|
| 387 |
+
|
| 388 |
+
logger.info(f"Created {len(chapters)} chapters with estimated timestamps")
|
| 389 |
+
return chapters
|
| 390 |
+
|
| 391 |
+
# If we couldn't match timestamps with titles, try another approach
|
| 392 |
+
# Look for chapter data in the JavaScript
|
| 393 |
+
chapter_data_pattern = r'chapterRenderer":\s*\{[^}]*"title":\s*\{"simpleText":\s*"([^"]+)"\}[^}]*"timeRangeStartMillis":\s*(\d+)'
|
| 394 |
+
chapter_data_matches = re.findall(chapter_data_pattern, html_content)
|
| 395 |
+
|
| 396 |
+
logger.info(f"Found {len(chapter_data_matches)} chapters in JavaScript data")
|
| 397 |
+
|
| 398 |
+
if chapter_data_matches:
|
| 399 |
+
for title, start_time_ms in chapter_data_matches:
|
| 400 |
+
start_time = int(start_time_ms) / 1000 # Convert to seconds
|
| 401 |
+
|
| 402 |
+
chapters.append({
|
| 403 |
+
"title": title.strip(),
|
| 404 |
+
"start_time": start_time,
|
| 405 |
+
"time_str": f"{int(start_time // 60)}:{int(start_time % 60):02d}"
|
| 406 |
+
})
|
| 407 |
|
| 408 |
+
# If chapters found, process them
|
| 409 |
+
if chapters:
|
| 410 |
+
# Try to get video length from HTML
|
| 411 |
+
length_pattern = r'"lengthSeconds":"(\d+)"'
|
| 412 |
+
length_match = re.search(length_pattern, html_content)
|
| 413 |
+
video_length = 0
|
| 414 |
+
|
| 415 |
+
if length_match:
|
| 416 |
+
video_length = int(length_match.group(1))
|
| 417 |
+
else:
|
| 418 |
+
# Default to a large value if we can't find the video length
|
| 419 |
+
video_length = 3600 # 1 hour
|
| 420 |
+
|
| 421 |
+
# Sort chapters by start time
|
| 422 |
+
chapters = sorted(chapters, key=lambda x: x["start_time"])
|
| 423 |
+
|
| 424 |
+
# Calculate end times for each chapter
|
| 425 |
+
for i in range(len(chapters) - 1):
|
| 426 |
+
chapters[i]["end_time"] = chapters[i + 1]["start_time"]
|
| 427 |
+
|
| 428 |
+
# Set end time for last chapter to video length
|
| 429 |
+
if chapters:
|
| 430 |
+
chapters[-1]["end_time"] = video_length
|
| 431 |
+
|
| 432 |
+
logger.info(f"Found {len(chapters)} chapters from JavaScript data")
|
| 433 |
+
return chapters
|
| 434 |
+
|
| 435 |
+
except Exception as e:
|
| 436 |
+
logger.error(f"Error extracting chapters from HTML: {e}")
|
| 437 |
+
|
| 438 |
+
# Method 2: Try using pytube to get the player_response directly
|
| 439 |
+
try:
|
| 440 |
+
yt = YouTube(video_url)
|
| 441 |
+
logger.info("Successfully created YouTube object with pytube")
|
| 442 |
|
| 443 |
+
# Get player_response from pytube
|
| 444 |
+
try:
|
| 445 |
+
player_response = json.loads(yt.player_config['args']['player_response'])
|
| 446 |
+
logger.info("Successfully got player_response from pytube")
|
| 447 |
+
|
| 448 |
+
# Save player response for debugging
|
| 449 |
+
save_debug_info(video_id, player_response, "pytube_player_response")
|
| 450 |
|
| 451 |
+
# Try to find chapters in different locations within the player response
|
| 452 |
+
|
| 453 |
+
# Look in multiMarkersPlayerBarRenderer
|
| 454 |
+
try:
|
| 455 |
+
markers_map = player_response.get('playerOverlays', {}).get('playerOverlayRenderer', {}).get(
|
| 456 |
+
'decoratedPlayerBarRenderer', {}).get('decoratedPlayerBarRenderer', {}).get(
|
| 457 |
+
'playerBar', {}).get('multiMarkersPlayerBarRenderer', {}).get('markersMap', [])
|
| 458 |
+
|
| 459 |
+
if markers_map:
|
| 460 |
+
logger.info(f"Found markers map with {len(markers_map)} entries")
|
| 461 |
+
|
| 462 |
+
for marker in markers_map:
|
| 463 |
+
marker_key = marker.get('key', '')
|
| 464 |
+
logger.info(f"Found marker with key: {marker_key}")
|
| 465 |
+
|
| 466 |
+
if marker_key == 'CHAPTER_MARKERS_KEY':
|
| 467 |
+
chapters_data = marker.get('value', {}).get('chapters', [])
|
| 468 |
+
|
| 469 |
+
if chapters_data:
|
| 470 |
+
logger.info(f"Found {len(chapters_data)} chapters in marker")
|
| 471 |
+
|
| 472 |
+
for chapter in chapters_data:
|
| 473 |
+
chapter_renderer = chapter.get('chapterRenderer', {})
|
| 474 |
+
title = chapter_renderer.get('title', {}).get('simpleText', '')
|
| 475 |
+
start_time_ms = chapter_renderer.get('timeRangeStartMillis', 0)
|
| 476 |
+
start_time = start_time_ms / 1000 # Convert to seconds
|
| 477 |
+
|
| 478 |
+
chapters.append({
|
| 479 |
+
"title": title,
|
| 480 |
+
"start_time": start_time,
|
| 481 |
+
"time_str": f"{int(start_time // 60)}:{int(start_time % 60):02d}"
|
| 482 |
+
})
|
| 483 |
+
except Exception as e:
|
| 484 |
+
logger.error(f"Error extracting chapters from multiMarkersPlayerBarRenderer: {e}")
|
| 485 |
+
|
| 486 |
+
# Look in chapterMarkersRenderer
|
| 487 |
+
if not chapters:
|
| 488 |
+
try:
|
| 489 |
+
chapter_markers = player_response.get('playerOverlays', {}).get('playerOverlayRenderer', {}).get(
|
| 490 |
+
'decoratedPlayerBarRenderer', {}).get('decoratedPlayerBarRenderer', {}).get(
|
| 491 |
+
'playerBar', {}).get('chapterMarkersRenderer', {}).get('markersMap', [])
|
| 492 |
+
|
| 493 |
+
if chapter_markers:
|
| 494 |
+
logger.info(f"Found chapter markers in chapterMarkersRenderer: {len(chapter_markers)}")
|
| 495 |
+
for marker in chapter_markers:
|
| 496 |
+
chapters_data = marker.get('value', {}).get('chapters', [])
|
| 497 |
+
if chapters_data:
|
| 498 |
+
logger.info(f"Found chapters data: {len(chapters_data)} chapters")
|
| 499 |
+
for chapter in chapters_data:
|
| 500 |
+
title = chapter.get('chapterRenderer', {}).get('title', {}).get('simpleText', '')
|
| 501 |
+
start_time_ms = chapter.get('chapterRenderer', {}).get('timeRangeStartMillis', 0)
|
| 502 |
+
start_time = start_time_ms / 1000 # Convert to seconds
|
| 503 |
+
|
| 504 |
+
chapters.append({
|
| 505 |
+
"title": title,
|
| 506 |
+
"start_time": start_time,
|
| 507 |
+
"time_str": f"{int(start_time // 60)}:{int(start_time % 60):02d}"
|
| 508 |
+
})
|
| 509 |
+
except Exception as e:
|
| 510 |
+
logger.error(f"Error extracting chapters from chapterMarkersRenderer: {e}")
|
| 511 |
+
|
| 512 |
+
# If chapters found, process them
|
| 513 |
+
if chapters:
|
| 514 |
+
# Get video length
|
| 515 |
+
video_length = float(player_response.get('videoDetails', {}).get('lengthSeconds', 0))
|
| 516 |
+
|
| 517 |
+
# Sort chapters by start time
|
| 518 |
+
chapters = sorted(chapters, key=lambda x: x["start_time"])
|
| 519 |
+
|
| 520 |
+
# Calculate end times for each chapter
|
| 521 |
+
for i in range(len(chapters) - 1):
|
| 522 |
+
chapters[i]["end_time"] = chapters[i + 1]["start_time"]
|
| 523 |
+
|
| 524 |
+
# Set end time for last chapter to video length
|
| 525 |
+
if chapters:
|
| 526 |
+
chapters[-1]["end_time"] = video_length
|
| 527 |
+
|
| 528 |
+
logger.info(f"Found {len(chapters)} chapters for video {video_id}")
|
| 529 |
+
return chapters
|
| 530 |
|
| 531 |
+
except Exception as e:
|
| 532 |
+
logger.error(f"Error extracting chapters from player_response: {e}")
|
|
|
|
| 533 |
|
| 534 |
+
# If no chapters found in player_response, try to extract from description
|
| 535 |
+
if not chapters:
|
| 536 |
+
try:
|
| 537 |
+
description = yt.description
|
| 538 |
+
logger.info(f"Got video description, length: {len(description)}")
|
| 539 |
+
|
| 540 |
+
# Common chapter patterns in descriptions
|
| 541 |
+
chapter_patterns = [
|
| 542 |
+
r'(\d+:\d+(?::\d+)?)\s*[-–—]\s*(.+?)(?=\n\d+:\d+|\Z)', # 00:00 - Chapter name
|
| 543 |
+
r'(\d+:\d+(?::\d+)?)\s*(.+?)(?=\n\d+:\d+|\Z)' # 00:00 Chapter name
|
| 544 |
+
]
|
| 545 |
+
|
| 546 |
+
for pattern in chapter_patterns:
|
| 547 |
+
matches = re.findall(pattern, description)
|
| 548 |
+
logger.info(f"Found {len(matches)} potential chapter matches with pattern {pattern}")
|
| 549 |
+
|
| 550 |
+
if matches:
|
| 551 |
+
for time_str, title in matches:
|
| 552 |
+
# Convert time string to seconds
|
| 553 |
+
parts = time_str.split(':')
|
| 554 |
+
if len(parts) == 2:
|
| 555 |
+
seconds = int(parts[0]) * 60 + int(parts[1])
|
| 556 |
+
else:
|
| 557 |
+
seconds = int(parts[0]) * 3600 + int(parts[1]) * 60 + int(parts[2])
|
| 558 |
+
|
| 559 |
+
chapters.append({
|
| 560 |
+
"title": title.strip(),
|
| 561 |
+
"start_time": seconds,
|
| 562 |
+
"time_str": time_str
|
| 563 |
+
})
|
| 564 |
+
|
| 565 |
+
# If chapters found, process them
|
| 566 |
+
if chapters:
|
| 567 |
+
# Get video length
|
| 568 |
+
video_length = yt.length
|
| 569 |
+
|
| 570 |
+
# Sort chapters by start time
|
| 571 |
+
chapters = sorted(chapters, key=lambda x: x["start_time"])
|
| 572 |
+
|
| 573 |
+
# Calculate end times for each chapter
|
| 574 |
+
for i in range(len(chapters) - 1):
|
| 575 |
+
chapters[i]["end_time"] = chapters[i + 1]["start_time"]
|
| 576 |
+
|
| 577 |
+
# Set end time for last chapter to video length
|
| 578 |
+
if chapters:
|
| 579 |
+
chapters[-1]["end_time"] = video_length
|
| 580 |
+
|
| 581 |
+
logger.info(f"Found {len(chapters)} chapters from description")
|
| 582 |
+
return chapters
|
| 583 |
+
except Exception as e:
|
| 584 |
+
logger.error(f"Error extracting chapters from description: {e}")
|
| 585 |
|
| 586 |
except Exception as e:
|
| 587 |
+
logger.error(f"Error getting chapters with pytube: {e}")
|
| 588 |
+
|
| 589 |
+
# If no chapters found, return empty list
|
| 590 |
+
logger.info(f"No chapters found for video {video_id}")
|
| 591 |
+
return []
|
| 592 |
+
|
| 593 |
+
def save_debug_info(video_id: str, data: Dict[str, Any], prefix: str = "debug"):
|
| 594 |
+
"""Save debug information to a file."""
|
| 595 |
+
try:
|
| 596 |
+
debug_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "debug")
|
| 597 |
+
os.makedirs(debug_dir, exist_ok=True)
|
| 598 |
+
|
| 599 |
+
debug_file = os.path.join(debug_dir, f"{prefix}_{video_id}.json")
|
| 600 |
+
with open(debug_file, "w", encoding="utf-8") as f:
|
| 601 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 602 |
+
|
| 603 |
+
logger.info(f"Saved debug information to {debug_file}")
|
| 604 |
+
except Exception as e:
|
| 605 |
+
logger.error(f"Error saving debug information: {e}")
|
| 606 |
|
| 607 |
# Main application functions
|
| 608 |
+
def process_video(video_url: str, progress=gr.Progress()):
|
| 609 |
"""Process YouTube video and generate step-by-step guide."""
|
| 610 |
+
logger.info(f"Processing video: {video_url}")
|
| 611 |
+
|
| 612 |
result = {
|
| 613 |
"video_info": {},
|
| 614 |
"chapters": [],
|
| 615 |
"steps": [],
|
| 616 |
+
"memory_usage": {},
|
| 617 |
"error": None,
|
| 618 |
"video_id": None
|
| 619 |
}
|
|
|
|
| 621 |
try:
|
| 622 |
# Extract video ID
|
| 623 |
video_id = extract_video_id(video_url)
|
| 624 |
+
logger.info(f"Extracted video ID: {video_id}")
|
| 625 |
if not video_id:
|
| 626 |
result["error"] = "Invalid YouTube URL"
|
| 627 |
+
logger.error("Invalid YouTube URL")
|
| 628 |
return (
|
| 629 |
ui_components.format_video_info({}),
|
| 630 |
ui_components.format_chapters([]),
|
|
|
|
| 636 |
|
| 637 |
progress(0.1, "Extracting video information...")
|
| 638 |
result["video_info"] = get_video_info(video_id)
|
| 639 |
+
logger.info(f"Video info: {json.dumps(result['video_info'], indent=2)}")
|
| 640 |
+
|
| 641 |
+
# Check if there was an error getting video info
|
| 642 |
+
if "error" in result["video_info"]:
|
| 643 |
+
logger.warning(f"Warning in video info: {result['video_info']['error']}")
|
| 644 |
+
# Continue anyway, as we can still try to process the video
|
| 645 |
|
| 646 |
progress(0.2, "Getting video transcript...")
|
| 647 |
transcript = get_transcript(video_id)
|
| 648 |
+
logger.info(f"Transcript length: {len(transcript) if transcript else 0} segments")
|
| 649 |
+
|
| 650 |
+
# We'll continue even if transcript is empty or contains dummy data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 651 |
|
| 652 |
progress(0.4, "Detecting video chapters...")
|
| 653 |
chapters = get_video_chapters(video_id)
|
| 654 |
+
logger.info(f"Detected chapters: {len(chapters)} chapters")
|
| 655 |
result["chapters"] = chapters
|
| 656 |
|
| 657 |
progress(0.6, "Processing transcript...")
|
| 658 |
processor = SmoLAgentProcessor()
|
| 659 |
+
logger.info("Initialized SmoLAgentProcessor")
|
| 660 |
+
steps = processor.process_transcript(transcript, chapters)
|
| 661 |
+
logger.info(f"Processed transcript: {len(steps)} steps generated")
|
| 662 |
+
result["steps"] = steps
|
| 663 |
|
| 664 |
progress(0.9, "Finalizing guide...")
|
| 665 |
result["memory_usage"] = get_memory_usage()
|
|
|
|
| 672 |
steps_df = ui_components.steps_to_dataframe(result["steps"])
|
| 673 |
memory_html = ui_components.format_memory_usage(result["memory_usage"])
|
| 674 |
|
| 675 |
+
logger.info(f"Final steps dataframe shape: {steps_df.shape if hasattr(steps_df, 'shape') else 'No dataframe'}")
|
| 676 |
return video_info_html, chapters_html, steps_df, memory_html
|
| 677 |
|
| 678 |
except Exception as e:
|
| 679 |
+
logger.error(f"Error processing video: {str(e)}")
|
| 680 |
+
import traceback
|
| 681 |
+
logger.error(traceback.format_exc())
|
| 682 |
result["error"] = str(e)
|
| 683 |
return (
|
| 684 |
ui_components.format_video_info(result.get("video_info", {})),
|
requirements.txt
CHANGED
|
@@ -7,8 +7,9 @@ pygments==2.16.1
|
|
| 7 |
requests==2.31.0
|
| 8 |
beautifulsoup4==4.12.2
|
| 9 |
pydantic==2.5.2
|
| 10 |
-
huggingface_hub
|
| 11 |
numpy==1.26.2
|
| 12 |
pillow==10.1.0
|
| 13 |
tqdm==4.66.1
|
| 14 |
psutil==5.9.6
|
|
|
|
|
|
| 7 |
requests==2.31.0
|
| 8 |
beautifulsoup4==4.12.2
|
| 9 |
pydantic==2.5.2
|
| 10 |
+
huggingface_hub>=0.28.1
|
| 11 |
numpy==1.26.2
|
| 12 |
pillow==10.1.0
|
| 13 |
tqdm==4.66.1
|
| 14 |
psutil==5.9.6
|
| 15 |
+
torch==2.0.0
|
smolagent_processor.py
CHANGED
|
@@ -287,7 +287,8 @@ class TranscriptProcessor:
|
|
| 287 |
"""Extract steps using rule-based approach."""
|
| 288 |
steps = []
|
| 289 |
current_text = ""
|
| 290 |
-
current_timestamp =
|
|
|
|
| 291 |
|
| 292 |
for transcript_segment in segment["segments"]:
|
| 293 |
text = transcript_segment["text"]
|
|
@@ -295,6 +296,7 @@ class TranscriptProcessor:
|
|
| 295 |
|
| 296 |
# Check for step indicators
|
| 297 |
if re.match(r'^\d+[\.\)]|^Step|^First|^Next|^Then|^Finally|^Now', text, re.IGNORECASE):
|
|
|
|
| 298 |
# If we have accumulated text, create a step
|
| 299 |
if current_text:
|
| 300 |
# Check for code in the current text
|
|
@@ -317,7 +319,11 @@ class TranscriptProcessor:
|
|
| 317 |
current_timestamp = start
|
| 318 |
else:
|
| 319 |
# Continue current step
|
| 320 |
-
current_text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
# Add the last step
|
| 323 |
if current_text:
|
|
@@ -335,6 +341,59 @@ class TranscriptProcessor:
|
|
| 335 |
)
|
| 336 |
steps.append(step)
|
| 337 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
return steps
|
| 339 |
|
| 340 |
def process_transcript(self, transcript: List[Dict[str, Any]],
|
|
|
|
| 287 |
"""Extract steps using rule-based approach."""
|
| 288 |
steps = []
|
| 289 |
current_text = ""
|
| 290 |
+
current_timestamp = 0
|
| 291 |
+
step_found = False
|
| 292 |
|
| 293 |
for transcript_segment in segment["segments"]:
|
| 294 |
text = transcript_segment["text"]
|
|
|
|
| 296 |
|
| 297 |
# Check for step indicators
|
| 298 |
if re.match(r'^\d+[\.\)]|^Step|^First|^Next|^Then|^Finally|^Now', text, re.IGNORECASE):
|
| 299 |
+
step_found = True
|
| 300 |
# If we have accumulated text, create a step
|
| 301 |
if current_text:
|
| 302 |
# Check for code in the current text
|
|
|
|
| 319 |
current_timestamp = start
|
| 320 |
else:
|
| 321 |
# Continue current step
|
| 322 |
+
if current_text:
|
| 323 |
+
current_text += " " + text
|
| 324 |
+
else:
|
| 325 |
+
current_text = text
|
| 326 |
+
current_timestamp = start
|
| 327 |
|
| 328 |
# Add the last step
|
| 329 |
if current_text:
|
|
|
|
| 341 |
)
|
| 342 |
steps.append(step)
|
| 343 |
|
| 344 |
+
# If no steps were found with step indicators, create steps based on time intervals
|
| 345 |
+
if not step_found and len(segment["segments"]) > 0:
|
| 346 |
+
logger.info("No step indicators found, creating steps based on time intervals")
|
| 347 |
+
# Create steps every 30 seconds or so
|
| 348 |
+
interval = 30 # seconds
|
| 349 |
+
current_step_text = ""
|
| 350 |
+
current_step_timestamp = segment["segments"][0]["start"]
|
| 351 |
+
last_timestamp = current_step_timestamp
|
| 352 |
+
|
| 353 |
+
for transcript_segment in segment["segments"]:
|
| 354 |
+
text = transcript_segment["text"]
|
| 355 |
+
start = transcript_segment["start"]
|
| 356 |
+
|
| 357 |
+
# If more than interval seconds have passed, create a new step
|
| 358 |
+
if start - last_timestamp > interval:
|
| 359 |
+
if current_step_text:
|
| 360 |
+
code_blocks = self.code_detector.extract_code_blocks(current_step_text)
|
| 361 |
+
is_code = len(code_blocks) > 0
|
| 362 |
+
code_content = code_blocks[0][0] if is_code else None
|
| 363 |
+
code_language = code_blocks[0][1] if is_code else None
|
| 364 |
+
|
| 365 |
+
step = Step(
|
| 366 |
+
text=current_step_text,
|
| 367 |
+
timestamp=current_step_timestamp,
|
| 368 |
+
is_code=is_code,
|
| 369 |
+
code_content=code_content,
|
| 370 |
+
code_language=code_language
|
| 371 |
+
)
|
| 372 |
+
steps.append(step)
|
| 373 |
+
|
| 374 |
+
current_step_text = text
|
| 375 |
+
current_step_timestamp = start
|
| 376 |
+
else:
|
| 377 |
+
current_step_text += " " + text
|
| 378 |
+
|
| 379 |
+
last_timestamp = start
|
| 380 |
+
|
| 381 |
+
# Add the last step
|
| 382 |
+
if current_step_text:
|
| 383 |
+
code_blocks = self.code_detector.extract_code_blocks(current_step_text)
|
| 384 |
+
is_code = len(code_blocks) > 0
|
| 385 |
+
code_content = code_blocks[0][0] if is_code else None
|
| 386 |
+
code_language = code_blocks[0][1] if is_code else None
|
| 387 |
+
|
| 388 |
+
step = Step(
|
| 389 |
+
text=current_step_text,
|
| 390 |
+
timestamp=current_step_timestamp,
|
| 391 |
+
is_code=is_code,
|
| 392 |
+
code_content=code_content,
|
| 393 |
+
code_language=code_language
|
| 394 |
+
)
|
| 395 |
+
steps.append(step)
|
| 396 |
+
|
| 397 |
return steps
|
| 398 |
|
| 399 |
def process_transcript(self, transcript: List[Dict[str, Any]],
|