Merge main: resolve conflicts with Batch 3 (existing instrumental)

Resolves merge conflicts between Batch 4 (YouTube URL input) and Batch 3
(existing instrumental) which was merged while this branch was in progress.

Combined changes:
- Both Batch 3 and Batch 4 are now marked complete in documentation
- Remote CLI now supports both URL input AND existing_instrumental parameter
- Tests include both existing instrumental tests and URL input tests
- Updated poetry.lock with yt-dlp dependency
This commit is contained in:
Andrew Beveridge committed 2025-12-11 02:30:13 -05:00
commit 441f99edeb
9 files changed
+621 -87

No files matched your search

+95 -1
View File
@@ -13,9 +13,11 @@ Supports two upload flows:
import asyncio
import json
import logging
import tempfile
import os
from fastapi import APIRouter, UploadFile, File, Form, HTTPException, BackgroundTasks, Request, Body
from pathlib import Path
from typing import Optional, List, Dict, Any
from typing import Optional, List, Dict, Any, Tuple
from pydantic import BaseModel, Field
@@ -126,6 +128,9 @@ class CreateJobWithUploadUrlsRequest(BaseModel):
clean_instrumental_model: Optional[str] = Field(None, description="Model for clean instrumental separation")
backing_vocals_models: Optional[List[str]] = Field(None, description="Models for backing vocals separation")
other_stems_models: Optional[List[str]] = Field(None, description="Models for other stems")
# Existing instrumental configuration (Batch 3)
existing_instrumental: bool = Field(False, description="Whether an existing instrumental file is being uploaded")
class SignedUploadUrl(BaseModel):
@@ -630,9 +635,62 @@ VALID_FILE_TYPES = {
'style_cdg_title_background': ALLOWED_IMAGE_EXTENSIONS,
'style_cdg_outro_background': ALLOWED_IMAGE_EXTENSIONS,
'lyrics_file': {'.txt', '.docx', '.rtf'},
'existing_instrumental': ALLOWED_AUDIO_EXTENSIONS, # Batch 3: user-provided instrumental
}
async def _validate_audio_durations(
storage: StorageService,
audio_gcs_path: str,
instrumental_gcs_path: str,
tolerance_seconds: float = 0.5
) -> Tuple[bool, float, float]:
"""
Validate that audio and instrumental files have matching durations.
Downloads both files to temp directory and checks their durations using pydub.
Args:
storage: StorageService instance
audio_gcs_path: GCS path to main audio file
instrumental_gcs_path: GCS path to instrumental file
tolerance_seconds: Maximum allowed difference in seconds (default 0.5s)
Returns:
Tuple of (is_valid, audio_duration, instrumental_duration)
"""
from pydub import AudioSegment
temp_dir = tempfile.mkdtemp(prefix="duration_check_")
try:
# Download audio file
audio_local = os.path.join(temp_dir, "audio" + Path(audio_gcs_path).suffix)
storage.download_file(audio_gcs_path, audio_local)
# Download instrumental file
instrumental_local = os.path.join(temp_dir, "instrumental" + Path(instrumental_gcs_path).suffix)
storage.download_file(instrumental_gcs_path, instrumental_local)
# Get durations using pydub (returns milliseconds)
audio_segment = AudioSegment.from_file(audio_local)
audio_duration = len(audio_segment) / 1000.0 # Convert to seconds
instrumental_segment = AudioSegment.from_file(instrumental_local)
instrumental_duration = len(instrumental_segment) / 1000.0 # Convert to seconds
# Check if durations are within tolerance
difference = abs(audio_duration - instrumental_duration)
is_valid = difference <= tolerance_seconds
return is_valid, audio_duration, instrumental_duration
finally:
# Clean up temp files
import shutil
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
def _get_gcs_path_for_file(job_id: str, file_type: str, filename: str) -> str:
"""Generate the GCS path for a file based on its type."""
ext = Path(filename).suffix.lower()
@@ -647,6 +705,9 @@ def _get_gcs_path_for_file(job_id: str, file_type: str, filename: str) -> str:
return f"uploads/{job_id}/style/{asset_key}{ext}"
elif file_type == 'lyrics_file':
return f"uploads/{job_id}/lyrics/user_lyrics{ext}"
elif file_type == 'existing_instrumental':
# Batch 3: user-provided instrumental file
return f"uploads/{job_id}/audio/existing_instrumental{ext}"
else:
return f"uploads/{job_id}/other/{filename}"
@@ -862,6 +923,8 @@ async def mark_uploads_complete(
prefix = f"uploads/{job_id}/style/{asset_key}"
elif file_type == 'lyrics_file':
prefix = f"uploads/{job_id}/lyrics/user_lyrics"
elif file_type == 'existing_instrumental':
prefix = f"uploads/{job_id}/audio/existing_instrumental"
# List files with this prefix to find the actual uploaded file
files = storage_service.list_files(prefix)
@@ -886,6 +949,37 @@ async def mark_uploads_complete(
style_assets[asset_key] = gcs_path
elif file_type == 'lyrics_file':
update_data['lyrics_file_gcs_path'] = gcs_path
elif file_type == 'existing_instrumental':
update_data['existing_instrumental_gcs_path'] = gcs_path
# Validate existing instrumental duration if provided (Batch 3)
audio_gcs_path = update_data.get('input_media_gcs_path')
instrumental_gcs_path = update_data.get('existing_instrumental_gcs_path')
if audio_gcs_path and instrumental_gcs_path:
logger.info(f"Validating instrumental duration for job {job_id}")
try:
duration_valid, audio_duration, instrumental_duration = await _validate_audio_durations(
storage_service, audio_gcs_path, instrumental_gcs_path
)
if not duration_valid:
raise HTTPException(
status_code=400,
detail={
"error": "duration_mismatch",
"message": f"Instrumental duration ({instrumental_duration:.2f}s) does not match audio duration ({audio_duration:.2f}s). "
f"Difference must be within 0.5 seconds.",
"audio_duration": audio_duration,
"instrumental_duration": instrumental_duration,
"difference": abs(audio_duration - instrumental_duration),
}
)
logger.info(f"Duration validation passed: audio={audio_duration:.2f}s, instrumental={instrumental_duration:.2f}s")
except HTTPException:
raise
except Exception as e:
logger.warning(f"Duration validation failed with error: {e}. Proceeding without validation.")
# Don't block the job if we can't validate - the video worker will fail more gracefully
# Add style assets to update if any
if style_assets:
+6
View File
@@ -222,6 +222,9 @@ class Job(BaseModel):
backing_vocals_models: Optional[List[str]] = None # Models for backing vocals separation
other_stems_models: Optional[List[str]] = None # Models for other stems (bass, drums, etc.)
# Existing instrumental configuration (Batch 3)
existing_instrumental_gcs_path: Optional[str] = None # GCS path to user-provided instrumental file
# Processing state
track_output_dir: Optional[str] = None # Local output directory (temp)
audio_hash: Optional[str] = None # Hash for deduplication
@@ -395,6 +398,9 @@ class JobCreate(BaseModel):
backing_vocals_models: Optional[List[str]] = None # Models for backing vocals separation
other_stems_models: Optional[List[str]] = None # Models for other stems (bass, drums, etc.)
# Existing instrumental configuration (Batch 3)
existing_instrumental_gcs_path: Optional[str] = None # GCS path to user-provided instrumental file
# Request metadata (set by API endpoint from request headers)
request_metadata: Dict[str, Any] = Field(default_factory=dict)
"""
+229 -46
View File
@@ -219,13 +219,13 @@ class TestJobModelFieldPresence:
def test_input_media_gcs_path_field_exists(self):
"""Test that Job model has input_media_gcs_path field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow()
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC)
)
# This should not raise AttributeError
@@ -234,13 +234,13 @@ class TestJobModelFieldPresence:
def test_input_media_gcs_path_can_be_set(self):
"""Test that input_media_gcs_path can be set."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
input_media_gcs_path="uploads/test123/file.flac"
)
@@ -249,13 +249,13 @@ class TestJobModelFieldPresence:
def test_pydantic_doesnt_ignore_input_media_gcs_path(self):
"""Test that Pydantic includes input_media_gcs_path in serialization."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
input_media_gcs_path="uploads/test123/file.flac"
)
@@ -294,13 +294,13 @@ class TestLyricsConfigurationFields:
def test_job_has_lyrics_artist_field(self):
"""Test that Job model has lyrics_artist field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
lyrics_artist="Override Artist"
)
@@ -310,13 +310,13 @@ class TestLyricsConfigurationFields:
def test_job_has_lyrics_title_field(self):
"""Test that Job model has lyrics_title field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
lyrics_title="Override Title"
)
@@ -326,13 +326,13 @@ class TestLyricsConfigurationFields:
def test_job_has_lyrics_file_gcs_path_field(self):
"""Test that Job model has lyrics_file_gcs_path field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
lyrics_file_gcs_path="uploads/test123/lyrics/user_lyrics.txt"
)
@@ -342,13 +342,13 @@ class TestLyricsConfigurationFields:
def test_job_has_subtitle_offset_ms_field(self):
"""Test that Job model has subtitle_offset_ms field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
subtitle_offset_ms=500
)
@@ -358,13 +358,13 @@ class TestLyricsConfigurationFields:
def test_subtitle_offset_default_is_zero(self):
"""Test that subtitle_offset_ms defaults to 0."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow()
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC)
)
assert job.subtitle_offset_ms == 0
@@ -433,13 +433,13 @@ class TestAudioModelConfigurationFields:
def test_job_has_clean_instrumental_model_field(self):
"""Test that Job model has clean_instrumental_model field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
clean_instrumental_model="custom_model.ckpt"
)
@@ -449,13 +449,13 @@ class TestAudioModelConfigurationFields:
def test_job_has_backing_vocals_models_field(self):
"""Test that Job model has backing_vocals_models field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
backing_vocals_models=["model1.ckpt", "model2.ckpt"]
)
@@ -465,13 +465,13 @@ class TestAudioModelConfigurationFields:
def test_job_has_other_stems_models_field(self):
"""Test that Job model has other_stems_models field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
other_stems_models=["htdemucs_6s.yaml"]
)
@@ -481,13 +481,13 @@ class TestAudioModelConfigurationFields:
def test_audio_model_fields_are_optional(self):
"""Test that audio model fields default to None."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow()
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC)
)
assert job.clean_instrumental_model is None
@@ -526,13 +526,13 @@ class TestAudioModelConfigurationFields:
def test_pydantic_includes_audio_model_fields_in_serialization(self):
"""Test that Pydantic includes audio model fields in serialization."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
clean_instrumental_model="custom.ckpt",
backing_vocals_models=["bv1.ckpt", "bv2.ckpt"],
other_stems_models=["stems.yaml"]
@@ -783,6 +783,190 @@ class TestCreateJobWithUploadUrlsValidation:
assert 'invalid_type' not in VALID_FILE_TYPES
# ============================================================================
# Batch 3: Existing Instrumental Tests
# ============================================================================
class TestExistingInstrumentalSupport:
"""Test existing instrumental support (Batch 3)."""
def test_valid_file_types_includes_existing_instrumental(self):
"""Test that existing_instrumental is a valid file type."""
from backend.api.routes.file_upload import VALID_FILE_TYPES, ALLOWED_AUDIO_EXTENSIONS
assert 'existing_instrumental' in VALID_FILE_TYPES
assert VALID_FILE_TYPES['existing_instrumental'] == ALLOWED_AUDIO_EXTENSIONS
def test_existing_instrumental_gcs_path(self):
"""Test GCS path generation for existing instrumental file."""
from backend.api.routes.file_upload import _get_gcs_path_for_file
path = _get_gcs_path_for_file("test123", "existing_instrumental", "instrumental.flac")
assert path == "uploads/test123/audio/existing_instrumental.flac"
def test_existing_instrumental_gcs_path_mp3(self):
"""Test GCS path generation for existing instrumental MP3 file."""
from backend.api.routes.file_upload import _get_gcs_path_for_file
path = _get_gcs_path_for_file("test123", "existing_instrumental", "instrumental.mp3")
assert path == "uploads/test123/audio/existing_instrumental.mp3"
def test_existing_instrumental_gcs_path_wav(self):
"""Test GCS path generation for existing instrumental WAV file."""
from backend.api.routes.file_upload import _get_gcs_path_for_file
path = _get_gcs_path_for_file("test123", "existing_instrumental", "my_instrumental.wav")
assert path == "uploads/test123/audio/existing_instrumental.wav"
def test_job_has_existing_instrumental_gcs_path_field(self):
"""Test that Job model has existing_instrumental_gcs_path field."""
from backend.models.job import Job
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
existing_instrumental_gcs_path="uploads/test123/audio/existing_instrumental.flac"
)
assert hasattr(job, 'existing_instrumental_gcs_path')
assert job.existing_instrumental_gcs_path == "uploads/test123/audio/existing_instrumental.flac"
def test_existing_instrumental_gcs_path_optional(self):
"""Test that existing_instrumental_gcs_path is optional."""
from backend.models.job import Job
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC)
)
assert job.existing_instrumental_gcs_path is None
def test_job_create_has_existing_instrumental_gcs_path_field(self):
"""Test that JobCreate model has existing_instrumental_gcs_path field."""
from backend.models.job import JobCreate
job_create = JobCreate(
artist="Artist",
title="Title",
existing_instrumental_gcs_path="uploads/test123/audio/existing_instrumental.flac"
)
assert hasattr(job_create, 'existing_instrumental_gcs_path')
assert job_create.existing_instrumental_gcs_path == "uploads/test123/audio/existing_instrumental.flac"
def test_job_create_existing_instrumental_optional(self):
"""Test that existing_instrumental_gcs_path is optional in JobCreate."""
from backend.models.job import JobCreate
job_create = JobCreate(
artist="Artist",
title="Title"
)
assert job_create.existing_instrumental_gcs_path is None
def test_pydantic_includes_existing_instrumental_in_serialization(self):
"""Test that Pydantic includes existing_instrumental_gcs_path in serialization."""
from backend.models.job import Job
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
existing_instrumental_gcs_path="uploads/test123/audio/existing_instrumental.flac"
)
job_dict = job.model_dump()
assert "existing_instrumental_gcs_path" in job_dict
assert job_dict["existing_instrumental_gcs_path"] == "uploads/test123/audio/existing_instrumental.flac"
@pytest.mark.parametrize("filename,expected_valid", [
("instrumental.mp3", True),
("instrumental.flac", True),
("instrumental.wav", True),
("instrumental.m4a", True),
("instrumental.ogg", True),
("instrumental.aac", True),
("instrumental.txt", False),
("instrumental.pdf", False),
])
def test_existing_instrumental_extension_validation(self, filename, expected_valid):
"""Test that only valid audio extensions are accepted for existing instrumental."""
from pathlib import Path
from backend.api.routes.file_upload import VALID_FILE_TYPES
allowed_extensions = VALID_FILE_TYPES['existing_instrumental']
file_ext = Path(filename).suffix.lower()
is_valid = file_ext in allowed_extensions
assert is_valid == expected_valid
def test_create_job_with_upload_urls_request_has_existing_instrumental_flag(self):
"""Test that CreateJobWithUploadUrlsRequest has existing_instrumental field."""
from backend.api.routes.file_upload import CreateJobWithUploadUrlsRequest, FileUploadRequest
request = CreateJobWithUploadUrlsRequest(
artist="Test Artist",
title="Test Song",
files=[
FileUploadRequest(filename="test.flac", content_type="audio/flac", file_type="audio"),
FileUploadRequest(filename="instr.flac", content_type="audio/flac", file_type="existing_instrumental"),
],
existing_instrumental=True
)
assert request.existing_instrumental is True
def test_create_job_with_upload_urls_request_existing_instrumental_default_false(self):
"""Test that existing_instrumental defaults to False."""
from backend.api.routes.file_upload import CreateJobWithUploadUrlsRequest, FileUploadRequest
request = CreateJobWithUploadUrlsRequest(
artist="Test Artist",
title="Test Song",
files=[
FileUploadRequest(filename="test.flac", content_type="audio/flac", file_type="audio"),
]
)
assert request.existing_instrumental is False
class TestDurationValidation:
"""Test audio duration validation for existing instrumental (Batch 3)."""
def test_validate_audio_durations_function_exists(self):
"""Test that _validate_audio_durations function exists."""
from backend.api.routes.file_upload import _validate_audio_durations
assert callable(_validate_audio_durations)
@pytest.mark.asyncio
async def test_duration_validation_returns_tuple(self):
"""Test that duration validation returns correct tuple structure."""
# This is a structural test - actual implementation tested in integration tests
# The function should return (is_valid: bool, audio_duration: float, instrumental_duration: float)
from backend.api.routes.file_upload import _validate_audio_durations
import inspect
# Verify it's an async function
assert inspect.iscoroutinefunction(_validate_audio_durations)
# ============================================================================
# Batch 4: YouTube URL Input Tests
# ============================================================================
class TestURLValidation:
"""Test URL validation for URL-based job submission."""
@@ -980,13 +1164,13 @@ class TestJobModelURLField:
def test_job_has_url_field(self):
"""Test that Job model has url field."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
)
@@ -996,13 +1180,13 @@ class TestJobModelURLField:
def test_job_url_is_optional(self):
"""Test that url field is optional."""
from backend.models.job import Job
from datetime import datetime
from datetime import datetime, UTC
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow()
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC)
)
assert job.url is None
@@ -1060,4 +1244,3 @@ class TestIsUrlFunction:
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+93
View File
@@ -577,6 +577,99 @@ class TestJobModelIntegration:
assert restored_job.state_data["lyrics_metadata"]["line_count"] == 50
class TestExistingInstrumentalModelFields:
"""Test existing instrumental field in Job and JobCreate models (Batch 3)."""
def test_job_has_existing_instrumental_gcs_path(self):
"""Test that Job model has existing_instrumental_gcs_path field."""
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
existing_instrumental_gcs_path="uploads/test123/audio/existing_instrumental.flac"
)
assert hasattr(job, 'existing_instrumental_gcs_path')
assert job.existing_instrumental_gcs_path == "uploads/test123/audio/existing_instrumental.flac"
def test_job_existing_instrumental_defaults_to_none(self):
"""Test that existing_instrumental_gcs_path defaults to None."""
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC)
)
assert job.existing_instrumental_gcs_path is None
def test_job_create_has_existing_instrumental_gcs_path(self):
"""Test that JobCreate model has existing_instrumental_gcs_path field."""
from backend.models.job import JobCreate
job_create = JobCreate(
artist="Artist",
title="Title",
existing_instrumental_gcs_path="uploads/test/audio/existing_instrumental.mp3"
)
assert job_create.existing_instrumental_gcs_path == "uploads/test/audio/existing_instrumental.mp3"
def test_job_create_existing_instrumental_optional(self):
"""Test that existing_instrumental_gcs_path is optional in JobCreate."""
from backend.models.job import JobCreate
job_create = JobCreate(
artist="Artist",
title="Title"
)
assert job_create.existing_instrumental_gcs_path is None
def test_existing_instrumental_roundtrip_serialization(self):
"""Test that existing_instrumental_gcs_path survives serialization/deserialization."""
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
existing_instrumental_gcs_path="uploads/test123/audio/existing_instrumental.wav"
)
# Serialize to dict (simulates writing to Firestore)
job_dict = job.model_dump()
# Convert to JSON and back (simulates Firestore storage)
json_str = json.dumps(job_dict, default=str)
restored_dict = json.loads(json_str)
# Deserialize back to Job (simulates reading from Firestore)
restored_job = Job(**restored_dict)
assert restored_job.existing_instrumental_gcs_path == "uploads/test123/audio/existing_instrumental.wav"
def test_job_with_full_audio_config(self):
"""Test Job with complete audio configuration including existing instrumental."""
job = Job(
job_id="test123",
status=JobStatus.AUDIO_COMPLETE,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test Artist",
title="Test Song",
# Audio model configuration
clean_instrumental_model="model_bs_roformer_ep_317_sdr_12.9755.ckpt",
backing_vocals_models=["mel_band_roformer_karaoke.ckpt"],
other_stems_models=["htdemucs_6s.yaml"],
# Existing instrumental
existing_instrumental_gcs_path="uploads/test123/audio/my_instrumental.flac",
)
assert job.clean_instrumental_model == "model_bs_roformer_ep_317_sdr_12.9755.ckpt"
assert job.existing_instrumental_gcs_path == "uploads/test123/audio/my_instrumental.flac"
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+35 -11
View File
@@ -135,9 +135,20 @@ async def generate_video(job_id: str) -> bool:
os.chdir(temp_dir)
# Get the selected instrumental file path
# Batch 3: If user provided existing instrumental, use that; otherwise use AI-separated
instrumental_selection = job.state_data['instrumental_selection']
instrumental_suffix = "Clean" if instrumental_selection == 'clean' else "Backing"
instrumental_file = os.path.join(temp_dir, f"{base_name} (Instrumental {instrumental_suffix}).flac")
existing_instrumental_path = getattr(job, 'existing_instrumental_gcs_path', None)
if existing_instrumental_path:
# User provided existing instrumental
ext = Path(existing_instrumental_path).suffix.lower()
instrumental_file = os.path.join(temp_dir, f"{base_name} (Instrumental User){ext}")
instrumental_source = "user-provided"
else:
# Use AI-separated instrumental
instrumental_suffix = "Clean" if instrumental_selection == 'clean' else "Backing"
instrumental_file = os.path.join(temp_dir, f"{base_name} (Instrumental {instrumental_suffix}).flac")
instrumental_source = instrumental_selection
# Transition to encoding state
job_manager.transition_to_state(
@@ -153,7 +164,9 @@ async def generate_video(job_id: str) -> bool:
job_log.info(f" enable_txt: {getattr(job, 'enable_txt', False)}")
job_log.info(f" brand_prefix: {getattr(job, 'brand_prefix', None)}")
job_log.info(f" discord_webhook: {'configured' if getattr(job, 'discord_webhook_url', None) else 'not configured'}")
job_log.info(f" instrumental: {instrumental_selection}")
job_log.info(f" instrumental source: {instrumental_source}")
if existing_instrumental_path:
job_log.info(f" using user-provided instrumental (selection was: {instrumental_selection})")
# Create KaraokeFinalise with ALL the parameters from the job
# This reuses all existing functionality!
@@ -487,15 +500,26 @@ async def _setup_working_directory(
storage.download_file(lyrics_video_url, lyrics_video_path)
log_progress("Downloaded karaoke video")
# Download selected instrumental
# Download instrumental - either user-provided existing instrumental or AI-separated
existing_instrumental_path = getattr(job, 'existing_instrumental_gcs_path', None)
instrumental_selection = job.state_data['instrumental_selection']
instrumental_key = 'instrumental_clean' if instrumental_selection == 'clean' else 'instrumental_with_backing'
instrumental_url = job.file_urls['stems'][instrumental_key]
instrumental_suffix = "Clean" if instrumental_selection == 'clean' else "Backing"
instrumental_path = os.path.join(temp_dir, f"{base_name} (Instrumental {instrumental_suffix}).flac")
log_progress(f"Downloading {instrumental_selection} instrumental audio...")
storage.download_file(instrumental_url, instrumental_path)
log_progress(f"Downloaded instrumental ({instrumental_selection})")
if existing_instrumental_path:
# Batch 3: Use user-provided existing instrumental
ext = Path(existing_instrumental_path).suffix.lower()
instrumental_path = os.path.join(temp_dir, f"{base_name} (Instrumental User){ext}")
log_progress(f"Downloading user-provided existing instrumental...")
storage.download_file(existing_instrumental_path, instrumental_path)
log_progress("Downloaded user-provided instrumental")
else:
# Use AI-separated instrumental based on selection
instrumental_key = 'instrumental_clean' if instrumental_selection == 'clean' else 'instrumental_with_backing'
instrumental_url = job.file_urls['stems'][instrumental_key]
instrumental_suffix = "Clean" if instrumental_selection == 'clean' else "Backing"
instrumental_path = os.path.join(temp_dir, f"{base_name} (Instrumental {instrumental_suffix}).flac")
log_progress(f"Downloading {instrumental_selection} instrumental audio...")
storage.download_file(instrumental_url, instrumental_path)
log_progress(f"Downloaded instrumental ({instrumental_selection})")
# Download LRC file if available (needed for CDG/TXT)
lyrics_urls = job.file_urls.get('lyrics', {})
@@ -1,7 +1,7 @@
# Backend Feature Parity Plan
**Last Updated:** 2024-12-11
**Status:** ✅ Core Feature Parity Achieved (v0.71.0) | Batch 1 ✅ | Batch 2 ✅ | Batch 4 ✅
**Status:** ✅ Core Feature Parity Achieved (v0.71.0) | Batch 1 ✅ | Batch 2 ✅ | Batch 3 ✅ | Batch 4 ✅
This document tracks the progress toward complete feature parity between the local `karaoke-gen` CLI and the cloud backend, enabling the `karaoke-gen-remote` CLI to have equivalent functionality.
@@ -14,7 +14,7 @@ This document tracks the progress toward complete feature parity between the loc
| Lyrics Configuration | 5 | 5 | **100%** | ✅ **Batch 1 Complete** (L1-L4) |
| Style Configuration | 1 | 4 | **25%** | S2-S4 are MEDIUM priority |
| Workflow Control | 0 | 3 | **0%** | W1/W2 HIGH, W7 MEDIUM (skip flags deferred) |
| Audio Processing | 3 | 4 | **75%** | ✅ **Batch 2 Complete** (AP1-AP3), AP5 remaining |
| Audio Processing | 4 | 4 | **100%** | ✅ **Batch 2 & 3 Complete** (AP1-AP3, AP5) |
| Input Modes | 2 | 3 | **67%** | ✅ **Batch 4 Complete** (P2), P3 remaining |
| Audio Fetching | 0 | 1 | **0%** | A1 is HIGH priority |
| Flags to Remove | - | - | - | I3-I6, AP6 (simplify codebase) |
@@ -166,15 +166,15 @@ This section provides a comprehensive comparison of all CLI parameters between `
| AP2 | `--backing_vocals_models` | ✅ | ✅ | **Stage 2 separation model(s).** AI model(s) for separating lead vocals from backing vocals. Default: `mel_band_roformer_karaoke_aufr33_viperx_sdr_10.1956.ckpt`. |
| AP3 | `--other_stems_models` | ✅ | ✅ | **Multi-stem separation model.** AI model for separating drums, bass, guitar, piano, other. Default: `htdemucs_6s.yaml`. |
| AP4 | `--model_file_dir` | ✅ | N/A | **Model cache directory.** Where to store/load AI model files. Default: `/tmp/audio-separator-models/`. |
| AP5 | `--existing_instrumental` | ✅ | ❌ | **Use pre-made instrumental.** Path to an existing instrumental file to use instead of running AI separation. Useful when you have a better instrumental from another source (official instrumental release, manual editing, etc.). Skips audio separation entirely. |
| AP5 | `--existing_instrumental` | ✅ | ✅ | **Use pre-made instrumental.** Path to an existing instrumental file to use instead of running AI separation. Useful when you have a better instrumental from another source (official instrumental release, manual editing, etc.). Backend still runs separation for stems, but uses provided instrumental for final video. |
| AP6 | `--instrumental_format` | 🗑️ | N/A | **REMOVE FROM LOCAL CLI.** FLAC is always fine, no need for format option. |
**Remote Implementation Status:** ✅ **BATCH 2 COMPLETE** (PR #9, v0.71.24)
**Remote Implementation Status:** ✅ **BATCH 2 & 3 COMPLETE** (PR #9 v0.71.24, PR #10 v0.71.26)
- **AP1 `--clean_instrumental_model`**: ✅ IMPLEMENTED. Sent as form field to backend. Audio worker passes to Modal API.
- **AP2 `--backing_vocals_models`**: ✅ IMPLEMENTED. Sent as comma-separated list, parsed and passed to Modal API.
- **AP3 `--other_stems_models`**: ✅ IMPLEMENTED. Sent as comma-separated list, parsed and passed to Modal API.
- **AP4 `--model_file_dir`**: N/A for remote - Modal API handles model caching internally.
- **AP5 `--existing_instrumental`**: HIGH PRIORITY (Batch 3). User uploads instrumental file with job. Backend STILL runs separation (for consistent stem outputs in download folder), but uses provided instrumental for karaoke remux and final video. **VALIDATION REQUIRED**: At job start, validate provided instrumental duration matches input audio duration (within 0.5 seconds tolerance) to ensure sync before proceeding.
- **AP5 `--existing_instrumental`**: ✅ IMPLEMENTED (Batch 3). User uploads instrumental file with job. Backend validates duration match (±0.5s), still runs separation for stems, but uses provided instrumental for final video encoding.
- **AP6**: REMOVE from local CLI - FLAC is always fine.
---
@@ -345,21 +345,25 @@ Bump the patch version in `pyproject.toml` before committing.
---
### Batch 3: Existing Instrumental Support (HIGH)
### Batch 3: Existing Instrumental Support (HIGH) ✅ COMPLETE
**Parameters:** AP5 `--existing_instrumental`
**Scope:**
- Remote CLI: Upload instrumental file alongside audio file
- Backend: Accept instrumental file upload
- Backend: Validate duration matches input audio (±0.5s) at job start
- Backend: Still run separation for stems, but use provided instrumental for final video
- Backend: Store instrumental path, render worker uses it instead of separated instrumental
**Status:** ✅ **IMPLEMENTED** - PR #10 merged (v0.71.26)
**Files to modify:**
- `karaoke_gen/utils/remote_cli.py` - upload instrumental file
- `backend/api/routes/file_upload.py` - accept instrumental upload, add validation
- `backend/workers/audio_worker.py` - mark instrumental source in outputs
- `backend/workers/render_worker.py` - use provided instrumental if present
**Implementation Summary:**
- Remote CLI: Added `--existing_instrumental` parameter, uploads file as `existing_instrumental` type
- Backend: Added `existing_instrumental` to VALID_FILE_TYPES with audio extension validation
- Backend: Duration validation (±0.5s tolerance) at uploads-complete endpoint using pydub
- Backend: Job model has `existing_instrumental_gcs_path` field
- Backend: Video worker downloads user-provided instrumental if present, uses it for final encoding
**Files modified:**
- `karaoke_gen/utils/remote_cli.py` - submit_job() with existing_instrumental param
- `backend/api/routes/file_upload.py` - VALID_FILE_TYPES, GCS path generation, duration validation
- `backend/models/job.py` - existing_instrumental_gcs_path field
- `backend/workers/video_worker.py` - downloads and uses user-provided instrumental
**Tests added:** 20+ unit tests covering model fields, file type validation, GCS path generation, duration validation
**Complexity:** Medium (file upload + validation + conditional logic)
@@ -510,7 +514,7 @@ Bump the patch version in `pyproject.toml` before committing.
|-------|------------|----------|------------|--------|
| 1 | L1-L4 (lyrics config) | HIGH | Low-Medium | ✅ **COMPLETE** (PR #8) |
| 2 | AP1-AP3 (model selection) | HIGH | Low | ✅ **COMPLETE** (PR #9) |
| 3 | AP5 (existing instrumental) | HIGH | Medium | ⏳ Pending |
| 3 | AP5 (existing instrumental) | HIGH | Medium | ✅ **COMPLETE** (PR #10) |
| 4 | P2 (YouTube URLs) | HIGH | Medium-High | ✅ **COMPLETE** |
| 5 | P3, A1 (flacfetch search) | HIGH | High | ⏳ Pending |
| 6 | W1, W2, F15 (two-phase) | HIGH | High | ⏳ Pending |
@@ -519,9 +523,9 @@ Bump the patch version in `pyproject.toml` before committing.
| 9 | W7 (edit-lyrics) | MEDIUM | Medium-High | ⏳ Pending |
| 10 | F14, D2, D3, removals | LOW | Mixed | ⏳ Pending |
**Recommended Order:** ~~1~~ → ~~2~~ → 3 → ~~4~~ → 5 → 6 → 7 → 8 → 9 → 10
**Recommended Order:** ~~1~~ → ~~2~~ → ~~3~~ → ~~4~~ → 5 → 6 → 7 → 8 → 9 → 10
Batches 1-3 are quick wins that add significant value. Batches 4-6 are larger but high priority. Batches 7-10 can be done as time permits.
Batches 1-4 are quick wins that add significant value. Batches 5-6 are larger but high priority. Batches 7-10 can be done as time permits.
---
@@ -629,6 +633,7 @@ The karaoke-gen system supports multiple interfaces to the same core functionali
| Feature | Version | PR | Notes |
|---------|---------|-----|-------|
| YouTube URL input | v0.71.26 | - | `<url>` positional argument for YouTube/online URLs |
| Existing instrumental | v0.71.26 | #10 | `--existing_instrumental` - upload and use user-provided instrumental |
| Audio model selection | v0.71.24 | #9 | `--clean_instrumental_model`, `--backing_vocals_models`, `--other_stems_models` |
| Lyrics override params | v0.71.22 | #8 | `--lyrics_artist`, `--lyrics_title`, `--lyrics_file`, `--subtitle_offset_ms` |
@@ -636,7 +641,6 @@ The karaoke-gen system supports multiple interfaces to the same core functionali
| Feature | Priority | Notes |
|---------|----------|-------|
| Existing instrumental | HIGH | `--existing_instrumental` for re-processing |
| Style override | MEDIUM | `--style_override` for quick tweaks |
| Skip separation/transcription | MEDIUM | Workflow control for re-processing |
| Edit lyrics mode | MEDIUM | `--edit-lyrics` for fixing existing tracks |
@@ -887,6 +891,16 @@ DEFAULT_DISCORD_WEBHOOK_URL=https://discord.com/...
## Recent Changes Log
### 2024-12-11: v0.71.26 - Batch 3 Complete (Existing Instrumental Support)
**PR #10: Existing Instrumental Support for Remote CLI**
- ✅ `--existing_instrumental` - Upload and use user-provided instrumental file
- Duration validation (±0.5s tolerance) before processing starts
- Backend still runs AI separation for stem downloads, but uses provided instrumental for final video
- Added 20+ unit tests for new functionality
- Files: `remote_cli.py`, `file_upload.py`, `job.py`, `video_worker.py`
### 2024-12-11: v0.71.24 - Batch 2 Complete (Audio Model Selection)
**PR #9: Audio Model Selection for Remote CLI**
@@ -963,12 +977,11 @@ DEFAULT_DISCORD_WEBHOOK_URL=https://discord.com/...
- `--clean_instrumental_model`, `--backing_vocals_models`, `--other_stems_models`
- PR #9, v0.71.24
3. **Existing Instrumental** (Batch 3 - NEXT)
- Add `--existing_instrumental` upload support
- Files: `remote_cli.py`, `file_upload.py`, `audio_worker.py`, `render_worker.py`
- Enables: Re-processing with better instrumentals
3. ✅ **Existing Instrumental** (Batch 3 - COMPLETE)
- Added `--existing_instrumental` upload support
- PR #10, v0.71.26
4. **YouTube URL Input** (Batch 4)
4. **YouTube URL Input** (Batch 4 - NEXT)
- Accept YouTube URLs as input
- Requires: yt-dlp in container
- Enables: Processing niche live versions only on YouTube
+18 -2
View File
@@ -388,6 +388,8 @@ class RemoteKaraokeClient:
clean_instrumental_model: Optional[str] = None,
backing_vocals_models: Optional[list] = None,
other_stems_models: Optional[list] = None,
# Existing instrumental (Batch 3)
existing_instrumental: Optional[str] = None,
) -> Dict[str, Any]:
"""
Submit a new karaoke generation job with optional style configuration.
@@ -418,6 +420,7 @@ class RemoteKaraokeClient:
clean_instrumental_model: Model for clean instrumental separation
backing_vocals_models: List of models for backing vocals separation
other_stems_models: List of models for other stems (bass, drums, etc.)
existing_instrumental: Path to existing instrumental file to use instead of AI separation
"""
file_path = Path(filepath)
@@ -483,6 +486,17 @@ class RemoteKaraokeClient:
local_files['lyrics_file'] = lyrics_file
self.logger.info(f"Will upload lyrics file: {lyrics_file}")
# Add existing instrumental file if provided (Batch 3)
if existing_instrumental and os.path.isfile(existing_instrumental):
content_type = self._get_content_type(existing_instrumental)
files_info.append({
'filename': Path(existing_instrumental).name,
'content_type': content_type,
'file_type': 'existing_instrumental'
})
local_files['existing_instrumental'] = existing_instrumental
self.logger.info(f"Will upload existing instrumental: {existing_instrumental}")
# Step 2: Create job and get signed upload URLs
self.logger.info(f"Creating job at {self.config.service_url}/api/jobs/create-with-upload-urls")
@@ -1881,8 +1895,6 @@ def main():
ignored_features.append("--skip-transcription")
if args.lyrics_only:
ignored_features.append("--lyrics-only")
if args.existing_instrumental:
ignored_features.append("--existing_instrumental")
if args.background_video:
ignored_features.append("--background_video")
if getattr(args, 'auto_download', False):
@@ -2011,6 +2023,8 @@ def main():
logger.info(f"Backing Vocals Models: {args.backing_vocals_models}")
if getattr(args, 'other_stems_models', None):
logger.info(f"Other Stems Models: {args.other_stems_models}")
if getattr(args, 'existing_instrumental', None):
logger.info(f"Existing Instrumental: {args.existing_instrumental}")
logger.info(f"Service URL: {config.service_url}")
logger.info(f"Review UI: {config.review_ui_url}")
if config.non_interactive:
@@ -2081,6 +2095,8 @@ def main():
clean_instrumental_model=getattr(args, 'clean_instrumental_model', None),
backing_vocals_models=getattr(args, 'backing_vocals_models', None),
other_stems_models=getattr(args, 'other_stems_models', None),
# Existing instrumental (Batch 3)
existing_instrumental=getattr(args, 'existing_instrumental', None),
)
job_id = result.get('job_id')
style_assets = result.get('style_assets_uploaded', [])
Generated
+1 -1
View File
@@ -9512,4 +9512,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
content-hash = "833da1a111ed1fc56eff6e363e36d7196d9dd144050d95c0aff4a301497853b5"
content-hash = "b3cf2a2a221d29bc8f928632f6f362259faba425476d65795a7bb163f7e3a7a2"
+105
View File
@@ -652,6 +652,111 @@ class TestRemoteKaraokeClient:
# Verify uploads were called for both files
assert mock_upload.call_count == 2
@patch.object(RemoteKaraokeClient, '_upload_file_to_signed_url')
@patch.object(RemoteKaraokeClient, '_request')
def test_submit_job_with_existing_instrumental(self, mock_request, mock_upload, client, tmp_path):
"""Test job submission with existing instrumental via signed URL flow (Batch 3)."""
# Create test files
audio_file = tmp_path / "test.mp3"
audio_file.write_bytes(b"audio data")
instrumental_file = tmp_path / "instrumental.flac"
instrumental_file.write_bytes(b"instrumental data")
# Mock create job response (first API call)
create_response = MagicMock()
create_response.status_code = 200
create_response.json.return_value = {
"status": "success",
"job_id": "job-with-instrumental-123",
"upload_urls": [
{
"file_type": "audio",
"gcs_path": "uploads/job-with-instrumental-123/audio/test.mp3",
"upload_url": "https://storage.googleapis.com/signed-url-audio",
"content_type": "audio/mpeg"
},
{
"file_type": "existing_instrumental",
"gcs_path": "uploads/job-with-instrumental-123/audio/existing_instrumental.flac",
"upload_url": "https://storage.googleapis.com/signed-url-instrumental",
"content_type": "audio/flac"
}
]
}
# Mock uploads complete response (second API call)
complete_response = MagicMock()
complete_response.status_code = 200
complete_response.json.return_value = {
"status": "success",
"job_id": "job-with-instrumental-123",
"message": "Processing started"
}
mock_request.side_effect = [create_response, complete_response]
mock_upload.return_value = True
result = client.submit_job(
str(audio_file),
"Test Artist",
"Test Title",
existing_instrumental=str(instrumental_file)
)
assert result["status"] == "success"
assert result["job_id"] == "job-with-instrumental-123"
# Verify uploads were called for both audio and instrumental
assert mock_upload.call_count == 2
@patch.object(RemoteKaraokeClient, '_upload_file_to_signed_url')
@patch.object(RemoteKaraokeClient, '_request')
def test_submit_job_existing_instrumental_nonexistent_file(self, mock_request, mock_upload, client, tmp_path):
"""Test that nonexistent instrumental file is silently ignored."""
# Create test audio file
audio_file = tmp_path / "test.mp3"
audio_file.write_bytes(b"audio data")
# Mock create job response (instrumental not included since file doesn't exist)
create_response = MagicMock()
create_response.status_code = 200
create_response.json.return_value = {
"status": "success",
"job_id": "job-no-instrumental-456",
"upload_urls": [
{
"file_type": "audio",
"gcs_path": "uploads/job-no-instrumental-456/audio/test.mp3",
"upload_url": "https://storage.googleapis.com/signed-url-audio",
"content_type": "audio/mpeg"
}
]
}
# Mock uploads complete response
complete_response = MagicMock()
complete_response.status_code = 200
complete_response.json.return_value = {
"status": "success",
"job_id": "job-no-instrumental-456",
"message": "Processing started"
}
mock_request.side_effect = [create_response, complete_response]
mock_upload.return_value = True
# Pass nonexistent instrumental path - should be silently ignored
result = client.submit_job(
str(audio_file),
"Test Artist",
"Test Title",
existing_instrumental="/nonexistent/instrumental.flac"
)
assert result["status"] == "success"
# Only audio should be uploaded (instrumental file doesn't exist)
assert mock_upload.call_count == 1
@patch.object(RemoteKaraokeClient, '_request')
def test_submit_job_api_error(self, mock_request, client, tmp_path):
"""Test handling API error during job creation."""