Spaces:
Sleeping
Sleeping
Ahmed Mostafa commited on
Commit Β·
97fbe9e
1
Parent(s): e7a10ab
config node.js
Browse files
run.py
CHANGED
|
@@ -5,6 +5,7 @@ Provides CLI interface and server startup.
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import sys
|
|
|
|
| 8 |
import argparse
|
| 9 |
from pathlib import Path
|
| 10 |
|
|
@@ -14,71 +15,88 @@ from src.utils.config import settings
|
|
| 14 |
logger = setup_logger(__name__)
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def run_server():
|
| 18 |
"""Start the FastAPI server with CORS enabled for Flutter Web."""
|
| 19 |
import uvicorn
|
| 20 |
from fastapi.middleware.cors import CORSMiddleware
|
| 21 |
-
from src.api.main import app
|
|
|
|
|
|
|
| 22 |
|
| 23 |
logger.info("Configuring CORS for Flutter Web...")
|
| 24 |
|
| 25 |
-
# Add CORS Middleware to allow requests from Chrome/Flutter
|
| 26 |
app.add_middleware(
|
| 27 |
CORSMiddleware,
|
| 28 |
-
allow_origins=["*"],
|
| 29 |
allow_credentials=True,
|
| 30 |
-
allow_methods=["*"],
|
| 31 |
-
allow_headers=["*"],
|
| 32 |
)
|
| 33 |
|
| 34 |
logger.info("Starting YouTube Study Notes AI server...")
|
| 35 |
-
logger.info(
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
-
logger.info(
|
| 39 |
-
f"API Documentation: http://{settings.api_host}:{settings.api_port}/docs"
|
| 40 |
-
)
|
| 41 |
|
| 42 |
-
# Run the server using the app object directly
|
| 43 |
-
# Note: reload is disabled here to ensure CORS settings are applied correctly from this script
|
| 44 |
uvicorn.run(app, host=settings.api_host, port=settings.api_port, log_level="info")
|
| 45 |
|
| 46 |
|
| 47 |
def run_cli(youtube_url: str, output_file: str = None):
|
| 48 |
-
"""
|
| 49 |
-
Run note generation from command line.
|
| 50 |
-
|
| 51 |
-
Args:
|
| 52 |
-
youtube_url: YouTube video URL
|
| 53 |
-
output_file: Optional output file path
|
| 54 |
-
"""
|
| 55 |
from src.audio.downloader import YouTubeDownloader
|
| 56 |
from src.transcription.whisper_transcriber import WhisperTranscriber
|
| 57 |
from src.summarization.note_generator import NoteGenerator
|
| 58 |
|
|
|
|
|
|
|
| 59 |
logger.info("Starting CLI mode")
|
| 60 |
logger.info(f"Processing URL: {youtube_url}")
|
| 61 |
|
| 62 |
try:
|
| 63 |
-
# Step 1: Download audio
|
| 64 |
logger.info("Step 1/3: Downloading audio...")
|
| 65 |
downloader = YouTubeDownloader()
|
| 66 |
video_info = downloader.get_video_info(youtube_url)
|
| 67 |
audio_file = downloader.download_audio(youtube_url)
|
| 68 |
|
| 69 |
-
# Step 2: Transcribe
|
| 70 |
logger.info("Step 2/3: Transcribing audio...")
|
| 71 |
transcriber = WhisperTranscriber()
|
| 72 |
transcript_data = transcriber.transcribe(audio_file)
|
| 73 |
|
| 74 |
-
# Step 3: Generate notes
|
| 75 |
logger.info("Step 3/3: Generating notes...")
|
| 76 |
note_gen = NoteGenerator()
|
| 77 |
notes = note_gen.generate_notes_from_full_transcript(
|
| 78 |
transcript_data["text"], video_info["title"]
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Format and save
|
| 82 |
final_notes = note_gen.format_final_notes(
|
| 83 |
notes, video_info["title"], youtube_url, video_info["duration"]
|
| 84 |
)
|
|
@@ -93,7 +111,6 @@ def run_cli(youtube_url: str, output_file: str = None):
|
|
| 93 |
logger.info(f"β
Notes saved to: {output_path}")
|
| 94 |
print(f"\nβ
Success! Notes saved to: {output_path}")
|
| 95 |
|
| 96 |
-
# Cleanup
|
| 97 |
downloader.cleanup(audio_file)
|
| 98 |
|
| 99 |
except Exception as e:
|
|
@@ -107,20 +124,13 @@ def main():
|
|
| 107 |
parser = argparse.ArgumentParser(
|
| 108 |
description="YouTube Study Notes AI - Generate structured notes from educational videos"
|
| 109 |
)
|
| 110 |
-
|
| 111 |
parser.add_argument(
|
| 112 |
"mode",
|
| 113 |
choices=["server", "cli"],
|
| 114 |
help="Run mode: server (API + web UI) or cli (direct processing)",
|
| 115 |
)
|
| 116 |
-
|
| 117 |
-
parser.add_argument(
|
| 118 |
-
"--url", type=str, help="YouTube video URL (required for cli mode)"
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
parser.add_argument(
|
| 122 |
-
"--output", type=str, help="Output file path (optional for cli mode)"
|
| 123 |
-
)
|
| 124 |
|
| 125 |
args = parser.parse_args()
|
| 126 |
|
|
@@ -134,4 +144,4 @@ def main():
|
|
| 134 |
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
-
main()
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
+
import subprocess
|
| 9 |
import argparse
|
| 10 |
from pathlib import Path
|
| 11 |
|
|
|
|
| 15 |
logger = setup_logger(__name__)
|
| 16 |
|
| 17 |
|
| 18 |
+
def check_environment():
|
| 19 |
+
"""Log key dependency versions to confirm runtime environment."""
|
| 20 |
+
# Check Node.js
|
| 21 |
+
try:
|
| 22 |
+
node_version = subprocess.check_output(
|
| 23 |
+
["node", "--version"], stderr=subprocess.STDOUT
|
| 24 |
+
).decode().strip()
|
| 25 |
+
logger.info(f"β
Node.js available: {node_version} β yt-dlp JS challenges will be solved")
|
| 26 |
+
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 27 |
+
logger.warning("β Node.js NOT found β yt-dlp will fail to solve JS challenges. Add 'nodejs' to Dockerfile.")
|
| 28 |
+
|
| 29 |
+
# Check yt-dlp
|
| 30 |
+
try:
|
| 31 |
+
ytdlp_version = subprocess.check_output(
|
| 32 |
+
["yt-dlp", "--version"], stderr=subprocess.STDOUT
|
| 33 |
+
).decode().strip()
|
| 34 |
+
logger.info(f"β
yt-dlp version: {ytdlp_version}")
|
| 35 |
+
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 36 |
+
logger.warning("β yt-dlp not found in PATH")
|
| 37 |
+
|
| 38 |
+
# Check ffmpeg
|
| 39 |
+
try:
|
| 40 |
+
ffmpeg_out = subprocess.check_output(
|
| 41 |
+
["ffmpeg", "-version"], stderr=subprocess.STDOUT
|
| 42 |
+
).decode().splitlines()[0]
|
| 43 |
+
logger.info(f"β
ffmpeg available: {ffmpeg_out}")
|
| 44 |
+
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 45 |
+
logger.warning("β ffmpeg NOT found β audio extraction will fail")
|
| 46 |
+
|
| 47 |
+
|
| 48 |
def run_server():
|
| 49 |
"""Start the FastAPI server with CORS enabled for Flutter Web."""
|
| 50 |
import uvicorn
|
| 51 |
from fastapi.middleware.cors import CORSMiddleware
|
| 52 |
+
from src.api.main import app
|
| 53 |
+
|
| 54 |
+
check_environment()
|
| 55 |
|
| 56 |
logger.info("Configuring CORS for Flutter Web...")
|
| 57 |
|
|
|
|
| 58 |
app.add_middleware(
|
| 59 |
CORSMiddleware,
|
| 60 |
+
allow_origins=["*"],
|
| 61 |
allow_credentials=True,
|
| 62 |
+
allow_methods=["*"],
|
| 63 |
+
allow_headers=["*"],
|
| 64 |
)
|
| 65 |
|
| 66 |
logger.info("Starting YouTube Study Notes AI server...")
|
| 67 |
+
logger.info(f"Server will be available at http://{settings.api_host}:{settings.api_port}")
|
| 68 |
+
logger.info(f"API Documentation: http://{settings.api_host}:{settings.api_port}/docs")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
|
|
|
| 70 |
uvicorn.run(app, host=settings.api_host, port=settings.api_port, log_level="info")
|
| 71 |
|
| 72 |
|
| 73 |
def run_cli(youtube_url: str, output_file: str = None):
|
| 74 |
+
"""Run note generation from command line."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
from src.audio.downloader import YouTubeDownloader
|
| 76 |
from src.transcription.whisper_transcriber import WhisperTranscriber
|
| 77 |
from src.summarization.note_generator import NoteGenerator
|
| 78 |
|
| 79 |
+
check_environment()
|
| 80 |
+
|
| 81 |
logger.info("Starting CLI mode")
|
| 82 |
logger.info(f"Processing URL: {youtube_url}")
|
| 83 |
|
| 84 |
try:
|
|
|
|
| 85 |
logger.info("Step 1/3: Downloading audio...")
|
| 86 |
downloader = YouTubeDownloader()
|
| 87 |
video_info = downloader.get_video_info(youtube_url)
|
| 88 |
audio_file = downloader.download_audio(youtube_url)
|
| 89 |
|
|
|
|
| 90 |
logger.info("Step 2/3: Transcribing audio...")
|
| 91 |
transcriber = WhisperTranscriber()
|
| 92 |
transcript_data = transcriber.transcribe(audio_file)
|
| 93 |
|
|
|
|
| 94 |
logger.info("Step 3/3: Generating notes...")
|
| 95 |
note_gen = NoteGenerator()
|
| 96 |
notes = note_gen.generate_notes_from_full_transcript(
|
| 97 |
transcript_data["text"], video_info["title"]
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
final_notes = note_gen.format_final_notes(
|
| 101 |
notes, video_info["title"], youtube_url, video_info["duration"]
|
| 102 |
)
|
|
|
|
| 111 |
logger.info(f"β
Notes saved to: {output_path}")
|
| 112 |
print(f"\nβ
Success! Notes saved to: {output_path}")
|
| 113 |
|
|
|
|
| 114 |
downloader.cleanup(audio_file)
|
| 115 |
|
| 116 |
except Exception as e:
|
|
|
|
| 124 |
parser = argparse.ArgumentParser(
|
| 125 |
description="YouTube Study Notes AI - Generate structured notes from educational videos"
|
| 126 |
)
|
|
|
|
| 127 |
parser.add_argument(
|
| 128 |
"mode",
|
| 129 |
choices=["server", "cli"],
|
| 130 |
help="Run mode: server (API + web UI) or cli (direct processing)",
|
| 131 |
)
|
| 132 |
+
parser.add_argument("--url", type=str, help="YouTube video URL (required for cli mode)")
|
| 133 |
+
parser.add_argument("--output", type=str, help="Output file path (optional for cli mode)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
args = parser.parse_args()
|
| 136 |
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
+
main()
|