Compare commits

...
Author SHA1 Message Date
Andrew BeveridgeandClaude Opus 4.5 5600df6783 ci: skip disk cleanup when backend deployment not needed
Move version check before the "Free disk space" step and add a
conditional so it only runs when deployment is actually needed.
This saves ~3+ minutes on pushes to main when the version tag
already exists.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 01:42:11 -05:00
Andrew Beveridge ☄️ 10cb868481 Merge pull request #80 from nomadkaraoke/feature/local-whisper-transcriber
Add local Whisper transcription support
2025-12-28 01:24:18 -05:00
Andrew Beveridge 02a64faecc Updated deps 2025-12-28 01:07:25 -05:00
Andrew BeveridgeandClaude Opus 4.5 ba4ec72040 Merge main into feature/local-whisper-transcriber
Resolved version conflict by keeping 0.76.24 from feature branch.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 01:07:06 -05:00
Andrew Beveridge ☄️ ae2846b90b Merge pull request #85 from nomadkaraoke/fix/instrumental-ui-duration-nan
Fix instrumental UI NaN duration causing seek to fail
2025-12-28 01:05:27 -05:00
Andrew BeveridgeandClaude Opus 4.5 8c0eab7d7b Increase logo height to 40px
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 01:04:13 -05:00
Andrew BeveridgeandClaude Opus 4.5 0f51f595a3 Replace microphone emoji with Nomad Karaoke logo in instrumental UI
- Added logo-img CSS class for proper image sizing
- Updated header to use the SVG logo instead of emoji

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 01:02:18 -05:00
Andrew BeveridgeandClaude Opus 4.5 98bb30114e Fix instrumental UI NaN duration causing seek to fail
The waveform API returns `duration_seconds` but the frontend was reading
`waveformData.duration` which is undefined, causing duration to be NaN.

Fixes:
- Read `duration_seconds` from API response instead of `duration`
- Add guard in formatTime() to return '0:00' for NaN/Infinity
- Add guard in seekTo() to prevent setting audio.currentTime to NaN
- Add guards in waveform click handlers to prevent NaN calculations

This fixes the 'NaN:NaN' display and the TypeError when clicking to seek.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 01:01:26 -05:00
Andrew Beveridge ☄️ e88110a25c Merge pull request #84 from nomadkaraoke/fix/instrumental-ui-auth
Fix instrumental UI authentication token not being passed in URL
2025-12-28 00:48:34 -05:00
Andrew Beveridge 12a62db7f8 Remove test files and add to gitignore 2025-12-28 00:48:13 -05:00
Andrew Beveridge ☄️ c4a0cb2a63 Merge pull request #83 from nomadkaraoke/fix/docker-cache-ignore-error
Fix Docker registry cache error on first build
2025-12-28 00:47:45 -05:00
Andrew BeveridgeandClaude Opus 4.5 b0a16a90a4 Fix OutputPaths attribute access (dataclass not dict)
OutputPaths is a dataclass, not a dictionary. Changed .get() calls
to proper attribute access (.video, .ass).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:46:54 -05:00
Andrew BeveridgeandClaude Opus 4.5 2f6cd7a745 Fix Docker registry cache error and optimize CI workflow
Changes:
1. Add ignore-error=true to cache-from/cache-to so builds continue
   gracefully when cache doesn't exist (first build)

2. Add aggressive disk space cleanup at start of deploy-backend using
   jlumbroso/free-disk-space action (fixes "No space left on device")

3. Skip tests on main branch - tests only run on PRs now
   - PRs run full test suite with path filtering
   - Main branch only runs build/deploy jobs
   - CI Gate only required for PRs, not main

This significantly speeds up deployments on main since tests
already passed on the PR before merge.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:46:02 -05:00
Andrew BeveridgeandClaude Opus 4.5 1da1a17235 Fix instrumental UI authentication token not being passed in URL
The instrumental review UI was getting 401 errors because the frontend
JobCard component was looking for the token in job.state_data.instrumental_token
when it's actually stored at job.instrumental_token (same pattern as review_token).

Changes:
- Add instrumental_token field to Job interface in api.ts
- Fix JobCard.tsx getInstrumentalUrl() to use job.instrumental_token
- Update test to use correct token location

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:40:44 -05:00
Andrew Beveridge daa6807b39 Remove accidentally committed test audio files 2025-12-28 00:36:34 -05:00
Andrew BeveridgeandClaude Opus 4.5 06c8587e99 Fix missing lyrics_results argument in generate_outputs call
The OutputGenerator.generate_outputs() method requires a lyrics_results
argument. Since we're at the video rendering stage after loading
corrections from JSON, we pass an empty dict as the lyrics were already
written during the transcription phase.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:35:24 -05:00
Andrew Beveridge 81c7d1cc7f Merge main into feature/local-whisper-transcriber 2025-12-28 00:31:56 -05:00
Andrew BeveridgeandClaude Opus 4.5 b2c8608e0c Use default styles when no output_styles_json provided
When rendering video or CDG without a styles file, the OutputGenerator
now uses sensible default karaoke styles instead of raising an error.
This allows end-to-end karaoke generation to work without requiring a
styles JSON file.

Changes:
- Add _get_default_styles() method with embedded default karaoke styles
- Modify init to use defaults when styles file is missing/None
- Update test to verify default styles are loaded correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:29:18 -05:00
Andrew Beveridge ☄️ d6f22f5494 Merge pull request #82 from nomadkaraoke/feature/github-actions-docker-builds
Replace Cloud Build with GitHub Actions for Docker builds
2025-12-28 00:25:11 -05:00
Andrew BeveridgeandClaude Opus 4.5 ea1c659828 Replace Cloud Build with GitHub Actions for Docker builds
Cost optimization: Migrate from expensive Cloud Build ($100+/month) to
free GitHub Actions with registry caching.

Changes:
- Add .dockerignore to reduce build context (~1GB → ~10MB)
- Rewrite deploy-backend job to use docker/build-push-action
- Use Artifact Registry as persistent layer cache (mode=max)
- Keep cloudbuild*.yaml as fallback for manual builds
- Add documentation for new build process

Benefits:
- $0 monthly cost (public repo, free GitHub Actions)
- Faster builds with registry-cached layers
- Simpler deployment pipeline (all in one workflow)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:22:39 -05:00
Andrew BeveridgeandClaude Opus 4.5 8bd8f3343f Fix pip extras for local-whisper installation
Poetry extras require the optional dependency to be in the main
[tool.poetry.dependencies] section with `optional = true`, not just
in a separate group. The group syntax is only for Poetry's own
`poetry install --with` command.

Now `pip install karaoke-gen[local-whisper]` correctly installs
whisper-timestamped.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:19:19 -05:00
Andrew BeveridgeandClaude Opus 4.5 ce07080fc7 Add disk space cleanup to backend-unit-tests CI job
Prevents transient "No space left on device" failures on GitHub Actions
runners by freeing up disk space before running tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:13:13 -05:00
Andrew BeveridgeandClaude Opus 4.5 eceab9f422 Fix LocalWhisper integration in lyrics_processor.py
- Add LocalWhisper check to _check_transcription_providers()
- Update error message to include LocalWhisper as option 3
- Pass runpod_api_key, whisper_runpod_id, and enable_local_whisper
  to TranscriberConfig

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 00:04:31 -05:00
Andrew Beveridge ☄️ e4223634d1 Merge pull request #81 from nomadkaraoke/fix/remove-instrumental-selector-export
Fix stale exports in job/index.ts and add frontend build CI check
2025-12-27 23:52:42 -05:00
Andrew BeveridgeandClaude Opus 4.5 6586ea3584 Update poetry.lock for local-whisper extras
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:52:23 -05:00
Andrew BeveridgeandClaude Opus 4.5 b7c1850a3b Fix stale exports in job/index.ts and add frontend build CI check
The previous PR removed InstrumentalSelector.tsx but didn't update the
barrel export file, breaking the production build.

Changes:
- Remove stale InstrumentalSelector and StatusBadge exports from index.ts
- Add frontend-build job to CI workflow to catch build failures on PRs
- Add frontend-build to ci-gate and deploy-frontend dependencies

This ensures future PRs will fail CI if they break the frontend build.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:51:42 -05:00
Andrew BeveridgeandClaude Opus 4.5 e4dbd7393e Add local Whisper transcription support
Implements LocalWhisperTranscriber for running Whisper inference locally
using the whisper-timestamped library, addressing issue #68.

Changes:
- Add LocalWhisperTranscriber class with lazy model loading
- Support auto-detection of device (CPU/CUDA/MPS) with fallback
- Add configuration via environment variables (WHISPER_MODEL_SIZE,
  WHISPER_DEVICE, WHISPER_CACHE_DIR, WHISPER_LANGUAGE)
- Integrate into controller with priority 3 (fallback after cloud services)
- Add whisper-timestamped as optional dependency [local-whisper]
- Add comprehensive unit tests
- Update README with setup instructions and troubleshooting guide

Installation:
  pip install karaoke-gen[local-whisper]

For CPU-only systems:
  pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu
  pip install karaoke-gen[local-whisper]

Fixes #68

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:40:34 -05:00
Andrew Beveridge ☄️ 7736a4a8f7 Merge pull request #79 from nomadkaraoke/feature/instrumental-review-ui
Replace primitive instrumental selector with full-featured review UI
2025-12-27 23:36:45 -05:00
Andrew BeveridgeandClaude Opus 4.5 156b83ace1 Replace primitive instrumental selector with full-featured review UI
The web frontend now opens the same rich instrumental review UI that the
CLI uses. This provides waveform visualization, audio playback for
different stems, mute region selection, and custom instrumental creation.

Changes:
- Add instrumental review static HTML to frontend/public/instrumental/
- Update JobCard to open /instrumental/ page instead of showing dialog
- Remove InstrumentalSelector.tsx primitive dialog component
- Remove orphaned instrumental-review components (never used)
- Update tests to reflect link-based navigation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:34:20 -05:00
Andrew Beveridge ☄️ 8978eb6d96 Merge pull request #77 from nomadkaraoke/fix/flacfetch-youtube-url
Update flacfetch to 0.15.1 (fixes YouTube URL download)
2025-12-27 23:10:12 -05:00
Andrew Beveridge ☄️ c4143947ca Merge pull request #78 from nomadkaraoke/feature/mandatory-worktrees
Make git worktrees mandatory for all changes
2025-12-27 23:09:07 -05:00
Andrew BeveridgeandClaude Opus 4.5 8a28c95786 Simplify flacfetch workflow: no PR required
flacfetch changes can be committed directly to main once tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:07:36 -05:00
Andrew BeveridgeandClaude Opus 4.5 48acb6c4e1 Add flacfetch as a related project with workflow docs
Documents that flacfetch is a dependency we own, located at
/Users/andrew/Projects/flacfetch, and provides the workflow for
making changes to it and updating karaoke-gen's dependency.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:07:05 -05:00
Andrew BeveridgeandClaude Opus 4.5 da60495144 Clarify version bump rule applies only to code changes
Documentation-only changes (CLAUDE.md, README.md, etc.) don't require
version bumps since they don't affect deployed code.

Also reverts the unnecessary version bump from the previous commit.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:04:39 -05:00
Andrew BeveridgeandClaude Opus 4.5 e282f40758 Make git worktrees mandatory for all changes
Since all changes must go through a PR, worktrees are now required for
all changes (not just non-trivial ones). This keeps the main directory
clean on main and enables parallel work.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 23:03:54 -05:00
Andrew BeveridgeandClaude Opus 4.5 aaeda8679a Update flacfetch to 0.15.1 (fixes YouTube URL download)
Fixes the "Release.__init__() missing 1 required positional argument: 'quality'"
error that occurred when downloading from YouTube URLs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:59:31 -05:00
Andrew Beveridge ☄️ 6da6656c15 Merge pull request #76 from nomadkaraoke/fix/remove-deployment-tracking
Remove deployment tracking to fix PR banner
2025-12-27 22:50:44 -05:00
Andrew BeveridgeandClaude Opus 4.5 d81591ca99 Remove deployment tracking to fix PR banner
Remove the `environment` block from the deploy-frontend job to prevent
GitHub from creating deployment entries. This fixes the "This branch
has not been deployed" banner that appears on all PRs.

The deployment to GitHub Pages still works via actions/deploy-pages@v4,
but without tracked environment deployments.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:49:44 -05:00
Andrew Beveridge ☄️ 0e66ac84ec Merge pull request #74 from nomadkaraoke/fix/youtube-url-support
Fix YouTube URL support in karaoke-gen CLI
2025-12-27 22:48:12 -05:00
Andrew BeveridgeandClaude Opus 4.5 d8e3542850 Address CodeRabbit lint feedback
- Remove f-string prefix from strings without placeholders (F541)
- Add 'from e' to ImportError handler to preserve stack trace (B904)
- Use raw string for regex pattern in pytest match (RUF043)
- Remove unused result variables (F841)
- Remove redundant KaraokePrep import in fixture (F811)
- Rename unused mock parameters with underscore prefix (ARG002)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:36:22 -05:00
Andrew Beveridge ☄️ 8b66e97459 Merge pull request #75 from nomadkaraoke/fix/progress-display-during-parallel-processing
Fix progress display to show step 4 when workers are active during download
2025-12-27 22:34:53 -05:00
Andrew BeveridgeandClaude Opus 4.5 5c71d58a8d Fix progress display to show step 4 when workers are active during download
When the backend status is "downloading" but parallel workers (audio/lyrics)
are actively running, the frontend now correctly displays step 4 progress
instead of appearing stuck at step 3 "Downloading".

This improves UX by showing detailed worker progress (e.g., "Audio 1/2 + Transcribing")
when both workers are active, making the job appear responsive.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:26:26 -05:00
Andrew Beveridge 8dc0a96734 Added CI gate check 2025-12-27 22:24:52 -05:00
Andrew BeveridgeandClaude Opus 4.5 78d7eedeee Add tests for YouTube URL support
Add comprehensive test coverage for the new download_from_url()
functionality:

- FlacFetchAudioFetcher.download_from_url() tests:
  - YouTube URL download success
  - youtu.be short URL support
  - Embed URL support
  - Filename generation from artist/title
  - Filename fallback to video ID
  - Error handling for download failures
  - Output directory creation

- RemoteFlacFetchAudioFetcher.download_from_url() tests:
  - Verifies local flacfetch is used (not remote API)
  - Video ID extraction
  - Error handling

- KaraokePrep URL handling tests:
  - process() with YouTube URL
  - process() with URL but no artist/title
  - process() with youtu.be short URL
  - _is_url() helper method edge cases

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:18:17 -05:00
Andrew Beveridge 6fa81c1b69 Updated dependencies 2025-12-27 22:10:59 -05:00
Andrew BeveridgeandClaude Opus 4.5 706e6f7527 Fix YouTube URL support in karaoke-gen CLI
Restore YouTube URL input functionality that was broken during
flacfetch integration. Now users can pass a YouTube URL as the
first argument to karaoke-gen again.

Changes:
- Add download_from_url() abstract method to AudioFetcher base class
- Implement download_from_url() in FlacFetchAudioFetcher using
  flacfetch's download_by_id() method for direct YouTube downloads
- Implement download_from_url() in RemoteFlacFetchAudioFetcher
  using local flacfetch for YouTube (no remote API needed)
- Update KaraokePrep.process() to detect URLs and route them to
  the new download_from_url() method instead of flacfetch search
- Add _is_url() helper method for URL detection
- Add _use_url_download flag to differentiate URL downloads from
  artist+title searches

Fixes #67

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:08:19 -05:00
Andrew Beveridge ☄️ 8bbb1351f8 Merge pull request #73 from nomadkaraoke/feature/ci-improvements
ci: Add path filtering, caching, and frontend tests to GHA workflow
2025-12-27 21:50:35 -05:00
Andrew BeveridgeandClaude Opus 4.5 18e97dc0a0 ci: Use poetry.lock exclusively for deps filter
Remove pyproject.toml from the deps filter since poetry.lock is
already used as the source of truth for dependency changes elsewhere
(e.g., base image rebuild triggers). Version bumps alone shouldn't
trigger full test suites.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:48:50 -05:00
Andrew BeveridgeandClaude Opus 4.5 642c80164c ci: Add path filtering, caching, and frontend tests to GHA workflow
- Add `changes` job using dorny/paths-filter@v3 to detect what changed
- Add frontend-unit-tests job (Jest with coverage)
- Make all test jobs conditional based on path changes:
  - Frontend tests: run only if frontend/** changed
  - Backend tests: run if backend/** or package or deps changed
  - Package tests: run if karaoke_gen/** or tests/** or deps changed
- Add Poetry dependency caching using actions/cache@v4:
  - Cache .venv directory keyed on poetry.lock hash
  - Shared cache between package, backend, and lint jobs
- Add npm caching using actions/setup-node's built-in cache feature
- Add Yarn caching for lyrics frontend in deploy-frontend job
- Add spaCy model caching
- Jobs always run on main branch to ensure full validation before deploy
- Add summary table to changes job showing what areas changed

This significantly reduces CI time for focused PRs (e.g., frontend-only
changes skip all backend tests) while ensuring comprehensive testing
before merging to main.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:40:56 -05:00
Andrew Beveridge ☄️andClaude Opus 4.5 46eca212fe Fix: Prepare theme styles when creating job via audio search (#72)
* Fix: Prepare theme styles when creating job via audio search

When creating a job via the audio search endpoint with a theme_id,
the theme styles were not being prepared. This resulted in jobs that
had theme_id set but no style_params_gcs_path or style_assets, causing
preview videos to have black backgrounds instead of themed backgrounds.

The fix adds a call to _prepare_theme_for_job() in audio_search.py
when theme_id is provided and no custom style files are being uploaded.
This matches the behavior of file_upload.py.

Also adds regression tests to catch this issue:
- test_audio_search_with_theme_calls_prepare_job_style
- test_audio_search_code_handles_theme_preparation

Fixes issue observed in production job ffb0b8fa.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Remove unused variable from test (CodeRabbit feedback)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:38:05 -05:00
Andrew BeveridgeandClaude Opus 4.5 8d8e64ccc0 docs: Add job debugging guide with tested gcloud commands
Add comprehensive documentation for debugging jobs in production:
- Firestore job data access via Python script
- Cloud Logging queries for job-specific logs
- GCS file listing and inspection
- Cloud Run service health checks
- Cloud Tasks queue monitoring
- Common debugging scenarios

Also adds scripts/get_job.py helper for quick job lookups.

All commands tested against real job ID ffb0b8fa.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:26:10 -05:00
Andrew Beveridge ☄️ 50f895e88a Merge pull request #71 from nomadkaraoke/feature/improve-job-status-display
feat: Improve job status display with step-based progress
2025-12-27 21:24:44 -05:00
Andrew BeveridgeandClaude Opus 4.5 7218b4d176 feat: Add step-based job status display with progress indicator
- Create job-status.ts utility that maps 28+ backend statuses to a 10-step user-friendly system
- Display status as "[X/10] Status..." format for better progress visibility
- Add visual progress bar that matches status color
- Show "Action needed" badge for blocking states (review, instrumental selection)
- Handle parallel processing (audio + lyrics) with combined status display
- Add comprehensive unit tests (40+ test cases) for all status mappings
- Update JobCard to use new StatusIndicator and ProgressBar components

This improves user experience by providing clearer feedback about job progress
instead of showing static labels like "Downloading..." for extended periods.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 21:17:44 -05:00
Andrew BeveridgeandClaude Opus 4.5 c052fec326 Fix E2E test API request timeouts
Increase timeout for Playwright API requests from default 60s to 120s
to handle slower API responses during job polling.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 20:54:56 -05:00
Andrew BeveridgeandClaude Opus 4.5 70eca20c7a Consolidate production E2E tests into single comprehensive flow
Merged production-full-journey.spec.ts and production-theme-system.spec.ts
into production-e2e.spec.ts. The combined test:

- Uses cached song (piri/dog) for fast flacfetch responses
- Tests theme selection as part of the core journey
- Validates theme_id is correctly set on jobs
- Follows the complete flow: search -> audio -> review -> instrumental -> complete

This eliminates redundancy and ensures theme testing is integrated with
the main E2E flow rather than being a separate concern.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 20:35:03 -05:00
Andrew BeveridgeandClaude Opus 4.5 0e6a0b43ce Update production E2E tests to use real cached song
Use "piri" / "dog" for all production E2E tests instead of fake
test data. This leverages flacfetch's new caching system for fast
search responses without repeated API calls to trackers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 20:31:50 -05:00
Andrew BeveridgeandClaude Opus 4.5 3cbfbd14d1 Fix: Pass theme configuration from JobCreate to Job
The create_job() function was not passing theme_id, color_overrides,
style_params_gcs_path, or style_assets from JobCreate to the Job
constructor. This caused jobs created via audio search to have null
theme_id even when a theme was specified.

Also enables video recording for all Playwright tests.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 20:17:42 -05:00
Andrew Beveridge ☄️ 2f4265e9d0 Fix: Add theme support to audio search endpoint (#70)
Fixes bug where themes selected in the frontend were not being passed through when creating jobs via the Search tab. Audio search jobs now correctly receive theme_id and color_overrides, and CDG/TXT outputs are automatically enabled when a theme is selected.

- Add theme_id and color_overrides fields to AudioSearchRequest
- Add _resolve_cdg_txt_defaults() helper for smart CDG/TXT defaults
- Add comprehensive backend tests for theme support
- Add E2E Playwright tests for production theme system with full UI flow
2025-12-27 19:15:28 -05:00
Andrew Beveridge ☄️ 4a74a2cc3a Add theme system for pre-made video styles (#69)
Adds a comprehensive theme system for karaoke video styles:

- Theme selection and customizable color overrides for karaoke jobs
- Public Themes API for listing, previewing, and fetching theme details
- Theme selector and color-overrides UI in job submission flows
- CLI option to specify a theme when preparing/submitting jobs
- Thread-safe singleton initialization for ThemeService

🤖 Generated with Claude Code
2025-12-27 17:55:47 -05:00
Andrew BeveridgeandClaude Opus 4.5 cdae92d0b7 Fix review-data endpoint to use fallback audio sources
The endpoint required file_urls.lyrics.audio which was never set by
the lyrics worker. Now falls back to:
1. stems.lead_vocals (best for reviewing lyrics sync)
2. input_media_gcs_path (original audio)

This fixes jobs getting stuck in awaiting_review status with
"Review data not available" error.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 13:47:52 -05:00
Andrew BeveridgeandClaude Opus 4.5 6e3cf007d2 Add Claude automation service account for long-lived GCP access
- Create claude-automation SA with read-only GCP permissions:
  - run.viewer, logging.viewer, monitoring.viewer
  - cloudbuild.builds.viewer, cloudtrace.user
  - cloudtasks.viewer, storage.objectViewer
- Enable impersonation for andrew@beveridge.uk and admin@nomadkaraoke.com
- Add playwright-report-prod to gitignore

This allows Claude Code CLI to monitor deployments, view logs, and
check build status without requiring frequent OAuth re-authentication.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 13:29:33 -05:00
Andrew BeveridgeandClaude Opus 4.5 bf6f3ba78b Fix CDG/TXT generation failing for jobs without custom styles
Changed enable_cdg and enable_txt defaults from true to false since
CDG/TXT generation requires a complete style configuration with 35+
parameters. Jobs created without custom styles would fail at the
video generation stage with "Missing required style parameters".

Also updated E2E tests to properly interact with the lyrics review UI:
- Click "Preview Video" button to trigger preview generation
- Wait for "Complete Review" button to appear
- Submit the review to advance the job to instrumental selection

Changes:
- backend/models/job.py: Default enable_cdg/enable_txt to False
- backend/models/requests.py: Same
- backend/api/routes/file_upload.py: Same
- backend/api/routes/audio_search.py: Same
- backend/tests: Updated tests expecting new defaults
- frontend/e2e: Improved review UI interaction in E2E tests
- pyproject.toml: Bump version to 0.76.6

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 11:52:04 -05:00
Andrew Beveridge ☄️andClaude Opus 4.5 d1a5892793 Enable YouTube upload by default for web service jobs (#66)
Root cause: Jobs created via the web UI were missing youtube_description_template
field, which caused the video_worker to skip YouTube upload even when credentials
were configured.

Changes:
- Add default_enable_youtube_upload, default_brand_prefix, default_youtube_description
  settings to backend/config.py
- Apply defaults in audio_search.py when creating jobs
- Add DEFAULT_ENABLE_YOUTUBE_UPLOAD=true and DEFAULT_BRAND_PREFIX=NOMAD env vars
  to cloudbuild.yaml for production deployment

This ensures web service jobs will automatically upload to YouTube and use the
NOMAD brand prefix without requiring explicit request parameters.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 10:39:53 -05:00
Andrew Beveridge ☄️ 4a7b616dde Merge pull request #65 from nomadkaraoke/fix/e2e-test-strict-mode-violations
Fix E2E tests for production - strict mode violations
2025-12-27 09:13:44 -05:00
Andrew Beveridge ☄️andcoderabbitai[bot] e40a122841 Update frontend/e2e/production-full-journey.spec.ts
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-12-27 09:12:51 -05:00
Andrew BeveridgeandClaude Opus 4.5 251d3fa971 Fix E2E tests for production - strict mode violations
Fixed Playwright strict mode violations in production E2E tests where
multiple elements matched locators. Added .first() to selectors for
Review Lyrics and Select Instrumental buttons when multiple jobs
exist in the queue.

Test results: 8/8 tests passing including full E2E journey.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 04:55:42 -05:00
Andrew Beveridge ☄️ a24060c4d2 Merge pull request #64 from nomadkaraoke/refactor/defer-countdown-always
Refactor: Always defer countdown to post-review phase
2025-12-27 04:05:29 -05:00
Andrew Beveridge 2ab0f0dea3 Added agent instructions for current goal 2025-12-27 04:04:47 -05:00
Andrew Beveridge 41df208e5d Merge main into refactor/defer-countdown-always 2025-12-27 03:57:44 -05:00
Andrew Beveridge ☄️ 7ffb219b9f Merge pull request #63 from nomadkaraoke/feature/retry-cancelled-jobs
feat: Allow retrying cancelled jobs
2025-12-27 03:56:29 -05:00
Andrew BeveridgeandClaude Opus 4.5 f740c68f4b Refactor: Always defer countdown to post-review phase
This removes the is_serverless environment detection from lyrics_processor.py
and makes both local CLI and cloud backend follow the same pattern:
1. Transcription (no countdown) - timestamps match original audio
2. Human review - UI shows accurate, unshifted timestamps
3. Add countdown (if song starts within 3s)
4. Render video

Previously, the serverless detection only checked for Modal environment
variables (MODAL_TASK_ID, MODAL_FUNCTION_NAME, /.modal), missing Cloud Run
(K_SERVICE). This caused the review UI to show shifted timestamps when
running on Cloud Run.

Instead of adding more environment detection, this refactor ensures
consistent behavior regardless of environment:

- lyrics_processor.py: Always sets add_countdown=False and render_video=False
- karaoke_gen.py: Added post-transcription section that loads corrections,
  processes countdown via CountdownProcessor, and renders video via
  OutputGenerator

This aligns with the architectural goals in BACKEND-FEATURE-PARITY-PLAN.md
to share more code between local and cloud paths.

Fixes: Lyrics review UI showing countdown-shifted timestamps on Cloud Run

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:52:04 -05:00
Andrew BeveridgeandClaude Opus 4.5 14c0abd40f feat: Allow retrying cancelled jobs
- Add CANCELLED to state transitions allowing retry to DOWNLOADING,
  INSTRUMENTAL_SELECTED, REVIEW_COMPLETE, and LYRICS_COMPLETE
- Add DOWNLOADING to FAILED transitions for restart from beginning
- Update retry endpoint to accept both FAILED and CANCELLED jobs
- Add new retry path that restarts from beginning when input audio exists
- Update frontend JobActions to show retry button for cancelled jobs
- Add tests for cancelled job retry functionality

This enables users to restart cancelled jobs from their last checkpoint
or from the beginning if the input audio is still available in GCS.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:42:38 -05:00
Andrew Beveridge 4b01f1100f Added testing and docs instructions for claude 2025-12-27 03:17:11 -05:00
68 changed files with 9831 additions and 1330 deletions

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
# .dockerignore - Reduce Docker build context size
# This is critical for fast builds as it reduces upload time and cache invalidation
# Git
.git/
.gitignore
.gitmodules
# Node.js / Frontend (not needed for backend image)
node_modules/
frontend/
frontend-react/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Python virtual environments (use Docker's Python instead)
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
.venv/
venv/
backend/venv/
ENV/
env/
*.egg-info/
.eggs/
dist/
build/
*.egg
# IDE
.idea/
.vscode/
*.swp
*.swo
*~
.DS_Store
# Testing (not needed in production image)
.pytest_cache/
.coverage
htmlcov/
.tox/
.nox/
coverage.xml
*.cover
tests/
backend/tests/
# Documentation (not needed in Docker image)
docs/
*.md
!README.md
# Infrastructure (not needed for Docker build)
infrastructure/
# CI/CD configs
.github/
# Cloud Build configs (keep for reference but not in image)
cloudbuild*.yaml
# Local development files
lyrics_transcriber_local/
*.local
.env
.env.*
*.bak
*.tmp
Thumbs.db
# Large test/sample files
tests/data/
*.mp3
*.wav
*.mp4
*.mkv
*.flac
+436 -145
View File
@@ -17,12 +17,120 @@ concurrency:
jobs:
# ============================================
# PACKAGE TESTS (karaoke-gen Python CLI)
# PATH FILTERING - Determine what changed
# Only needed for PRs (main branch skips tests)
# ============================================
changes:
name: "Detect Changes"
runs-on: ubuntu-latest
if: github.ref != 'refs/heads/main'
outputs:
frontend: ${{ steps.filter.outputs.frontend }}
backend: ${{ steps.filter.outputs.backend }}
package: ${{ steps.filter.outputs.package }}
infra: ${{ steps.filter.outputs.infra }}
deps: ${{ steps.filter.outputs.deps }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
frontend:
- 'frontend/**'
backend:
- 'backend/**'
package:
- 'karaoke_gen/**'
- 'lyrics_transcriber_temp/**'
- 'tests/**'
infra:
- 'infrastructure/**'
deps:
- 'poetry.lock'
- name: Summary
run: |
echo "### Change Detection Results" >> $GITHUB_STEP_SUMMARY
echo "| Area | Changed |" >> $GITHUB_STEP_SUMMARY
echo "|------|---------|" >> $GITHUB_STEP_SUMMARY
echo "| Frontend | ${{ steps.filter.outputs.frontend }} |" >> $GITHUB_STEP_SUMMARY
echo "| Backend | ${{ steps.filter.outputs.backend }} |" >> $GITHUB_STEP_SUMMARY
echo "| Package | ${{ steps.filter.outputs.package }} |" >> $GITHUB_STEP_SUMMARY
echo "| Infrastructure | ${{ steps.filter.outputs.infra }} |" >> $GITHUB_STEP_SUMMARY
echo "| Dependencies | ${{ steps.filter.outputs.deps }} |" >> $GITHUB_STEP_SUMMARY
# ============================================
# FRONTEND TESTS (PRs only - main skips tests)
# ============================================
frontend-unit-tests:
name: "Frontend - Unit Tests"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && needs.changes.outputs.frontend == 'true'
steps:
- uses: actions/checkout@v4
- name: Setup Node.js with caching
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
working-directory: frontend
run: npm ci --legacy-peer-deps
- name: Run unit tests with coverage
working-directory: frontend
run: npm run test:ci
- name: Upload coverage report
if: always()
uses: actions/upload-artifact@v4
with:
name: frontend-coverage
path: frontend/coverage/
retention-days: 7
frontend-build:
name: "Frontend - Build"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && needs.changes.outputs.frontend == 'true'
steps:
- uses: actions/checkout@v4
- name: Setup Node.js with caching
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
working-directory: frontend
run: npm ci --legacy-peer-deps
- name: Build frontend
working-directory: frontend
env:
NEXT_PUBLIC_API_URL: https://api.nomadkaraoke.com
run: npm run build
# ============================================
# PACKAGE TESTS (PRs only - main skips tests)
# ============================================
package-unit-tests:
name: "Package - Unit Tests (70% coverage)"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && (needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true')
steps:
- uses: actions/checkout@v4
with:
@@ -39,6 +147,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install --with dev
@@ -47,6 +163,12 @@ jobs:
sudo apt-get update
sudo apt-get install -y ffmpeg
- name: Cache spaCy model
uses: actions/cache@v4
with:
path: ~/.cache/spacy
key: spacy-en_core_web_sm-${{ runner.os }}
- name: Install spaCy language models
run: poetry run python -m spacy download en_core_web_sm
@@ -65,6 +187,8 @@ jobs:
package-integration-tests:
name: "Package - Integration Tests"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && (needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true')
steps:
- uses: actions/checkout@v4
with:
@@ -81,6 +205,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install --with dev
@@ -89,16 +221,27 @@ jobs:
sudo apt-get update
sudo apt-get install -y ffmpeg
- name: Cache spaCy model
uses: actions/cache@v4
with:
path: ~/.cache/spacy
key: spacy-en_core_web_sm-${{ runner.os }}
- name: Install spaCy language models
run: poetry run python -m spacy download en_core_web_sm
- name: Run integration tests
run: poetry run pytest tests/integration/ -v
package-build:
name: "Package - Build & Install"
package-build-test:
name: "Package - Build & Install (Test)"
runs-on: ubuntu-latest
needs: [package-unit-tests, package-integration-tests]
needs: [changes, package-unit-tests, package-integration-tests]
if: >-
github.ref != 'refs/heads/main' && always() &&
(needs.package-unit-tests.result == 'success' || needs.package-unit-tests.result == 'skipped') &&
(needs.package-integration-tests.result == 'success' || needs.package-integration-tests.result == 'skipped') &&
(needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true')
steps:
- uses: actions/checkout@v4
with:
@@ -115,6 +258,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Build package
run: poetry build
@@ -134,55 +285,15 @@ jobs:
pip install dist/*.whl
python -m karaoke_gen.utils.gen_cli --help
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: package-dist
path: dist/
retention-days: 1
# ============================================
# BACKEND TESTS (Cloud Run API service)
# BACKEND TESTS (PRs only - main skips tests)
# ============================================
backend-unit-tests:
name: "Backend - Unit Tests"
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
- name: Set up Python 3.13
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Install dependencies
run: poetry install
- name: Run all backend unit tests
env:
GOOGLE_CLOUD_PROJECT: test-project
GCS_BUCKET_NAME: test-bucket
FIRESTORE_COLLECTION: jobs
ENVIRONMENT: test
run: |
poetry run pytest \
backend/tests/ \
--ignore=backend/tests/emulator \
-v --tb=short
backend-emulator-tests:
name: "Backend - Emulator Integration Tests"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && (needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true')
steps:
- name: Free disk space
run: |
@@ -207,6 +318,70 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-backend-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-backend-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install
- name: Run all backend unit tests
env:
GOOGLE_CLOUD_PROJECT: test-project
GCS_BUCKET_NAME: test-bucket
FIRESTORE_COLLECTION: jobs
ENVIRONMENT: test
run: |
poetry run pytest \
backend/tests/ \
--ignore=backend/tests/emulator \
-v --tb=short
backend-emulator-tests:
name: "Backend - Emulator Integration Tests"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && (needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true' || needs.changes.outputs.deps == 'true')
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@main
with:
tool-cache: false
android: true
haskell: true
dotnet: true
large-packages: false
docker-images: false
swap-storage: false
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: false
- name: Set up Python 3.13
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-backend-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-backend-${{ runner.os }}-py3.13-
- name: Set up Java 21 (required by Firestore emulator)
uses: actions/setup-java@v4
with:
@@ -243,6 +418,8 @@ jobs:
backend-lint:
name: "Backend - Code Quality"
runs-on: ubuntu-latest
needs: changes
if: github.ref != 'refs/heads/main' && (needs.changes.outputs.backend == 'true' || needs.changes.outputs.package == 'true')
steps:
- name: Checkout code
uses: actions/checkout@v4
@@ -260,6 +437,14 @@ jobs:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-lint-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-lint-${{ runner.os }}-py3.13-
- name: Install dependencies
run: poetry install
@@ -269,9 +454,78 @@ jobs:
echo "✅ Basic syntax validation passed"
# ============================================
# DEPLOYMENTS (main branch only)
# CI GATE - Required status check for PRs
# Only runs on PRs, not on main
# ============================================
ci-gate:
name: "CI Gate"
runs-on: ubuntu-latest
needs: [frontend-unit-tests, frontend-build, package-unit-tests, package-integration-tests, backend-unit-tests, backend-emulator-tests, backend-lint]
if: github.ref != 'refs/heads/main' && always()
steps:
- name: Check results
run: |
results=(
"${{ needs.frontend-unit-tests.result }}"
"${{ needs.frontend-build.result }}"
"${{ needs.package-unit-tests.result }}"
"${{ needs.package-integration-tests.result }}"
"${{ needs.backend-unit-tests.result }}"
"${{ needs.backend-emulator-tests.result }}"
"${{ needs.backend-lint.result }}"
)
for result in "${results[@]}"; do
if [[ "$result" == "failure" || "$result" == "cancelled" ]]; then
echo "❌ CI failed"
exit 1
fi
done
echo "✅ CI passed"
# ============================================
# DEPLOYMENTS (main branch only)
# Tests already passed on PR, so we skip them here
# ============================================
package-build:
name: "Package - Build"
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Set up Python 3.13
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-package-${{ runner.os }}-py3.13-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-package-${{ runner.os }}-py3.13-
- name: Build package
run: poetry build
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: package-dist
path: dist/
retention-days: 1
package-publish:
name: "Deploy - Publish to PyPI"
runs-on: ubuntu-latest
@@ -363,17 +617,16 @@ jobs:
name: "Deploy - Frontend (GitHub Pages)"
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
- name: Setup Node.js with caching
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Enable Yarn (for lyrics frontend)
run: corepack enable
@@ -390,6 +643,14 @@ jobs:
run: npm run build
# ===== Build Lyrics Review Frontend (Vite) =====
- name: Cache Yarn dependencies (lyrics frontend)
uses: actions/cache@v4
with:
path: lyrics_transcriber_temp/lyrics_transcriber/frontend/node_modules
key: yarn-lyrics-${{ runner.os }}-${{ hashFiles('lyrics_transcriber_temp/lyrics_transcriber/frontend/yarn.lock') }}
restore-keys: |
yarn-lyrics-${{ runner.os }}-
- name: Install lyrics frontend dependencies
working-directory: lyrics_transcriber_temp/lyrics_transcriber/frontend
run: yarn install --frozen-lockfile
@@ -427,12 +688,21 @@ jobs:
deploy-backend:
name: "Deploy - Backend (Cloud Run)"
runs-on: ubuntu-latest
needs: [backend-unit-tests, backend-emulator-tests, backend-lint]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
permissions:
contents: read
id-token: write
env:
REGISTRY: us-central1-docker.pkg.dev
PROJECT_ID: nomadkaraoke
REPOSITORY: karaoke-repo
BASE_IMAGE_NAME: karaoke-backend-base
APP_IMAGE_NAME: karaoke-backend
steps:
# ============================================
# Check if deployment is needed FIRST
# This avoids wasting 3+ mins on disk cleanup when skipping
# ============================================
- name: Checkout code
uses: actions/checkout@v4
with:
@@ -453,7 +723,7 @@ jobs:
VERSION=$(poetry version --short)
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "📋 Current version: $VERSION"
if git ls-remote --tags origin | grep -q "refs/tags/v$VERSION$"; then
echo "needs_deploy=false" >> $GITHUB_OUTPUT
echo "⏭️ Tag v$VERSION already exists - skipping backend deployment"
@@ -463,6 +733,22 @@ jobs:
echo "✨ Tag v$VERSION does not exist - proceeding with deployment"
fi
# ============================================
# Free disk space only if deploying
# This prevents "No space left on device" errors during Docker build
# ============================================
- name: Free disk space (aggressive)
if: steps.version_check.outputs.needs_deploy == 'true'
uses: jlumbroso/free-disk-space@main
with:
tool-cache: true
android: true
haskell: true
dotnet: true
large-packages: true
docker-images: true
swap-storage: true
- name: Authenticate to Google Cloud (Workload Identity)
if: steps.version_check.outputs.needs_deploy == 'true'
id: auth
@@ -477,21 +763,37 @@ jobs:
with:
version: "latest"
- name: Check if base image needs rebuild
- name: Configure Docker for Artifact Registry
if: steps.version_check.outputs.needs_deploy == 'true'
run: gcloud auth configure-docker ${{ env.REGISTRY }} --quiet
# ============================================
# Docker Build with BuildKit + Registry Cache
# This replaces expensive Cloud Build with free GitHub Actions
# Registry cache persists between builds for fast rebuilds
# ============================================
- name: Set up Docker Buildx
if: steps.version_check.outputs.needs_deploy == 'true'
uses: docker/setup-buildx-action@v3
- name: Calculate base image hash
if: steps.version_check.outputs.needs_deploy == 'true'
id: base-hash
run: |
# Hash both poetry.lock AND Dockerfile.base - rebuild if either changes
CURRENT_HASH=$(cat poetry.lock backend/Dockerfile.base | sha256sum | cut -d' ' -f1)
echo "hash=$CURRENT_HASH" >> $GITHUB_OUTPUT
echo "📋 Base content hash: $CURRENT_HASH"
- name: Check if base image exists and is current
if: steps.version_check.outputs.needs_deploy == 'true'
id: check-base
run: |
echo "🔍 Checking if base image needs rebuild..."
BASE_IMAGE="${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:latest"
# Hash both poetry.lock AND Dockerfile.base - rebuild if either changes
CURRENT_HASH=$(cat poetry.lock backend/Dockerfile.base | sha256sum | cut -d' ' -f1)
echo "Current combined hash (poetry.lock + Dockerfile.base): $CURRENT_HASH"
gcloud auth configure-docker us-central1-docker.pkg.dev --quiet
BASE_IMAGE="us-central1-docker.pkg.dev/nomadkaraoke/karaoke-repo/karaoke-backend-base:latest"
# Try to get the existing base image's hash label
EXISTING_HASH=""
if docker pull $BASE_IMAGE 2>/dev/null; then
EXISTING_HASH=$(docker inspect $BASE_IMAGE --format='{{index .Config.Labels "base.content.hash"}}' 2>/dev/null || echo "")
# Fallback to old label name for backwards compatibility
@@ -500,10 +802,10 @@ jobs:
fi
echo "Existing base image hash: $EXISTING_HASH"
else
echo "⚠️ No existing base image found"
echo "⚠️ No existing base image found - will build"
fi
if [ "$CURRENT_HASH" != "$EXISTING_HASH" ]; then
if [ "${{ steps.base-hash.outputs.hash }}" != "$EXISTING_HASH" ]; then
echo "🔄 Base image needs rebuild (poetry.lock or Dockerfile.base changed)"
echo "needs_rebuild=true" >> $GITHUB_OUTPUT
else
@@ -511,88 +813,73 @@ jobs:
echo "needs_rebuild=false" >> $GITHUB_OUTPUT
fi
- name: Rebuild base image (if needed)
- name: Build and push base image (if needed)
if: steps.version_check.outputs.needs_deploy == 'true' && steps.check-base.outputs.needs_rebuild == 'true'
run: |
echo "🏗️ Rebuilding base image with updated dependencies..."
uses: docker/build-push-action@v6
with:
context: .
file: backend/Dockerfile.base
push: true
tags: |
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:${{ github.sha }}
# Registry cache for ALL layers (mode=max) - persists between builds
# ignore-error=true allows build to continue if cache doesn't exist yet
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:cache,ignore-error=true
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.BASE_IMAGE_NAME }}:cache,mode=max,ignore-error=true
build-args: |
BUILD_DATE=${{ github.run_id }}
BUILD_ID=${{ github.sha }}
BASE_CONTENT_HASH=${{ steps.base-hash.outputs.hash }}
# Provenance disabled for smaller image and faster push
provenance: false
BASE_BUILD_ID=$(gcloud builds submit \
--config=cloudbuild-base.yaml \
--project=nomadkaraoke \
--format='value(id)' \
--async \
.)
- name: Build and push app image
if: steps.version_check.outputs.needs_deploy == 'true'
uses: docker/build-push-action@v6
with:
context: .
file: backend/Dockerfile
push: true
tags: |
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:latest
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:${{ github.sha }}
${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:v${{ steps.version_check.outputs.version }}
# Registry cache for app layers
# ignore-error=true allows build to continue if cache doesn't exist yet
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:cache,ignore-error=true
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:cache,mode=max,ignore-error=true
provenance: false
echo "Base Build ID: $BASE_BUILD_ID"
echo "Build URL: https://console.cloud.google.com/cloud-build/builds/$BASE_BUILD_ID?project=nomadkaraoke"
# ============================================
# Deploy to Cloud Run
# ============================================
echo "Waiting for base image build to complete..."
while true; do
BUILD_STATUS=$(gcloud builds describe $BASE_BUILD_ID --project=nomadkaraoke --format='value(status)')
echo "Base build status: $BUILD_STATUS"
if [ "$BUILD_STATUS" = "SUCCESS" ]; then
echo "✅ Base image build completed successfully"
break
elif [ "$BUILD_STATUS" = "FAILURE" ] || [ "$BUILD_STATUS" = "CANCELLED" ] || [ "$BUILD_STATUS" = "TIMEOUT" ]; then
echo "❌ Base image build failed with status: $BUILD_STATUS"
echo ""
echo "==================== BUILD LOGS ===================="
gcloud logging read "resource.type=build AND resource.labels.build_id=$BASE_BUILD_ID" \
--project=nomadkaraoke \
--format="value(textPayload)" \
--limit=200 \
--order=asc || echo "Failed to retrieve logs from Cloud Logging"
echo "===================================================="
gcloud builds log $BASE_BUILD_ID --project=nomadkaraoke 2>&1 || true
exit 1
fi
sleep 30
done
- name: Trigger Cloud Build
- name: Deploy to Cloud Run
if: steps.version_check.outputs.needs_deploy == 'true'
run: |
echo "🚀 Triggering Cloud Build for deployment..."
echo "Commit: ${{ github.sha }}"
echo "Version: ${{ steps.version_check.outputs.version }}"
VERSION="${{ steps.version_check.outputs.version }}"
IMAGE="${{ env.REGISTRY }}/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.APP_IMAGE_NAME }}:${{ github.sha }}"
BUILD_ID=$(gcloud builds submit \
--config=cloudbuild.yaml \
--project=nomadkaraoke \
--format='value(id)' \
--async \
.)
echo "🚀 Deploying to Cloud Run..."
echo "Image: $IMAGE"
echo "Version: $VERSION"
echo "Build ID: $BUILD_ID"
echo "Build URL: https://console.cloud.google.com/cloud-build/builds/$BUILD_ID?project=nomadkaraoke"
echo "Waiting for build to complete..."
while true; do
BUILD_STATUS=$(gcloud builds describe $BUILD_ID --project=nomadkaraoke --format='value(status)')
echo "Build status: $BUILD_STATUS"
if [ "$BUILD_STATUS" = "SUCCESS" ]; then
echo "✅ Build completed successfully"
break
elif [ "$BUILD_STATUS" = "FAILURE" ] || [ "$BUILD_STATUS" = "CANCELLED" ] || [ "$BUILD_STATUS" = "TIMEOUT" ]; then
echo "❌ Build failed with status: $BUILD_STATUS"
echo ""
echo "==================== BUILD LOGS ===================="
gcloud logging read "resource.type=build AND resource.labels.build_id=$BUILD_ID" \
--project=nomadkaraoke \
--format="value(textPayload)" \
--limit=200 \
--order=asc || echo "Failed to retrieve logs from Cloud Logging"
echo "===================================================="
echo ""
gcloud builds log $BUILD_ID --project=nomadkaraoke 2>&1 || true
exit 1
fi
sleep 30
done
gcloud run deploy karaoke-backend \
--image $IMAGE \
--region us-central1 \
--platform managed \
--allow-unauthenticated \
--memory 16Gi \
--cpu 8 \
--cpu-boost \
--execution-environment gen2 \
--timeout 1800 \
--concurrency 1 \
--max-instances 50 \
--min-instances 0 \
--set-env-vars "GOOGLE_CLOUD_PROJECT=${{ env.PROJECT_ID }},GCS_BUCKET_NAME=karaoke-gen-storage-${{ env.PROJECT_ID }},FIRESTORE_COLLECTION=jobs,ENVIRONMENT=production,AUDIO_SEPARATOR_API_URL=https://nomadkaraoke--audio-separator-api.modal.run,KARAOKE_GEN_VERSION=$VERSION,DEFAULT_DROPBOX_PATH=/MediaUnsynced/Karaoke/Tracks-Organized,DEFAULT_GDRIVE_FOLDER_ID=1laRKAyxo0v817SstfM5XkpbWiNKNAMSX,ENABLE_CLOUD_TASKS=true,CLOUD_RUN_SERVICE_URL=https://api.nomadkaraoke.com,GCP_REGION=us-central1,DEFAULT_ENABLE_YOUTUBE_UPLOAD=true,DEFAULT_BRAND_PREFIX=NOMAD" \
--set-secrets "AUDIOSHAKE_API_TOKEN=audioshake-api-key:latest,GENIUS_API_TOKEN=genius-api-key:latest,SPOTIFY_COOKIE_SP_DC=spotify-cookie:latest,RAPIDAPI_KEY=rapidapi-key:latest,DEFAULT_DISCORD_WEBHOOK_URL=discord-releases-webhook:latest,ADMIN_TOKENS=admin-tokens:latest,RED_API_KEY=red-api-key:latest,RED_API_URL=red-api-url:latest,OPS_API_KEY=ops-api-key:latest,OPS_API_URL=ops-api-url:latest,FLACFETCH_API_URL=flacfetch-api-url:latest,FLACFETCH_API_KEY=flacfetch-api-key:latest"
- name: Verify deployment and version
if: steps.version_check.outputs.needs_deploy == 'true'
@@ -604,22 +891,22 @@ jobs:
echo "Service URL: $SERVICE_URL"
echo "Expected version: $EXPECTED_VERSION"
# Wait for new revision to be ready
echo "Waiting for new revision to be ready..."
sleep 15
# Basic health check
curl -f "$SERVICE_URL/api/health" || exit 1
echo "✅ Service is healthy"
# Version verification with retries (Cloud Run may take time to route to new revision)
MAX_RETRIES=6
RETRY_DELAY=10
for i in $(seq 1 $MAX_RETRIES); do
echo "Checking deployed version (attempt $i/$MAX_RETRIES)..."
DEPLOYED_VERSION=$(curl -sf "$SERVICE_URL/api/health/detailed" | jq -r '.version // empty')
if [ "$DEPLOYED_VERSION" = "$EXPECTED_VERSION" ]; then
echo "✅ Version verified: $DEPLOYED_VERSION"
break
@@ -643,7 +930,11 @@ jobs:
echo "Commit: ${{ github.sha }}"
echo "Branch: ${{ github.ref_name }}"
echo "Run: ${{ github.run_number }}"
echo ""
echo "📊 Cost savings: This build used FREE GitHub Actions instead of Cloud Build"
echo " Previously: ~\$0.064/min with N1_HIGHCPU_32"
echo " Now: \$0 (public repo) with registry-cached builds"
- name: Skipped deployment summary
if: steps.version_check.outputs.needs_deploy == 'false'
run: |
+6
View File
@@ -12,6 +12,9 @@ tests/outputs
/input
/output/
# Cloud Infra
.emulator-logs
# Next.js / React Frontend (./frontend)
node_modules/
frontend/node_modules/
@@ -224,3 +227,6 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
flacfetch
# Test output directories
Rick Astley*/
+123
View File
@@ -0,0 +1,123 @@
# Agent Instructions: Nomad Karaoke E2E Production Testing
## Context
You are acting on behalf of Andrew, the creator of Nomad Karaoke, who is building a fully functional end-to-end karaoke video generation platform. The platform has two modes:
1. **Local CLI** (`karaoke-gen`) - For power users running locally
2. **Public Web Service** (`gen.nomadkaraoke.com`) - For paying customers ($5/track via Stripe)
## Current State
The platform has been under heavy development with many features implemented, but **end-to-end functionality for real users on gen.nomadkaraoke.com is not fully working yet**. The goal is to make the complete user journey functional in production.
## Business Model (Planned)
- **buy.nomadkaraoke.com** - Customer intro/landing page with Stripe payment flow
- Payment generates a single-use token
- Customer is redirected to **gen.nomadkaraoke.com** with token ready to use
- Each track costs $5
## Your Primary Mission
**Test and fix the production system until end-to-end karaoke generation works reliably.**
### The Complete User Journey to Test
1. **Input** - User enters artist name and song title
2. **Audio Selection** - User selects audio source
3. **Download & Preparation** - System downloads and prepares audio (wait for completion)
4. **Lyrics Review** - User reviews/corrects lyrics in the web UI
5. **Preview Video** - Wait for preview video to load
6. **Submit Review** - User submits corrected lyrics
7. **Render Stage** - Wait for render to complete
8. **Instrumental Selection** - User selects instrumental via web UI
9. **Final Render & Distribution** - Wait for final render and distribution
10. **Verification** - Click to view uploaded YouTube video
11. **Verification** - Click Dropbox folder link to view shared folder
## Your Working Process
Use Playwright to automate and test this journey against **live production** at `gen.nomadkaraoke.com`.
### The Fix Loop
When you encounter something broken:
1. **Investigate** - Understand the root cause
2. **Fix** - Implement the fix
3. **Improve Tests** - Add/update tests to cover the issue
4. **Refactor** - Clean up if necessary
5. **PR** - Create a pull request with clear description
6. **CI** - Ensure all checks pass
7. **Merge** - Merge the PR
8. **Deploy** - Wait for deployment to complete
9. **Retest** - Test again in production
10. **Repeat** - Continue until everything works
## Key Rules
### From CLAUDE.md (Must Follow)
- **Never commit directly to `main`** - Always use feature branches
- **Always run tests before committing** - `make test` for backend, `cd frontend && npm run test:all` for frontend
- **Bump version** in `pyproject.toml` for every commit
- **Use git worktrees** for non-trivial changes
- **Infrastructure changes** must go through Pulumi IaC
### Testing Commands
```bash
# Backend tests (10 min timeout, limit output)
make test 2>&1 | tail -n 500
# Frontend tests
cd frontend && npm run test:all 2>&1 | tail -n 200
```
### Pre-commit Checklist
- [ ] Tests pass
- [ ] Version bumped in `pyproject.toml`
- [ ] PR created with clear description
- [ ] CI checks pass before merge
## Permissions
You have permission to:
- Run any command necessary
- Use Playwright to interact with browsers
- Make changes to any part of the codebase
- Create PRs and merge them (after CI passes)
- Test against live production
## Success Criteria
The mission is complete when:
1. Any artist/title can be entered on gen.nomadkaraoke.com
2. The full generation process completes successfully
3. A karaoke video is uploaded to YouTube
4. Files are distributed to Dropbox
5. The entire flow is covered by automated E2E tests
## Key URLs
- **Production Frontend**: https://gen.nomadkaraoke.com
- **Production API**: https://api.nomadkaraoke.com (inferred from README)
- **Lyrics Review UI**: https://gen.nomadkaraoke.com/lyrics/
## Reference Documentation
- `/Users/andrew/Projects/karaoke-gen/README.md` - Main project documentation
- `/Users/andrew/Projects/karaoke-gen/CLAUDE.md` - AI assistant guidelines
- `/Users/andrew/Projects/karaoke-gen/docs/01-reference/` - Reference documentation
- `/Users/andrew/Projects/karaoke-gen/docs/00-current-plan/` - Planning documents (may be outdated)
## Notes
- Some docs in `docs/00-current-plan/` may be outdated - review, evaluate, and move/delete as appropriate
- Focus on making things work, not perfect
- Ship iteratively - fix one thing at a time
- Document issues you find as you go
+40 -11
View File
@@ -2,6 +2,14 @@
This document contains guidelines and rules for AI assistants working on this codebase.
## Core Principles
**Always be testing.** Proactively evaluate, read, run, fix, and improve tests whenever you touch any part of the system—frontend, backend, infrastructure. Every code change should be thoroughly tested. Leave everything better tested than you found it.
**Always be documenting.** Continuously read, write, improve, update, and organize markdown documentation. Reference existing docs and keep them accurate as the codebase evolves.
**Build for maintainability.** Keep code SOLID, modular, and clean. Favor simplicity and clarity. Future developers (and AI agents) will thank you.
## Branching Workflow
**Never commit directly to `main`.** Always work on a feature branch.
@@ -10,18 +18,15 @@ Create descriptive branch names like `feature/add-discord-notifications`, `fix/a
### Using Git Worktrees
**For non-trivial changes** (new features, multi-file edits, refactors):
- Create a git worktree in the parent folder before making any file changes:
```bash
git worktree add ../karaoke-gen-feat-xyz feature/xyz
```
**All changes require a git worktree.** Since all changes must go through a PR, always create a worktree in the parent folder before making any file changes:
```bash
git worktree add -b feature/xyz ../karaoke-gen-feat-xyz main
```
- This keeps the main directory clean on `main` and enables parallel work on multiple features
- Name the worktree folder descriptively, e.g., `../karaoke-gen-fix-audio-sync`
**For trivial changes** (typos, single-line fixes):
- Creating a branch in the current directory is acceptable
- Use `git checkout -b fix/typo-xyz` before making changes
**Cleanup:** When done with a worktree, remove it with:
```bash
git worktree remove ../karaoke-gen-feat-xyz
@@ -67,11 +72,13 @@ Do not attempt to work around auth issues or make assumptions about credentials.
## Version Management
When committing and pushing changes, always bump the patch version in `pyproject.toml`.
When committing and pushing **code changes**, bump the patch version in `pyproject.toml`.
The version is located at `tool.poetry.version` in `pyproject.toml`. For example, if the current version is `0.70.3`, bump it to `0.70.4`.
This ensures every commit pushed to the repository has a unique version number for tracking and deployment purposes.
**Skip version bumps** for documentation-only changes (e.g., updating `CLAUDE.md`, `README.md`, or other markdown files) that don't affect deployed code.
This ensures every code release has a unique version number for tracking and deployment purposes.
## Testing Requirements
@@ -121,3 +128,25 @@ Individual test commands:
**Do not commit or push code that fails tests. Fix any test failures before proceeding.**
## Related Projects
### flacfetch
**Location:** `/Users/andrew/Projects/flacfetch`
The `flacfetch` package is a dependency of karaoke-gen that we also own. When working on features that interact with flacfetch, you can read and modify its code directly.
**Workflow for flacfetch changes:**
1. Navigate to `/Users/andrew/Projects/flacfetch`
2. Make changes directly on `main` (no PR required for this repo)
3. Run tests and ensure they pass
4. Bump the version in flacfetch's `pyproject.toml`
5. Commit and push to `main`
6. Wait for CI workflow to complete and auto-release the new version
7. Return to karaoke-gen and update the poetry dependency:
```bash
poetry update flacfetch
```
8. Commit the updated `poetry.lock` in karaoke-gen
+72 -2
View File
@@ -71,8 +71,40 @@ export AUDIOSHAKE_API_TOKEN="your_audioshake_token"
Get an API key at [https://www.audioshake.ai/](https://www.audioshake.ai/) - business only, at time of writing this.
#### Option 2: Whisper via RunPod
Open-source alternative using OpenAI's Whisper model on RunPod infrastructure.
#### Option 2: Local Whisper (No Cloud Required)
Run Whisper directly on your local machine using whisper-timestamped. Works on CPU, NVIDIA GPU (CUDA), or Apple Silicon.
```bash
# Install with local Whisper support
pip install karaoke-gen[local-whisper]
# Optional: Configure model size (tiny, base, small, medium, large)
export WHISPER_MODEL_SIZE="medium"
# Optional: Force specific device (cpu, cuda, mps)
export WHISPER_DEVICE="cpu"
```
**Model Size Guide:**
| Model | VRAM | Speed | Quality |
|-------|------|-------|---------|
| tiny | ~1GB | Fast | Lower |
| base | ~1GB | Fast | Basic |
| small | ~2GB | Medium | Good |
| medium | ~5GB | Slower | Better |
| large | ~10GB | Slowest | Best |
**CPU-Only Installation** (no GPU required):
```bash
# Pre-install CPU-only PyTorch first
pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu
pip install karaoke-gen[local-whisper]
```
Local Whisper runs automatically as a fallback when no cloud transcription services are configured.
#### Option 3: Whisper via RunPod
Cloud-based alternative using OpenAI's Whisper model on RunPod infrastructure.
```bash
export RUNPOD_API_KEY="your_runpod_key"
@@ -574,6 +606,44 @@ If the output video has quality problems:
- Check available codecs: `ffmpeg -codecs`
- For 4K output, ensure sufficient disk space (10GB+ per track)
### Local Whisper Issues
#### GPU Out of Memory
If you get CUDA out of memory errors:
```bash
# Use a smaller model
export WHISPER_MODEL_SIZE="small" # or "tiny"
# Or force CPU mode
export WHISPER_DEVICE="cpu"
```
#### Slow Transcription on CPU
CPU transcription is significantly slower than GPU. For faster processing:
- Use a smaller model (`tiny` or `base`)
- Consider using cloud transcription (AudioShake or RunPod)
- On Apple Silicon, the `small` model offers good speed/quality balance
#### Model Download Issues
Whisper models are downloaded on first use (~1-3GB depending on size). If downloads fail:
- Check your internet connection
- Set a custom cache directory: `export WHISPER_CACHE_DIR="/path/with/space"`
- Models are cached in `~/.cache/whisper/` by default
#### whisper-timestamped Not Found
If you get "whisper-timestamped is not installed":
```bash
pip install karaoke-gen[local-whisper]
# Or install directly:
pip install whisper-timestamped
```
#### Disabling Local Whisper
If you want to disable local Whisper (e.g., to force cloud transcription):
```bash
export ENABLE_LOCAL_WHISPER="false"
```
---
## 🧪 Development
+82 -16
View File
@@ -81,13 +81,17 @@ class AudioSearchRequest(BaseModel):
"""Request to search for audio by artist and title."""
artist: str = Field(..., description="Artist name to search for")
title: str = Field(..., description="Song title to search for")
# Auto-download mode
auto_download: bool = Field(False, description="Automatically select best result and download")
# Processing options
enable_cdg: bool = Field(True, description="Generate CDG+MP3 package")
enable_txt: bool = Field(True, description="Generate TXT+MP3 package")
# Theme configuration
theme_id: Optional[str] = Field(None, description="Theme ID to use (e.g., 'nomad', 'default'). If set, CDG/TXT are enabled by default.")
color_overrides: Optional[Dict[str, str]] = Field(None, description="Color overrides: artist_color, title_color, sung_lyrics_color, unsung_lyrics_color (hex #RRGGBB)")
# Processing options (CDG/TXT require style config or theme, disabled by default unless theme is set)
enable_cdg: Optional[bool] = Field(None, description="Generate CDG+MP3 package. Default: True if theme_id set, False otherwise")
enable_txt: Optional[bool] = Field(None, description="Generate TXT+MP3 package. Default: True if theme_id set, False otherwise")
# Finalisation options
brand_prefix: Optional[str] = Field(None, description="Brand code prefix (e.g., NOMAD)")
@@ -180,6 +184,35 @@ class AudioSelectResponse(BaseModel):
selected_provider: str
def _resolve_cdg_txt_defaults(
theme_id: Optional[str],
enable_cdg: Optional[bool],
enable_txt: Optional[bool]
) -> Tuple[bool, bool]:
"""
Resolve CDG/TXT settings based on theme and explicit settings.
When a theme is selected, CDG and TXT are enabled by default.
Explicit True/False values always override the default.
Args:
theme_id: Theme identifier (if any)
enable_cdg: Explicit CDG setting (None means use default)
enable_txt: Explicit TXT setting (None means use default)
Returns:
Tuple of (resolved_enable_cdg, resolved_enable_txt)
"""
# Default based on whether theme is set
default_enabled = theme_id is not None
# Explicit values override defaults, None uses default
resolved_cdg = enable_cdg if enable_cdg is not None else default_enabled
resolved_txt = enable_txt if enable_txt is not None else default_enabled
return resolved_cdg, resolved_txt
def extract_request_metadata(request: Request, created_from: str = "audio_search") -> Dict[str, Any]:
"""Extract metadata from request for job tracking."""
headers = dict(request.headers)
@@ -424,12 +457,17 @@ async def search_audio(
effective_dropbox_path = body.dropbox_path or settings.default_dropbox_path
effective_gdrive_folder_id = body.gdrive_folder_id or settings.default_gdrive_folder_id
effective_discord_webhook_url = body.discord_webhook_url or settings.default_discord_webhook_url
# Apply defaults for YouTube/Dropbox distribution (for web service)
effective_enable_youtube_upload = body.enable_youtube_upload or settings.default_enable_youtube_upload
effective_brand_prefix = body.brand_prefix or settings.default_brand_prefix
effective_youtube_description = body.youtube_description or settings.default_youtube_description
# Validate credentials if distribution services are requested
invalid_services = []
credential_manager = get_credential_manager()
if body.enable_youtube_upload:
if effective_enable_youtube_upload:
result = credential_manager.check_youtube_credentials()
if result.status != CredentialStatus.VALID:
invalid_services.append(f"youtube ({result.message})")
@@ -456,17 +494,24 @@ async def search_audio(
# Extract request metadata
request_metadata = extract_request_metadata(request, created_from="audio_search")
# Resolve CDG/TXT defaults based on theme
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
body.theme_id, body.enable_cdg, body.enable_txt
)
# Create job
job_create = JobCreate(
artist=body.artist,
title=body.title,
enable_cdg=body.enable_cdg,
enable_txt=body.enable_txt,
brand_prefix=body.brand_prefix,
enable_youtube_upload=body.enable_youtube_upload,
youtube_description=body.youtube_description,
youtube_description_template=body.youtube_description, # video_worker reads this field
theme_id=body.theme_id,
color_overrides=body.color_overrides or {},
enable_cdg=resolved_cdg,
enable_txt=resolved_txt,
brand_prefix=effective_brand_prefix,
enable_youtube_upload=effective_enable_youtube_upload,
youtube_description=effective_youtube_description,
youtube_description_template=effective_youtube_description, # video_worker reads this field
discord_webhook_url=effective_discord_webhook_url,
dropbox_path=effective_dropbox_path,
gdrive_folder_id=effective_gdrive_folder_id,
@@ -492,7 +537,28 @@ async def search_audio(
'audio_search_title': body.title,
'auto_download': body.auto_download,
})
# If theme is set and no custom style files are being uploaded, prepare theme style now
# This copies the theme's style_params.json to the job folder so LyricsTranscriber
# can access the style configuration for preview videos
if body.theme_id and not body.style_files:
from backend.api.routes.file_upload import _prepare_theme_for_job
try:
style_params_path, theme_style_assets, youtube_desc = _prepare_theme_for_job(
job_id, body.theme_id, body.color_overrides
)
theme_update = {
'style_params_gcs_path': style_params_path,
'style_assets': theme_style_assets,
}
if youtube_desc and not effective_youtube_description:
theme_update['youtube_description_template'] = youtube_desc
job_manager.update_job(job_id, theme_update)
logger.info(f"Applied theme '{body.theme_id}' to job {job_id}")
except Exception as e:
logger.warning(f"Failed to prepare theme '{body.theme_id}' for job {job_id}: {e}")
# Continue without theme - job can still be processed with defaults
# Handle style file uploads if provided
style_upload_urls: List[StyleUploadUrl] = []
style_assets = {}
+201 -38
View File
@@ -22,10 +22,12 @@ from typing import Optional, List, Dict, Any, Tuple
from pydantic import BaseModel, Field
from backend.models.job import JobCreate, JobStatus
from backend.models.theme import ColorOverrides
from backend.services.job_manager import JobManager
from backend.services.storage_service import StorageService
from backend.services.worker_service import get_worker_service
from backend.services.credential_manager import get_credential_manager, CredentialStatus
from backend.services.theme_service import get_theme_service
from backend.config import get_settings
from backend.version import VERSION
from backend.services.metrics import metrics
@@ -51,15 +53,19 @@ class CreateJobFromUrlRequest(BaseModel):
"""Request to create a job from a YouTube/online URL."""
# Required fields
url: str = Field(..., description="YouTube or other video URL to download audio from")
# Optional fields - will be auto-detected from URL if not provided
artist: Optional[str] = Field(None, description="Artist name (auto-detected from URL if not provided)")
title: Optional[str] = Field(None, description="Song title (auto-detected from URL if not provided)")
# Processing options
enable_cdg: bool = Field(True, description="Generate CDG+MP3 package")
enable_txt: bool = Field(True, description="Generate TXT+MP3 package")
# Theme configuration (use pre-made theme from GCS)
theme_id: Optional[str] = Field(None, description="Theme ID to use (e.g., 'nomad', 'default'). If set, CDG/TXT are enabled by default.")
color_overrides: Optional[Dict[str, str]] = Field(None, description="Color overrides: artist_color, title_color, sung_lyrics_color, unsung_lyrics_color (hex #RRGGBB)")
# Processing options (CDG/TXT require style config or theme, disabled by default unless theme is set)
enable_cdg: Optional[bool] = Field(None, description="Generate CDG+MP3 package. Default: True if theme_id set, False otherwise")
enable_txt: Optional[bool] = Field(None, description="Generate TXT+MP3 package. Default: True if theme_id set, False otherwise")
# Finalisation options
brand_prefix: Optional[str] = Field(None, description="Brand code prefix (e.g., NOMAD)")
enable_youtube_upload: bool = Field(False, description="Upload to YouTube")
@@ -67,19 +73,19 @@ class CreateJobFromUrlRequest(BaseModel):
discord_webhook_url: Optional[str] = Field(None, description="Discord webhook URL for notifications")
webhook_url: Optional[str] = Field(None, description="Generic webhook URL")
user_email: Optional[str] = Field(None, description="User email for notifications")
# Distribution options (native API - preferred for remote CLI)
dropbox_path: Optional[str] = Field(None, description="Dropbox folder path for organized output")
gdrive_folder_id: Optional[str] = Field(None, description="Google Drive folder ID for public share uploads")
# Legacy distribution options (rclone - deprecated)
organised_dir_rclone_root: Optional[str] = Field(None, description="[Deprecated] rclone remote path")
# Lyrics configuration
lyrics_artist: Optional[str] = Field(None, description="Override artist name for lyrics search")
lyrics_title: Optional[str] = Field(None, description="Override title for lyrics search")
subtitle_offset_ms: int = Field(0, description="Subtitle timing offset in milliseconds")
# Audio separation model configuration
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")
@@ -102,10 +108,14 @@ class CreateJobWithUploadUrlsRequest(BaseModel):
artist: str = Field(..., description="Artist name")
title: str = Field(..., description="Song title")
files: List[FileUploadRequest] = Field(..., description="List of files to upload")
# Processing options
enable_cdg: bool = Field(True, description="Generate CDG+MP3 package")
enable_txt: bool = Field(True, description="Generate TXT+MP3 package")
# Theme configuration (use pre-made theme from GCS)
theme_id: Optional[str] = Field(None, description="Theme ID to use (e.g., 'nomad', 'default'). If set, CDG/TXT are enabled by default.")
color_overrides: Optional[Dict[str, str]] = Field(None, description="Color overrides: artist_color, title_color, sung_lyrics_color, unsung_lyrics_color (hex #RRGGBB)")
# Processing options (CDG/TXT require style config or theme, disabled by default unless theme is set)
enable_cdg: Optional[bool] = Field(None, description="Generate CDG+MP3 package. Default: True if theme_id set, False otherwise")
enable_txt: Optional[bool] = Field(None, description="Generate TXT+MP3 package. Default: True if theme_id set, False otherwise")
# Finalisation options
brand_prefix: Optional[str] = Field(None, description="Brand code prefix (e.g., NOMAD)")
@@ -226,7 +236,7 @@ ALLOWED_FONT_EXTENSIONS = {'.ttf', '.otf', '.woff', '.woff2'}
async def _trigger_workers_parallel(job_id: str) -> None:
"""
Trigger both audio and lyrics workers in parallel.
FastAPI's BackgroundTasks runs async tasks sequentially, so we use
asyncio.gather to ensure both workers start at the same time.
"""
@@ -236,6 +246,83 @@ async def _trigger_workers_parallel(job_id: str) -> None:
)
def _resolve_cdg_txt_defaults(
theme_id: Optional[str],
enable_cdg: Optional[bool],
enable_txt: Optional[bool]
) -> Tuple[bool, bool]:
"""
Resolve CDG/TXT settings based on theme and explicit settings.
When a theme is selected, CDG and TXT are enabled by default.
Explicit True/False values always override the default.
Args:
theme_id: Theme identifier (if any)
enable_cdg: Explicit CDG setting (None means use default)
enable_txt: Explicit TXT setting (None means use default)
Returns:
Tuple of (resolved_enable_cdg, resolved_enable_txt)
"""
# Default based on whether theme is set
default_enabled = theme_id is not None
# Resolve with explicit override taking precedence
resolved_cdg = enable_cdg if enable_cdg is not None else default_enabled
resolved_txt = enable_txt if enable_txt is not None else default_enabled
return resolved_cdg, resolved_txt
def _prepare_theme_for_job(
job_id: str,
theme_id: str,
color_overrides: Optional[Dict[str, str]] = None
) -> Tuple[str, Dict[str, str], Optional[str]]:
"""
Prepare theme style files for a job.
Args:
job_id: The job ID
theme_id: Theme identifier
color_overrides: Optional color override dict
Returns:
Tuple of (style_params_gcs_path, style_assets, youtube_description)
Raises:
HTTPException: If theme not found
"""
theme_service = get_theme_service()
# Verify theme exists
if not theme_service.theme_exists(theme_id):
raise HTTPException(
status_code=400,
detail=f"Theme not found: {theme_id}. Use GET /api/themes to list available themes."
)
# Convert dict to ColorOverrides model if provided
color_overrides_model = None
if color_overrides:
color_overrides_model = ColorOverrides(**color_overrides)
# Prepare job style from theme
style_params_path, style_assets = theme_service.prepare_job_style(
job_id=job_id,
theme_id=theme_id,
color_overrides=color_overrides_model
)
# Get YouTube description template if available
youtube_desc = theme_service.get_youtube_description(theme_id)
logger.info(f"Prepared theme '{theme_id}' for job {job_id}")
return style_params_path, style_assets, youtube_desc
@router.post("/jobs/upload")
async def upload_and_create_job(
request: Request,
@@ -254,9 +341,9 @@ async def upload_and_create_job(
style_cdg_instrumental_background: Optional[UploadFile] = File(None, description="CDG instrumental background"),
style_cdg_title_background: Optional[UploadFile] = File(None, description="CDG title screen background"),
style_cdg_outro_background: Optional[UploadFile] = File(None, description="CDG outro screen background"),
# Processing options
enable_cdg: bool = Form(True, description="Generate CDG+MP3 package"),
enable_txt: bool = Form(True, description="Generate TXT+MP3 package"),
# Processing options (CDG/TXT require style config, disabled by default)
enable_cdg: bool = Form(False, description="Generate CDG+MP3 package (requires style config)"),
enable_txt: bool = Form(False, description="Generate TXT+MP3 package (requires style config)"),
# Finalisation options
brand_prefix: Optional[str] = Form(None, description="Brand code prefix (e.g., NOMAD)"),
enable_youtube_upload: bool = Form(False, description="Upload to YouTube"),
@@ -681,10 +768,14 @@ class CreateFinaliseOnlyJobRequest(BaseModel):
artist: str = Field(..., description="Artist name")
title: str = Field(..., description="Song title")
files: List[FinaliseOnlyFileRequest] = Field(..., description="List of prep output files to upload")
# Processing options
enable_cdg: bool = Field(True, description="Generate CDG+MP3 package")
enable_txt: bool = Field(True, description="Generate TXT+MP3 package")
# Theme configuration (use pre-made theme from GCS)
theme_id: Optional[str] = Field(None, description="Theme ID to use (e.g., 'nomad', 'default'). If set, CDG/TXT are enabled by default.")
color_overrides: Optional[Dict[str, str]] = Field(None, description="Color overrides: artist_color, title_color, sung_lyrics_color, unsung_lyrics_color (hex #RRGGBB)")
# Processing options (CDG/TXT require style config or theme, disabled by default unless theme is set)
enable_cdg: Optional[bool] = Field(None, description="Generate CDG+MP3 package. Default: True if theme_id set, False otherwise")
enable_txt: Optional[bool] = Field(None, description="Generate TXT+MP3 package. Default: True if theme_id set, False otherwise")
# Finalisation options
brand_prefix: Optional[str] = Field(None, description="Brand code prefix (e.g., NOMAD)")
@@ -859,17 +950,27 @@ async def create_job_with_upload_urls(
# Extract request metadata for tracking
request_metadata = extract_request_metadata(request, created_from="signed_url_upload")
# Get original audio filename
audio_file = audio_files[0]
# Resolve CDG/TXT defaults based on theme
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
body.theme_id, body.enable_cdg, body.enable_txt
)
# Check if style_params is being uploaded (overrides theme)
has_style_params_upload = any(f.file_type == 'style_params' for f in body.files)
# Create job
job_create = JobCreate(
artist=body.artist,
title=body.title,
filename=audio_file.filename,
enable_cdg=body.enable_cdg,
enable_txt=body.enable_txt,
theme_id=body.theme_id,
color_overrides=body.color_overrides or {},
enable_cdg=resolved_cdg,
enable_txt=resolved_txt,
brand_prefix=body.brand_prefix,
enable_youtube_upload=body.enable_youtube_upload,
youtube_description=body.youtube_description,
@@ -889,11 +990,26 @@ async def create_job_with_upload_urls(
)
job = job_manager.create_job(job_create)
job_id = job.job_id
# Record job creation metric
metrics.record_job_created(job_id, source="upload")
logger.info(f"Created job {job_id} for {body.artist} - {body.title} (signed URL upload flow)")
# If theme is set and no style_params uploaded, prepare theme style now
if body.theme_id and not has_style_params_upload:
style_params_path, style_assets, youtube_desc = _prepare_theme_for_job(
job_id, body.theme_id, body.color_overrides
)
# Update job with theme style data
update_data = {
'style_params_gcs_path': style_params_path,
'style_assets': style_assets,
}
if youtube_desc and not body.youtube_description:
update_data['youtube_description_template'] = youtube_desc
job_manager.update_job(job_id, update_data)
logger.info(f"Applied theme '{body.theme_id}' to job {job_id}")
# Generate signed upload URLs for each file
upload_urls = []
@@ -1245,19 +1361,26 @@ async def create_job_from_url(
# Extract request metadata for tracking
request_metadata = extract_request_metadata(request, created_from="url")
# Use provided artist/title or leave as None (will be auto-detected by audio worker)
artist = body.artist
title = body.title
# Resolve CDG/TXT defaults based on theme
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
body.theme_id, body.enable_cdg, body.enable_txt
)
# Create job with URL
job_create = JobCreate(
url=body.url,
artist=artist,
title=title,
filename=None, # No file uploaded
enable_cdg=body.enable_cdg,
enable_txt=body.enable_txt,
theme_id=body.theme_id,
color_overrides=body.color_overrides or {},
enable_cdg=resolved_cdg,
enable_txt=resolved_txt,
brand_prefix=body.brand_prefix,
enable_youtube_upload=body.enable_youtube_upload,
youtube_description=body.youtube_description,
@@ -1277,9 +1400,24 @@ async def create_job_from_url(
)
job = job_manager.create_job(job_create)
job_id = job.job_id
# Record job creation metric
metrics.record_job_created(job_id, source="url")
# If theme is set, prepare theme style now
if body.theme_id:
style_params_path, style_assets, youtube_desc = _prepare_theme_for_job(
job_id, body.theme_id, body.color_overrides
)
# Update job with theme style data
update_data = {
'style_params_gcs_path': style_params_path,
'style_assets': style_assets,
}
if youtube_desc and not body.youtube_description:
update_data['youtube_description_template'] = youtube_desc
job_manager.update_job(job_id, update_data)
logger.info(f"Applied theme '{body.theme_id}' to job {job_id}")
logger.info(f"Created URL-based job {job_id} for URL: {body.url}")
if artist:
@@ -1462,14 +1600,24 @@ async def create_finalise_only_job(
# Extract request metadata
request_metadata = extract_request_metadata(request, created_from="finalise_only_upload")
# Resolve CDG/TXT defaults based on theme
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
body.theme_id, body.enable_cdg, body.enable_txt
)
# Check if style_params is being uploaded (overrides theme)
has_style_params_upload = any(f.file_type == 'style_params' for f in body.files)
# Create job with finalise_only=True
job_create = JobCreate(
artist=body.artist,
title=body.title,
filename="finalise_only", # No single audio file - using prep outputs
enable_cdg=body.enable_cdg,
enable_txt=body.enable_txt,
theme_id=body.theme_id,
color_overrides=body.color_overrides or {},
enable_cdg=resolved_cdg,
enable_txt=resolved_txt,
brand_prefix=body.brand_prefix,
enable_youtube_upload=body.enable_youtube_upload,
youtube_description=body.youtube_description,
@@ -1482,11 +1630,26 @@ async def create_finalise_only_job(
)
job = job_manager.create_job(job_create)
job_id = job.job_id
# Record job creation metric
metrics.record_job_created(job_id, source="finalise")
logger.info(f"Created finalise-only job {job_id} for {body.artist} - {body.title}")
# If theme is set and no style_params uploaded, prepare theme style now
if body.theme_id and not has_style_params_upload:
style_params_path, style_assets, youtube_desc = _prepare_theme_for_job(
job_id, body.theme_id, body.color_overrides
)
# Update job with theme style data
update_data = {
'style_params_gcs_path': style_params_path,
'style_assets': style_assets,
}
if youtube_desc and not body.youtube_description:
update_data['youtube_description_template'] = youtube_desc
job_manager.update_job(job_id, update_data)
logger.info(f"Applied theme '{body.theme_id}' to job {job_id}")
# Generate signed upload URLs for each file
upload_urls = []
+67 -15
View File
@@ -336,12 +336,27 @@ async def get_review_data(
# Get URLs from file_urls
corrections_url = job.file_urls.get('lyrics', {}).get('corrections')
audio_url = job.file_urls.get('lyrics', {}).get('audio')
if not corrections_url or not audio_url:
# For audio, try multiple sources in order of preference:
# 1. Explicit lyrics audio (if worker uploaded it)
# 2. Lead vocals stem (best for reviewing lyrics sync)
# 3. Input media (original audio)
audio_url = (
job.file_urls.get('lyrics', {}).get('audio') or
job.file_urls.get('stems', {}).get('lead_vocals') or
job.input_media_gcs_path
)
if not corrections_url:
raise HTTPException(
status_code=500,
detail="Review data not available"
detail="Corrections data not available"
)
if not audio_url:
raise HTTPException(
status_code=500,
detail="Audio not available for review"
)
# Generate signed URLs for direct access
@@ -1097,32 +1112,34 @@ async def retry_job(
auth_data: Tuple[str, UserType, int] = Depends(require_auth)
) -> dict:
"""
Retry a failed job from the last successful checkpoint.
This endpoint allows resuming jobs that failed during:
Retry a failed or cancelled job from the last successful checkpoint.
This endpoint allows resuming jobs that failed or were cancelled during:
- Audio processing (re-runs from beginning if input audio exists)
- Video generation (re-runs video worker)
- Encoding (re-runs video worker)
- Packaging (re-runs video worker)
The retry logic determines the appropriate stage to resume from
based on what files/state already exist.
"""
job = job_manager.get_job(job_id)
if not job:
raise HTTPException(status_code=404, detail="Job not found")
if job.status != JobStatus.FAILED:
if job.status not in [JobStatus.FAILED, JobStatus.CANCELLED]:
raise HTTPException(
status_code=400,
detail=f"Only failed jobs can be retried (current status: {job.status})"
detail=f"Only failed or cancelled jobs can be retried (current status: {job.status})"
)
try:
# Determine retry point based on what's already complete
error_details = job.error_details or {}
error_stage = error_details.get('stage', 'unknown')
logger.info(f"Job {job_id}: Retrying from failed stage '{error_stage}'")
original_status = job.status
logger.info(f"Job {job_id}: Retrying from {original_status} state (error stage: '{error_stage}')")
# Check what state exists to determine retry point
file_urls = job.file_urls or {}
@@ -1230,11 +1247,46 @@ async def retry_job(
"retry_stage": "screens_generation"
}
# If we have input audio (uploaded or from URL), restart from beginning
elif job.input_media_gcs_path or job.url:
logger.info(f"Job {job_id}: Has input audio, restarting from beginning")
# Clear error state and any partial progress
job_manager.update_job(job_id, {
'error_message': None,
'error_details': None,
'state_data': {}, # Clear parallel worker progress
})
# Reset to DOWNLOADING and trigger audio worker
if not job_manager.transition_to_state(
job_id=job_id,
new_status=JobStatus.DOWNLOADING,
progress=5,
message=f"Restarting job from {original_status} state"
):
raise HTTPException(
status_code=500,
detail="Failed to transition job status for retry"
)
# Trigger audio worker (which kicks off parallel audio + lyrics processing)
background_tasks.add_task(worker_service.trigger_audio_worker, job_id)
background_tasks.add_task(worker_service.trigger_lyrics_worker, job_id)
return {
"status": "success",
"job_id": job_id,
"job_status": "downloading",
"message": f"Job restarted from {original_status} state",
"retry_stage": "from_beginning"
}
else:
# Can't determine a safe retry point, need to restart from beginning
# No input audio available - job needs to be resubmitted
raise HTTPException(
status_code=400,
detail="Cannot determine safe retry point. Job may need to be resubmitted."
detail="Cannot retry: no input audio available. Job must be resubmitted."
)
except HTTPException:
+162
View File
@@ -0,0 +1,162 @@
"""
Theme API routes for listing and retrieving theme information.
Themes are pre-created style configurations that users can select when creating jobs.
These endpoints are public (no authentication required) to allow theme selection
before job creation.
"""
import logging
from typing import Optional
from fastapi import APIRouter, HTTPException
from backend.models.theme import ThemeDetailResponse, ThemesListResponse
from backend.services.theme_service import get_theme_service
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/themes", tags=["themes"])
@router.get("", response_model=ThemesListResponse)
async def list_themes() -> ThemesListResponse:
"""
List all available themes.
Returns a list of themes with their metadata and preview image URLs.
Preview URLs are signed and valid for 60 minutes.
This endpoint is public and does not require authentication.
"""
try:
theme_service = get_theme_service()
themes = theme_service.list_themes()
return ThemesListResponse(themes=themes)
except Exception as e:
logger.error(f"Error listing themes: {e}")
raise HTTPException(
status_code=500,
detail="Failed to load themes"
)
@router.get("/{theme_id}", response_model=ThemeDetailResponse)
async def get_theme(theme_id: str) -> ThemeDetailResponse:
"""
Get detailed information about a specific theme.
Returns the full theme details including style parameters.
This can be used to preview theme settings or inspect configuration.
Args:
theme_id: The unique identifier of the theme
Returns:
ThemeDetailResponse with full theme details
Raises:
404: Theme not found
"""
try:
theme_service = get_theme_service()
theme = theme_service.get_theme(theme_id)
if theme is None:
raise HTTPException(
status_code=404,
detail=f"Theme not found: {theme_id}"
)
return ThemeDetailResponse(theme=theme)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting theme {theme_id}: {e}")
raise HTTPException(
status_code=500,
detail="Failed to load theme"
)
@router.get("/{theme_id}/preview")
async def get_theme_preview(theme_id: str) -> dict:
"""
Get a signed URL for the theme's preview image.
This endpoint is useful when you need just the preview URL
without loading the full theme details.
Args:
theme_id: The unique identifier of the theme
Returns:
Object with preview_url field
Raises:
404: Theme not found or no preview available
"""
try:
theme_service = get_theme_service()
if not theme_service.theme_exists(theme_id):
raise HTTPException(
status_code=404,
detail=f"Theme not found: {theme_id}"
)
theme = theme_service.get_theme(theme_id)
if theme is None or theme.preview_url is None:
raise HTTPException(
status_code=404,
detail=f"No preview available for theme: {theme_id}"
)
return {"preview_url": theme.preview_url}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting preview for theme {theme_id}: {e}")
raise HTTPException(
status_code=500,
detail="Failed to load theme preview"
)
@router.get("/{theme_id}/youtube-description")
async def get_theme_youtube_description(theme_id: str) -> dict:
"""
Get the YouTube description template for a theme.
Some themes include a default YouTube video description template
that can be used when uploading videos.
Args:
theme_id: The unique identifier of the theme
Returns:
Object with description field containing the template text,
or null if no template is available
Raises:
404: Theme not found
"""
try:
theme_service = get_theme_service()
if not theme_service.theme_exists(theme_id):
raise HTTPException(
status_code=404,
detail=f"Theme not found: {theme_id}"
)
description = theme_service.get_youtube_description(theme_id)
return {"description": description}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting YouTube description for theme {theme_id}: {e}")
raise HTTPException(
status_code=500,
detail="Failed to load YouTube description"
)
+10
View File
@@ -69,6 +69,16 @@ class Settings(BaseSettings):
default_discord_webhook_url: Optional[str] = (
os.getenv("DEFAULT_DISCORD_WEBHOOK_URL", "").strip() or None
)
# Default values for web service jobs (YouTube/Dropbox distribution)
default_enable_youtube_upload: bool = os.getenv("DEFAULT_ENABLE_YOUTUBE_UPLOAD", "false").lower() in ("true", "1", "yes")
default_brand_prefix: Optional[str] = os.getenv("DEFAULT_BRAND_PREFIX")
default_youtube_description: str = os.getenv(
"DEFAULT_YOUTUBE_DESCRIPTION",
"Karaoke video created with Nomad Karaoke (https://nomadkaraoke.com)\n\n"
"AI-powered vocal separation and synchronized lyrics.\n\n"
"#karaoke #music #singing #instrumental #lyrics"
)
# Secret Manager cache
_secret_cache: Dict[str, str] = {}
+2 -1
View File
@@ -7,7 +7,7 @@ from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from backend.config import settings
from backend.api.routes import health, jobs, internal, file_upload, review, auth, audio_search
from backend.api.routes import health, jobs, internal, file_upload, review, auth, audio_search, themes
from backend.services.tracing import setup_tracing, instrument_app, get_current_trace_id
from backend.services.structured_logging import setup_structured_logging
@@ -108,6 +108,7 @@ app.include_router(internal.router, prefix="/api") # Internal worker endpoints
app.include_router(review.router, prefix="/api") # Review UI compatibility endpoints
app.include_router(auth.router, prefix="/api") # OAuth credential management
app.include_router(audio_search.router, prefix="/api") # Audio search (artist+title mode)
app.include_router(themes.router, prefix="/api") # Theme selection for styles
@app.get("/")
+38 -10
View File
@@ -134,17 +134,23 @@ STATE_TRANSITIONS = {
JobStatus.UPLOADING: [JobStatus.NOTIFYING, JobStatus.COMPLETE, JobStatus.FAILED],
JobStatus.NOTIFYING: [JobStatus.COMPLETE, JobStatus.FAILED],
# Terminal states - COMPLETE, PREP_COMPLETE, and CANCELLED have no transitions
# FAILED allows retry transitions to resume from checkpoints
# Terminal states - COMPLETE, PREP_COMPLETE have no transitions
# FAILED and CANCELLED allow retry transitions to resume from checkpoints
# PREP_COMPLETE allows finalise-only continuation
JobStatus.COMPLETE: [],
JobStatus.PREP_COMPLETE: [JobStatus.AWAITING_INSTRUMENTAL_SELECTION, JobStatus.FAILED], # Finalise-only continues from here
JobStatus.FAILED: [
JobStatus.DOWNLOADING, # Retry from beginning (if input audio exists)
JobStatus.INSTRUMENTAL_SELECTED, # Retry from video generation
JobStatus.REVIEW_COMPLETE, # Retry from render stage
JobStatus.LYRICS_COMPLETE, # Retry from screens generation
],
JobStatus.CANCELLED: [
JobStatus.DOWNLOADING, # Retry from beginning (if input audio exists)
JobStatus.INSTRUMENTAL_SELECTED, # Retry from video generation
JobStatus.REVIEW_COMPLETE, # Retry from render stage
JobStatus.LYRICS_COMPLETE, # Retry from screens generation
],
JobStatus.CANCELLED: [],
# Legacy states (for backward compatibility)
JobStatus.QUEUED: [JobStatus.PENDING],
@@ -192,14 +198,25 @@ class Job(BaseModel):
input_media_gcs_path: Optional[str] = None # GCS path to uploaded file
# User preferences
enable_cdg: bool = True # Generate CDG+MP3 package
enable_txt: bool = True # Generate TXT+MP3 package
enable_cdg: bool = False # Generate CDG+MP3 package (requires style config)
enable_txt: bool = False # Generate TXT+MP3 package (requires style config)
enable_youtube_upload: bool = False # Upload to YouTube
youtube_description: Optional[str] = None # YouTube video description
webhook_url: Optional[str] = None # Webhook for notifications
user_email: Optional[str] = None # Email for notifications
# Style configuration (uploaded files)
# Theme configuration (pre-made themes from GCS)
theme_id: Optional[str] = None # Theme identifier (e.g., "nomad", "default")
color_overrides: Dict[str, str] = Field(default_factory=dict)
"""
User color overrides applied on top of theme. Keys:
- artist_color: Hex color for artist name (#RRGGBB)
- title_color: Hex color for song title
- sung_lyrics_color: Hex color for highlighted lyrics
- unsung_lyrics_color: Hex color for unhighlighted lyrics
"""
# Style configuration (uploaded files - used when theme_id is not set)
style_params_gcs_path: Optional[str] = None # GCS path to style_params.json
style_assets: Dict[str, str] = Field(default_factory=dict)
"""
@@ -394,16 +411,27 @@ class JobCreate(BaseModel):
artist: Optional[str] = None
title: Optional[str] = None
filename: Optional[str] = None # Original uploaded filename
# Optional preferences
enable_cdg: bool = True
enable_txt: bool = True
enable_cdg: bool = False # Requires style config
enable_txt: bool = False # Requires style config
enable_youtube_upload: bool = False
youtube_description: Optional[str] = None
webhook_url: Optional[str] = None
user_email: Optional[str] = None
# Style configuration (will be populated after file upload)
# Theme configuration (pre-made themes from GCS)
theme_id: Optional[str] = None # Theme identifier (e.g., "nomad", "default")
color_overrides: Dict[str, str] = Field(default_factory=dict)
"""
User color overrides applied on top of theme. Keys:
- artist_color: Hex color for artist name (#RRGGBB)
- title_color: Hex color for song title
- sung_lyrics_color: Hex color for highlighted lyrics
- unsung_lyrics_color: Hex color for unhighlighted lyrics
"""
# Style configuration (will be populated after file upload, or from theme)
style_params_gcs_path: Optional[str] = None
style_assets: Dict[str, str] = Field(default_factory=dict)
+5 -5
View File
@@ -12,8 +12,8 @@ class URLSubmissionRequest(BaseModel):
title: Optional[str] = None # Auto-detected if not provided
# Optional preferences
enable_cdg: bool = True
enable_txt: bool = True
enable_cdg: bool = False # Requires style config
enable_txt: bool = False # Requires style config
enable_youtube_upload: bool = False
youtube_description: Optional[str] = None
webhook_url: Optional[str] = None
@@ -24,10 +24,10 @@ class UploadSubmissionRequest(BaseModel):
"""Request to submit a job from an uploaded file."""
artist: str
title: str
# Optional preferences
enable_cdg: bool = True
enable_txt: bool = True
enable_cdg: bool = False # Requires style config
enable_txt: bool = False # Requires style config
enable_youtube_upload: bool = False
youtube_description: Optional[str] = None
webhook_url: Optional[str] = None
+153
View File
@@ -0,0 +1,153 @@
"""
Theme data models for karaoke-gen.
Themes are pre-created style configurations stored in GCS that users can select
when creating karaoke jobs. Each theme includes:
- Style parameters (fonts, colors, backgrounds, layouts)
- Asset files (images, fonts)
- Preview images
- Optional YouTube description template
"""
from datetime import datetime
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field
class ThemeSummary(BaseModel):
"""Summary of a theme for listing purposes."""
id: str = Field(..., description="Unique theme identifier (e.g., 'nomad', 'default')")
name: str = Field(..., description="Human-readable theme name")
description: str = Field(..., description="Brief description of the theme style")
preview_url: Optional[str] = Field(None, description="Signed URL for preview image")
thumbnail_url: Optional[str] = Field(None, description="Signed URL for smaller thumbnail")
is_default: bool = Field(False, description="Whether this is the default theme")
class ThemeMetadata(BaseModel):
"""Metadata for a single theme in the registry."""
id: str
name: str
description: str
is_default: bool = False
created_at: Optional[datetime] = None
class ThemeRegistry(BaseModel):
"""Registry of all available themes, stored as _metadata.json in GCS."""
version: int = 1
themes: List[ThemeMetadata] = Field(default_factory=list)
class ThemeDetail(BaseModel):
"""Full theme details including style parameters."""
id: str
name: str
description: str
preview_url: Optional[str] = None
is_default: bool = False
style_params: Dict[str, Any] = Field(default_factory=dict)
has_youtube_description: bool = False
class ColorOverrides(BaseModel):
"""
User-customizable color overrides that can be applied on top of a theme.
All colors are hex format (#RRGGBB). When applied:
- artist_color: Applied to intro.artist_color, end.artist_color, cdg.artist_color
- title_color: Applied to intro.title_color, end.title_color, cdg.title_color
- sung_lyrics_color: Applied to karaoke.primary_color (converted to RGBA), cdg.active_fill
- unsung_lyrics_color: Applied to karaoke.secondary_color (converted to RGBA), cdg.inactive_fill
"""
artist_color: Optional[str] = Field(
None,
pattern=r"^#[0-9A-Fa-f]{6}$",
description="Hex color for artist name on intro/end screens",
)
title_color: Optional[str] = Field(
None,
pattern=r"^#[0-9A-Fa-f]{6}$",
description="Hex color for song title on intro/end screens",
)
sung_lyrics_color: Optional[str] = Field(
None,
pattern=r"^#[0-9A-Fa-f]{6}$",
description="Hex color for lyrics being sung (highlighted)",
)
unsung_lyrics_color: Optional[str] = Field(
None,
pattern=r"^#[0-9A-Fa-f]{6}$",
description="Hex color for lyrics not yet sung",
)
def has_overrides(self) -> bool:
"""Check if any color overrides are set."""
return any(
[
self.artist_color,
self.title_color,
self.sung_lyrics_color,
self.unsung_lyrics_color,
]
)
def to_dict(self) -> Dict[str, str]:
"""Convert to dict, excluding None values."""
return {k: v for k, v in self.model_dump().items() if v is not None}
class ThemesListResponse(BaseModel):
"""Response from GET /api/themes endpoint."""
themes: List[ThemeSummary]
class ThemeDetailResponse(BaseModel):
"""Response from GET /api/themes/{theme_id} endpoint."""
theme: ThemeDetail
# Utility functions for color conversion
def hex_to_rgba(hex_color: str, alpha: int = 255) -> str:
"""
Convert hex color (#RRGGBB) to RGBA format for ASS subtitles.
ASS subtitle format uses "R, G, B, A" string format.
Args:
hex_color: Hex color string like "#7070F7"
alpha: Alpha value 0-255 (default 255 = opaque)
Returns:
RGBA string like "112, 112, 247, 255"
"""
hex_color = hex_color.lstrip("#")
r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)
return f"{r}, {g}, {b}, {alpha}"
def rgba_to_hex(rgba_str: str) -> str:
"""
Convert RGBA format to hex color.
Args:
rgba_str: RGBA string like "112, 112, 247, 255"
Returns:
Hex color string like "#7070F7"
"""
parts = [int(x.strip()) for x in rgba_str.split(",")]
r, g, b = parts[0], parts[1], parts[2]
return f"#{r:02x}{g:02x}{b:02x}"
+5
View File
@@ -61,6 +61,11 @@ class JobManager:
discord_webhook_url=job_create.discord_webhook_url,
dropbox_path=job_create.dropbox_path,
gdrive_folder_id=job_create.gdrive_folder_id,
# Theme configuration
theme_id=job_create.theme_id,
color_overrides=job_create.color_overrides,
style_params_gcs_path=job_create.style_params_gcs_path,
style_assets=job_create.style_assets,
# Two-phase workflow (Batch 6)
prep_only=job_create.prep_only,
finalise_only=job_create.finalise_only,
+469
View File
@@ -0,0 +1,469 @@
"""
Theme service for managing pre-created style themes.
Themes are stored in GCS at themes/{theme_id}/ with:
- style_params.json: Complete style configuration
- preview.png: Preview image for theme selection UI
- youtube_description.txt: Optional YouTube description template
- assets/: Fonts, backgrounds, and other assets
The service provides:
- Theme listing with signed preview URLs
- Theme detail retrieval
- Color override application
- Job style preparation (copying theme to job folder)
"""
import copy
import json
import logging
import os
import tempfile
import threading
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Tuple
from backend.models.theme import (
ColorOverrides,
ThemeDetail,
ThemeMetadata,
ThemeRegistry,
ThemeSummary,
hex_to_rgba,
)
from backend.services.storage_service import StorageService
logger = logging.getLogger(__name__)
# GCS paths for themes
THEMES_PREFIX = "themes"
METADATA_FILE = f"{THEMES_PREFIX}/_metadata.json"
class ThemeService:
"""Service for managing themes from GCS."""
def __init__(self, storage: Optional[StorageService] = None):
"""
Initialize the theme service.
Args:
storage: StorageService instance (creates new one if not provided)
"""
self.storage = storage or StorageService()
self._metadata_cache: Optional[ThemeRegistry] = None
self._cache_time: Optional[datetime] = None
self.CACHE_TTL_SECONDS = 300 # 5 minute cache for theme metadata
def _get_theme_path(self, theme_id: str, filename: str = "") -> str:
"""Get the GCS path for a theme file."""
if filename:
return f"{THEMES_PREFIX}/{theme_id}/{filename}"
return f"{THEMES_PREFIX}/{theme_id}"
def _is_cache_valid(self) -> bool:
"""Check if the metadata cache is still valid."""
if self._metadata_cache is None or self._cache_time is None:
return False
age = datetime.now() - self._cache_time
return age.total_seconds() < self.CACHE_TTL_SECONDS
def _load_metadata(self, force_refresh: bool = False) -> ThemeRegistry:
"""
Load theme metadata from GCS with caching.
Args:
force_refresh: Force reload from GCS even if cache is valid
Returns:
ThemeRegistry containing all theme metadata
"""
if not force_refresh and self._is_cache_valid():
return self._metadata_cache # type: ignore
try:
data = self.storage.download_json(METADATA_FILE)
self._metadata_cache = ThemeRegistry(**data)
self._cache_time = datetime.now()
logger.info(f"Loaded theme metadata: {len(self._metadata_cache.themes)} themes")
return self._metadata_cache
except Exception as e:
logger.error(f"Failed to load theme metadata: {e}")
# Return empty registry on error
return ThemeRegistry(version=1, themes=[])
def list_themes(self) -> List[ThemeSummary]:
"""
List all available themes with signed preview URLs.
Returns:
List of ThemeSummary objects with preview URLs
"""
metadata = self._load_metadata()
summaries = []
for theme in metadata.themes:
try:
# Generate signed URLs for preview images
preview_path = self._get_theme_path(theme.id, "preview.png")
thumbnail_path = self._get_theme_path(theme.id, "preview_thumbnail.png")
preview_url = None
thumbnail_url = None
if self.storage.file_exists(preview_path):
preview_url = self.storage.generate_signed_url(preview_path, expiration_minutes=60)
# Try thumbnail, fall back to main preview
if self.storage.file_exists(thumbnail_path):
thumbnail_url = self.storage.generate_signed_url(thumbnail_path, expiration_minutes=60)
elif preview_url:
thumbnail_url = preview_url
summaries.append(
ThemeSummary(
id=theme.id,
name=theme.name,
description=theme.description,
preview_url=preview_url,
thumbnail_url=thumbnail_url,
is_default=theme.is_default,
)
)
except Exception as e:
logger.warning(f"Error processing theme {theme.id}: {e}")
# Still include theme but without preview URLs
summaries.append(
ThemeSummary(
id=theme.id,
name=theme.name,
description=theme.description,
is_default=theme.is_default,
)
)
return summaries
def get_theme(self, theme_id: str) -> Optional[ThemeDetail]:
"""
Get full theme details including style parameters.
Args:
theme_id: The theme identifier
Returns:
ThemeDetail if found, None otherwise
"""
metadata = self._load_metadata()
# Find theme in metadata
theme_meta = next((t for t in metadata.themes if t.id == theme_id), None)
if not theme_meta:
logger.warning(f"Theme not found: {theme_id}")
return None
try:
# Load style_params.json
style_params_path = self._get_theme_path(theme_id, "style_params.json")
style_params = self.storage.download_json(style_params_path)
# Check for YouTube description
youtube_desc_path = self._get_theme_path(theme_id, "youtube_description.txt")
has_youtube_desc = self.storage.file_exists(youtube_desc_path)
# Generate preview URL
preview_path = self._get_theme_path(theme_id, "preview.png")
preview_url = None
if self.storage.file_exists(preview_path):
preview_url = self.storage.generate_signed_url(preview_path, expiration_minutes=60)
return ThemeDetail(
id=theme_id,
name=theme_meta.name,
description=theme_meta.description,
preview_url=preview_url,
is_default=theme_meta.is_default,
style_params=style_params,
has_youtube_description=has_youtube_desc,
)
except Exception as e:
logger.error(f"Error loading theme {theme_id}: {e}")
return None
def get_theme_style_params(self, theme_id: str) -> Optional[Dict[str, Any]]:
"""
Get just the style_params.json for a theme.
Args:
theme_id: The theme identifier
Returns:
Style parameters dict if found, None otherwise
"""
try:
style_params_path = self._get_theme_path(theme_id, "style_params.json")
return self.storage.download_json(style_params_path)
except Exception as e:
logger.error(f"Error loading style params for theme {theme_id}: {e}")
return None
def get_youtube_description(self, theme_id: str) -> Optional[str]:
"""
Get the YouTube description template for a theme.
Args:
theme_id: The theme identifier
Returns:
YouTube description text if found, None otherwise
"""
try:
youtube_desc_path = self._get_theme_path(theme_id, "youtube_description.txt")
if not self.storage.file_exists(youtube_desc_path):
return None
# Download to temp file and read
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as tmp:
tmp_path = tmp.name
try:
self.storage.download_file(youtube_desc_path, tmp_path)
with open(tmp_path, "r") as f:
return f.read()
finally:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception as e:
logger.error(f"Error loading YouTube description for theme {theme_id}: {e}")
return None
def apply_color_overrides(
self, style_params: Dict[str, Any], overrides: ColorOverrides
) -> Dict[str, Any]:
"""
Apply user color overrides to theme style parameters.
Args:
style_params: Base style parameters from theme
overrides: User color overrides
Returns:
Modified style parameters with overrides applied
"""
if not overrides.has_overrides():
return style_params
# Deep copy to avoid modifying original
result = copy.deepcopy(style_params)
# Apply artist_color to intro, end, and CDG sections
if overrides.artist_color:
if "intro" in result:
result["intro"]["artist_color"] = overrides.artist_color
if "end" in result:
result["end"]["artist_color"] = overrides.artist_color
if "cdg" in result:
result["cdg"]["artist_color"] = overrides.artist_color
# Apply title_color to intro, end, and CDG sections
if overrides.title_color:
if "intro" in result:
result["intro"]["title_color"] = overrides.title_color
if "end" in result:
result["end"]["title_color"] = overrides.title_color
if "cdg" in result:
result["cdg"]["title_color"] = overrides.title_color
# Apply sung_lyrics_color (karaoke uses RGBA, CDG uses hex)
if overrides.sung_lyrics_color:
if "karaoke" in result:
result["karaoke"]["primary_color"] = hex_to_rgba(overrides.sung_lyrics_color)
if "cdg" in result:
result["cdg"]["active_fill"] = overrides.sung_lyrics_color
# Apply unsung_lyrics_color (karaoke uses RGBA, CDG uses hex)
if overrides.unsung_lyrics_color:
if "karaoke" in result:
result["karaoke"]["secondary_color"] = hex_to_rgba(overrides.unsung_lyrics_color)
if "cdg" in result:
result["cdg"]["inactive_fill"] = overrides.unsung_lyrics_color
return result
def prepare_job_style(
self,
job_id: str,
theme_id: str,
color_overrides: Optional[ColorOverrides] = None,
) -> Tuple[str, Dict[str, str]]:
"""
Prepare style files for a job by copying theme to job folder.
This method:
1. Loads the theme's style_params.json
2. Applies any color overrides
3. Updates asset paths to point to theme assets (shared, not copied)
4. Uploads modified style_params.json to job folder
5. Returns the GCS path and style_assets mapping
Args:
job_id: The job ID
theme_id: The theme to use
color_overrides: Optional color overrides
Returns:
Tuple of (style_params_gcs_path, style_assets dict)
Raises:
ValueError: If theme not found
"""
# Load theme style params
style_params = self.get_theme_style_params(theme_id)
if style_params is None:
raise ValueError(f"Theme not found: {theme_id}")
# Apply color overrides if provided
if color_overrides:
style_params = self.apply_color_overrides(style_params, color_overrides)
# Update asset paths to point to theme's shared assets
# Theme assets stay in themes/{theme_id}/assets/ - they're shared
style_assets = self._build_style_assets_mapping(theme_id, style_params)
# Update style_params to use theme asset paths (not job-specific)
style_params = self._update_asset_paths_in_style(theme_id, style_params)
# Upload modified style_params.json to job's style folder
job_style_path = f"uploads/{job_id}/style/style_params.json"
self.storage.upload_json(job_style_path, style_params)
logger.info(f"Prepared job {job_id} style from theme {theme_id}")
return job_style_path, style_assets
def _build_style_assets_mapping(
self, theme_id: str, style_params: Dict[str, Any]
) -> Dict[str, str]:
"""
Build the style_assets mapping for a theme.
Maps asset keys to their GCS paths in the theme folder.
"""
style_assets = {}
theme_assets_prefix = f"{THEMES_PREFIX}/{theme_id}/assets"
# Check each potential asset type
asset_checks = [
("intro_background", "intro", "background_image"),
("karaoke_background", "karaoke", "background_image"),
("end_background", "end", "background_image"),
("font", "intro", "font"), # Font is typically shared across sections
("cdg_instrumental_background", "cdg", "instrumental_background"),
("cdg_title_background", "cdg", "title_screen_background"),
("cdg_outro_background", "cdg", "outro_background"),
]
for asset_key, section, field in asset_checks:
if section in style_params and field in style_params[section]:
value = style_params[section][field]
if value and isinstance(value, str):
# Check if it's a theme asset path or already has the full path
if value.startswith(f"{THEMES_PREFIX}/"):
# Already a full theme path
style_assets[asset_key] = value
elif not value.startswith("/") and not value.startswith("gs://"):
# Relative path - prepend theme assets prefix
asset_path = f"{theme_assets_prefix}/{os.path.basename(value)}"
if self.storage.file_exists(asset_path):
style_assets[asset_key] = asset_path
return style_assets
def _update_asset_paths_in_style(
self, theme_id: str, style_params: Dict[str, Any]
) -> Dict[str, Any]:
"""
Update asset paths in style_params to use full GCS theme paths.
This ensures the style_loader can find assets when processing.
"""
result = copy.deepcopy(style_params)
theme_assets_prefix = f"{THEMES_PREFIX}/{theme_id}/assets"
# Fields that contain asset paths
path_fields = {
"intro": ["background_image", "font"],
"end": ["background_image", "font"],
"karaoke": ["background_image", "font_path"],
"cdg": [
"font_path",
"instrumental_background",
"title_screen_background",
"outro_background",
],
}
for section, fields in path_fields.items():
if section not in result:
continue
for field in fields:
if field not in result[section]:
continue
value = result[section][field]
if value and isinstance(value, str):
# Skip if already a full path or URL
if value.startswith(f"{THEMES_PREFIX}/") or value.startswith("gs://"):
continue
# Skip absolute local paths (shouldn't happen but be safe)
if value.startswith("/"):
continue
# Update to full theme asset path
result[section][field] = f"{theme_assets_prefix}/{os.path.basename(value)}"
return result
def theme_exists(self, theme_id: str) -> bool:
"""
Check if a theme exists.
Args:
theme_id: The theme identifier
Returns:
True if theme exists, False otherwise
"""
metadata = self._load_metadata()
return any(t.id == theme_id for t in metadata.themes)
def get_default_theme_id(self) -> Optional[str]:
"""
Get the ID of the default theme.
Returns:
Default theme ID if one exists, None otherwise
"""
metadata = self._load_metadata()
default_theme = next((t for t in metadata.themes if t.is_default), None)
return default_theme.id if default_theme else None
def invalidate_cache(self) -> None:
"""Force invalidation of the metadata cache."""
self._metadata_cache = None
self._cache_time = None
logger.info("Theme metadata cache invalidated")
# Singleton instance with thread-safe initialization
_theme_service: Optional[ThemeService] = None
_theme_service_lock = threading.Lock()
def get_theme_service() -> ThemeService:
"""Get or create the singleton ThemeService instance (thread-safe)."""
global _theme_service
if _theme_service is None:
with _theme_service_lock:
# Double-check after acquiring lock
if _theme_service is None:
_theme_service = ThemeService()
return _theme_service
@@ -330,7 +330,7 @@ class TestReviewEndpoints:
class TestRenderVideoWorker:
"""Test render video worker endpoint."""
def test_render_video_worker_endpoint_exists(self, client, auth_headers):
"""Test that render-video worker endpoint exists."""
# Create a job
@@ -340,9 +340,9 @@ class TestRenderVideoWorker:
json={"url": "https://youtube.com/watch?v=render-test"}
)
job_id = create_resp.json()["job_id"]
time.sleep(0.2)
# Test render-video worker endpoint exists
response = client.post(
"/api/internal/workers/render-video",
+248
View File
@@ -798,6 +798,254 @@ class TestRemoteDownloadPath:
assert result.filepath == "/tmp/test.flac"
class TestAudioSearchThemeSupport:
"""Test theme support in audio search requests.
These tests verify that theme_id and color_overrides are properly:
1. Accepted in AudioSearchRequest model
2. Passed through to JobCreate
3. Result in correct CDG/TXT defaults when theme is set
4. Theme style is PREPARED (copied to job folder) when theme_id is set
"""
def test_audio_search_request_accepts_theme_id(self):
"""Test AudioSearchRequest model has theme_id field."""
from backend.api.routes.audio_search import AudioSearchRequest
request = AudioSearchRequest(
artist="Test Artist",
title="Test Song",
theme_id="nomad"
)
assert request.theme_id == "nomad"
def test_audio_search_request_accepts_color_overrides(self):
"""Test AudioSearchRequest model has color_overrides field."""
from backend.api.routes.audio_search import AudioSearchRequest
request = AudioSearchRequest(
artist="Test Artist",
title="Test Song",
theme_id="nomad",
color_overrides={
"artist_color": "#ff0000",
"title_color": "#00ff00",
}
)
assert request.color_overrides["artist_color"] == "#ff0000"
assert request.color_overrides["title_color"] == "#00ff00"
def test_audio_search_request_theme_defaults_cdg_txt(self):
"""Test that when theme_id is set, CDG/TXT defaults to enabled.
This is the key behavior: selecting a theme should automatically
enable CDG and TXT output formats.
"""
from backend.api.routes.audio_search import AudioSearchRequest, _resolve_cdg_txt_defaults
# When theme_id is set, enable_cdg/enable_txt should default to True
request = AudioSearchRequest(
artist="Test Artist",
title="Test Song",
theme_id="nomad"
# enable_cdg and enable_txt are None (not specified)
)
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
request.theme_id, request.enable_cdg, request.enable_txt
)
assert resolved_cdg is True, "CDG should be enabled by default when theme is set"
assert resolved_txt is True, "TXT should be enabled by default when theme is set"
def test_audio_search_request_no_theme_no_cdg_txt(self):
"""Test that without theme_id, CDG/TXT defaults to disabled."""
from backend.api.routes.audio_search import AudioSearchRequest, _resolve_cdg_txt_defaults
request = AudioSearchRequest(
artist="Test Artist",
title="Test Song"
# No theme_id, no enable_cdg, no enable_txt
)
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
request.theme_id, request.enable_cdg, request.enable_txt
)
assert resolved_cdg is False, "CDG should be disabled by default without theme"
assert resolved_txt is False, "TXT should be disabled by default without theme"
def test_explicit_cdg_txt_overrides_theme_default(self):
"""Test that explicit enable_cdg/enable_txt values override theme defaults."""
from backend.api.routes.audio_search import AudioSearchRequest, _resolve_cdg_txt_defaults
# Theme set (would default to True), but explicitly disabled
request = AudioSearchRequest(
artist="Test Artist",
title="Test Song",
theme_id="nomad",
enable_cdg=False,
enable_txt=False,
)
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
request.theme_id, request.enable_cdg, request.enable_txt
)
assert resolved_cdg is False, "Explicit False should override theme default"
assert resolved_txt is False, "Explicit False should override theme default"
def test_explicit_cdg_txt_enables_without_theme(self):
"""Test that explicit True enables CDG/TXT even without theme."""
from backend.api.routes.audio_search import AudioSearchRequest, _resolve_cdg_txt_defaults
# No theme (would default to False), but explicitly enabled
request = AudioSearchRequest(
artist="Test Artist",
title="Test Song",
enable_cdg=True,
enable_txt=True,
)
resolved_cdg, resolved_txt = _resolve_cdg_txt_defaults(
request.theme_id, request.enable_cdg, request.enable_txt
)
assert resolved_cdg is True, "Explicit True should enable CDG without theme"
assert resolved_txt is True, "Explicit True should enable TXT without theme"
def test_job_create_receives_theme_from_audio_search(self):
"""Test that JobCreate model can receive theme_id and color_overrides."""
job_create = JobCreate(
artist="Test Artist",
title="Test Song",
theme_id="nomad",
color_overrides={
"artist_color": "#ff0000",
"sung_lyrics_color": "#00ff00"
},
enable_cdg=True,
enable_txt=True,
)
assert job_create.theme_id == "nomad"
assert job_create.color_overrides["artist_color"] == "#ff0000"
assert job_create.color_overrides["sung_lyrics_color"] == "#00ff00"
assert job_create.enable_cdg is True
assert job_create.enable_txt is True
def test_job_model_stores_theme_configuration(self):
"""Test Job model stores theme configuration correctly."""
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
artist="Test Artist",
title="Test Song",
theme_id="nomad",
color_overrides={
"artist_color": "#ff0000"
},
enable_cdg=True,
enable_txt=True,
)
assert job.theme_id == "nomad"
assert job.color_overrides["artist_color"] == "#ff0000"
assert job.enable_cdg is True
assert job.enable_txt is True
class TestAudioSearchThemePreparation:
"""Test that audio search endpoint properly prepares theme styles.
CRITICAL: This test class was added to catch the bug where audio_search.py
accepted theme_id but never called prepare_job_style(), resulting in jobs
with theme_id set but no style_params_gcs_path or style_assets.
The symptom: Preview videos show black background instead of themed background.
Job ID ffb0b8fa in production demonstrated this issue.
"""
def test_audio_search_with_theme_calls_prepare_job_style(self):
"""Test that creating a job via audio search with theme_id prepares the style.
CRITICAL: This test verifies that when a job is created via the audio-search
endpoint with a theme_id (and no custom style files), the code calls
prepare_job_style() to set:
1. style_params_gcs_path (pointing to the copied style_params.json)
2. style_assets (populated with asset mappings)
Without this, LyricsTranscriber won't have access to the theme's styles
and preview videos will have black backgrounds instead of themed ones.
"""
# Import the endpoint module to access internal functions
from backend.api.routes import audio_search as audio_search_module
# Check if there's an import of prepare_job_style from theme_service
source_code_path = audio_search_module.__file__
with open(source_code_path, 'r') as f:
source_code = f.read()
# Check for either:
# 1. Import of _prepare_theme_for_job from file_upload
# 2. Import of prepare_job_style from theme_service
# 3. Inline call to theme_service.prepare_job_style
has_theme_prep_import = (
'_prepare_theme_for_job' in source_code or
'prepare_job_style' in source_code
)
assert has_theme_prep_import, (
"audio_search.py does not import or call prepare_job_style() or _prepare_theme_for_job(). "
"When theme_id is provided without custom style files, the endpoint MUST call "
"prepare_job_style() to copy the theme's style_params.json to the job folder. "
"Without this, LyricsTranscriber won't have style configuration and preview "
"videos will have black backgrounds instead of themed ones."
)
def test_audio_search_code_handles_theme_preparation(self):
"""Verify audio_search.py has theme preparation logic similar to file_upload.py.
This test compares audio_search.py with file_upload.py to ensure they have
similar theme preparation patterns. file_upload.py correctly prepares themes,
audio_search.py should do the same.
"""
from backend.api.routes import audio_search as audio_search_module
from backend.api.routes import file_upload as file_upload_module
# Read source code of both modules
with open(audio_search_module.__file__, 'r') as f:
audio_search_code = f.read()
with open(file_upload_module.__file__, 'r') as f:
file_upload_code = f.read()
# file_upload.py has this pattern for theme preparation:
# if body.theme_id and not has_style_params_upload:
# style_params_path, style_assets, youtube_desc = _prepare_theme_for_job(...)
assert '_prepare_theme_for_job' in file_upload_code, \
"file_upload.py should have _prepare_theme_for_job function"
# audio_search.py should have similar pattern
# Look for either the function or a similar conditional check
# STRICT CHECK: Look for actual theme preparation logic being CALLED, not just mentioned
has_theme_preparation_call = (
# Check for import or call of theme preparation function
'_prepare_theme_for_job(' in audio_search_code or
'prepare_job_style(' in audio_search_code
)
assert has_theme_preparation_call, (
"audio_search.py does not CALL theme preparation logic. "
"It accepts theme_id but never prepares the theme style like file_upload.py does. "
"Add a call to _prepare_theme_for_job() when theme_id is set and no style files are uploaded."
)
class TestFlacfetchIntegration:
"""
Integration tests that verify flacfetch imports work correctly.
+9 -7
View File
@@ -1122,9 +1122,10 @@ class TestFinaliseOnlyModels:
FinaliseOnlyFileRequest(filename="with_vocals.mkv", content_type="video/mkv", file_type="with_vocals"),
]
)
assert request.enable_cdg is True
assert request.enable_txt is True
# CDG/TXT default to None (server resolves based on theme_id)
assert request.enable_cdg is None
assert request.enable_txt is None
assert request.brand_prefix is None
assert request.keep_brand_code is None
assert request.enable_youtube_upload is False
@@ -1270,13 +1271,14 @@ class TestCreateJobFromUrlRequest:
request = CreateJobFromUrlRequest(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"
)
assert request.url == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
assert request.artist is None
assert request.title is None
assert request.enable_cdg is True
assert request.enable_txt is True
# CDG/TXT default to None (server resolves based on theme_id)
assert request.enable_cdg is None
assert request.enable_txt is None
def test_create_with_artist_and_title(self):
"""Test creating request with URL, artist, and title."""
from backend.api.routes.file_upload import CreateJobFromUrlRequest
+3 -2
View File
@@ -146,8 +146,9 @@ class TestUploadSubmissionRequest:
def test_default_options(self):
"""Test default option values."""
request = UploadSubmissionRequest(artist="Test", title="Test")
assert request.enable_cdg is True
assert request.enable_txt is True
# CDG/TXT disabled by default (requires style config)
assert request.enable_cdg is False
assert request.enable_txt is False
assert request.enable_youtube_upload is False
+162 -1
View File
@@ -67,12 +67,31 @@ class TestJobStatusTransitions:
"""Test FAILED status can transition to retry checkpoint states."""
from backend.models.job import STATE_TRANSITIONS
valid_transitions = STATE_TRANSITIONS.get(JobStatus.FAILED, [])
# FAILED should allow retry transitions
assert JobStatus.INSTRUMENTAL_SELECTED in valid_transitions, "FAILED should allow retry to INSTRUMENTAL_SELECTED"
assert JobStatus.REVIEW_COMPLETE in valid_transitions, "FAILED should allow retry to REVIEW_COMPLETE"
assert JobStatus.LYRICS_COMPLETE in valid_transitions, "FAILED should allow retry to LYRICS_COMPLETE"
def test_cancelled_can_transition_for_retry(self):
"""Test CANCELLED status can transition to retry checkpoint states."""
from backend.models.job import STATE_TRANSITIONS
valid_transitions = STATE_TRANSITIONS.get(JobStatus.CANCELLED, [])
# CANCELLED should allow same retry transitions as FAILED
assert JobStatus.INSTRUMENTAL_SELECTED in valid_transitions, "CANCELLED should allow retry to INSTRUMENTAL_SELECTED"
assert JobStatus.REVIEW_COMPLETE in valid_transitions, "CANCELLED should allow retry to REVIEW_COMPLETE"
assert JobStatus.LYRICS_COMPLETE in valid_transitions, "CANCELLED should allow retry to LYRICS_COMPLETE"
assert JobStatus.DOWNLOADING in valid_transitions, "CANCELLED should allow retry to DOWNLOADING (restart)"
def test_failed_can_restart_from_beginning(self):
"""Test FAILED status can transition to DOWNLOADING for restart from beginning."""
from backend.models.job import STATE_TRANSITIONS
valid_transitions = STATE_TRANSITIONS.get(JobStatus.FAILED, [])
# FAILED should allow restart from beginning
assert JobStatus.DOWNLOADING in valid_transitions, "FAILED should allow restart to DOWNLOADING"
class TestJobModelSerialization:
"""Tests for Job model serialization to/from API."""
@@ -119,3 +138,145 @@ class TestJobModelSerialization:
assert job.url is None
assert job.file_urls == {}
class TestRetryEndpoint:
"""Tests for the retry job endpoint."""
@pytest.fixture
def mock_job_manager(self):
"""Create mock job manager."""
return MagicMock()
@pytest.fixture
def mock_worker_service(self):
"""Create mock worker service."""
return MagicMock()
def test_retry_rejects_pending_job(self, mock_job_manager):
"""Test retry endpoint rejects jobs that are not failed or cancelled."""
# Create a pending job
job = Job(
job_id="test123",
status=JobStatus.PENDING,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song"
)
mock_job_manager.get_job.return_value = job
# The endpoint should reject this with a 400 error
# (status check happens before any logic)
assert job.status not in [JobStatus.FAILED, JobStatus.CANCELLED]
def test_retry_accepts_failed_job(self, mock_job_manager):
"""Test retry endpoint accepts failed jobs."""
job = Job(
job_id="test123",
status=JobStatus.FAILED,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song",
input_media_gcs_path="jobs/test123/input/audio.flac"
)
mock_job_manager.get_job.return_value = job
# Status should be accepted
assert job.status in [JobStatus.FAILED, JobStatus.CANCELLED]
def test_retry_accepts_cancelled_job(self, mock_job_manager):
"""Test retry endpoint accepts cancelled jobs."""
job = Job(
job_id="test123",
status=JobStatus.CANCELLED,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song",
input_media_gcs_path="jobs/test123/input/audio.flac"
)
mock_job_manager.get_job.return_value = job
# Status should be accepted
assert job.status in [JobStatus.FAILED, JobStatus.CANCELLED]
def test_retry_checkpoint_detection_video_generation(self):
"""Test retry detects video generation checkpoint."""
job = Job(
job_id="test123",
status=JobStatus.FAILED,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song",
file_urls={
'videos': {'with_vocals': 'gs://bucket/path/video.mkv'}
},
state_data={
'instrumental_selection': 'clean'
}
)
# This job has video and instrumental selection - should retry from video generation
has_video = job.file_urls.get('videos', {}).get('with_vocals')
has_instrumental_selection = (job.state_data or {}).get('instrumental_selection')
assert has_video and has_instrumental_selection
def test_retry_checkpoint_detection_render_stage(self):
"""Test retry detects render stage checkpoint."""
job = Job(
job_id="test123",
status=JobStatus.FAILED,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song",
file_urls={
'lyrics': {'corrections': 'gs://bucket/path/corrections.json'},
'screens': {'title': 'gs://bucket/path/title.mov'}
}
)
# This job has corrections and screens - should retry from render
has_corrections = job.file_urls.get('lyrics', {}).get('corrections')
has_screens = job.file_urls.get('screens', {}).get('title')
has_video = job.file_urls.get('videos', {}).get('with_vocals')
assert has_corrections and has_screens and not has_video
def test_retry_checkpoint_detection_from_beginning(self):
"""Test retry detects need to restart from beginning."""
job = Job(
job_id="test123",
status=JobStatus.CANCELLED,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song",
input_media_gcs_path="jobs/test123/input/audio.flac",
file_urls={} # No progress yet
)
# This job has input audio but no other files - should restart from beginning
has_input = job.input_media_gcs_path
has_stems = job.file_urls.get('stems', {}).get('instrumental_clean')
has_corrections = job.file_urls.get('lyrics', {}).get('corrections')
assert has_input and not has_stems and not has_corrections
def test_retry_no_input_audio(self):
"""Test retry fails when no input audio available."""
job = Job(
job_id="test123",
status=JobStatus.CANCELLED,
created_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
artist="Test",
title="Song",
# No input_media_gcs_path and no url
file_urls={}
)
# This job has no input audio - should not be retryable
has_input = job.input_media_gcs_path or job.url
assert not has_input
+516
View File
@@ -0,0 +1,516 @@
"""
Tests for the theme service and API endpoints.
Tests theme listing, retrieval, color override application, and job style preparation.
"""
import json
import pytest
from unittest.mock import MagicMock, patch, Mock
from datetime import datetime
from backend.models.theme import (
ThemeSummary,
ThemeDetail,
ThemeMetadata,
ThemeRegistry,
ColorOverrides,
hex_to_rgba,
rgba_to_hex,
)
from backend.services.theme_service import ThemeService, get_theme_service
# =============================================================================
# Color Conversion Tests
# =============================================================================
class TestColorConversion:
"""Tests for hex/rgba color conversion utilities."""
def test_hex_to_rgba_basic(self):
"""Test basic hex to RGBA conversion."""
result = hex_to_rgba("#7070F7")
assert result == "112, 112, 247, 255"
def test_hex_to_rgba_with_custom_alpha(self):
"""Test hex to RGBA with custom alpha."""
result = hex_to_rgba("#FF0000", alpha=128)
assert result == "255, 0, 0, 128"
def test_hex_to_rgba_lowercase(self):
"""Test hex with lowercase letters."""
result = hex_to_rgba("#abcdef")
assert result == "171, 205, 239, 255"
def test_hex_to_rgba_without_hash(self):
"""Test hex without leading hash."""
result = hex_to_rgba("FFFFFF")
assert result == "255, 255, 255, 255"
def test_rgba_to_hex_basic(self):
"""Test basic RGBA to hex conversion."""
result = rgba_to_hex("112, 112, 247, 255")
assert result == "#7070f7"
def test_rgba_to_hex_ignores_alpha(self):
"""Test that alpha is ignored in conversion."""
result = rgba_to_hex("255, 0, 0, 128")
assert result == "#ff0000"
# =============================================================================
# ColorOverrides Model Tests
# =============================================================================
class TestColorOverrides:
"""Tests for ColorOverrides model."""
def test_has_overrides_empty(self):
"""Test has_overrides with no overrides set."""
overrides = ColorOverrides()
assert overrides.has_overrides() is False
def test_has_overrides_with_artist_color(self):
"""Test has_overrides with artist_color set."""
overrides = ColorOverrides(artist_color="#FF0000")
assert overrides.has_overrides() is True
def test_has_overrides_with_all_colors(self):
"""Test has_overrides with all colors set."""
overrides = ColorOverrides(
artist_color="#FF0000",
title_color="#00FF00",
sung_lyrics_color="#0000FF",
unsung_lyrics_color="#FFFF00",
)
assert overrides.has_overrides() is True
def test_to_dict_excludes_none(self):
"""Test to_dict excludes None values."""
overrides = ColorOverrides(artist_color="#FF0000")
result = overrides.to_dict()
assert result == {"artist_color": "#FF0000"}
assert "title_color" not in result
def test_color_validation_valid(self):
"""Test color validation accepts valid hex colors."""
overrides = ColorOverrides(artist_color="#AbCdEf")
assert overrides.artist_color == "#AbCdEf"
def test_color_validation_invalid(self):
"""Test color validation rejects invalid colors."""
with pytest.raises(ValueError):
ColorOverrides(artist_color="red")
with pytest.raises(ValueError):
ColorOverrides(artist_color="#GGG")
# =============================================================================
# ThemeService Tests
# =============================================================================
class TestThemeService:
"""Tests for ThemeService."""
@pytest.fixture
def mock_storage(self):
"""Create a mock storage service."""
storage = MagicMock()
return storage
@pytest.fixture
def sample_metadata(self):
"""Sample theme metadata for testing."""
return {
"version": 1,
"themes": [
{
"id": "nomad",
"name": "Nomad Karaoke",
"description": "Golden artist text, professional look",
"is_default": True,
},
{
"id": "default",
"name": "Default",
"description": "Clean white text on black background",
"is_default": False,
},
],
}
@pytest.fixture
def sample_style_params(self):
"""Sample style params for testing."""
return {
"intro": {
"artist_color": "#ffdf6b",
"title_color": "#ffffff",
"background_image": "intro_bg.png",
"font": "Montserrat-Bold.ttf",
},
"end": {
"artist_color": "#ffdf6b",
"title_color": "#ffffff",
"background_image": "end_bg.png",
"font": "Montserrat-Bold.ttf",
},
"karaoke": {
"primary_color": "112, 112, 247, 255",
"secondary_color": "255, 255, 255, 255",
"background_image": "karaoke_bg.png",
"font_path": "Montserrat-Bold.ttf",
},
"cdg": {
"active_fill": "#7070F7",
"inactive_fill": "#FFFFFF",
"font_path": "Montserrat-Bold.ttf",
},
}
def test_list_themes_returns_summaries(self, mock_storage, sample_metadata):
"""Test list_themes returns ThemeSummary objects."""
mock_storage.download_json.return_value = sample_metadata
mock_storage.file_exists.return_value = True
mock_storage.generate_signed_url.return_value = "https://signed-url.com/preview.png"
service = ThemeService(storage=mock_storage)
themes = service.list_themes()
assert len(themes) == 2
assert themes[0].id == "nomad"
assert themes[0].name == "Nomad Karaoke"
assert themes[0].is_default is True
assert themes[0].preview_url is not None
def test_list_themes_handles_missing_preview(self, mock_storage, sample_metadata):
"""Test list_themes handles missing preview images gracefully."""
mock_storage.download_json.return_value = sample_metadata
mock_storage.file_exists.return_value = False
service = ThemeService(storage=mock_storage)
themes = service.list_themes()
assert len(themes) == 2
assert themes[0].preview_url is None
def test_get_theme_returns_detail(self, mock_storage, sample_metadata, sample_style_params):
"""Test get_theme returns ThemeDetail with style params."""
mock_storage.download_json.side_effect = [sample_metadata, sample_style_params]
mock_storage.file_exists.return_value = True
mock_storage.generate_signed_url.return_value = "https://signed-url.com/preview.png"
service = ThemeService(storage=mock_storage)
theme = service.get_theme("nomad")
assert theme is not None
assert theme.id == "nomad"
assert theme.name == "Nomad Karaoke"
assert "intro" in theme.style_params
assert theme.style_params["intro"]["artist_color"] == "#ffdf6b"
def test_get_theme_returns_none_for_unknown(self, mock_storage, sample_metadata):
"""Test get_theme returns None for unknown theme ID."""
mock_storage.download_json.return_value = sample_metadata
service = ThemeService(storage=mock_storage)
theme = service.get_theme("unknown-theme")
assert theme is None
def test_theme_exists_true(self, mock_storage, sample_metadata):
"""Test theme_exists returns True for existing theme."""
mock_storage.download_json.return_value = sample_metadata
service = ThemeService(storage=mock_storage)
assert service.theme_exists("nomad") is True
def test_theme_exists_false(self, mock_storage, sample_metadata):
"""Test theme_exists returns False for unknown theme."""
mock_storage.download_json.return_value = sample_metadata
service = ThemeService(storage=mock_storage)
assert service.theme_exists("unknown") is False
def test_get_default_theme_id(self, mock_storage, sample_metadata):
"""Test get_default_theme_id returns the default theme."""
mock_storage.download_json.return_value = sample_metadata
service = ThemeService(storage=mock_storage)
assert service.get_default_theme_id() == "nomad"
def test_get_default_theme_id_no_default(self, mock_storage):
"""Test get_default_theme_id returns None when no default."""
mock_storage.download_json.return_value = {
"version": 1,
"themes": [{"id": "test", "name": "Test", "description": "Test", "is_default": False}],
}
service = ThemeService(storage=mock_storage)
assert service.get_default_theme_id() is None
def test_apply_color_overrides_no_changes(self, sample_style_params):
"""Test apply_color_overrides with empty overrides."""
service = ThemeService()
overrides = ColorOverrides()
result = service.apply_color_overrides(sample_style_params, overrides)
# Should return original (no deep copy needed when no changes)
assert result == sample_style_params
def test_apply_color_overrides_artist_color(self, sample_style_params):
"""Test apply_color_overrides applies artist_color."""
service = ThemeService()
overrides = ColorOverrides(artist_color="#FF0000")
result = service.apply_color_overrides(sample_style_params, overrides)
assert result["intro"]["artist_color"] == "#FF0000"
assert result["end"]["artist_color"] == "#FF0000"
assert result["cdg"]["artist_color"] == "#FF0000"
# Original should be unchanged
assert sample_style_params["intro"]["artist_color"] == "#ffdf6b"
def test_apply_color_overrides_sung_lyrics_color(self, sample_style_params):
"""Test apply_color_overrides applies sung_lyrics_color with conversion."""
service = ThemeService()
overrides = ColorOverrides(sung_lyrics_color="#FF0000")
result = service.apply_color_overrides(sample_style_params, overrides)
# Karaoke uses RGBA format
assert result["karaoke"]["primary_color"] == "255, 0, 0, 255"
# CDG uses hex
assert result["cdg"]["active_fill"] == "#FF0000"
def test_apply_color_overrides_unsung_lyrics_color(self, sample_style_params):
"""Test apply_color_overrides applies unsung_lyrics_color."""
service = ThemeService()
overrides = ColorOverrides(unsung_lyrics_color="#00FF00")
result = service.apply_color_overrides(sample_style_params, overrides)
assert result["karaoke"]["secondary_color"] == "0, 255, 0, 255"
assert result["cdg"]["inactive_fill"] == "#00FF00"
def test_metadata_cache(self, mock_storage, sample_metadata):
"""Test metadata caching works."""
mock_storage.download_json.return_value = sample_metadata
service = ThemeService(storage=mock_storage)
# First call loads from GCS
service.list_themes()
assert mock_storage.download_json.call_count == 1
# Second call uses cache
service.list_themes()
assert mock_storage.download_json.call_count == 1
def test_invalidate_cache(self, mock_storage, sample_metadata):
"""Test cache invalidation."""
mock_storage.download_json.return_value = sample_metadata
service = ThemeService(storage=mock_storage)
# Load metadata
service.list_themes()
assert mock_storage.download_json.call_count == 1
# Invalidate cache
service.invalidate_cache()
# Next call should reload
service.list_themes()
assert mock_storage.download_json.call_count == 2
def test_prepare_job_style(self, mock_storage, sample_metadata, sample_style_params):
"""Test prepare_job_style creates job style from theme."""
mock_storage.download_json.side_effect = [sample_metadata, sample_style_params]
mock_storage.file_exists.return_value = True
service = ThemeService(storage=mock_storage)
style_path, style_assets = service.prepare_job_style("job123", "nomad")
# Should upload modified style_params.json
mock_storage.upload_json.assert_called_once()
assert style_path == "uploads/job123/style/style_params.json"
def test_prepare_job_style_with_overrides(self, mock_storage, sample_metadata, sample_style_params):
"""Test prepare_job_style applies color overrides."""
# prepare_job_style calls get_theme_style_params which directly downloads style_params.json
# No metadata lookup happens unless we've cached it previously
mock_storage.download_json.return_value = sample_style_params
mock_storage.file_exists.return_value = True
service = ThemeService(storage=mock_storage)
overrides = ColorOverrides(artist_color="#FF0000")
style_path, style_assets = service.prepare_job_style("job123", "nomad", overrides)
# Check that upload_json was called with modified style params
call_args = mock_storage.upload_json.call_args
uploaded_path = call_args[0][0] # First positional arg is the path
uploaded_style = call_args[0][1] # Second positional arg is the data
assert uploaded_path == "uploads/job123/style/style_params.json"
# Check that artist_color override was applied
assert uploaded_style["intro"]["artist_color"] == "#FF0000"
assert uploaded_style["end"]["artist_color"] == "#FF0000"
def test_prepare_job_style_unknown_theme_raises(self, mock_storage, sample_metadata):
"""Test prepare_job_style raises for unknown theme."""
# First download_json returns metadata (no unknown theme)
# Second download_json for style_params.json should fail
def download_json_side_effect(path):
if "_metadata.json" in path:
return sample_metadata
# Unknown theme path - raise exception
raise Exception(f"File not found: {path}")
mock_storage.download_json.side_effect = download_json_side_effect
service = ThemeService(storage=mock_storage)
with pytest.raises(ValueError, match="Theme not found"):
service.prepare_job_style("job123", "unknown-theme")
def test_get_youtube_description(self, mock_storage, sample_metadata):
"""Test get_youtube_description returns template text."""
mock_storage.download_json.return_value = sample_metadata
mock_storage.file_exists.return_value = True
# Mock the download_file to write content to temp file
def mock_download(gcs_path, local_path):
with open(local_path, "w") as f:
f.write("Thank you for watching!")
mock_storage.download_file.side_effect = mock_download
service = ThemeService(storage=mock_storage)
desc = service.get_youtube_description("nomad")
assert desc == "Thank you for watching!"
def test_get_youtube_description_not_found(self, mock_storage, sample_metadata):
"""Test get_youtube_description returns None when no template."""
mock_storage.download_json.return_value = sample_metadata
mock_storage.file_exists.return_value = False
service = ThemeService(storage=mock_storage)
desc = service.get_youtube_description("nomad")
assert desc is None
# =============================================================================
# API Endpoint Tests
# =============================================================================
class TestThemeAPI:
"""Tests for theme API endpoints."""
@pytest.fixture
def sample_metadata(self):
"""Sample theme metadata."""
return {
"version": 1,
"themes": [
{
"id": "nomad",
"name": "Nomad Karaoke",
"description": "Golden artist text",
"is_default": True,
}
],
}
def test_list_themes_endpoint(self, test_client, sample_metadata):
"""Test GET /api/themes returns theme list."""
with patch("backend.api.routes.themes.get_theme_service") as mock_get_service:
mock_service = MagicMock()
mock_service.list_themes.return_value = [
ThemeSummary(
id="nomad",
name="Nomad Karaoke",
description="Golden artist text",
preview_url="https://example.com/preview.png",
is_default=True,
)
]
mock_get_service.return_value = mock_service
response = test_client.get("/api/themes")
assert response.status_code == 200
data = response.json()
assert len(data["themes"]) == 1
assert data["themes"][0]["id"] == "nomad"
assert data["themes"][0]["is_default"] is True
def test_get_theme_endpoint(self, test_client):
"""Test GET /api/themes/{theme_id} returns theme detail."""
with patch("backend.api.routes.themes.get_theme_service") as mock_get_service:
mock_service = MagicMock()
mock_service.get_theme.return_value = ThemeDetail(
id="nomad",
name="Nomad Karaoke",
description="Golden artist text",
is_default=True,
style_params={"intro": {"artist_color": "#ffdf6b"}},
)
mock_get_service.return_value = mock_service
response = test_client.get("/api/themes/nomad")
assert response.status_code == 200
data = response.json()
assert data["theme"]["id"] == "nomad"
assert "style_params" in data["theme"]
def test_get_theme_not_found(self, test_client):
"""Test GET /api/themes/{theme_id} returns 404 for unknown theme."""
with patch("backend.api.routes.themes.get_theme_service") as mock_get_service:
mock_service = MagicMock()
mock_service.get_theme.return_value = None
mock_get_service.return_value = mock_service
response = test_client.get("/api/themes/unknown")
assert response.status_code == 404
def test_get_theme_preview_endpoint(self, test_client):
"""Test GET /api/themes/{theme_id}/preview returns preview URL."""
with patch("backend.api.routes.themes.get_theme_service") as mock_get_service:
mock_service = MagicMock()
mock_service.theme_exists.return_value = True
mock_service.get_theme.return_value = ThemeDetail(
id="nomad",
name="Nomad Karaoke",
description="Golden artist text",
preview_url="https://example.com/preview.png",
is_default=True,
)
mock_get_service.return_value = mock_service
response = test_client.get("/api/themes/nomad/preview")
assert response.status_code == 200
data = response.json()
assert "preview_url" in data
def test_get_youtube_description_endpoint(self, test_client):
"""Test GET /api/themes/{theme_id}/youtube-description returns description."""
with patch("backend.api.routes.themes.get_theme_service") as mock_get_service:
mock_service = MagicMock()
mock_service.theme_exists.return_value = True
mock_service.get_youtube_description.return_value = "Thank you for watching!"
mock_get_service.return_value = mock_service
response = test_client.get("/api/themes/nomad/youtube-description")
assert response.status_code == 200
data = response.json()
assert data["description"] == "Thank you for watching!"
+1 -1
View File
@@ -75,7 +75,7 @@ steps:
--concurrency 1 \
--max-instances 50 \
--min-instances 0 \
--set-env-vars "GOOGLE_CLOUD_PROJECT=$PROJECT_ID,GCS_BUCKET_NAME=karaoke-gen-storage-$PROJECT_ID,FIRESTORE_COLLECTION=jobs,ENVIRONMENT=production,AUDIO_SEPARATOR_API_URL=https://nomadkaraoke--audio-separator-api.modal.run,KARAOKE_GEN_VERSION=$$VERSION,DEFAULT_DROPBOX_PATH=/MediaUnsynced/Karaoke/Tracks-Organized,DEFAULT_GDRIVE_FOLDER_ID=1laRKAyxo0v817SstfM5XkpbWiNKNAMSX,ENABLE_CLOUD_TASKS=true,CLOUD_RUN_SERVICE_URL=https://api.nomadkaraoke.com,GCP_REGION=us-central1" \
--set-env-vars "GOOGLE_CLOUD_PROJECT=$PROJECT_ID,GCS_BUCKET_NAME=karaoke-gen-storage-$PROJECT_ID,FIRESTORE_COLLECTION=jobs,ENVIRONMENT=production,AUDIO_SEPARATOR_API_URL=https://nomadkaraoke--audio-separator-api.modal.run,KARAOKE_GEN_VERSION=$$VERSION,DEFAULT_DROPBOX_PATH=/MediaUnsynced/Karaoke/Tracks-Organized,DEFAULT_GDRIVE_FOLDER_ID=1laRKAyxo0v817SstfM5XkpbWiNKNAMSX,ENABLE_CLOUD_TASKS=true,CLOUD_RUN_SERVICE_URL=https://api.nomadkaraoke.com,GCP_REGION=us-central1,DEFAULT_ENABLE_YOUTUBE_UPLOAD=true,DEFAULT_BRAND_PREFIX=NOMAD" \
--set-secrets "AUDIOSHAKE_API_TOKEN=audioshake-api-key:latest,GENIUS_API_TOKEN=genius-api-key:latest,SPOTIFY_COOKIE_SP_DC=spotify-cookie:latest,RAPIDAPI_KEY=rapidapi-key:latest,DEFAULT_DISCORD_WEBHOOK_URL=discord-releases-webhook:latest,ADMIN_TOKENS=admin-tokens:latest,RED_API_KEY=red-api-key:latest,RED_API_URL=red-api-url:latest,OPS_API_KEY=ops-api-key:latest,OPS_API_URL=ops-api-url:latest,FLACFETCH_API_URL=flacfetch-api-url:latest,FLACFETCH_API_KEY=flacfetch-api-key:latest"
images:
@@ -0,0 +1,233 @@
# Job Status Display Improvement Plan
**Status:** Planned
**Created:** 2025-12-27
**Priority:** HIGH - User experience improvement
## Problem Statement
Users see generic status messages like "Downloading..." that persist for too long, even after the download is complete and other processing stages are happening. This makes it hard to understand:
1. What stage the job is actually at
2. How much progress has been made
3. How much longer the job might take
## Current Implementation
### Backend Status System
The backend has 28 distinct job statuses organized into 8 processing stages:
```
Stage 1: Setup (PENDING)
Stage 2: Audio Search (SEARCHING_AUDIO, AWAITING_AUDIO_SELECTION, DOWNLOADING_AUDIO)
Stage 3: Download (DOWNLOADING)
Stage 4: Parallel Processing
- Audio: SEPARATING_STAGE1 → SEPARATING_STAGE2 → AUDIO_COMPLETE
- Lyrics: TRANSCRIBING → CORRECTING → LYRICS_COMPLETE
Stage 5: Screen Generation (GENERATING_SCREENS, APPLYING_PADDING)
Stage 6: Lyrics Review (AWAITING_REVIEW, IN_REVIEW, REVIEW_COMPLETE) ⚠️ BLOCKING
Stage 7: Video Rendering (RENDERING_VIDEO)
Stage 8: Instrumental Selection (AWAITING_INSTRUMENTAL_SELECTION) ⚠️ BLOCKING
Stage 9: Final Processing (INSTRUMENTAL_SELECTED, GENERATING_VIDEO, ENCODING, PACKAGING)
Stage 10: Distribution (UPLOADING, NOTIFYING, COMPLETE)
```
### Key Findings
1. **Progress field exists but unused**: The `Job` model has a `progress: number` field (0-100) that's returned by the API but NOT displayed in the frontend
2. **Parallel processing complicates status**: Audio and lyrics workers run in parallel, updating `state_data.audio_progress` and `state_data.lyrics_progress` independently
3. **Status labels are static**: Frontend shows "Downloading..." but doesn't reflect that parallel workers are running
4. **10-second polling**: Frontend polls every 10 seconds, so status updates are not real-time
## Proposed Solution
### 1. Step-Based Progress Display
Show a `[X/10] Status...` format that gives users a clear sense of overall progress:
```
[1/10] Setting up...
[2/10] Searching for audio...
[3/10] Downloading audio...
[4/10] Processing audio & lyrics... ← Parallel stage
[5/10] Generating screens...
[6/10] Needs review ← Blocking
[7/10] Rendering video...
[8/10] Select instrumental ← Blocking
[9/10] Encoding final video...
[10/10] Uploading...
```
### 2. Status-to-Step Mapping
```typescript
const STATUS_STEP_MAP: Record<string, { step: number; total: number; label: string }> = {
// Step 1: Setup
pending: { step: 1, total: 10, label: "Setting up" },
// Step 2: Audio Search (optional)
searching_audio: { step: 2, total: 10, label: "Searching for audio" },
awaiting_audio_selection: { step: 2, total: 10, label: "Select audio source" },
// Step 3: Download
downloading_audio: { step: 3, total: 10, label: "Downloading audio" },
downloading: { step: 3, total: 10, label: "Downloading" },
// Step 4: Parallel Processing (Audio + Lyrics)
separating_stage1: { step: 4, total: 10, label: "Separating audio (1/2)" },
separating_stage2: { step: 4, total: 10, label: "Separating audio (2/2)" },
audio_complete: { step: 4, total: 10, label: "Audio ready" },
transcribing: { step: 4, total: 10, label: "Transcribing lyrics" },
correcting: { step: 4, total: 10, label: "Correcting lyrics" },
lyrics_complete: { step: 4, total: 10, label: "Lyrics ready" },
// Step 5: Screen Generation
generating_screens: { step: 5, total: 10, label: "Generating screens" },
applying_padding: { step: 5, total: 10, label: "Syncing countdown" },
// Step 6: Review (BLOCKING)
awaiting_review: { step: 6, total: 10, label: "Needs review" },
in_review: { step: 6, total: 10, label: "In review" },
// Step 7: Video Rendering
review_complete: { step: 7, total: 10, label: "Review complete" },
rendering_video: { step: 7, total: 10, label: "Rendering video" },
// Step 8: Instrumental Selection (BLOCKING)
awaiting_instrumental_selection: { step: 8, total: 10, label: "Select instrumental" },
// Step 9: Final Encoding
instrumental_selected: { step: 9, total: 10, label: "Instrumental selected" },
generating_video: { step: 9, total: 10, label: "Generating final video" },
encoding: { step: 9, total: 10, label: "Encoding video" },
packaging: { step: 9, total: 10, label: "Packaging files" },
// Step 10: Distribution
uploading: { step: 10, total: 10, label: "Uploading" },
notifying: { step: 10, total: 10, label: "Sending notifications" },
complete: { step: 10, total: 10, label: "Complete" },
// Terminal states
failed: { step: 0, total: 10, label: "Failed" },
cancelled: { step: 0, total: 10, label: "Cancelled" },
prep_complete: { step: 10, total: 10, label: "Prep complete" },
};
```
### 3. Enhanced Parallel Processing Display
When audio and lyrics are processing in parallel, show both activities:
```typescript
// Check state_data for parallel progress
if (job.state_data?.audio_progress && job.state_data?.lyrics_progress) {
const audioStatus = job.state_data.audio_progress.stage;
const lyricsStatus = job.state_data.lyrics_progress.stage;
// Show combined status like "Audio: separating, Lyrics: transcribing"
if (audioStatus !== 'audio_complete' || lyricsStatus !== 'lyrics_complete') {
return {
step: 4,
total: 10,
label: buildParallelLabel(audioStatus, lyricsStatus)
};
}
}
```
### 4. Visual Progress Bar (Optional Enhancement)
Add a thin progress bar below the status text:
```tsx
<div className="w-full h-1 bg-slate-700 rounded mt-1">
<div
className="h-full bg-blue-500 rounded transition-all"
style={{ width: `${(step / total) * 100}%` }}
/>
</div>
```
## Implementation Steps
### Phase 1: Core Changes (MVP)
1. **Create status utility** (`frontend/lib/job-status.ts`):
- Define `STATUS_STEP_MAP` constant
- Create `getJobStep(job: Job)` function
- Create `formatStepStatus(step, total, label)` function
2. **Update JobCard component**:
- Import status utility
- Replace static `StatusText` with step-based format
- Keep existing color coding from `statusConfig`
3. **Add unit tests** (`frontend/__tests__/job-status.test.ts`):
- Test all status mappings
- Test parallel processing detection
- Test edge cases (unknown status, null values)
### Phase 2: Enhanced Parallel Processing (Optional)
1. **Add parallel progress detection**:
- Check `state_data.audio_progress` and `state_data.lyrics_progress`
- Build combined label showing both activities
2. **Add visual progress bar**:
- Thin progress indicator based on step number
- Smooth transitions between steps
### Phase 3: Backend Improvements (Future)
1. **Add estimated time remaining**:
- Track average duration per stage from historical data
- Return ETA in API response
2. **WebSocket for real-time updates**:
- Replace polling with push notifications
- Smoother progress updates during long operations
## Files to Modify
| File | Changes |
|------|---------|
| `frontend/lib/job-status.ts` | NEW: Status utility functions |
| `frontend/components/job/JobCard.tsx` | Update status display |
| `frontend/__tests__/job-status.test.ts` | NEW: Unit tests |
| `frontend/lib/api.ts` | Add `state_data` types (if needed) |
## Testing Plan
1. **Unit tests**:
- All status mappings return correct step/label
- Unknown statuses handled gracefully
- Parallel progress detection works
2. **Integration tests** (E2E):
- Verify status displays correctly at each stage
- Verify step numbers increment as job progresses
3. **Manual testing**:
- Submit a job and observe status progression
- Verify parallel processing shows meaningful status
## Success Criteria
- [ ] All statuses show `[X/10] Label` format
- [ ] Step numbers progress logically (never decrease except on retry)
- [ ] Parallel processing shows combined activity
- [ ] Blocking statuses clearly indicated for user action
- [ ] All existing tests pass
- [ ] New unit tests have >80% coverage
## Rollback Plan
The changes are frontend-only and purely cosmetic. If issues arise:
1. Revert the JobCard changes to use the old `statusConfig` approach
2. The backend API remains unchanged
## Notes
- The 10-step system is a simplification of the 28 backend statuses for user comprehension
- Parallel processing (step 4) groups multiple backend statuses into one user-facing step
- Blocking steps (6, 8) require user action and should be visually distinct
- The `progress` field from backend could be used for sub-step progress bars in the future
+485
View File
@@ -0,0 +1,485 @@
# Job Debugging Guide
This guide explains how to access debug info, observability, metrics, logs, and data about a given job ID in the karaoke-gen cloud backend.
**Last Updated:** 2025-12-28
---
## Prerequisites
### Service Account Authentication
For automated access, set the service account impersonation variable:
```bash
export GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=claude-automation@nomadkaraoke.iam.gserviceaccount.com
```
This service account has permissions to:
- Read Firestore documents
- Read Cloud Storage files
- Query Cloud Logging
- Describe Cloud Run services
- List Cloud Tasks queues
### Required Tools
- `gcloud` CLI (authenticated)
- Python 3.10+ with `google-cloud-firestore` installed
- Access to the `nomadkaraoke` GCP project
---
## Quick Reference
| Data Type | Location | Access Method |
|-----------|----------|---------------|
| Job state & metadata | Firestore `jobs/{job_id}` | Python script |
| Processing logs | Cloud Logging | `gcloud logging read` |
| Audio/video files | GCS bucket | `gcloud storage` |
| Service health | Cloud Run | `gcloud run services describe` |
| Task queues | Cloud Tasks | `gcloud tasks queues list` |
---
## 1. Fetching Job Data from Firestore
Firestore stores all job state including status, timeline, worker logs, and file URLs.
### Using Python (Recommended)
A helper script is included at `scripts/get_job.py`. You can also create your own:
```python
#!/usr/bin/env python3
"""Fetch and display job data from Firestore."""
import sys
import json
from google.cloud import firestore
def get_job(job_id: str):
db = firestore.Client(project='nomadkaraoke')
doc = db.collection('jobs').document(job_id).get()
if doc.exists:
data = doc.to_dict()
print(json.dumps(data, indent=2, default=str))
else:
print(f"Job {job_id} not found")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python get_job.py <job_id>")
sys.exit(1)
get_job(sys.argv[1])
```
Run it:
```bash
python get_job.py ffb0b8fa
```
### Example Output
```json
{
"status": "in_review",
"artist": "piri",
"title": "dog",
"created_at": "2025-12-28T01:55:23.914454",
"audio_search_artist": "piri",
"audio_search_title": "dog",
"timeline": [
{
"timestamp": "2025-12-28T01:55:23.995388",
"message": "Searching for: piri - dog",
"progress": 5,
"status": "searching_audio"
},
{
"timestamp": "2025-12-28T01:55:24.249594",
"message": "Found 20 audio sources. Waiting for selection.",
"progress": 10,
"status": "awaiting_audio_selection"
}
// ... more timeline entries
],
"worker_logs": [
{
"level": "INFO",
"message": "=== AUDIO WORKER STARTED ===",
"timestamp": "2025-12-28T01:55:37.559644Z",
"worker": "audio"
}
// ... more worker logs
]
}
```
### Key Fields to Check
| Field | Description |
|-------|-------------|
| `status` | Current job status (e.g., `in_review`, `complete`, `failed`) |
| `error_message` | Error description if job failed |
| `timeline` | Ordered list of status transitions with timestamps |
| `worker_logs` | Logs captured from worker processes |
| `file_urls` | Signed URLs for generated files |
| `state_data` | Worker-specific progress data |
---
## 2. Viewing Cloud Run Logs
Logs are stored in Cloud Logging and can be queried with `gcloud logging read`.
### Search Logs for a Job ID
```bash
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="karaoke-backend" AND "ffb0b8fa"' \
--project=nomadkaraoke \
--limit=50 \
--format='table(timestamp,severity,jsonPayload.message,jsonPayload.logger)'
```
### Filter by Severity (Errors Only)
```bash
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="karaoke-backend" AND "ffb0b8fa" AND severity>=ERROR' \
--project=nomadkaraoke \
--limit=20 \
--format=json
```
### Filter by Time Window
```bash
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="karaoke-backend" AND "ffb0b8fa" AND timestamp>="2025-12-28T01:55:00Z" AND timestamp<="2025-12-28T02:05:00Z"' \
--project=nomadkaraoke \
--limit=100 \
--order=asc \
--format='table(timestamp,severity,jsonPayload.message)'
```
### Search for Exceptions
```bash
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="karaoke-backend" AND (jsonPayload.message:"Exception" OR jsonPayload.message:"Traceback")' \
--project=nomadkaraoke \
--limit=10 \
--format='table(timestamp,severity,jsonPayload.message)'
```
### View HTTP Request Logs
```bash
gcloud logging read \
'resource.type="cloud_run_revision" AND resource.labels.service_name="karaoke-backend" AND httpRequest.requestUrl:"ffb0b8fa"' \
--project=nomadkaraoke \
--limit=20 \
--format='table(timestamp,httpRequest.status,httpRequest.requestMethod,httpRequest.requestUrl)'
```
---
## 3. Accessing Job Files in Cloud Storage
Files are stored in `gs://karaoke-gen-storage-nomadkaraoke/jobs/{job_id}/`.
### List All Files for a Job
```bash
gcloud storage ls -r gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/ --project=nomadkaraoke
```
Example output:
```
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/:
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/lyrics/:
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/lyrics/corrections.json
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/lyrics/karaoke.lrc
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/lyrics/piri - dog (Lyrics Genius).txt
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/lyrics/uncorrected.txt
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/previews/:
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/previews/86b528e04907.mp4
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/screens/:
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/screens/end.jpg
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/screens/end.mov
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/screens/title.jpg
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/screens/title.mov
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/:
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/backing_vocals.flac
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/instrumental_clean.flac
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/instrumental_with_backing.flac
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/lead_vocals.flac
gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/vocals_clean.flac
```
### View File Content (Small Files)
```bash
gcloud storage cat gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/lyrics/corrections.json --project=nomadkaraoke | head -50
```
### Get File Metadata
```bash
gcloud storage objects describe gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/instrumental_clean.flac --project=nomadkaraoke
```
Example output:
```yaml
bucket: karaoke-gen-storage-nomadkaraoke
content_type: application/octet-stream
crc32c_hash: /uXURg==
creation_time: 2025-12-28T01:57:44+0000
size: 18115881
storage_class: STANDARD
```
### Download a File
```bash
gcloud storage cp gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/stems/instrumental_clean.flac ./instrumental.flac --project=nomadkaraoke
```
### GCS Folder Structure
```
jobs/{job_id}/
├── lyrics/
│ ├── corrections.json # Lyrics with timing corrections
│ ├── karaoke.lrc # LRC format lyrics
│ ├── uncorrected.txt # Raw transcription
│ └── *.txt # Reference lyrics from sources
├── previews/
│ └── {hash}.mp4 # Preview video for lyrics review
├── screens/
│ ├── title.mov/.jpg/.png # Title screen
│ └── end.mov/.jpg/.png # End screen
├── stems/
│ ├── instrumental_clean.flac
│ ├── instrumental_with_backing.flac
│ ├── vocals_clean.flac
│ ├── lead_vocals.flac
│ └── backing_vocals.flac
├── videos/
│ └── with_vocals.mkv # Karaoke video with lyrics overlay
└── finals/
├── lossless_4k.mp4
├── lossless_4k.mkv
├── lossy_4k.mp4
└── lossy_720p.mp4
```
---
## 4. Cloud Run Service Status
### Check Service Health
```bash
gcloud run services describe karaoke-backend \
--project=nomadkaraoke \
--region=us-central1 \
--format='yaml(status.conditions,status.latestReadyRevisionName,status.url)'
```
Example output:
```yaml
status:
conditions:
- lastTransitionTime: '2025-12-28T01:29:43.465349Z'
status: 'True'
type: Ready
- lastTransitionTime: '2025-12-28T01:29:42.192338Z'
status: 'True'
type: ConfigurationsReady
- lastTransitionTime: '2025-12-28T01:29:43.430737Z'
status: 'True'
type: RoutesReady
latestReadyRevisionName: karaoke-backend-00203-c7q
url: https://karaoke-backend-ipzqd2k4yq-uc.a.run.app
```
### List Recent Revisions
```bash
gcloud run revisions list \
--service=karaoke-backend \
--project=nomadkaraoke \
--region=us-central1 \
--limit=5 \
--format='table(name,status.conditions.type,status.conditions.status,createTime)'
```
### View Revision Details
```bash
gcloud run revisions describe karaoke-backend-00203-c7q \
--project=nomadkaraoke \
--region=us-central1 \
--format='yaml(spec.containerConcurrency,spec.containers[0].resources)'
```
---
## 5. Cloud Tasks Queues
The backend uses Cloud Tasks for async worker processing.
### List All Queues
```bash
gcloud tasks queues list --project=nomadkaraoke --location=us-central1
```
Example output:
```
QUEUE_NAME STATE MAX_NUM_OF_TASKS MAX_RATE (/sec) MAX_ATTEMPTS
audio-worker-queue RUNNING 50 10.0 3
lyrics-worker-queue RUNNING 50 10.0 3
render-worker-queue RUNNING 20 5.0 2
screens-worker-queue RUNNING 100 50.0 3
video-worker-queue RUNNING 10 3.0 2
```
### View Queue Details
```bash
gcloud tasks queues describe audio-worker-queue \
--project=nomadkaraoke \
--location=us-central1
```
### List Tasks in a Queue
```bash
gcloud tasks list \
--queue=audio-worker-queue \
--project=nomadkaraoke \
--location=us-central1 \
--limit=10
```
---
## 6. Common Debugging Scenarios
### Scenario: Job Stuck in Status
1. **Check job state:**
```bash
python get_job.py ffb0b8fa | jq '.status, .error_message, .timeline[-3:]'
```
2. **Check for errors in logs:**
```bash
gcloud logging read 'resource.labels.service_name="karaoke-backend" AND "ffb0b8fa" AND severity>=ERROR' --project=nomadkaraoke --limit=5 --format=json
```
3. **Check if worker task is queued:**
```bash
gcloud tasks list --queue=audio-worker-queue --project=nomadkaraoke --location=us-central1
```
### Scenario: Job Failed
1. **Get error message from Firestore:**
```bash
python get_job.py ffb0b8fa | jq '.error_message, .error_details'
```
2. **Search for exception stack trace:**
```bash
gcloud logging read 'resource.labels.service_name="karaoke-backend" AND "ffb0b8fa" AND jsonPayload.message:"Traceback"' --project=nomadkaraoke --limit=3 --format='value(jsonPayload.message)'
```
### Scenario: Missing Output Files
1. **List all generated files:**
```bash
gcloud storage ls -r gs://karaoke-gen-storage-nomadkaraoke/jobs/ffb0b8fa/ --project=nomadkaraoke
```
2. **Check worker logs for upload failures:**
```bash
gcloud logging read 'resource.labels.service_name="karaoke-backend" AND "ffb0b8fa" AND (jsonPayload.message:"upload" OR jsonPayload.message:"GCS")' --project=nomadkaraoke --limit=20 --format='table(timestamp,jsonPayload.message)'
```
### Scenario: Audio Separation Slow/Failing
1. **Check audio worker logs:**
```bash
gcloud logging read 'resource.labels.service_name="karaoke-backend" AND "ffb0b8fa" AND jsonPayload.worker:"audio"' --project=nomadkaraoke --limit=30 --format='table(timestamp,jsonPayload.message)'
```
2. **Check Modal API status in worker logs:**
```bash
python get_job.py ffb0b8fa | jq '.worker_logs | map(select(.worker == "audio"))'
```
---
## 7. Useful Aliases
Add these to your shell profile for quick access:
```bash
# Job debugging aliases (update paths as needed)
alias kj='python scripts/get_job.py'
alias klogs='gcloud logging read "resource.labels.service_name=\"karaoke-backend\"" --project=nomadkaraoke --limit=50 --format="table(timestamp,severity,jsonPayload.message)"'
alias kfiles='gcloud storage ls -r gs://karaoke-gen-storage-nomadkaraoke/jobs/'
alias kservice='gcloud run services describe karaoke-backend --project=nomadkaraoke --region=us-central1'
alias kqueues='gcloud tasks queues list --project=nomadkaraoke --location=us-central1'
# Usage:
# kj ffb0b8fa # Get job data
# klogs | grep ffb0b8fa # Search logs
# kfiles ffb0b8fa/ # List job files
```
---
## 8. API Access (Alternative)
The backend API also provides job information if you have an access token.
### Get Job via API
```bash
export TOKEN="your-access-token"
curl -s -H "Authorization: Bearer $TOKEN" \
"https://api.nomadkaraoke.com/api/jobs/ffb0b8fa" | jq
```
### List Jobs
```bash
curl -s -H "Authorization: Bearer $TOKEN" \
"https://api.nomadkaraoke.com/api/jobs?limit=10" | jq
```
---
## Related Documentation
- [Architecture Overview](./ARCHITECTURE.md) - System architecture and data flow
- [Frontend Guide](./FRONTEND-GUIDE.md) - Web UI documentation
- [Backend Feature Parity](../00-current-plan/BACKEND-FEATURE-PARITY-PLAN.md) - Feature status
- [Scalable Architecture Plan](../00-current-plan/SCALABLE-ARCHITECTURE-PLAN.md) - Cloud Tasks and scaling
@@ -0,0 +1,197 @@
# GitHub Actions Docker Builds - Cost Optimization
**Date:** 2025-12-28
**Problem:** Cloud Build costing $100+/month with slow builds
**Solution:** Move Docker builds to GitHub Actions with registry caching
---
## Overview
This document describes the migration from Google Cloud Build to GitHub Actions for Docker image builds. This change eliminates Cloud Build costs while improving build speeds through better caching.
## Cost Comparison
| Approach | Monthly Cost | Build Speed | Caching |
|----------|-------------|-------------|---------|
| **Cloud Build (N1_HIGHCPU_32)** | ~$100+ | 5-15 min | Limited |
| **GitHub Actions + Registry Cache** | **$0** | 2-5 min | Excellent |
### Why Cloud Build Was Expensive
1. **N1_HIGHCPU_32 pricing**: ~$0.064/build-minute (21x more than default)
2. **No persistent layer cache**: Each build re-downloaded dependencies
3. **Cold start overhead**: Spinning up build VMs added latency
4. **Large dependency tree**: torch, transformers, etc. = ~500 packages
### Why GitHub Actions Is Free
- **Public repositories**: Unlimited build minutes on GitHub-hosted runners
- **Registry cache**: Layers cached in Artifact Registry, persist between builds
- **BuildKit mode=max**: Caches ALL intermediate layers, not just final
---
## Architecture
```
┌─────────────────────────────────────────────────────────────────────────┐
│ NEW BUILD PIPELINE │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ GitHub Actions (Free) │
│ ├── docker/setup-buildx-action (BuildKit) │
│ ├── google-github-actions/auth (Workload Identity) │
│ └── docker/build-push-action │
│ ├── cache-from: type=registry,ref=...karaoke-backend:cache │
│ └── cache-to: type=registry,ref=...karaoke-backend:cache,mode=max│
│ │
│ Artifact Registry │
│ ├── karaoke-backend:latest (production image) │
│ ├── karaoke-backend:cache (build cache - ALL layers) │
│ ├── karaoke-backend-base:latest (base image with deps) │
│ └── karaoke-backend-base:cache (base cache) │
│ │
│ Build Times (Expected): │
│ ├── First build (cold): ~8-10 min │
│ ├── Code-only changes: ~2-3 min (cache hit) │
│ └── Dependency changes: ~6-8 min (partial cache hit) │
│ │
└─────────────────────────────────────────────────────────────────────────┘
```
---
## How It Works
### Two-Stage Build Pattern
We maintain the existing two-stage build pattern for efficiency:
1. **Base Image** (`karaoke-backend-base`)
- Contains Python 3.11, ffmpeg, system dependencies
- All pip dependencies pre-installed
- Only rebuilt when `poetry.lock` or `Dockerfile.base` changes
- Hash-based change detection via image labels
2. **App Image** (`karaoke-backend`)
- Built FROM the base image
- Contains only application code
- Fast rebuilds (~2 min) for code-only changes
### Registry Cache
The key optimization is using Artifact Registry as a persistent layer cache:
```yaml
- uses: docker/build-push-action@v6
with:
cache-from: type=registry,ref=.../karaoke-backend:cache
cache-to: type=registry,ref=.../karaoke-backend:cache,mode=max
```
**Benefits:**
- `mode=max` caches ALL layers, not just the final image
- Cache persists between CI runs (unlike GitHub Actions cache which has 10GB limit)
- Layers only re-built when inputs change
---
## Files Changed
### New Files
- **`.dockerignore`** - Reduces build context from ~1GB to ~10MB
### Modified Files
- **`.github/workflows/ci.yml`** - `deploy-backend` job rewritten to use:
- `docker/setup-buildx-action@v3`
- `docker/build-push-action@v6`
- Registry caching to Artifact Registry
### Kept (as fallback)
- **`cloudbuild.yaml`** - Still works for manual triggers
- **`cloudbuild-base.yaml`** - Still works for manual base image builds
---
## Manual Builds (Fallback)
If GitHub Actions fails, you can still use Cloud Build:
```bash
# Build base image (only if dependencies changed)
gcloud builds submit --config=cloudbuild-base.yaml --project=nomadkaraoke
# Build and deploy app image
gcloud builds submit --config=cloudbuild.yaml --project=nomadkaraoke
```
---
## Monitoring
### Check Build Cache Usage
View cache images in Artifact Registry:
```bash
gcloud artifacts docker images list \
us-central1-docker.pkg.dev/nomadkaraoke/karaoke-repo \
--include-tags
```
You should see `*:cache` tags for both base and app images.
### Compare Build Times
- **GitHub Actions**: Check workflow run times in Actions tab
- **Previous Cloud Build**: ~5-15 min per build
- **Expected with cache**: ~2-3 min for code changes
---
## Troubleshooting
### Cache Miss (Slow Build)
If builds are slow despite no dependency changes:
1. Check if cache image exists:
```bash
docker pull us-central1-docker.pkg.dev/nomadkaraoke/karaoke-repo/karaoke-backend:cache
```
2. Force cache rebuild by pushing to `main` with a small change
### Disk Space Issues
GitHub Actions runners have limited disk. If base image builds fail:
1. The workflow includes `jlumbroso/free-disk-space` action
2. It removes Android SDK, Haskell, .NET, etc. to free ~20GB
### Authentication Issues
If `docker push` fails with permission errors:
1. Check Workload Identity is configured correctly
2. Verify service account has `roles/artifactregistry.writer`
---
## Future Improvements
1. **Parallel base/app builds**: If base is unchanged, start app build immediately
2. **GitHub Actions cache fallback**: Use GHA cache when registry is slow
3. **Build metrics**: Track build times and cache hit rates
---
## References
- [Docker Build with GitHub Actions](https://docs.docker.com/build/ci/github-actions/)
- [Registry Cache Backend](https://docs.docker.com/build/cache/backends/registry/)
- [Cache Management](https://docs.docker.com/build/ci/github-actions/cache/)
+1
View File
@@ -0,0 +1 @@
playwright-report-prod/
+459
View File
@@ -0,0 +1,459 @@
/**
* Unit tests for job-status.ts utility functions
*/
import { getJobStep, formatStepIndicator, isBlockingStatus, getJobProgressPercent, JobStep } from '../lib/job-status';
import type { Job } from '../lib/api';
// Helper to create a minimal Job object for testing
function createJob(status: string, stateData?: Record<string, any>): Job {
return {
job_id: 'test-job-123',
status,
progress: 0,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
state_data: stateData,
};
}
describe('getJobStep', () => {
describe('Step 1: Setup', () => {
it('returns step 1 for pending status', () => {
const job = createJob('pending');
const result = getJobStep(job);
expect(result.step).toBe(1);
expect(result.total).toBe(10);
expect(result.label).toBe('Setting up');
expect(result.isBlocking).toBe(false);
expect(result.color).toBe('text-slate-400');
});
});
describe('Step 2: Audio Search', () => {
it('returns step 2 for searching_audio status', () => {
const job = createJob('searching_audio');
const result = getJobStep(job);
expect(result.step).toBe(2);
expect(result.label).toBe('Searching for audio');
expect(result.isBlocking).toBe(false);
});
it('returns step 2 with blocking for awaiting_audio_selection', () => {
const job = createJob('awaiting_audio_selection');
const result = getJobStep(job);
expect(result.step).toBe(2);
expect(result.label).toBe('Select audio source');
expect(result.isBlocking).toBe(true);
expect(result.color).toBe('text-amber-400');
});
});
describe('Step 3: Download', () => {
it('returns step 3 for downloading_audio status', () => {
const job = createJob('downloading_audio');
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading audio');
});
it('returns step 3 for downloading status without worker progress', () => {
const job = createJob('downloading');
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading');
});
it('returns step 3 for downloading with empty state_data', () => {
const job = createJob('downloading', {});
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading');
});
});
describe('Step 3→4 Transition: Downloading with active workers', () => {
// When backend status is "downloading" but workers are actively running,
// the frontend should show step 4 (Processing) to avoid appearing stuck
it('returns step 4 when downloading with active audio worker', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'separating_stage1', progress: 10 },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.color).toBe('text-purple-400');
});
it('returns step 4 when downloading with active lyrics worker', () => {
const job = createJob('downloading', {
lyrics_progress: { stage: 'transcribing', progress: 10 },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.color).toBe('text-purple-400');
});
it('returns step 4 with combined label when both workers active', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'separating_stage1', progress: 10 },
lyrics_progress: { stage: 'transcribing', progress: 10 },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toMatch(/Audio.*\+.*Transcribing|Transcribing.*\+.*Audio/);
});
it('shows only active worker when one is complete', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'audio_complete' },
lyrics_progress: { stage: 'transcribing' },
audio_complete: true,
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Transcribing');
});
it('shows processing complete when both workers done', () => {
const job = createJob('downloading', {
audio_progress: { stage: 'audio_complete' },
lyrics_progress: { stage: 'lyrics_complete' },
audio_complete: true,
lyrics_complete: true,
});
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Processing complete');
});
it('returns step 3 when state_data has no stage info', () => {
// state_data exists but no actual progress stage set
const job = createJob('downloading', {
audio_progress: {},
lyrics_progress: {},
});
const result = getJobStep(job);
expect(result.step).toBe(3);
expect(result.label).toBe('Downloading');
});
});
describe('Step 4: Parallel Processing', () => {
it('returns step 4 for separating_stage1', () => {
const job = createJob('separating_stage1');
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Separating audio (1/2)');
});
it('returns step 4 for separating_stage2', () => {
const job = createJob('separating_stage2');
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Separating audio (2/2)');
});
it('returns step 4 for transcribing', () => {
const job = createJob('transcribing');
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Transcribing lyrics');
});
it('returns step 4 for correcting', () => {
const job = createJob('correcting');
const result = getJobStep(job);
expect(result.step).toBe(4);
expect(result.label).toBe('Correcting lyrics');
});
it('shows combined progress when both audio and lyrics are in progress', () => {
const job = createJob('separating_stage1', {
audio_progress: { stage: 'separating_stage1' },
lyrics_progress: { stage: 'transcribing' },
});
const result = getJobStep(job);
expect(result.step).toBe(4);
// Should show combined label
expect(result.label).toMatch(/Audio|Transcribing/);
});
it('shows combined status when audio done but lyrics in progress', () => {
const job = createJob('lyrics_complete', {
audio_progress: { stage: 'audio_complete' },
lyrics_progress: { stage: 'transcribing' },
audio_complete: true,
});
const result = getJobStep(job);
expect(result.step).toBe(4);
});
});
describe('Step 5: Screen Generation', () => {
it('returns step 5 for generating_screens', () => {
const job = createJob('generating_screens');
const result = getJobStep(job);
expect(result.step).toBe(5);
expect(result.label).toBe('Generating screens');
});
it('returns step 5 for applying_padding', () => {
const job = createJob('applying_padding');
const result = getJobStep(job);
expect(result.step).toBe(5);
expect(result.label).toBe('Syncing countdown');
});
});
describe('Step 6: Lyrics Review (BLOCKING)', () => {
it('returns step 6 with blocking for awaiting_review', () => {
const job = createJob('awaiting_review');
const result = getJobStep(job);
expect(result.step).toBe(6);
expect(result.label).toBe('Review lyrics');
expect(result.isBlocking).toBe(true);
expect(result.color).toBe('text-amber-400');
});
it('returns step 6 for in_review', () => {
const job = createJob('in_review');
const result = getJobStep(job);
expect(result.step).toBe(6);
expect(result.label).toBe('In review');
expect(result.isBlocking).toBe(true);
});
});
describe('Step 7: Video Rendering', () => {
it('returns step 7 for review_complete', () => {
const job = createJob('review_complete');
const result = getJobStep(job);
expect(result.step).toBe(7);
expect(result.label).toBe('Starting render');
});
it('returns step 7 for rendering_video', () => {
const job = createJob('rendering_video');
const result = getJobStep(job);
expect(result.step).toBe(7);
expect(result.label).toBe('Rendering video');
});
});
describe('Step 8: Instrumental Selection (BLOCKING)', () => {
it('returns step 8 with blocking for awaiting_instrumental_selection', () => {
const job = createJob('awaiting_instrumental_selection');
const result = getJobStep(job);
expect(result.step).toBe(8);
expect(result.label).toBe('Select instrumental');
expect(result.isBlocking).toBe(true);
expect(result.color).toBe('text-amber-400');
});
});
describe('Step 9: Final Encoding', () => {
it('returns step 9 for instrumental_selected', () => {
const job = createJob('instrumental_selected');
const result = getJobStep(job);
expect(result.step).toBe(9);
expect(result.label).toBe('Starting final encode');
});
it('returns step 9 for generating_video', () => {
const job = createJob('generating_video');
const result = getJobStep(job);
expect(result.step).toBe(9);
expect(result.label).toBe('Generating final video');
});
it('returns step 9 for encoding', () => {
const job = createJob('encoding');
const result = getJobStep(job);
expect(result.step).toBe(9);
expect(result.label).toBe('Encoding video');
});
it('returns step 9 for packaging', () => {
const job = createJob('packaging');
const result = getJobStep(job);
expect(result.step).toBe(9);
expect(result.label).toBe('Packaging files');
});
});
describe('Step 10: Distribution / Complete', () => {
it('returns step 10 for uploading', () => {
const job = createJob('uploading');
const result = getJobStep(job);
expect(result.step).toBe(10);
expect(result.label).toBe('Uploading');
expect(result.color).toBe('text-green-400');
});
it('returns step 10 for notifying', () => {
const job = createJob('notifying');
const result = getJobStep(job);
expect(result.step).toBe(10);
expect(result.label).toBe('Sending notifications');
});
it('returns step 10 for complete', () => {
const job = createJob('complete');
const result = getJobStep(job);
expect(result.step).toBe(10);
expect(result.label).toBe('Complete');
expect(result.color).toBe('text-green-400');
});
it('returns step 10 for prep_complete', () => {
const job = createJob('prep_complete');
const result = getJobStep(job);
expect(result.step).toBe(10);
expect(result.label).toBe('Prep complete');
});
});
describe('Terminal states', () => {
it('returns step 0 for failed status', () => {
const job = createJob('failed');
const result = getJobStep(job);
expect(result.step).toBe(0);
expect(result.label).toBe('Failed');
expect(result.color).toBe('text-red-400');
});
it('returns step 0 for cancelled status', () => {
const job = createJob('cancelled');
const result = getJobStep(job);
expect(result.step).toBe(0);
expect(result.label).toBe('Cancelled');
expect(result.color).toBe('text-slate-500');
});
});
describe('Unknown status handling', () => {
it('returns step 0 with humanized label for unknown status', () => {
const job = createJob('some_unknown_status');
const result = getJobStep(job);
expect(result.step).toBe(0);
expect(result.label).toBe('some unknown status');
expect(result.isBlocking).toBe(false);
expect(result.color).toBe('text-slate-400');
});
it('handles null status gracefully', () => {
const job = createJob(null as unknown as string);
const result = getJobStep(job);
expect(result.step).toBe(1); // Falls back to 'pending'
});
it('handles undefined status gracefully', () => {
const job = createJob(undefined as unknown as string);
const result = getJobStep(job);
expect(result.step).toBe(1); // Falls back to 'pending'
});
});
describe('Case insensitivity', () => {
it('handles uppercase status', () => {
const job = createJob('PENDING');
const result = getJobStep(job);
expect(result.step).toBe(1);
});
it('handles mixed case status', () => {
const job = createJob('Downloading');
const result = getJobStep(job);
expect(result.step).toBe(3);
});
});
});
describe('formatStepIndicator', () => {
it('formats active step correctly', () => {
expect(formatStepIndicator(4, 10, 'Processing')).toBe('[4/10] Processing');
});
it('omits step number for terminal states (step 0)', () => {
expect(formatStepIndicator(0, 10, 'Failed')).toBe('Failed');
});
it('formats first step correctly', () => {
expect(formatStepIndicator(1, 10, 'Setting up')).toBe('[1/10] Setting up');
});
it('formats last step correctly', () => {
expect(formatStepIndicator(10, 10, 'Complete')).toBe('[10/10] Complete');
});
});
describe('isBlockingStatus', () => {
it('returns true for awaiting_review', () => {
expect(isBlockingStatus('awaiting_review')).toBe(true);
});
it('returns true for in_review', () => {
expect(isBlockingStatus('in_review')).toBe(true);
});
it('returns true for awaiting_audio_selection', () => {
expect(isBlockingStatus('awaiting_audio_selection')).toBe(true);
});
it('returns true for awaiting_instrumental_selection', () => {
expect(isBlockingStatus('awaiting_instrumental_selection')).toBe(true);
});
it('returns false for non-blocking statuses', () => {
expect(isBlockingStatus('pending')).toBe(false);
expect(isBlockingStatus('downloading')).toBe(false);
expect(isBlockingStatus('complete')).toBe(false);
expect(isBlockingStatus('failed')).toBe(false);
});
it('returns false for unknown statuses', () => {
expect(isBlockingStatus('unknown_status')).toBe(false);
});
it('handles case insensitivity', () => {
expect(isBlockingStatus('AWAITING_REVIEW')).toBe(true);
});
it('handles null/undefined gracefully', () => {
expect(isBlockingStatus(null as unknown as string)).toBe(false);
expect(isBlockingStatus(undefined as unknown as string)).toBe(false);
});
});
describe('getJobProgressPercent', () => {
it('returns 0 for terminal states (step 0)', () => {
const job = createJob('failed');
expect(getJobProgressPercent(job)).toBe(0);
});
it('returns 10% for step 1', () => {
const job = createJob('pending');
expect(getJobProgressPercent(job)).toBe(10);
});
it('returns 40% for step 4', () => {
const job = createJob('transcribing');
expect(getJobProgressPercent(job)).toBe(40);
});
it('returns 60% for step 6', () => {
const job = createJob('awaiting_review');
expect(getJobProgressPercent(job)).toBe(60);
});
it('returns 100% for step 10', () => {
const job = createJob('complete');
expect(getJobProgressPercent(job)).toBe(100);
});
it('returns 0 for unknown status', () => {
const job = createJob('unknown_status_xyz');
expect(getJobProgressPercent(job)).toBe(0);
});
});
@@ -0,0 +1,237 @@
/**
* Tests for JobActions component
*/
import { render, screen } from '@testing-library/react'
import { JobActions } from '../job/JobActions'
import { Job } from '@/lib/api'
// Mock the API module
jest.mock('@/lib/api', () => ({
api: {
retryJob: jest.fn(),
cancelJob: jest.fn(),
deleteJob: jest.fn(),
}
}))
describe('JobActions', () => {
const mockOnRefresh = jest.fn()
const mockOnToggleLogs = jest.fn()
const baseJob: Partial<Job> = {
job_id: '123',
created_at: '2025-01-01T00:00:00Z',
updated_at: '2025-01-01T00:00:00Z',
artist: 'Test Artist',
title: 'Test Song',
}
beforeEach(() => {
jest.clearAllMocks()
})
describe('Retry button visibility', () => {
it('shows retry button for failed jobs', () => {
const failedJob = { ...baseJob, status: 'failed' } as Job
render(
<JobActions
job={failedJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Retry')).toBeInTheDocument()
})
it('shows retry button for cancelled jobs', () => {
const cancelledJob = { ...baseJob, status: 'cancelled' } as Job
render(
<JobActions
job={cancelledJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Retry')).toBeInTheDocument()
})
it('does not show retry button for pending jobs', () => {
const pendingJob = { ...baseJob, status: 'pending' } as Job
render(
<JobActions
job={pendingJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.queryByText('Retry')).not.toBeInTheDocument()
})
it('does not show retry button for processing jobs', () => {
const processingJob = { ...baseJob, status: 'processing' } as Job
render(
<JobActions
job={processingJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.queryByText('Retry')).not.toBeInTheDocument()
})
it('does not show retry button for complete jobs', () => {
const completeJob = { ...baseJob, status: 'complete' } as Job
render(
<JobActions
job={completeJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.queryByText('Retry')).not.toBeInTheDocument()
})
})
describe('Cancel button visibility', () => {
it('shows cancel button for pending jobs', () => {
const pendingJob = { ...baseJob, status: 'pending' } as Job
render(
<JobActions
job={pendingJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Cancel')).toBeInTheDocument()
})
it('does not show cancel button for failed jobs', () => {
const failedJob = { ...baseJob, status: 'failed' } as Job
render(
<JobActions
job={failedJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.queryByText('Cancel')).not.toBeInTheDocument()
})
it('does not show cancel button for cancelled jobs', () => {
const cancelledJob = { ...baseJob, status: 'cancelled' } as Job
render(
<JobActions
job={cancelledJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.queryByText('Cancel')).not.toBeInTheDocument()
})
})
describe('Delete button visibility', () => {
it('shows delete button for complete jobs', () => {
const completeJob = { ...baseJob, status: 'complete' } as Job
render(
<JobActions
job={completeJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Delete')).toBeInTheDocument()
})
it('shows delete button for failed jobs', () => {
const failedJob = { ...baseJob, status: 'failed' } as Job
render(
<JobActions
job={failedJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Delete')).toBeInTheDocument()
})
it('shows delete button for cancelled jobs', () => {
const cancelledJob = { ...baseJob, status: 'cancelled' } as Job
render(
<JobActions
job={cancelledJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Delete')).toBeInTheDocument()
})
it('does not show delete button for pending jobs', () => {
const pendingJob = { ...baseJob, status: 'pending' } as Job
render(
<JobActions
job={pendingJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.queryByText('Delete')).not.toBeInTheDocument()
})
})
describe('Show Logs button', () => {
it('always shows Show Logs button', () => {
const pendingJob = { ...baseJob, status: 'pending' } as Job
render(
<JobActions
job={pendingJob}
onRefresh={mockOnRefresh}
showLogs={false}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Show Logs')).toBeInTheDocument()
})
it('shows Hide Logs when logs are visible', () => {
const pendingJob = { ...baseJob, status: 'pending' } as Job
render(
<JobActions
job={pendingJob}
onRefresh={mockOnRefresh}
showLogs={true}
onToggleLogs={mockOnToggleLogs}
/>
)
expect(screen.getByText('Hide Logs')).toBeInTheDocument()
})
})
})
+13 -8
View File
@@ -29,10 +29,6 @@ jest.mock('../job/OutputLinks', () => ({
OutputLinks: () => <div data-testid="output-links">Output Links</div>
}))
jest.mock('../job/InstrumentalSelector', () => ({
InstrumentalSelector: () => <div data-testid="instrumental-selector">Instrumental Selector</div>
}))
jest.mock('../audio-search/AudioSearchDialog', () => ({
AudioSearchDialog: () => <div data-testid="audio-search-dialog">Audio Search Dialog</div>
}))
@@ -135,11 +131,20 @@ describe('JobCard', () => {
expect(screen.getByRole('button', { name: 'Select Audio' })).toBeInTheDocument()
})
it('shows instrumental selection button for awaiting_instrumental_selection status', () => {
const instrumentalJob = { ...mockJob, status: 'awaiting_instrumental_selection' }
it('shows instrumental selection link for awaiting_instrumental_selection status', () => {
const instrumentalJob = {
...mockJob,
status: 'awaiting_instrumental_selection',
instrumental_token: 'test-instrumental-token'
}
render(<JobCard job={instrumentalJob} onRefresh={mockOnRefresh} />)
expect(screen.getByRole('button', { name: 'Select Instrumental' })).toBeInTheDocument()
const instrumentalLink = screen.getByText('Select Instrumental')
expect(instrumentalLink).toBeInTheDocument()
const href = instrumentalLink.closest('a')?.getAttribute('href') || ''
expect(href).toContain('/instrumental/')
expect(href).toContain('baseApiUrl=')
expect(href).toContain('instrumentalToken=test-instrumental-token')
})
})
@@ -164,7 +169,7 @@ describe('JobCard', () => {
render(<JobCard job={instrumentalJob} onRefresh={mockOnRefresh} />)
expect(screen.getByText(/Will auto-select clean/)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Select Instrumental' })).not.toBeInTheDocument()
expect(screen.queryByText('Select Instrumental')).not.toBeInTheDocument()
})
it('shows auto-select message for awaiting_audio_selection status', () => {
@@ -1,263 +0,0 @@
"use client"
import { useRef, useState, useEffect, useCallback } from "react"
import { Button } from "@/components/ui/button"
import { Slider } from "@/components/ui/slider"
import { Play, Pause, SkipBack, SkipForward, Volume2, VolumeX } from "lucide-react"
interface AudioPlayerProps {
src: string
onTimeUpdate?: (currentTime: number) => void
onDurationChange?: (duration: number) => void
seekTo?: number
label?: string
className?: string
}
export function AudioPlayer({
src,
onTimeUpdate,
onDurationChange,
seekTo,
label,
className = "",
}: AudioPlayerProps) {
const audioRef = useRef<HTMLAudioElement>(null)
const [isPlaying, setIsPlaying] = useState(false)
const [currentTime, setCurrentTime] = useState(0)
const [duration, setDuration] = useState(0)
const [volume, setVolume] = useState(1)
const [isMuted, setIsMuted] = useState(false)
const [isLoading, setIsLoading] = useState(true)
// Handle external seek requests
useEffect(() => {
if (seekTo !== undefined && audioRef.current && !isNaN(seekTo)) {
audioRef.current.currentTime = seekTo
}
}, [seekTo])
// Handle audio events
useEffect(() => {
const audio = audioRef.current
if (!audio) return
const handleTimeUpdate = () => {
setCurrentTime(audio.currentTime)
onTimeUpdate?.(audio.currentTime)
}
const handleDurationChange = () => {
setDuration(audio.duration)
onDurationChange?.(audio.duration)
}
const handleLoadedData = () => {
setIsLoading(false)
setDuration(audio.duration)
onDurationChange?.(audio.duration)
}
const handleEnded = () => {
setIsPlaying(false)
}
const handleLoadStart = () => {
setIsLoading(true)
}
const handleCanPlay = () => {
setIsLoading(false)
}
// Sync isPlaying state with actual audio element state
const handlePlay = () => {
setIsPlaying(true)
}
const handlePause = () => {
setIsPlaying(false)
}
audio.addEventListener("timeupdate", handleTimeUpdate)
audio.addEventListener("durationchange", handleDurationChange)
audio.addEventListener("loadeddata", handleLoadedData)
audio.addEventListener("ended", handleEnded)
audio.addEventListener("loadstart", handleLoadStart)
audio.addEventListener("canplay", handleCanPlay)
audio.addEventListener("play", handlePlay)
audio.addEventListener("pause", handlePause)
return () => {
audio.removeEventListener("timeupdate", handleTimeUpdate)
audio.removeEventListener("durationchange", handleDurationChange)
audio.removeEventListener("loadeddata", handleLoadedData)
audio.removeEventListener("ended", handleEnded)
audio.removeEventListener("loadstart", handleLoadStart)
audio.removeEventListener("canplay", handleCanPlay)
audio.removeEventListener("play", handlePlay)
audio.removeEventListener("pause", handlePause)
}
}, [onTimeUpdate, onDurationChange])
// Update volume
useEffect(() => {
if (audioRef.current) {
audioRef.current.volume = isMuted ? 0 : volume
}
}, [volume, isMuted])
const togglePlay = useCallback(async () => {
const audio = audioRef.current
if (!audio) return
try {
if (audio.paused) {
await audio.play()
} else {
audio.pause()
}
// State will be updated by the play/pause event listeners
} catch (error) {
// Play was prevented (e.g., user hasn't interacted with page yet)
console.warn("Audio playback was prevented:", error)
}
}, [])
const handleSeek = useCallback((value: number[]) => {
const audio = audioRef.current
if (!audio) return
const newTime = value[0]
audio.currentTime = newTime
setCurrentTime(newTime)
}, [])
const skipBackward = useCallback(() => {
const audio = audioRef.current
if (!audio) return
audio.currentTime = Math.max(0, audio.currentTime - 5)
}, [])
const skipForward = useCallback(() => {
const audio = audioRef.current
if (!audio) return
audio.currentTime = Math.min(duration, audio.currentTime + 5)
}, [duration])
const toggleMute = useCallback(() => {
setIsMuted(!isMuted)
}, [isMuted])
const handleVolumeChange = useCallback((value: number[]) => {
setVolume(value[0])
if (value[0] > 0 && isMuted) {
setIsMuted(false)
}
}, [isMuted])
const formatTime = (time: number) => {
if (isNaN(time)) return "0:00"
const minutes = Math.floor(time / 60)
const seconds = Math.floor(time % 60)
return `${minutes}:${seconds.toString().padStart(2, "0")}`
}
return (
<div className={`bg-card rounded-lg p-4 ${className}`}>
<audio ref={audioRef} src={src} preload="metadata" />
{label && (
<div className="text-sm font-medium text-muted-foreground mb-3">
{label}
</div>
)}
{/* Progress bar */}
<div className="mb-4">
<Slider
value={[currentTime]}
max={duration || 100}
step={0.1}
onValueChange={handleSeek}
disabled={isLoading}
className="w-full"
/>
<div className="flex justify-between text-xs text-muted-foreground mt-1">
<span>{formatTime(currentTime)}</span>
<span>{formatTime(duration)}</span>
</div>
</div>
{/* Controls */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Button
variant="ghost"
size="icon"
onClick={skipBackward}
disabled={isLoading}
className="h-8 w-8"
>
<SkipBack className="h-4 w-4" />
</Button>
<Button
variant="default"
size="icon"
onClick={togglePlay}
disabled={isLoading}
className="h-10 w-10"
>
{isPlaying ? (
<Pause className="h-5 w-5" />
) : (
<Play className="h-5 w-5 ml-0.5" />
)}
</Button>
<Button
variant="ghost"
size="icon"
onClick={skipForward}
disabled={isLoading}
className="h-8 w-8"
>
<SkipForward className="h-4 w-4" />
</Button>
</div>
{/* Volume control */}
<div className="flex items-center gap-2">
<Button
variant="ghost"
size="icon"
onClick={toggleMute}
className="h-8 w-8"
>
{isMuted || volume === 0 ? (
<VolumeX className="h-4 w-4" />
) : (
<Volume2 className="h-4 w-4" />
)}
</Button>
<Slider
value={[isMuted ? 0 : volume]}
max={1}
step={0.1}
onValueChange={handleVolumeChange}
className="w-20"
/>
</div>
</div>
{isLoading && (
<div className="text-xs text-muted-foreground text-center mt-2">
Loading audio...
</div>
)}
</div>
)
}
@@ -1,3 +0,0 @@
export { WaveformDisplay } from "./waveform-display"
export { AudioPlayer } from "./audio-player"
export { RegionSelector } from "./region-selector"
@@ -1,222 +0,0 @@
"use client"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { Trash2, VolumeX, Plus } from "lucide-react"
interface MuteRegion {
start_seconds: number
end_seconds: number
}
interface AudibleSegment {
start_seconds: number
end_seconds: number
duration_seconds: number
avg_amplitude_db: number
}
interface RegionSelectorProps {
muteRegions: MuteRegion[]
audibleSegments?: AudibleSegment[]
onAddRegion: (region: MuteRegion) => void
onRemoveRegion: (index: number) => void
onClearAll: () => void
onSeekTo?: (time: number) => void
isSelectionMode: boolean
onToggleSelectionMode: () => void
className?: string
}
export function RegionSelector({
muteRegions,
audibleSegments = [],
onAddRegion,
onRemoveRegion,
onClearAll,
onSeekTo,
isSelectionMode,
onToggleSelectionMode,
className = "",
}: RegionSelectorProps) {
const formatTime = (seconds: number) => {
const mins = Math.floor(seconds / 60)
const secs = Math.floor(seconds % 60)
const ms = Math.floor((seconds % 1) * 100)
return `${mins}:${secs.toString().padStart(2, "0")}.${ms.toString().padStart(2, "0")}`
}
const formatDuration = (seconds: number) => {
if (seconds < 1) {
return `${Math.round(seconds * 1000)}ms`
}
return `${seconds.toFixed(1)}s`
}
const totalMutedDuration = muteRegions.reduce(
(sum, r) => sum + (r.end_seconds - r.start_seconds),
0
)
// Quick mute: add mute region for an audible segment
const handleQuickMute = (segment: AudibleSegment) => {
onAddRegion({
start_seconds: segment.start_seconds,
end_seconds: segment.end_seconds,
})
}
// Check if a segment is already muted
const isSegmentMuted = (segment: AudibleSegment) => {
return muteRegions.some(
(r) =>
r.start_seconds <= segment.start_seconds &&
r.end_seconds >= segment.end_seconds
)
}
return (
<div className={`space-y-4 ${className}`}>
{/* Selection Mode Toggle */}
<Card>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<CardTitle className="text-lg">Mute Regions</CardTitle>
<Button
variant={isSelectionMode ? "default" : "outline"}
size="sm"
onClick={onToggleSelectionMode}
>
<Plus className="h-4 w-4 mr-2" />
{isSelectionMode ? "Click waveform to select" : "Add Region"}
</Button>
</div>
</CardHeader>
<CardContent>
{muteRegions.length === 0 ? (
<p className="text-sm text-muted-foreground">
No mute regions selected. Click "Add Region" and drag on the waveform to select areas to mute.
</p>
) : (
<div className="space-y-2">
{muteRegions.map((region, index) => (
<div
key={index}
className="flex items-center justify-between p-2 bg-muted/50 rounded-lg group"
>
<div className="flex items-center gap-3">
<VolumeX className="h-4 w-4 text-destructive" />
<button
onClick={() => onSeekTo?.(region.start_seconds)}
className="text-sm font-mono hover:text-primary transition-colors"
>
{formatTime(region.start_seconds)}
</button>
<span className="text-muted-foreground">→</span>
<button
onClick={() => onSeekTo?.(region.end_seconds)}
className="text-sm font-mono hover:text-primary transition-colors"
>
{formatTime(region.end_seconds)}
</button>
<Badge variant="secondary" className="ml-2">
{formatDuration(region.end_seconds - region.start_seconds)}
</Badge>
</div>
<Button
variant="ghost"
size="icon"
onClick={() => onRemoveRegion(index)}
className="h-8 w-8 opacity-0 group-hover:opacity-100 transition-opacity"
>
<Trash2 className="h-4 w-4 text-destructive" />
</Button>
</div>
))}
<div className="flex items-center justify-between pt-2 border-t mt-3">
<span className="text-sm text-muted-foreground">
Total muted: {formatDuration(totalMutedDuration)}
</span>
<Button
variant="ghost"
size="sm"
onClick={onClearAll}
className="text-destructive hover:text-destructive"
>
Clear All
</Button>
</div>
</div>
)}
</CardContent>
</Card>
{/* Detected Audible Segments */}
{audibleSegments.length > 0 && (
<Card>
<CardHeader className="pb-3">
<CardTitle className="text-lg">Detected Backing Vocals</CardTitle>
</CardHeader>
<CardContent>
<p className="text-sm text-muted-foreground mb-3">
Click a segment to jump to that time, or click the mute button to add it as a mute region.
</p>
<div className="space-y-2 max-h-64 overflow-y-auto">
{audibleSegments.map((segment, index) => {
const isMuted = isSegmentMuted(segment)
return (
<div
key={index}
className={`flex items-center justify-between p-2 rounded-lg transition-colors ${
isMuted ? "bg-destructive/10" : "bg-muted/50 hover:bg-muted"
}`}
>
<button
onClick={() => onSeekTo?.(segment.start_seconds)}
className="flex items-center gap-3 text-left"
>
<span className="text-sm font-mono">
{formatTime(segment.start_seconds)} - {formatTime(segment.end_seconds)}
</span>
<Badge variant="outline" className="text-xs">
{formatDuration(segment.duration_seconds)}
</Badge>
<Badge
variant="outline"
className={`text-xs ${
segment.avg_amplitude_db > -20
? "border-yellow-500 text-yellow-500"
: "border-muted-foreground"
}`}
>
{segment.avg_amplitude_db.toFixed(1)}dB
</Badge>
</button>
{!isMuted && (
<Button
variant="ghost"
size="sm"
onClick={() => handleQuickMute(segment)}
className="text-xs"
>
<VolumeX className="h-3 w-3 mr-1" />
Mute
</Button>
)}
{isMuted && (
<Badge variant="destructive" className="text-xs">
Muted
</Badge>
)}
</div>
)
})}
</div>
</CardContent>
</Card>
)}
</div>
)
}
@@ -1,259 +0,0 @@
"use client"
import { useRef, useEffect, useState, useCallback } from "react"
interface AudibleSegment {
start_seconds: number
end_seconds: number
duration_seconds: number
avg_amplitude_db: number
}
interface MuteRegion {
start_seconds: number
end_seconds: number
}
interface WaveformDisplayProps {
amplitudes: number[]
duration: number
currentTime?: number
segments?: AudibleSegment[]
muteRegions?: MuteRegion[]
onSeek?: (time: number) => void
onRegionSelect?: (region: MuteRegion) => void
isSelecting?: boolean
width?: number
height?: number
className?: string
}
export function WaveformDisplay({
amplitudes,
duration,
currentTime = 0,
segments = [],
muteRegions = [],
onSeek,
onRegionSelect,
isSelecting = false,
width = 800,
height = 150,
className = "",
}: WaveformDisplayProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const [isDragging, setIsDragging] = useState(false)
const [dragStart, setDragStart] = useState<number | null>(null)
const [dragEnd, setDragEnd] = useState<number | null>(null)
const [hoveredTime, setHoveredTime] = useState<number | null>(null)
// Convert time to x position
const timeToX = useCallback((time: number) => {
return (time / duration) * width
}, [duration, width])
// Convert x position to time
const xToTime = useCallback((x: number) => {
return (x / width) * duration
}, [duration, width])
// Draw the waveform
useEffect(() => {
const canvas = canvasRef.current
if (!canvas) return
const ctx = canvas.getContext("2d")
if (!ctx) return
// Clear canvas
ctx.fillStyle = "#1a1a2e"
ctx.fillRect(0, 0, width, height)
const barWidth = width / amplitudes.length
const centerY = height / 2
// Draw mute regions background
ctx.fillStyle = "rgba(255, 107, 107, 0.2)"
for (const region of muteRegions) {
const startX = timeToX(region.start_seconds)
const endX = timeToX(region.end_seconds)
ctx.fillRect(startX, 0, endX - startX, height)
}
// Draw current drag selection
if (isDragging && dragStart !== null && dragEnd !== null) {
const startX = Math.min(timeToX(dragStart), timeToX(dragEnd))
const endX = Math.max(timeToX(dragStart), timeToX(dragEnd))
ctx.fillStyle = "rgba(255, 107, 107, 0.3)"
ctx.fillRect(startX, 0, endX - startX, height)
}
// Draw segment highlights
for (const segment of segments) {
const startX = timeToX(segment.start_seconds)
const endX = timeToX(segment.end_seconds)
const startIdx = Math.floor((segment.start_seconds / duration) * amplitudes.length)
const endIdx = Math.ceil((segment.end_seconds / duration) * amplitudes.length)
ctx.fillStyle = "rgba(233, 69, 96, 0.4)"
for (let i = startIdx; i < endIdx && i < amplitudes.length; i++) {
const amplitude = amplitudes[i]
const barHeight = amplitude * height * 0.8
const x = i * barWidth
ctx.fillRect(x, centerY - barHeight / 2, barWidth - 1, barHeight)
}
}
// Draw main waveform
ctx.fillStyle = "#4a90d9"
for (let i = 0; i < amplitudes.length; i++) {
const amplitude = amplitudes[i]
const barHeight = amplitude * height * 0.8
const x = i * barWidth
ctx.fillRect(x, centerY - barHeight / 2, barWidth - 1, barHeight)
}
// Draw silence threshold line
ctx.strokeStyle = "rgba(255, 255, 255, 0.2)"
ctx.setLineDash([5, 5])
ctx.beginPath()
const thresholdY = height * 0.5 // Visual representation
ctx.moveTo(0, thresholdY)
ctx.lineTo(width, thresholdY)
ctx.stroke()
ctx.setLineDash([])
// Draw playhead
if (currentTime > 0) {
const playheadX = timeToX(currentTime)
ctx.strokeStyle = "#ffffff"
ctx.lineWidth = 2
ctx.beginPath()
ctx.moveTo(playheadX, 0)
ctx.lineTo(playheadX, height)
ctx.stroke()
}
// Draw hover position
if (hoveredTime !== null && !isDragging) {
const hoverX = timeToX(hoveredTime)
ctx.strokeStyle = "rgba(255, 255, 255, 0.5)"
ctx.lineWidth = 1
ctx.beginPath()
ctx.moveTo(hoverX, 0)
ctx.lineTo(hoverX, height)
ctx.stroke()
}
// Draw time axis
ctx.fillStyle = "#ffffff"
ctx.font = "10px sans-serif"
ctx.textAlign = "center"
const tickInterval = duration > 300 ? 60 : duration > 60 ? 30 : 10
for (let t = 0; t <= duration; t += tickInterval) {
const x = timeToX(t)
const minutes = Math.floor(t / 60)
const seconds = Math.floor(t % 60)
const label = `${minutes}:${seconds.toString().padStart(2, "0")}`
ctx.fillText(label, x, height - 5)
// Tick mark
ctx.strokeStyle = "rgba(255, 255, 255, 0.3)"
ctx.beginPath()
ctx.moveTo(x, height - 20)
ctx.lineTo(x, height - 15)
ctx.stroke()
}
}, [amplitudes, duration, currentTime, segments, muteRegions, width, height, isDragging, dragStart, dragEnd, hoveredTime, timeToX])
// Handle mouse events for seeking and region selection
const handleMouseDown = (e: React.MouseEvent<HTMLCanvasElement>) => {
const canvas = canvasRef.current
if (!canvas) return
const rect = canvas.getBoundingClientRect()
const x = e.clientX - rect.left
const time = xToTime(x)
if (isSelecting) {
setIsDragging(true)
setDragStart(time)
setDragEnd(time)
} else if (onSeek) {
onSeek(time)
}
}
const handleMouseMove = (e: React.MouseEvent<HTMLCanvasElement>) => {
const canvas = canvasRef.current
if (!canvas) return
const rect = canvas.getBoundingClientRect()
const x = e.clientX - rect.left
const time = xToTime(x)
setHoveredTime(time)
if (isDragging && isSelecting) {
setDragEnd(time)
}
}
const handleMouseUp = () => {
if (isDragging && dragStart !== null && dragEnd !== null && onRegionSelect) {
const start = Math.min(dragStart, dragEnd)
const end = Math.max(dragStart, dragEnd)
// Only create region if it's at least 0.1 seconds
if (end - start >= 0.1) {
onRegionSelect({ start_seconds: start, end_seconds: end })
}
}
setIsDragging(false)
setDragStart(null)
setDragEnd(null)
}
const handleMouseLeave = () => {
setHoveredTime(null)
if (isDragging) {
handleMouseUp()
}
}
// Format time for tooltip
const formatTime = (time: number) => {
const minutes = Math.floor(time / 60)
const seconds = Math.floor(time % 60)
const ms = Math.floor((time % 1) * 100)
return `${minutes}:${seconds.toString().padStart(2, "0")}.${ms.toString().padStart(2, "0")}`
}
return (
<div className={`relative ${className}`}>
<canvas
ref={canvasRef}
width={width}
height={height}
className={`rounded-lg ${isSelecting ? "cursor-crosshair" : "cursor-pointer"}`}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseLeave}
/>
{/* Time tooltip */}
{hoveredTime !== null && (
<div
className="absolute top-0 transform -translate-x-1/2 bg-black/80 text-white text-xs px-2 py-1 rounded pointer-events-none"
style={{ left: timeToX(hoveredTime) }}
>
{formatTime(hoveredTime)}
</div>
)}
</div>
)
}
+190
View File
@@ -0,0 +1,190 @@
"use client"
import { useState } from "react"
import type { ColorOverrides } from "@/lib/video-themes"
import { Label } from "@/components/ui/label"
import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible"
import { ChevronDown, ChevronUp, Paintbrush, RotateCcw } from "lucide-react"
interface ColorOverridesProps {
value: ColorOverrides
onChange: (overrides: ColorOverrides) => void
disabled?: boolean
}
interface ColorFieldProps {
id: string
label: string
description: string
value?: string
onChange: (value: string | undefined) => void
disabled?: boolean
}
function ColorField({ id, label, description, value, onChange, disabled }: ColorFieldProps) {
return (
<div className="space-y-1">
<div className="flex items-center justify-between">
<Label htmlFor={id} className="text-sm text-slate-300">
{label}
</Label>
{value && (
<button
type="button"
onClick={() => onChange(undefined)}
className="text-xs text-slate-500 hover:text-slate-300"
disabled={disabled}
>
Reset
</button>
)}
</div>
<div className="flex items-center gap-2">
<div
className="w-8 h-8 rounded border border-slate-600 flex-shrink-0"
style={{ backgroundColor: value || "#333333" }}
/>
<Input
id={id}
type="text"
placeholder="#RRGGBB"
value={value || ""}
onChange={(e) => {
const val = e.target.value.trim()
if (val === "") {
onChange(undefined)
} else if (/^#[0-9A-Fa-f]{0,6}$/.test(val)) {
onChange(val)
}
}}
disabled={disabled}
className="bg-slate-800 border-slate-700 text-white placeholder:text-slate-500 font-mono text-sm"
/>
<Input
type="color"
value={value || "#333333"}
onChange={(e) => onChange(e.target.value)}
disabled={disabled}
className="w-10 h-8 p-0 border-0 bg-transparent cursor-pointer"
/>
</div>
<p className="text-xs text-slate-500">{description}</p>
</div>
)
}
export function ColorOverridesPanel({ value, onChange, disabled }: ColorOverridesProps) {
const [isOpen, setIsOpen] = useState(false)
const hasAnyOverrides = !!(
value.artist_color ||
value.title_color ||
value.sung_lyrics_color ||
value.unsung_lyrics_color
)
function handleResetAll() {
onChange({})
}
function updateField(field: keyof ColorOverrides, newValue: string | undefined) {
onChange({
...value,
[field]: newValue,
})
}
return (
<Collapsible open={isOpen} onOpenChange={setIsOpen}>
<CollapsibleTrigger asChild>
<Button
variant="ghost"
type="button"
className="w-full justify-between px-3 py-2 h-auto text-slate-300 hover:text-white hover:bg-slate-700/50"
>
<span className="flex items-center gap-2">
<Paintbrush className="w-4 h-4" />
Customize Colors
{hasAnyOverrides && (
<span className="text-xs bg-amber-500/20 text-amber-400 px-1.5 py-0.5 rounded">
Modified
</span>
)}
</span>
{isOpen ? (
<ChevronUp className="w-4 h-4" />
) : (
<ChevronDown className="w-4 h-4" />
)}
</Button>
</CollapsibleTrigger>
<CollapsibleContent className="mt-2">
<div className="space-y-4 p-4 rounded-lg border border-slate-700 bg-slate-800/50">
<div className="flex items-center justify-between">
<p className="text-sm text-slate-400">
Override theme colors (optional)
</p>
{hasAnyOverrides && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={handleResetAll}
disabled={disabled}
className="text-slate-400 hover:text-white"
>
<RotateCcw className="w-3 h-3 mr-1" />
Reset All
</Button>
)}
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<ColorField
id="artist-color"
label="Artist Color"
description="Color for artist name on title/end screens"
value={value.artist_color}
onChange={(v) => updateField("artist_color", v)}
disabled={disabled}
/>
<ColorField
id="title-color"
label="Title Color"
description="Color for song title on title/end screens"
value={value.title_color}
onChange={(v) => updateField("title_color", v)}
disabled={disabled}
/>
<ColorField
id="sung-lyrics-color"
label="Sung Lyrics Color"
description="Color for lyrics being sung (highlighted)"
value={value.sung_lyrics_color}
onChange={(v) => updateField("sung_lyrics_color", v)}
disabled={disabled}
/>
<ColorField
id="unsung-lyrics-color"
label="Unsung Lyrics Color"
description="Color for lyrics not yet sung"
value={value.unsung_lyrics_color}
onChange={(v) => updateField("unsung_lyrics_color", v)}
disabled={disabled}
/>
</div>
</div>
</CollapsibleContent>
</Collapsible>
)
}
@@ -1,166 +0,0 @@
"use client"
import { useState, useEffect } from "react"
import { api, InstrumentalOption } from "@/lib/api"
import { Button } from "@/components/ui/button"
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { Loader2, Music2, Play, Pause } from "lucide-react"
interface InstrumentalSelectorProps {
jobId: string
open: boolean
onClose: () => void
onSelect: () => void
}
export function InstrumentalSelector({ jobId, open, onClose, onSelect }: InstrumentalSelectorProps) {
const [options, setOptions] = useState<InstrumentalOption[]>([])
const [isLoading, setIsLoading] = useState(true)
const [isSelecting, setIsSelecting] = useState(false)
const [error, setError] = useState("")
const [playingId, setPlayingId] = useState<string | null>(null)
const [audioElements, setAudioElements] = useState<Map<string, HTMLAudioElement>>(new Map())
useEffect(() => {
if (open) {
loadOptions()
}
return () => {
// Cleanup audio elements
audioElements.forEach(audio => {
audio.pause()
audio.src = ""
})
}
}, [open])
async function loadOptions() {
setIsLoading(true)
setError("")
try {
const data = await api.getInstrumentalOptions(jobId)
setOptions(data.options || [])
} catch (err: any) {
console.error("Failed to load instrumental options:", err)
setError(err.message || "Failed to load options")
} finally {
setIsLoading(false)
}
}
async function handleSelect(selection: 'clean' | 'with_backing') {
setIsSelecting(true)
try {
await api.selectInstrumental(jobId, selection)
onSelect()
onClose()
} catch (err: any) {
console.error("Failed to select instrumental:", err)
setError(err.message || "Failed to select instrumental")
} finally {
setIsSelecting(false)
}
}
function togglePlayback(optionId: string, audioUrl: string) {
if (playingId === optionId) {
// Pause current
const audio = audioElements.get(optionId)
if (audio) {
audio.pause()
}
setPlayingId(null)
} else {
// Stop any currently playing audio
audioElements.forEach(audio => audio.pause())
// Play new audio
let audio = audioElements.get(optionId)
if (!audio) {
audio = new Audio(audioUrl)
audio.addEventListener('ended', () => setPlayingId(null))
audioElements.set(optionId, audio)
setAudioElements(new Map(audioElements))
}
audio.play()
setPlayingId(optionId)
}
}
return (
<Dialog open={open} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-2xl bg-slate-900 border-slate-700">
<DialogHeader>
<DialogTitle className="text-white">Select Instrumental Version</DialogTitle>
<DialogDescription className="text-slate-400">
Choose between a clean instrumental or one with backing vocals
</DialogDescription>
</DialogHeader>
{isLoading ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-slate-400" />
<span className="ml-2 text-slate-400">Loading options...</span>
</div>
) : error ? (
<div className="text-center py-8 text-red-400">{error}</div>
) : options.length === 0 ? (
<div className="text-center py-8 text-slate-500">
<Music2 className="w-12 h-12 mx-auto mb-3 opacity-50" />
<p>No instrumental options available</p>
</div>
) : (
<div className="space-y-3">
{options.map((option) => (
<div
key={option.id}
className="border border-slate-700 rounded-lg p-4 hover:bg-slate-800/50 transition-colors"
>
<div className="flex items-center justify-between gap-3">
<div className="flex-1">
<p className="font-medium text-white">{option.label}</p>
{option.duration_seconds && (
<p className="text-xs text-slate-400 mt-1">
Duration: {Math.floor(option.duration_seconds / 60)}:
{(option.duration_seconds % 60).toString().padStart(2, '0')}
</p>
)}
</div>
<div className="flex items-center gap-2">
{option.audio_url && (
<Button
size="sm"
variant="ghost"
onClick={() => togglePlayback(option.id, option.audio_url)}
className="text-slate-400 hover:text-white"
>
{playingId === option.id ? (
<Pause className="w-4 h-4" />
) : (
<Play className="w-4 h-4" />
)}
</Button>
)}
<Button
size="sm"
onClick={() => handleSelect(option.id as 'clean' | 'with_backing')}
disabled={isSelecting}
className="bg-pink-600 hover:bg-pink-500 text-white"
>
{isSelecting ? (
<Loader2 className="w-3 h-3 animate-spin" />
) : (
"Select"
)}
</Button>
</div>
</div>
</div>
))}
</div>
)}
</DialogContent>
</Dialog>
)
}
+1 -1
View File
@@ -19,7 +19,7 @@ export function JobActions({ job, onRefresh, showLogs, onToggleLogs }: JobAction
const [isCancelling, setIsCancelling] = useState(false)
const [isDeleting, setIsDeleting] = useState(false)
const canRetry = job.status === "failed"
const canRetry = job.status === "failed" || job.status === "cancelled"
const canCancel = !["complete", "failed", "cancelled"].includes(job.status)
const canDelete = ["complete", "failed", "cancelled"].includes(job.status)
+78 -45
View File
@@ -8,41 +8,65 @@ import { Loader2, ExternalLink, Zap } from "lucide-react"
import { JobActions } from "./JobActions"
import { JobLogs } from "./JobLogs"
import { OutputLinks } from "./OutputLinks"
import { InstrumentalSelector } from "./InstrumentalSelector"
import { AudioSearchDialog } from "../audio-search/AudioSearchDialog"
import { getJobStep, formatStepIndicator, getJobProgressPercent } from "@/lib/job-status"
// Status text styling (subtle colored text instead of badges)
const statusConfig: Record<string, { textColor: string; label: string }> = {
pending: { textColor: "text-slate-400", label: "Pending" },
searching_audio: { textColor: "text-blue-400", label: "Searching..." },
awaiting_audio_selection: { textColor: "text-amber-400", label: "Needs audio" },
downloading: { textColor: "text-blue-400", label: "Downloading..." },
downloading_audio: { textColor: "text-blue-400", label: "Downloading..." },
separating_stage1: { textColor: "text-purple-400", label: "Separating 1/2..." },
separating_stage2: { textColor: "text-purple-400", label: "Separating 2/2..." },
audio_complete: { textColor: "text-purple-400", label: "Audio ready" },
transcribing: { textColor: "text-blue-400", label: "Transcribing..." },
correcting: { textColor: "text-blue-400", label: "Correcting..." },
lyrics_complete: { textColor: "text-teal-400", label: "Lyrics ready" },
generating_screens: { textColor: "text-cyan-400", label: "Generating screens..." },
awaiting_review: { textColor: "text-amber-400", label: "Needs review" },
in_review: { textColor: "text-blue-400", label: "In review..." },
review_complete: { textColor: "text-teal-400", label: "Review complete" },
rendering_video: { textColor: "text-indigo-400", label: "Rendering..." },
awaiting_instrumental_selection: { textColor: "text-amber-400", label: "Needs instrumental" },
instrumental_selected: { textColor: "text-pink-400", label: "Instrumental selected" },
generating_video: { textColor: "text-violet-400", label: "Generating video..." },
encoding: { textColor: "text-violet-400", label: "Encoding..." },
packaging: { textColor: "text-violet-400", label: "Packaging..." },
uploading: { textColor: "text-green-400", label: "Uploading..." },
complete: { textColor: "text-green-400", label: "Complete" },
failed: { textColor: "text-red-400", label: "Failed" },
cancelled: { textColor: "text-slate-500", label: "Cancelled" },
/**
* StatusIndicator component - Shows step-based progress with visual indicator
*
* Displays: "[4/10] Processing..." with colored text and optional progress bar
*/
function StatusIndicator({ job }: { job: Job }) {
const { step, total, label, isBlocking, color } = getJobStep(job)
const formattedStatus = formatStepIndicator(step, total, label)
return (
<span className="inline-flex items-center gap-1.5">
<span className={color}>{formattedStatus}</span>
{isBlocking && (
<span className="text-amber-400 text-[10px] font-medium uppercase tracking-wide">
Action needed
</span>
)}
</span>
)
}
function StatusText({ status }: { status: string }) {
const config = statusConfig[status] || { textColor: "text-slate-400", label: status }
return <span className={config.textColor}>{config.label}</span>
/**
* Progress bar component - Visual indicator of job progress
*/
function ProgressBar({ job }: { job: Job }) {
const progressPercent = getJobProgressPercent(job)
const { step, color } = getJobStep(job)
// Don't show progress bar for terminal states
if (step === 0 || job.status === "complete" || job.status === "prep_complete") {
return null
}
// Map color class to background color for the bar
const barColorMap: Record<string, string> = {
"text-slate-400": "bg-slate-400",
"text-blue-400": "bg-blue-400",
"text-purple-400": "bg-purple-400",
"text-teal-400": "bg-teal-400",
"text-cyan-400": "bg-cyan-400",
"text-amber-400": "bg-amber-400",
"text-indigo-400": "bg-indigo-400",
"text-pink-400": "bg-pink-400",
"text-violet-400": "bg-violet-400",
"text-green-400": "bg-green-400",
}
const barColor = barColorMap[color] || "bg-blue-400"
return (
<div className="w-full h-1 bg-slate-700/50 rounded-full mt-2 overflow-hidden">
<div
className={`h-full ${barColor} rounded-full transition-all duration-500 ease-out`}
style={{ width: `${progressPercent}%` }}
/>
</div>
)
}
interface JobCardProps {
@@ -53,7 +77,6 @@ interface JobCardProps {
export function JobCard({ job, onRefresh }: JobCardProps) {
const [showLogs, setShowLogs] = useState(false)
const [showAudioSearch, setShowAudioSearch] = useState(false)
const [showInstrumentalSelector, setShowInstrumentalSelector] = useState(false)
const { enabled: autoModeEnabled, isProcessing: isAutoProcessing } = useAutoMode()
const createdAt = new Date(job.created_at).toLocaleString()
@@ -88,6 +111,18 @@ export function JobCard({ job, onRefresh }: JobCardProps) {
return url
}
// Build instrumental review URL for awaiting_instrumental_selection status
const getInstrumentalUrl = () => {
const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'https://api.nomadkaraoke.com'
const baseApiUrl = `${backendUrl}/api/jobs/${job.job_id}`
const encodedApiUrl = encodeURIComponent(baseApiUrl)
let url = `/instrumental/?baseApiUrl=${encodedApiUrl}`
if (job.instrumental_token) {
url += `&instrumentalToken=${encodeURIComponent(job.instrumental_token)}`
}
return url
}
// Render primary action button based on status
const renderPrimaryAction = () => {
if (autoModeEnabled && isInteractive) {
@@ -138,13 +173,15 @@ export function JobCard({ job, onRefresh }: JobCardProps) {
if (job.status === "awaiting_instrumental_selection") {
return (
<Button
size="sm"
onClick={() => setShowInstrumentalSelector(true)}
className="text-xs h-7 px-3 bg-blue-600 hover:bg-blue-500"
<a
href={getInstrumentalUrl()}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-xs px-3 py-1.5 rounded bg-blue-600 hover:bg-blue-500 text-white"
>
<ExternalLink className="w-3 h-3" />
Select Instrumental
</Button>
</a>
)
}
@@ -164,9 +201,12 @@ export function JobCard({ job, onRefresh }: JobCardProps) {
{/* Meta row: ID, date, status */}
<p className="text-xs text-slate-500 mt-1">
<span className="text-slate-600">ID:</span> {job.job_id} <span className="text-slate-600">•</span> {createdAt} <span className="text-slate-600">•</span> <StatusText status={job.status} />
<span className="text-slate-600">ID:</span> {job.job_id} <span className="text-slate-600">•</span> {createdAt} <span className="text-slate-600">•</span> <StatusIndicator job={job} />
</p>
{/* Progress bar for active jobs */}
<ProgressBar job={job} />
{/* Error message if any */}
{job.error_message && (
<div className="mt-2 text-xs text-red-400 bg-red-500/10 rounded p-2 break-words">
@@ -207,13 +247,6 @@ export function JobCard({ job, onRefresh }: JobCardProps) {
onClose={() => setShowAudioSearch(false)}
onSelect={onRefresh}
/>
<InstrumentalSelector
jobId={job.job_id}
open={showInstrumentalSelector}
onClose={() => setShowInstrumentalSelector(false)}
onSelect={onRefresh}
/>
</div>
)
}
+63 -3
View File
@@ -2,10 +2,14 @@
import { useState } from "react"
import { api, ApiError } from "@/lib/api"
import type { ColorOverrides } from "@/lib/video-themes"
import { cleanColorOverrides } from "@/lib/video-themes"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { ThemeSelector } from "./ThemeSelector"
import { ColorOverridesPanel } from "./ColorOverrides"
import { Upload, Youtube, Music, Loader2 } from "lucide-react"
interface JobSubmissionProps {
@@ -31,6 +35,10 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
const [searchArtist, setSearchArtist] = useState("")
const [searchTitle, setSearchTitle] = useState("")
// Theme selection (shared across all tabs)
const [selectedTheme, setSelectedTheme] = useState<string | undefined>()
const [colorOverrides, setColorOverrides] = useState<ColorOverrides>({})
async function handleUploadSubmit(e: React.FormEvent) {
e.preventDefault()
setError("")
@@ -46,7 +54,10 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
setIsSubmitting(true)
try {
await api.uploadJob(uploadFile, uploadArtist.trim(), uploadTitle.trim())
await api.uploadJob(uploadFile, uploadArtist.trim(), uploadTitle.trim(), {
theme_id: selectedTheme,
color_overrides: cleanColorOverrides(colorOverrides),
})
setUploadFile(null)
setUploadArtist("")
setUploadTitle("")
@@ -76,7 +87,11 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
await api.createJobFromUrl(
youtubeUrl.trim(),
youtubeArtist.trim() || undefined,
youtubeTitle.trim() || undefined
youtubeTitle.trim() || undefined,
{
theme_id: selectedTheme,
color_overrides: cleanColorOverrides(colorOverrides),
}
)
setYoutubeUrl("")
setYoutubeArtist("")
@@ -104,7 +119,10 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
setIsSubmitting(true)
try {
await api.searchAudio(searchArtist.trim(), searchTitle.trim())
await api.searchAudio(searchArtist.trim(), searchTitle.trim(), false, {
theme_id: selectedTheme,
color_overrides: cleanColorOverrides(colorOverrides),
})
setSearchArtist("")
setSearchTitle("")
onJobCreated()
@@ -200,6 +218,20 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
</div>
</div>
{/* Theme Selection */}
<div className="space-y-4 pt-4 border-t border-slate-700">
<ThemeSelector
value={selectedTheme}
onChange={setSelectedTheme}
disabled={isSubmitting}
/>
<ColorOverridesPanel
value={colorOverrides}
onChange={setColorOverrides}
disabled={isSubmitting}
/>
</div>
{error && activeTab === "upload" && (
<p className="text-sm text-red-400 bg-red-500/10 rounded p-2">{error}</p>
)}
@@ -264,6 +296,20 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
</div>
</div>
{/* Theme Selection */}
<div className="space-y-4 pt-4 border-t border-slate-700">
<ThemeSelector
value={selectedTheme}
onChange={setSelectedTheme}
disabled={isSubmitting}
/>
<ColorOverridesPanel
value={colorOverrides}
onChange={setColorOverrides}
disabled={isSubmitting}
/>
</div>
{error && activeTab === "url" && (
<p className="text-sm text-red-400 bg-red-500/10 rounded p-2">{error}</p>
)}
@@ -312,6 +358,20 @@ export function JobSubmission({ onJobCreated }: JobSubmissionProps) {
</div>
</div>
{/* Theme Selection */}
<div className="space-y-4 pt-4 border-t border-slate-700">
<ThemeSelector
value={selectedTheme}
onChange={setSelectedTheme}
disabled={isSubmitting}
/>
<ColorOverridesPanel
value={colorOverrides}
onChange={setColorOverrides}
disabled={isSubmitting}
/>
</div>
{error && activeTab === "search" && (
<p className="text-sm text-red-400 bg-red-500/10 rounded p-2">{error}</p>
)}
+133
View File
@@ -0,0 +1,133 @@
"use client"
import { useState, useEffect } from "react"
import { api } from "@/lib/api"
import type { VideoThemeSummary } from "@/lib/video-themes"
import { Label } from "@/components/ui/label"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { Skeleton } from "@/components/ui/skeleton"
import { Palette, AlertCircle } from "lucide-react"
interface ThemeSelectorProps {
value?: string
onChange: (themeId: string | undefined) => void
disabled?: boolean
}
export function ThemeSelector({ value, onChange, disabled }: ThemeSelectorProps) {
const [themes, setThemes] = useState<VideoThemeSummary[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
useEffect(() => {
async function loadThemes() {
try {
setLoading(true)
setError(null)
const themeList = await api.listThemes()
setThemes(themeList)
// If no value is set and there's a default theme, select it
if (!value && themeList.length > 0) {
const defaultTheme = themeList.find(t => t.is_default)
if (defaultTheme) {
onChange(defaultTheme.id)
} else {
onChange(themeList[0].id)
}
}
} catch (err) {
console.error("Failed to load themes:", err)
setError("Failed to load themes")
} finally {
setLoading(false)
}
}
loadThemes()
}, []) // Only run once on mount
const selectedTheme = themes.find(t => t.id === value)
if (loading) {
return (
<div className="space-y-2">
<Label className="text-slate-200 flex items-center gap-2">
<Palette className="w-4 h-4" />
Video Theme
</Label>
<Skeleton className="h-10 w-full bg-slate-700" />
</div>
)
}
if (error || themes.length === 0) {
return (
<div className="space-y-2">
<Label className="text-slate-200 flex items-center gap-2">
<Palette className="w-4 h-4" />
Video Theme
</Label>
<div className="flex items-center gap-2 text-sm text-amber-400 bg-amber-500/10 rounded p-2">
<AlertCircle className="w-4 h-4" />
{error || "No themes available"}
</div>
</div>
)
}
return (
<div className="space-y-2">
<Label htmlFor="theme-select" className="text-slate-200 flex items-center gap-2">
<Palette className="w-4 h-4" />
Video Theme
</Label>
<Select
value={value || ""}
onValueChange={(val) => onChange(val || undefined)}
disabled={disabled}
>
<SelectTrigger
id="theme-select"
className="bg-slate-800 border-slate-700 text-white"
>
<SelectValue placeholder="Select a theme..." />
</SelectTrigger>
<SelectContent className="bg-slate-800 border-slate-700">
{themes.map((theme) => (
<SelectItem
key={theme.id}
value={theme.id}
className="text-white hover:bg-slate-700 focus:bg-slate-700"
>
<div className="flex items-center gap-2">
<span>{theme.name}</span>
{theme.is_default && (
<span className="text-xs text-amber-400">(Default)</span>
)}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
{/* Theme description and preview */}
{selectedTheme && (
<div className="mt-2 space-y-2">
<p className="text-sm text-slate-400">{selectedTheme.description}</p>
{selectedTheme.thumbnail_url && (
<div className="relative w-full aspect-video max-w-xs rounded-lg overflow-hidden border border-slate-700">
<img
src={selectedTheme.thumbnail_url}
alt={`${selectedTheme.name} preview`}
className="w-full h-full object-cover"
/>
</div>
)}
</div>
)}
</div>
)
}
+1 -2
View File
@@ -1,6 +1,5 @@
export { JobCard, StatusBadge } from "./JobCard"
export { JobCard } from "./JobCard"
export { JobActions } from "./JobActions"
export { JobLogs } from "./JobLogs"
export { OutputLinks } from "./OutputLinks"
export { InstrumentalSelector } from "./InstrumentalSelector"
+318
View File
@@ -0,0 +1,318 @@
import { test, expect, Page } from '@playwright/test';
/**
* Production E2E test - Single complete karaoke generation flow.
*
* This test runs against gen.nomadkaraoke.com and creates a REAL karaoke video.
*
* Run with:
* npx playwright test --config=playwright.production.config.ts --headed
*
* The test follows the complete user journey:
* 1. Select a theme
* 2. Enter artist/title and search
* 3. Select an audio source
* 4. Wait for processing
* 5. Complete lyrics review
* 6. Select instrumental
* 7. Wait for final render
* 8. Verify completion
*/
const ACCESS_TOKEN = process.env.KARAOKE_ACCESS_TOKEN;
const PROD_URL = 'https://gen.nomadkaraoke.com';
const API_URL = 'https://api.nomadkaraoke.com';
// Real song - uses cached flacfetch results
const TEST_ARTIST = 'piri';
const TEST_TITLE = 'dog';
async function authenticatePage(page: Page) {
if (!ACCESS_TOKEN) {
throw new Error('KARAOKE_ACCESS_TOKEN environment variable is required');
}
await page.addInitScript((token) => {
localStorage.setItem('karaoke_access_token', token);
}, ACCESS_TOKEN);
}
test.describe('Production E2E', () => {
test('Complete karaoke generation flow', async ({ page, request }) => {
test.setTimeout(900000); // 15 minutes
await authenticatePage(page);
// ========== STEP 1: Go to homepage ==========
console.log('\n=== STEP 1: Loading homepage ===');
await page.goto(PROD_URL);
await page.waitForLoadState('networkidle');
await expect(page.locator('h1')).toContainText('Karaoke Generator');
console.log('Homepage loaded');
// ========== STEP 2: Go to Search tab and select theme ==========
console.log('\n=== STEP 2: Search tab + theme selection ===');
await page.getByRole('tab', { name: /search/i }).click();
await page.waitForTimeout(1000);
// Select theme (first one in dropdown)
const themeSelect = page.locator('button[role="combobox"]').first();
if (await themeSelect.isVisible({ timeout: 3000 })) {
const currentTheme = await themeSelect.textContent();
console.log(`Current theme: ${currentTheme}`);
if (currentTheme?.toLowerCase().includes('select')) {
await themeSelect.click();
await page.waitForTimeout(500);
await page.locator('[role="option"]').first().click();
await page.waitForTimeout(500);
console.log('Selected first theme');
}
}
// ========== STEP 3: Fill in artist/title and search ==========
console.log('\n=== STEP 3: Filling form and searching ===');
await page.getByLabel('Artist').fill(TEST_ARTIST);
await page.getByLabel('Title').fill(TEST_TITLE);
console.log(`Searching for: ${TEST_ARTIST} - ${TEST_TITLE}`);
await page.screenshot({ path: 'test-results/step3-before-search.png' });
await page.getByRole('button', { name: /search.*create/i }).click();
// Wait for search to complete (loading spinner -> results)
console.log('Waiting for search results...');
await page.waitForTimeout(10000);
await page.screenshot({ path: 'test-results/step3-after-search.png' });
// ========== STEP 4: Select audio source ==========
console.log('\n=== STEP 4: Selecting audio source ===');
// The search creates a job and shows audio options
// Look for "Select Audio" button on the job card
const selectAudioBtn = page.getByRole('button', { name: /select audio/i }).first();
if (await selectAudioBtn.isVisible({ timeout: 30000 })) {
console.log('Found "Select Audio" button, clicking...');
await selectAudioBtn.click();
await page.waitForTimeout(2000);
// A dialog should open with audio options
const dialog = page.locator('[role="dialog"]');
if (await dialog.isVisible({ timeout: 5000 })) {
await page.screenshot({ path: 'test-results/step4-audio-dialog.png' });
// Find all "Select" buttons in the dialog
const selectButtons = dialog.getByRole('button', { name: /^select$/i });
const count = await selectButtons.count();
console.log(`Found ${count} audio options`);
if (count > 0) {
console.log('Clicking first audio option...');
await selectButtons.first().click();
// Wait for dialog to close and download to start
await page.waitForTimeout(5000);
console.log('Audio selected, download should start');
} else {
console.log('ERROR: No Select buttons found in dialog');
const dialogContent = await dialog.textContent();
console.log('Dialog content:', dialogContent?.substring(0, 500));
}
} else {
console.log('ERROR: Dialog did not open');
}
} else {
console.log('ERROR: "Select Audio" button not found');
// Maybe job auto-started? Check job status
const pageContent = await page.locator('body').textContent();
console.log('Page contains:', pageContent?.substring(0, 500));
}
await page.screenshot({ path: 'test-results/step4-after-audio-select.png' });
// ========== STEP 5: Wait for processing and lyrics review ==========
console.log('\n=== STEP 5: Waiting for job to reach review stage ===');
// Poll the jobs API to check status
let jobId: string | null = null;
let jobStatus: string | null = null;
for (let i = 0; i < 60; i++) { // 5 minutes of polling
const response = await request.get(`${API_URL}/api/jobs`, {
headers: { 'Authorization': `Bearer ${ACCESS_TOKEN}` },
timeout: 120000 // 2 minute timeout for API calls
});
if (response.ok()) {
const data = await response.json();
const jobs = data.jobs || data;
// Find our job
const ourJob = Array.isArray(jobs)
? jobs.find((j: any) =>
j.artist?.toLowerCase() === TEST_ARTIST.toLowerCase() &&
j.title?.toLowerCase() === TEST_TITLE.toLowerCase())
: null;
if (ourJob) {
jobId = ourJob.job_id;
jobStatus = ourJob.status;
console.log(`Job ${jobId}: ${jobStatus}`);
// Check if we've reached a milestone
if (jobStatus === 'in_review') {
console.log('Job reached lyrics review stage!');
break;
} else if (jobStatus === 'awaiting_instrumental_selection') {
console.log('Job skipped review, at instrumental selection');
break;
} else if (jobStatus === 'completed') {
console.log('Job already completed!');
break;
} else if (jobStatus === 'failed') {
throw new Error(`Job failed: ${ourJob.error_message || 'Unknown error'}`);
}
}
}
// Refresh the page periodically
if (i % 6 === 0) {
await page.getByRole('button', { name: /refresh/i }).click().catch(() => {});
}
await page.waitForTimeout(5000);
}
if (!jobId) {
throw new Error('Could not find our job after 5 minutes');
}
await page.screenshot({ path: 'test-results/step5-job-status.png' });
// ========== STEP 6: Complete lyrics review (if needed) ==========
if (jobStatus === 'in_review') {
console.log('\n=== STEP 6: Completing lyrics review ===');
await page.reload();
await page.waitForLoadState('networkidle');
const reviewBtn = page.getByRole('button', { name: /review.*lyrics/i }).first();
if (await reviewBtn.isVisible({ timeout: 10000 })) {
console.log('Opening lyrics review...');
// This opens a new tab
const [popup] = await Promise.all([
page.context().waitForEvent('page', { timeout: 15000 }).catch(() => null),
reviewBtn.click()
]);
if (popup) {
await popup.waitForLoadState('networkidle');
await popup.waitForTimeout(5000);
await popup.screenshot({ path: 'test-results/step6-review-ui.png', fullPage: true });
// Click Preview Video
const previewBtn = popup.getByRole('button', { name: /preview.*video/i }).first();
if (await previewBtn.isVisible({ timeout: 5000 })) {
console.log('Clicking Preview Video...');
await previewBtn.click();
await popup.waitForTimeout(30000); // Wait for preview to generate
// Click Complete Review
const completeBtn = popup.getByRole('button', { name: /complete.*review/i }).first();
if (await completeBtn.isVisible({ timeout: 60000 })) {
console.log('Clicking Complete Review...');
await completeBtn.click();
await popup.waitForTimeout(3000);
}
}
if (!popup.isClosed()) {
await popup.close();
}
}
}
}
// ========== STEP 7: Handle instrumental selection (if needed) ==========
console.log('\n=== STEP 7: Checking for instrumental selection ===');
// Wait and check status
for (let i = 0; i < 12; i++) { // 1 minute
const response = await request.get(`${API_URL}/api/jobs/${jobId}`, {
headers: { 'Authorization': `Bearer ${ACCESS_TOKEN}` },
timeout: 120000
});
if (response.ok()) {
const job = await response.json();
console.log(`Job status: ${job.status}`);
if (job.status === 'awaiting_instrumental_selection') {
console.log('Handling instrumental selection...');
await page.reload();
await page.waitForLoadState('networkidle');
const instrumentalBtn = page.getByRole('button', { name: /select.*instrumental/i }).first();
if (await instrumentalBtn.isVisible({ timeout: 10000 })) {
await instrumentalBtn.click();
await page.waitForTimeout(2000);
const dialog = page.locator('[role="dialog"]');
if (await dialog.isVisible()) {
await page.screenshot({ path: 'test-results/step7-instrumental.png' });
// Select clean instrumental
const cleanBtn = dialog.getByRole('button').first();
await cleanBtn.click();
await page.waitForTimeout(3000);
console.log('Selected instrumental');
}
}
break;
} else if (job.status === 'completed' || job.status === 'processing') {
break;
}
}
await page.waitForTimeout(5000);
}
// ========== STEP 8: Wait for completion ==========
console.log('\n=== STEP 8: Waiting for completion ===');
for (let i = 0; i < 60; i++) { // 5 minutes
const response = await request.get(`${API_URL}/api/jobs/${jobId}`, {
headers: { 'Authorization': `Bearer ${ACCESS_TOKEN}` },
timeout: 120000
});
if (response.ok()) {
const job = await response.json();
console.log(`Job status: ${job.status}`);
if (job.status === 'completed') {
console.log('\n🎉 JOB COMPLETED SUCCESSFULLY!');
console.log(`Theme ID: ${job.theme_id}`);
console.log(`YouTube URL: ${job.youtube_url || 'N/A'}`);
console.log(`Dropbox URL: ${job.dropbox_url || 'N/A'}`);
// Final verification
expect(job.theme_id).toBeTruthy();
await page.reload();
await page.screenshot({ path: 'test-results/step8-completed.png', fullPage: true });
return; // SUCCESS
} else if (job.status === 'failed') {
throw new Error(`Job failed: ${job.error_message || 'Unknown error'}`);
}
}
await page.waitForTimeout(5000);
}
throw new Error('Job did not complete within timeout');
});
});
+74 -7
View File
@@ -2,6 +2,8 @@
* API client for karaoke-gen backend
*/
import type { VideoThemeSummary, VideoThemeDetail, ThemesListResponse, ThemeDetailResponse, ColorOverrides } from './video-themes';
// In development, use relative URLs to go through Next.js proxy (avoids CORS)
// In production (static export), use the full backend URL
const API_BASE_URL = typeof window !== 'undefined' && window.location.hostname === 'localhost'
@@ -61,6 +63,7 @@ export interface Job {
message?: string;
}>;
review_token?: string;
instrumental_token?: string;
audio_hash?: string;
}
@@ -220,13 +223,15 @@ export const api = {
enable_txt?: boolean;
brand_prefix?: string;
enable_youtube_upload?: boolean;
theme_id?: string;
color_overrides?: ColorOverrides;
}
): Promise<UploadJobResponse> {
const formData = new FormData();
formData.append('file', file);
formData.append('artist', artist);
formData.append('title', title);
if (options?.enable_cdg !== undefined) {
formData.append('enable_cdg', String(options.enable_cdg));
}
@@ -239,13 +244,19 @@ export const api = {
if (options?.enable_youtube_upload !== undefined) {
formData.append('enable_youtube_upload', String(options.enable_youtube_upload));
}
if (options?.theme_id) {
formData.append('theme_id', options.theme_id);
}
if (options?.color_overrides) {
formData.append('color_overrides', JSON.stringify(options.color_overrides));
}
const response = await fetch(`${API_BASE_URL}/api/jobs/upload`, {
method: 'POST',
headers: getAuthHeaders(),
body: formData,
});
return handleResponse<UploadJobResponse>(response);
},
@@ -261,6 +272,8 @@ export const api = {
enable_txt?: boolean;
brand_prefix?: string;
enable_youtube_upload?: boolean;
theme_id?: string;
color_overrides?: ColorOverrides;
}
): Promise<{ status: string; job_id: string; message: string }> {
const body: Record<string, any> = { url };
@@ -270,7 +283,9 @@ export const api = {
if (options?.enable_txt !== undefined) body.enable_txt = options.enable_txt;
if (options?.brand_prefix) body.brand_prefix = options.brand_prefix;
if (options?.enable_youtube_upload !== undefined) body.enable_youtube_upload = options.enable_youtube_upload;
if (options?.theme_id) body.theme_id = options.theme_id;
if (options?.color_overrides) body.color_overrides = options.color_overrides;
const response = await fetch(`${API_BASE_URL}/api/jobs/create-from-url`, {
method: 'POST',
headers: {
@@ -279,7 +294,7 @@ export const api = {
},
body: JSON.stringify(body),
});
return handleResponse(response);
},
@@ -387,6 +402,8 @@ export const api = {
enable_txt?: boolean;
brand_prefix?: string;
enable_youtube_upload?: boolean;
theme_id?: string;
color_overrides?: ColorOverrides;
}
): Promise<AudioSearchResponse> {
const body: Record<string, any> = { artist, title, auto_download: autoDownload };
@@ -394,7 +411,9 @@ export const api = {
if (options?.enable_txt !== undefined) body.enable_txt = options.enable_txt;
if (options?.brand_prefix) body.brand_prefix = options.brand_prefix;
if (options?.enable_youtube_upload !== undefined) body.enable_youtube_upload = options.enable_youtube_upload;
if (options?.theme_id) body.theme_id = options.theme_id;
if (options?.color_overrides) body.color_overrides = options.color_overrides;
const response = await fetch(`${API_BASE_URL}/api/audio-search/search`, {
method: 'POST',
headers: {
@@ -403,7 +422,7 @@ export const api = {
},
body: JSON.stringify(body),
});
return handleResponse(response);
},
@@ -450,6 +469,54 @@ export const api = {
});
return handleResponse(response);
},
// ==========================================================================
// Theme API endpoints
// ==========================================================================
/**
* List all available video themes
*/
async listThemes(): Promise<VideoThemeSummary[]> {
const response = await fetch(`${API_BASE_URL}/api/themes`, {
headers: getAuthHeaders()
});
const data = await handleResponse<ThemesListResponse>(response);
return data.themes;
},
/**
* Get full details for a specific theme
*/
async getTheme(themeId: string): Promise<VideoThemeDetail> {
const response = await fetch(`${API_BASE_URL}/api/themes/${themeId}`, {
headers: getAuthHeaders()
});
const data = await handleResponse<ThemeDetailResponse>(response);
return data.theme;
},
/**
* Get preview URL for a theme
*/
async getThemePreview(themeId: string): Promise<string | null> {
const response = await fetch(`${API_BASE_URL}/api/themes/${themeId}/preview`, {
headers: getAuthHeaders()
});
const data = await handleResponse<{ preview_url: string | null }>(response);
return data.preview_url;
},
/**
* Get YouTube description template for a theme
*/
async getThemeYoutubeDescription(themeId: string): Promise<string | null> {
const response = await fetch(`${API_BASE_URL}/api/themes/${themeId}/youtube-description`, {
headers: getAuthHeaders()
});
const data = await handleResponse<{ description: string | null }>(response);
return data.description;
},
};
export { ApiError };
+266
View File
@@ -0,0 +1,266 @@
/**
* Job Status Display Utilities
*
* Maps backend job statuses to user-friendly step-based progress indicators.
* The 10-step system simplifies 28+ backend statuses for better user comprehension.
*/
import type { Job } from "./api";
export interface JobStep {
step: number;
total: number;
label: string;
isBlocking: boolean;
color: string;
}
/**
* Status-to-step mapping configuration.
*
* Steps are organized as:
* 1. Setup
* 2. Audio Search (optional)
* 3. Download
* 4. Processing (Audio + Lyrics in parallel)
* 5. Screen Generation
* 6. Lyrics Review (BLOCKING)
* 7. Video Rendering
* 8. Instrumental Selection (BLOCKING)
* 9. Final Encoding
* 10. Distribution / Complete
*/
const STATUS_CONFIG: Record<
string,
{ step: number; label: string; isBlocking: boolean; color: string }
> = {
// Step 1: Setup
pending: { step: 1, label: "Setting up", isBlocking: false, color: "text-slate-400" },
// Step 2: Audio Search (optional path)
searching_audio: { step: 2, label: "Searching for audio", isBlocking: false, color: "text-blue-400" },
awaiting_audio_selection: { step: 2, label: "Select audio source", isBlocking: true, color: "text-amber-400" },
// Step 3: Download
downloading_audio: { step: 3, label: "Downloading audio", isBlocking: false, color: "text-blue-400" },
downloading: { step: 3, label: "Downloading", isBlocking: false, color: "text-blue-400" },
// Step 4: Parallel Processing (Audio + Lyrics)
separating_stage1: { step: 4, label: "Separating audio (1/2)", isBlocking: false, color: "text-purple-400" },
separating_stage2: { step: 4, label: "Separating audio (2/2)", isBlocking: false, color: "text-purple-400" },
audio_complete: { step: 4, label: "Audio ready, processing lyrics", isBlocking: false, color: "text-purple-400" },
transcribing: { step: 4, label: "Transcribing lyrics", isBlocking: false, color: "text-blue-400" },
correcting: { step: 4, label: "Correcting lyrics", isBlocking: false, color: "text-blue-400" },
lyrics_complete: { step: 4, label: "Lyrics ready, processing audio", isBlocking: false, color: "text-teal-400" },
// Step 5: Screen Generation
generating_screens: { step: 5, label: "Generating screens", isBlocking: false, color: "text-cyan-400" },
applying_padding: { step: 5, label: "Syncing countdown", isBlocking: false, color: "text-cyan-400" },
// Step 6: Review (BLOCKING - requires user action)
awaiting_review: { step: 6, label: "Review lyrics", isBlocking: true, color: "text-amber-400" },
in_review: { step: 6, label: "In review", isBlocking: true, color: "text-blue-400" },
// Step 7: Video Rendering
review_complete: { step: 7, label: "Starting render", isBlocking: false, color: "text-teal-400" },
rendering_video: { step: 7, label: "Rendering video", isBlocking: false, color: "text-indigo-400" },
// Step 8: Instrumental Selection (BLOCKING - requires user action)
awaiting_instrumental_selection: { step: 8, label: "Select instrumental", isBlocking: true, color: "text-amber-400" },
// Step 9: Final Encoding
instrumental_selected: { step: 9, label: "Starting final encode", isBlocking: false, color: "text-pink-400" },
generating_video: { step: 9, label: "Generating final video", isBlocking: false, color: "text-violet-400" },
encoding: { step: 9, label: "Encoding video", isBlocking: false, color: "text-violet-400" },
packaging: { step: 9, label: "Packaging files", isBlocking: false, color: "text-violet-400" },
// Step 10: Distribution / Complete
uploading: { step: 10, label: "Uploading", isBlocking: false, color: "text-green-400" },
notifying: { step: 10, label: "Sending notifications", isBlocking: false, color: "text-green-400" },
complete: { step: 10, label: "Complete", isBlocking: false, color: "text-green-400" },
prep_complete: { step: 10, label: "Prep complete", isBlocking: false, color: "text-green-400" },
// Terminal states (no step progression)
failed: { step: 0, label: "Failed", isBlocking: false, color: "text-red-400" },
cancelled: { step: 0, label: "Cancelled", isBlocking: false, color: "text-slate-500" },
};
const TOTAL_STEPS = 10;
/**
* Check if parallel workers (audio/lyrics) are actively running.
* This is indicated by state_data containing audio_progress or lyrics_progress.
*/
function isParallelProcessingActive(job: Job): boolean {
if (!job.state_data) return false;
const audioProgress = job.state_data.audio_progress as { stage?: string } | undefined;
const lyricsProgress = job.state_data.lyrics_progress as { stage?: string } | undefined;
// Workers are active if we have progress data with a stage
return !!(audioProgress?.stage || lyricsProgress?.stage);
}
/**
* Get the step information for a job based on its status.
*
* @param job - The job object with status and optional state_data
* @returns Step information including step number, label, and display properties
*/
export function getJobStep(job: Job): JobStep {
const status = job.status?.toLowerCase() || "pending";
const config = STATUS_CONFIG[status];
if (!config) {
// Unknown status - show generic processing state
return {
step: 0,
total: TOTAL_STEPS,
label: status.replace(/_/g, " "),
isBlocking: false,
color: "text-slate-400",
};
}
// Special case: "downloading" status but parallel workers are actually running.
// The backend sets status to "downloading" when audio download completes and workers start,
// but doesn't update to step 4 statuses until screens_worker runs.
// Show step 4 with detailed progress to avoid appearing "stuck" at downloading.
if (status === "downloading" && isParallelProcessingActive(job)) {
const enhancedLabel = getParallelProcessingLabel(job, "Processing");
return {
step: 4,
total: TOTAL_STEPS,
label: enhancedLabel,
isBlocking: false,
color: "text-purple-400", // Same as step 4 processing color
};
}
// During parallel processing (step 4), check state_data for more detail
if (config.step === 4 && job.state_data) {
const enhancedLabel = getParallelProcessingLabel(job, config.label);
return {
step: config.step,
total: TOTAL_STEPS,
label: enhancedLabel,
isBlocking: config.isBlocking,
color: config.color,
};
}
return {
step: config.step,
total: TOTAL_STEPS,
label: config.label,
isBlocking: config.isBlocking,
color: config.color,
};
}
/**
* Get enhanced label for parallel processing stage.
* Combines audio and lyrics progress when both are available.
*/
function getParallelProcessingLabel(job: Job, defaultLabel: string): string {
const audioProgress = job.state_data?.audio_progress as
| { stage?: string; message?: string }
| undefined;
const lyricsProgress = job.state_data?.lyrics_progress as
| { stage?: string; message?: string }
| undefined;
// If we have both progress indicators, show combined status
if (audioProgress && lyricsProgress) {
const audioStage = audioProgress.stage || "";
const lyricsStage = lyricsProgress.stage || "";
const audioDone = audioStage === "audio_complete" || job.state_data?.audio_complete;
const lyricsDone = lyricsStage === "lyrics_complete" || job.state_data?.lyrics_complete;
if (audioDone && lyricsDone) {
return "Processing complete";
}
// Show what's still running
const parts: string[] = [];
if (!audioDone) {
parts.push(getShortAudioStatus(audioStage));
}
if (!lyricsDone) {
parts.push(getShortLyricsStatus(lyricsStage));
}
if (parts.length > 0) {
return parts.join(" + ");
}
}
return defaultLabel;
}
function getShortAudioStatus(stage: string): string {
switch (stage) {
case "separating_stage1":
return "Audio 1/2";
case "separating_stage2":
return "Audio 2/2";
case "audio_complete":
return "Audio done";
default:
return "Audio";
}
}
function getShortLyricsStatus(stage: string): string {
switch (stage) {
case "transcribing":
return "Transcribing";
case "correcting":
return "Correcting";
case "lyrics_complete":
return "Lyrics done";
default:
return "Lyrics";
}
}
/**
* Format a step indicator string like "[4/10] Processing..."
*
* @param step - Current step number (0 for terminal states)
* @param total - Total number of steps
* @param label - Human-readable label
* @returns Formatted string
*/
export function formatStepIndicator(step: number, total: number, label: string): string {
if (step === 0) {
// Terminal states (failed, cancelled) don't show step numbers
return label;
}
return `[${step}/${total}] ${label}`;
}
/**
* Check if a job status requires user action.
*
* @param status - The job status string
* @returns true if the status is a blocking state requiring user action
*/
export function isBlockingStatus(status: string): boolean {
const config = STATUS_CONFIG[status?.toLowerCase()];
return config?.isBlocking ?? false;
}
/**
* Get the progress percentage for a job (0-100).
* This is based on step progression, not the backend progress field.
*
* @param job - The job object
* @returns Progress percentage (0-100)
*/
export function getJobProgressPercent(job: Job): number {
const { step, total } = getJobStep(job);
if (step === 0 || total === 0) return 0;
return Math.round((step / total) * 100);
}
+113
View File
@@ -0,0 +1,113 @@
/**
* Video theme types and utilities for karaoke video styles.
*
* These are pre-made style configurations stored in GCS that control
* the visual appearance of generated karaoke videos.
*/
/**
* Summary of a theme for listing in the theme selector.
*/
export interface VideoThemeSummary {
id: string;
name: string;
description: string;
preview_url: string | null;
thumbnail_url: string | null;
is_default: boolean;
}
/**
* Full theme details including style parameters.
*/
export interface VideoThemeDetail {
id: string;
name: string;
description: string;
preview_url: string | null;
is_default: boolean;
style_params: Record<string, any>;
has_youtube_description: boolean;
}
/**
* Response from GET /api/themes
*/
export interface ThemesListResponse {
themes: VideoThemeSummary[];
}
/**
* Response from GET /api/themes/{theme_id}
*/
export interface ThemeDetailResponse {
theme: VideoThemeDetail;
}
/**
* User color overrides for customizing a theme.
* All colors are hex format (#RRGGBB).
*/
export interface ColorOverrides {
/** Color for artist name on intro/end screens */
artist_color?: string;
/** Color for song title on intro/end screens */
title_color?: string;
/** Color for lyrics being sung (highlighted) */
sung_lyrics_color?: string;
/** Color for lyrics not yet sung */
unsung_lyrics_color?: string;
}
/**
* Check if color overrides has any values set.
*/
export function hasColorOverrides(overrides?: ColorOverrides): boolean {
if (!overrides) return false;
return !!(
overrides.artist_color ||
overrides.title_color ||
overrides.sung_lyrics_color ||
overrides.unsung_lyrics_color
);
}
/**
* Get clean color overrides object (only non-null values).
*/
export function cleanColorOverrides(overrides?: ColorOverrides): ColorOverrides | undefined {
if (!overrides) return undefined;
const clean: ColorOverrides = {};
if (overrides.artist_color) clean.artist_color = overrides.artist_color;
if (overrides.title_color) clean.title_color = overrides.title_color;
if (overrides.sung_lyrics_color) clean.sung_lyrics_color = overrides.sung_lyrics_color;
if (overrides.unsung_lyrics_color) clean.unsung_lyrics_color = overrides.unsung_lyrics_color;
return Object.keys(clean).length > 0 ? clean : undefined;
}
/**
* Validate hex color format.
*/
export function isValidHexColor(color: string): boolean {
return /^#[0-9A-Fa-f]{6}$/.test(color);
}
/**
* Default color override values based on common themes.
*/
export const DEFAULT_COLOR_PRESETS = {
nomad: {
artist_color: "#ffdf6b",
title_color: "#ffffff",
sung_lyrics_color: "#7070F7",
unsung_lyrics_color: "#ffffff",
},
classic: {
artist_color: "#ffffff",
title_color: "#ffffff",
sung_lyrics_color: "#00ff00",
unsung_lyrics_color: "#ffffff",
},
} as const;
+3
View File
@@ -18,6 +18,9 @@
"test:e2e:debug": "playwright test --debug",
"test:e2e:headed": "playwright test --headed",
"test:e2e:record": "RECORD_FIXTURES=true playwright test",
"test:e2e:prod": "playwright test --config=playwright.production.config.ts",
"test:e2e:prod:headed": "playwright test --config=playwright.production.config.ts --headed",
"test:e2e:prod:debug": "playwright test --config=playwright.production.config.ts --debug",
"test:all": "npm run test:unit && npm run test:e2e",
"fixtures:review": "npx ts-node e2e/fixtures/review-cli.ts"
},
+2 -2
View File
@@ -56,8 +56,8 @@ export default defineConfig({
// Screenshot on failure
screenshot: 'only-on-failure',
// Video on failure (helpful for debugging flows)
video: 'on-first-retry',
// Video recording (set to 'on' to always record, 'on-first-retry' for failures only)
video: 'on',
// Increase timeout for real API calls
actionTimeout: 30000,
+93
View File
@@ -0,0 +1,93 @@
import { defineConfig, devices } from '@playwright/test';
import * as fs from 'fs';
import * as path from 'path';
// Load .env.local file if it exists (for KARAOKE_ACCESS_TOKEN)
const envLocalPath = path.join(__dirname, '.env.local');
if (fs.existsSync(envLocalPath)) {
const envContent = fs.readFileSync(envLocalPath, 'utf-8');
for (const line of envContent.split('\n')) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const [key, ...valueParts] = trimmed.split('=');
const value = valueParts.join('=');
if (key && value && !process.env[key]) {
process.env[key] = value;
}
}
}
}
/**
* Playwright configuration for PRODUCTION E2E tests.
*
* Tests run directly against gen.nomadkaraoke.com (no local dev server).
* Used for testing the real production system end-to-end.
*
* Usage:
* npx playwright test --config=playwright.production.config.ts
* npm run test:e2e:prod
*/
export default defineConfig({
testDir: './e2e',
testMatch: '**/production-*.spec.ts',
// Run tests sequentially - we're testing real production
fullyParallel: false,
// Fail the build on CI if you accidentally left test.only in the source code
forbidOnly: !!process.env.CI,
// Retry failed tests in production (flakiness can happen)
retries: 2,
// Single worker - we're testing against real production services
workers: 1,
// Reporter to use
reporter: [
['html', { open: 'never', outputFolder: 'playwright-report-prod' }],
['list'],
],
// Shared settings for all tests
use: {
// Base URL for production
baseURL: 'https://gen.nomadkaraoke.com',
// Collect trace always for debugging production issues
trace: 'on',
// Always capture screenshots
screenshot: 'on',
// Always capture video for production debugging
video: 'on',
// Longer timeout for production API calls
actionTimeout: 60000,
// Log network requests
extraHTTPHeaders: {
'x-playwright-test': 'production',
},
},
// Much longer timeout for production tests (karaoke generation takes time!)
timeout: 900000, // 15 minutes per test
// Expect timeout for assertions
expect: {
timeout: 60000, // 60 seconds for expects
},
// Configure projects (browsers)
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
// No webServer needed - we're testing production directly
});
File diff suppressed because it is too large. Load diff
+84
View File
@@ -287,6 +287,87 @@ github_actions_iap_tunnel = gcp.projects.IAMMember(
member=github_actions_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# ==================== Claude Code Automation Service Account ====================
# This service account provides long-lived read-only access for Claude Code CLI
# to monitor deployments, view logs, and check build status without requiring
# frequent OAuth re-authentication.
claude_automation_sa = serviceaccount.Account(
"claude-automation-sa",
account_id="claude-automation",
display_name="Claude Code Automation",
description="Service account for Claude Code CLI with read-only GCP access",
)
# Grant Cloud Run viewer permissions (view services, revisions, status)
claude_run_viewer = gcp.projects.IAMMember(
"claude-automation-run-viewer",
project=project_id,
role="roles/run.viewer",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Grant Logging viewer permissions (read logs)
claude_logging_viewer = gcp.projects.IAMMember(
"claude-automation-logging-viewer",
project=project_id,
role="roles/logging.viewer",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Grant Monitoring viewer permissions (read metrics, dashboards)
claude_monitoring_viewer = gcp.projects.IAMMember(
"claude-automation-monitoring-viewer",
project=project_id,
role="roles/monitoring.viewer",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Grant Cloud Build viewer permissions (view build status, logs)
claude_cloudbuild_viewer = gcp.projects.IAMMember(
"claude-automation-cloudbuild-viewer",
project=project_id,
role="roles/cloudbuild.builds.viewer",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Grant Cloud Trace viewer permissions (view distributed traces)
claude_cloudtrace_viewer = gcp.projects.IAMMember(
"claude-automation-cloudtrace-viewer",
project=project_id,
role="roles/cloudtrace.user",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Grant Cloud Tasks viewer permissions (view queue status)
claude_cloudtasks_viewer = gcp.projects.IAMMember(
"claude-automation-cloudtasks-viewer",
project=project_id,
role="roles/cloudtasks.viewer",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Grant Storage viewer permissions (list buckets, view metadata - not object content)
claude_storage_viewer = gcp.projects.IAMMember(
"claude-automation-storage-viewer",
project=project_id,
role="roles/storage.objectViewer",
member=claude_automation_sa.email.apply(lambda email: f"serviceAccount:{email}"),
)
# Allow users to impersonate this service account
# This enables using `gcloud auth application-default login --impersonate-service-account`
# for long-lived credentials without downloadable keys
claude_automation_impersonation = gcp.serviceaccount.IAMBinding(
"claude-automation-impersonation",
service_account_id=claude_automation_sa.name,
role="roles/iam.serviceAccountTokenCreator",
members=[
"user:andrew@beveridge.uk",
"user:admin@nomadkaraoke.com",
],
)
# ==================== Cloud Tasks Queues (Phase 1 Scalability) ====================
# These queues provide guaranteed delivery and horizontal scaling for worker tasks.
# Workers are triggered via Cloud Tasks instead of BackgroundTasks, ensuring:
@@ -552,6 +633,9 @@ pulumi.export("backend_default_url", "https://karaoke-backend-ipzqd2k4yq-uc.a.ru
pulumi.export("github_actions_service_account", github_actions_sa.email)
pulumi.export("workload_identity_provider", workload_identity_provider.name)
# Claude Code automation exports
pulumi.export("claude_automation_service_account", claude_automation_sa.email)
# Cloud Tasks queue exports (Phase 1 scalability)
pulumi.export("audio_worker_queue", audio_worker_queue.name)
pulumi.export("lyrics_worker_queue", lyrics_worker_queue.name)
+218
View File
@@ -264,6 +264,36 @@ class AudioFetcher(ABC):
"""
pass
@abstractmethod
def download_from_url(
self,
url: str,
output_dir: str,
output_filename: Optional[str] = None,
artist: Optional[str] = None,
title: Optional[str] = None,
) -> AudioFetchResult:
"""
Download audio directly from a URL (e.g., YouTube URL).
This bypasses the search step and downloads directly from the provided URL.
Useful when the user provides a specific YouTube URL rather than artist/title.
Args:
url: The URL to download from (e.g., YouTube video URL)
output_dir: Directory to save the downloaded file
output_filename: Optional filename (without extension)
artist: Optional artist name for metadata
title: Optional title for metadata
Returns:
AudioFetchResult with the downloaded file path
Raises:
DownloadError: If download fails
"""
pass
class FlacFetchAudioFetcher(AudioFetcher):
"""
@@ -645,6 +675,94 @@ class FlacFetchAudioFetcher(AudioFetcher):
except Exception as e:
raise DownloadError(f"Failed to download {artist} - {title}: {e}") from e
def download_from_url(
self,
url: str,
output_dir: str,
output_filename: Optional[str] = None,
artist: Optional[str] = None,
title: Optional[str] = None,
) -> AudioFetchResult:
"""
Download audio directly from a URL (e.g., YouTube URL).
Uses flacfetch's download_by_id() method which supports direct YouTube downloads.
Args:
url: The URL to download from (e.g., YouTube video URL)
output_dir: Directory to save the downloaded file
output_filename: Optional filename (without extension)
artist: Optional artist name for metadata
title: Optional title for metadata
Returns:
AudioFetchResult with the downloaded file path
Raises:
DownloadError: If download fails
"""
import re
manager = self._get_manager()
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Detect source type from URL
source_name = "YouTube" # Default to YouTube for now
source_id = None
# Extract YouTube video ID from URL
youtube_patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})',
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
r'youtube\.com/v/([a-zA-Z0-9_-]{11})',
]
for pattern in youtube_patterns:
match = re.search(pattern, url)
if match:
source_id = match.group(1)
break
if not source_id:
# For other URLs, use the full URL as the source_id
source_id = url
# Generate filename if not provided
if output_filename is None:
if artist and title:
output_filename = f"{artist} - {title}"
else:
output_filename = source_id
self.logger.info(f"Downloading from URL: {url}")
try:
filepath = manager.download_by_id(
source_name=source_name,
source_id=source_id,
output_path=output_dir,
output_filename=output_filename,
download_url=url, # Pass full URL for direct download
)
if not filepath:
raise DownloadError(f"Download returned no file path for URL: {url}")
self.logger.info(f"Downloaded to: {filepath}")
return AudioFetchResult(
filepath=filepath,
artist=artist or "",
title=title or "",
provider=source_name,
duration=None, # Could extract from yt-dlp info if needed
quality=None,
)
except Exception as e:
raise DownloadError(f"Failed to download from URL {url}: {e}") from e
def _interruptible_search(self, manager, query) -> list:
"""
Run search in a way that can be interrupted by Ctrl+C.
@@ -1548,6 +1666,106 @@ class RemoteFlacFetchAudioFetcher(AudioFetcher):
print("\nCancelled")
raise UserCancelledError("Selection cancelled by user (Ctrl+C)")
def download_from_url(
self,
url: str,
output_dir: str,
output_filename: Optional[str] = None,
artist: Optional[str] = None,
title: Optional[str] = None,
) -> AudioFetchResult:
"""
Download audio directly from a URL (e.g., YouTube URL).
For YouTube URLs, this uses local flacfetch since YouTube downloads
don't require the remote flacfetch infrastructure (no torrents).
Args:
url: The URL to download from (e.g., YouTube video URL)
output_dir: Directory to save the downloaded file
output_filename: Optional filename (without extension)
artist: Optional artist name for metadata
title: Optional title for metadata
Returns:
AudioFetchResult with the downloaded file path
Raises:
DownloadError: If download fails
"""
import re
self.logger.info(f"[RemoteFlacFetcher] Downloading from URL: {url}")
self.logger.info("[RemoteFlacFetcher] Using local flacfetch for YouTube download (no remote API needed)")
try:
# Use local flacfetch for YouTube downloads - no need for remote API
# This avoids needing yt-dlp directly in karaoke-gen
from flacfetch.core.manager import FetchManager
from flacfetch.providers.youtube import YoutubeProvider
from flacfetch.downloaders.youtube import YoutubeDownloader
# Create a minimal local manager for YouTube downloads
manager = FetchManager()
manager.add_provider(YoutubeProvider())
manager.register_downloader("YouTube", YoutubeDownloader())
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Extract video ID from URL
source_id = None
youtube_patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})',
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
r'youtube\.com/v/([a-zA-Z0-9_-]{11})',
]
for pattern in youtube_patterns:
match = re.search(pattern, url)
if match:
source_id = match.group(1)
break
if not source_id:
source_id = url
# Generate filename if not provided
if output_filename is None:
if artist and title:
output_filename = f"{artist} - {title}"
else:
output_filename = source_id
# Use flacfetch's download_by_id for direct URL download
filepath = manager.download_by_id(
source_name="YouTube",
source_id=source_id,
output_path=output_dir,
output_filename=output_filename,
download_url=url,
)
if not filepath:
raise DownloadError(f"Download returned no file path for URL: {url}")
self.logger.info(f"[RemoteFlacFetcher] Downloaded to: {filepath}")
return AudioFetchResult(
filepath=filepath,
artist=artist or "",
title=title or "",
provider="YouTube",
duration=None,
quality=None,
)
except ImportError as e:
raise DownloadError(
f"flacfetch is required for URL downloads but import failed: {e}"
) from e
except Exception as e:
raise DownloadError(f"Failed to download from URL {url}: {e}") from e
# Alias for shorter name
RemoteFlacFetcher = RemoteFlacFetchAudioFetcher
+191 -25
View File
@@ -31,6 +31,12 @@ from .video_generator import VideoGenerator
from .video_background_processor import VideoBackgroundProcessor
from .audio_fetcher import create_audio_fetcher, AudioFetcherError, NoResultsError, UserCancelledError
# Import lyrics_transcriber components for post-review countdown and video rendering
from lyrics_transcriber.output.countdown_processor import CountdownProcessor
from lyrics_transcriber.output.generator import OutputGenerator
from lyrics_transcriber.types import CorrectionResult
from lyrics_transcriber.core.config import OutputConfig as LyricsOutputConfig
class KaraokePrep:
def __init__(
@@ -482,41 +488,56 @@ class KaraokePrep:
self.logger.info(f"Found existing media files matching extractor '{self.extractor}', skipping download/conversion.")
elif getattr(self, '_use_audio_fetcher', False):
# Use flacfetch to search and download audio
self.logger.info(f"Using flacfetch to search and download: {self.artist} - {self.title}")
try:
# Search and download audio using the AudioFetcher
fetch_result = self.audio_fetcher.search_and_download(
artist=self.artist,
title=self.title,
output_dir=track_output_dir,
output_filename=f"{artist_title} (flacfetch)",
auto_select=self.auto_download,
)
# Update extractor to reflect the actual provider used
self.extractor = f"flacfetch-{fetch_result.provider}"
# Check if this is a URL download or search+download
if getattr(self, '_use_url_download', False):
# Direct URL download (e.g., YouTube URL)
self.logger.info(f"Using flacfetch to download from URL: {self.url}")
fetch_result = self.audio_fetcher.download_from_url(
url=self.url,
output_dir=track_output_dir,
output_filename=f"{artist_title} (youtube)" if artist_title != "Unknown - Unknown" else None,
artist=self.artist,
title=self.title,
)
# Update extractor to reflect the source
self.extractor = "youtube"
else:
# Use flacfetch to search and download audio
self.logger.info(f"Using flacfetch to search and download: {self.artist} - {self.title}")
fetch_result = self.audio_fetcher.search_and_download(
artist=self.artist,
title=self.title,
output_dir=track_output_dir,
output_filename=f"{artist_title} (flacfetch)",
auto_select=self.auto_download,
)
# Update extractor to reflect the actual provider used
self.extractor = f"flacfetch-{fetch_result.provider}"
# Set up the output paths
output_filename_no_extension = os.path.join(track_output_dir, f"{artist_title} ({self.extractor})")
# Copy/move the downloaded file to the expected location
processed_track["input_media"] = self.file_handler.download_audio_from_fetcher_result(
fetch_result.filepath, output_filename_no_extension
)
self.logger.info(f"Audio downloaded from {fetch_result.provider}: {processed_track['input_media']}")
# Convert to WAV for audio processing
self.logger.info("Converting downloaded audio to WAV for processing...")
processed_track["input_audio_wav"] = self.file_handler.convert_to_wav(
processed_track["input_media"], output_filename_no_extension
)
# No still image for audio-only downloads
processed_track["input_still_image"] = None
except UserCancelledError:
# User cancelled - propagate up to CLI for graceful exit
raise
@@ -692,6 +713,113 @@ class KaraokePrep:
self.logger.info("=== Parallel Processing Complete ===")
# === POST-TRANSCRIPTION: Add countdown and render video ===
# Since lyrics_processor.py now always defers countdown and video rendering,
# we handle it here after human review is complete. This ensures the review UI
# shows accurate, unshifted timestamps (same behavior as cloud backend).
if processed_track.get("lyrics") and self.render_video:
self.logger.info("=== Processing Countdown and Video Rendering ===")
from .utils import sanitize_filename
sanitized_artist = sanitize_filename(self.artist)
sanitized_title = sanitize_filename(self.title)
lyrics_dir = os.path.join(track_output_dir, "lyrics")
# Find the corrections JSON file
corrections_filename = f"{sanitized_artist} - {sanitized_title} (Lyrics Corrections).json"
corrections_filepath = os.path.join(lyrics_dir, corrections_filename)
if os.path.exists(corrections_filepath):
self.logger.info(f"Loading corrections from: {corrections_filepath}")
with open(corrections_filepath, 'r', encoding='utf-8') as f:
corrections_data = json.load(f)
# Convert to CorrectionResult
correction_result = CorrectionResult.from_dict(corrections_data)
self.logger.info(f"Loaded CorrectionResult with {len(correction_result.corrected_segments)} segments")
# Get the audio file path
audio_path = processed_track["input_audio_wav"]
# Add countdown intro if needed (songs that start within 3 seconds)
self.logger.info("Processing countdown intro (if needed)...")
cache_dir = os.path.join(track_output_dir, "cache")
os.makedirs(cache_dir, exist_ok=True)
countdown_processor = CountdownProcessor(
cache_dir=cache_dir,
logger=self.logger,
)
correction_result, audio_path, padding_added, padding_seconds = countdown_processor.process(
correction_result=correction_result,
audio_filepath=audio_path,
)
# Update processed_track with countdown info
processed_track["countdown_padding_added"] = padding_added
processed_track["countdown_padding_seconds"] = padding_seconds
if padding_added:
processed_track["padded_vocals_audio"] = audio_path
self.logger.info(
f"=== COUNTDOWN PADDING ADDED ===\n"
f"Added {padding_seconds}s padding to audio and shifted timestamps.\n"
f"Instrumental tracks will be padded after separation to maintain sync."
)
else:
self.logger.info("No countdown needed - song starts after 3 seconds")
# Save the updated corrections with countdown timestamps
updated_corrections_data = correction_result.to_dict()
with open(corrections_filepath, 'w', encoding='utf-8') as f:
json.dump(updated_corrections_data, f, indent=2)
self.logger.info(f"Saved countdown-adjusted corrections to: {corrections_filepath}")
# Render video with lyrics
self.logger.info("Rendering karaoke video with synchronized lyrics...")
output_config = LyricsOutputConfig(
output_dir=lyrics_dir,
cache_dir=cache_dir,
output_styles_json=self.style_params_json,
render_video=True,
generate_cdg=False,
generate_plain_text=True,
generate_lrc=True,
video_resolution="4k",
subtitle_offset_ms=self.subtitle_offset_ms,
)
output_generator = OutputGenerator(output_config, self.logger)
output_prefix = f"{sanitized_artist} - {sanitized_title}"
outputs = output_generator.generate_outputs(
transcription_corrected=correction_result,
lyrics_results={}, # Lyrics already written during transcription phase
audio_filepath=audio_path,
output_prefix=output_prefix,
)
# Copy video to expected location in parent directory
if outputs and outputs.video:
source_video = outputs.video
dest_video = os.path.join(track_output_dir, f"{artist_title} (With Vocals).mkv")
shutil.copy2(source_video, dest_video)
self.logger.info(f"Video rendered successfully: {dest_video}")
processed_track["with_vocals_video"] = dest_video
# Update ASS filepath for video background processing
if outputs.ass:
processed_track["ass_filepath"] = outputs.ass
else:
self.logger.warning("Video rendering did not produce expected output")
else:
self.logger.warning(f"Corrections file not found: {corrections_filepath}")
self.logger.warning("Skipping countdown processing and video rendering")
elif not self.render_video:
self.logger.info("Video rendering disabled - skipping countdown and video generation")
# Apply video background if requested and lyrics were processed
if self.video_background_processor and processed_track.get("lyrics"):
self.logger.info("=== Processing Video Background ===")
@@ -991,17 +1119,56 @@ class KaraokePrep:
return tracks
def _is_url(self, string: str) -> bool:
"""Check if a string is a URL."""
return string is not None and (string.startswith("http://") or string.startswith("https://"))
async def process(self):
if self.input_media is not None and os.path.isdir(self.input_media):
self.logger.info(f"Input media {self.input_media} is a local folder, processing each file individually...")
return await self.process_folder()
elif self.input_media is not None and os.path.isfile(self.input_media):
self.logger.info(f"Input media {self.input_media} is a local file, audio download will be skipped")
return [await self.prep_single_track()]
elif self.input_media is not None and self._is_url(self.input_media):
# URL provided - download directly via flacfetch
self.logger.info(f"Input media {self.input_media} is a URL, downloading via flacfetch...")
# Extract video ID for metadata if it's a YouTube URL
video_id = None
youtube_patterns = [
r'(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})',
r'youtube\.com/embed/([a-zA-Z0-9_-]{11})',
r'youtube\.com/v/([a-zA-Z0-9_-]{11})',
]
for pattern in youtube_patterns:
match = re.search(pattern, self.input_media)
if match:
video_id = match.group(1)
break
# Set up the extracted_info for metadata consistency
self.extracted_info = {
"title": f"{self.artist} - {self.title}" if self.artist and self.title else video_id or "Unknown",
"artist": self.artist or "",
"track_title": self.title or "",
"extractor_key": "youtube",
"id": video_id or self.input_media,
"url": self.input_media,
"source": "youtube",
}
self.extractor = "youtube"
self.url = self.input_media
# Mark that we need to use audio fetcher for URL download
self._use_audio_fetcher = True
self._use_url_download = True # New flag for URL-based download
return [await self.prep_single_track()]
elif self.artist and self.title:
# No input file provided - use flacfetch to search and download audio
self.logger.info(f"No input file provided, using flacfetch to search for: {self.artist} - {self.title}")
# Set up the extracted_info for metadata consistency
self.extracted_info = {
"title": f"{self.artist} - {self.title}",
@@ -1014,13 +1181,12 @@ class KaraokePrep:
}
self.extractor = "flacfetch"
self.url = None # URL will be determined by flacfetch
# Mark that we need to use audio fetcher for download
self._use_audio_fetcher = True
return [await self.prep_single_track()]
else:
raise ValueError(
"Either a local file path or both artist and title must be provided. "
"URL-based input has been replaced with flacfetch audio fetching."
"Either a local file path, a URL, or both artist and title must be provided."
)
+37 -33
View File
@@ -170,15 +170,15 @@ class LyricsProcessor:
def _check_transcription_providers(self) -> dict:
"""
Check which transcription providers are configured and return their status.
Returns:
dict with 'configured' (list of provider names) and 'missing' (list of missing configs)
"""
load_dotenv()
configured = []
missing = []
# Check AudioShake
audioshake_token = os.getenv("AUDIOSHAKE_API_TOKEN")
if audioshake_token:
@@ -187,7 +187,7 @@ class LyricsProcessor:
else:
missing.append("AudioShake (AUDIOSHAKE_API_TOKEN)")
self.logger.debug("AudioShake transcription provider: not configured (missing AUDIOSHAKE_API_TOKEN)")
# Check Whisper via RunPod
runpod_key = os.getenv("RUNPOD_API_KEY")
whisper_id = os.getenv("WHISPER_RUNPOD_ID")
@@ -203,7 +203,16 @@ class LyricsProcessor:
else:
missing.append("Whisper (RUNPOD_API_KEY + WHISPER_RUNPOD_ID)")
self.logger.debug("Whisper transcription provider: not configured")
# Check Local Whisper (whisper-timestamped)
try:
import whisper_timestamped
configured.append("Local Whisper")
self.logger.debug("Local Whisper transcription provider: configured (whisper-timestamped installed)")
except ImportError:
missing.append("Local Whisper (pip install karaoke-gen[local-whisper])")
self.logger.debug("Local Whisper transcription provider: not configured (whisper-timestamped not installed)")
return {"configured": configured, "missing": missing}
def _build_transcription_provider_error_message(self, missing_providers: list) -> str:
@@ -221,12 +230,18 @@ class LyricsProcessor:
" - Set environment variable: AUDIOSHAKE_API_TOKEN=your_token\n"
" - Get an API key at: https://www.audioshake.ai/\n"
"\n"
"2. Whisper via RunPod (Open-source alternative)\n"
"2. Whisper via RunPod (Cloud-based open-source)\n"
" - Set environment variables:\n"
" RUNPOD_API_KEY=your_key\n"
" WHISPER_RUNPOD_ID=your_endpoint_id\n"
" - Set up a Whisper endpoint at: https://www.runpod.io/\n"
"\n"
"3. Local Whisper (No cloud required - runs on your machine)\n"
" - Install with: pip install karaoke-gen[local-whisper]\n"
" - For CPU-only: pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu\n"
" pip install karaoke-gen[local-whisper]\n"
" - Requires 2-10GB RAM depending on model size\n"
"\n"
"ALTERNATIVES:\n"
"\n"
"- Use --skip-lyrics flag to generate instrumental-only karaoke (no synchronized lyrics)\n"
@@ -348,6 +363,10 @@ class LyricsProcessor:
# Create config objects for LyricsTranscriber
transcriber_config = TranscriberConfig(
audioshake_api_token=env_config.get("audioshake_api_token"),
runpod_api_key=env_config.get("runpod_api_key"),
whisper_runpod_id=env_config.get("whisper_runpod_id"),
# Local Whisper is enabled by default as a fallback when no cloud providers are configured
enable_local_whisper=True,
)
lyrics_config = LyricsConfig(
@@ -364,35 +383,20 @@ class LyricsProcessor:
self.logger.info(f" rapidapi_key: {env_config.get('rapidapi_key')[:3] + '...' if env_config.get('rapidapi_key') else 'None'}")
self.logger.info(f" lyrics_file: {self.lyrics_file}")
# Detect if we're running in a serverless environment (Modal)
# Modal sets specific environment variables we can check for
is_serverless = (
os.getenv("MODAL_TASK_ID") is not None or
os.getenv("MODAL_FUNCTION_NAME") is not None or
os.path.exists("/.modal") # Modal creates this directory in containers
)
# In serverless environment, disable interactive review even if skip_transcription_review=False
# This preserves CLI behavior while fixing serverless hanging
enable_review_setting = not self.skip_transcription_review and not is_serverless
if is_serverless and not self.skip_transcription_review:
self.logger.info("Detected serverless environment - disabling interactive review to prevent hanging")
# In serverless environment, disable video generation during Phase 1 to save compute
# Video will be generated in Phase 2 after human review
serverless_render_video = render_video and not is_serverless
if is_serverless and render_video:
self.logger.info("Detected serverless environment - deferring video generation until after review")
if is_serverless:
self.logger.info("Detected serverless environment - deferring countdown processing until after review")
# Always defer countdown and video rendering to a later phase.
# This ensures the review UI (both local and cloud) shows original timing
# without the 3-second countdown shift. The caller is responsible for:
# - Local CLI: karaoke_gen.py adds countdown and renders video after transcription
# - Cloud backend: render_video_worker.py adds countdown and renders video
#
# This design ensures consistent behavior regardless of environment,
# and the review UI always shows accurate, unshifted timestamps.
self.logger.info("Deferring countdown and video rendering to post-review phase")
output_config = OutputConfig(
output_styles_json=self.style_params_json,
output_dir=lyrics_dir,
render_video=serverless_render_video, # Disable video in serverless Phase 1
render_video=False, # Always defer - caller handles video rendering after countdown
fetch_lyrics=True,
run_transcription=not self.skip_transcription,
run_correction=True,
@@ -400,9 +404,9 @@ class LyricsProcessor:
generate_lrc=True,
generate_cdg=False, # CDG generation disabled (not currently supported)
video_resolution="4k",
enable_review=enable_review_setting,
enable_review=not self.skip_transcription_review, # Honor the caller's setting
subtitle_offset_ms=self.subtitle_offset_ms,
add_countdown=not is_serverless, # Defer countdown to Phase 2 (render_video_worker)
add_countdown=False, # Always defer - caller handles countdown after review
)
# Add this log entry to debug the OutputConfig
+9 -1
View File
@@ -242,9 +242,17 @@ def create_parser(prog: str = "karaoke-gen") -> argparse.ArgumentParser:
# Style Configuration
style_group = parser.add_argument_group("Style Configuration")
style_group.add_argument(
"--theme",
help="Optional: Theme ID for pre-made styles stored in GCS (e.g., 'nomad', 'default'). "
"When using a theme, CDG/TXT are enabled by default. "
"Example: --theme=nomad",
)
style_group.add_argument(
"--style_params_json",
help="Optional: Path to JSON file containing style configuration. Example: --style_params_json='/path/to/style_params.json'",
help="Optional: Path to JSON file containing style configuration. "
"Takes precedence over --theme if both are provided. "
"Example: --style_params_json='/path/to/style_params.json'",
)
style_group.add_argument(
"--style_override",
+33 -6
View File
@@ -292,6 +292,8 @@ class RemoteKaraokeClient:
# Two-phase workflow (Batch 6)
prep_only: bool = False,
keep_brand_code: Optional[str] = None,
# Theme system
theme_id: Optional[str] = None,
) -> Dict[str, Any]:
"""
Submit a new karaoke generation job from a YouTube/online URL.
@@ -322,6 +324,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.)
theme_id: Theme ID from GCS themes (e.g., 'nomad', 'default')
"""
self.logger.info(f"Submitting URL-based job: {url}")
@@ -367,7 +370,10 @@ class RemoteKaraokeClient:
create_request['prep_only'] = prep_only
if keep_brand_code:
create_request['keep_brand_code'] = keep_brand_code
# Theme system
if theme_id:
create_request['theme_id'] = theme_id
self.logger.info(f"Creating URL-based job at {self.config.service_url}/api/jobs/create-from-url")
response = self._request('POST', '/api/jobs/create-from-url', json=create_request)
@@ -396,9 +402,9 @@ class RemoteKaraokeClient:
return result
def submit_job(
self,
filepath: str,
artist: str,
self,
filepath: str,
artist: str,
title: str,
style_params_path: Optional[str] = None,
enable_cdg: bool = True,
@@ -425,6 +431,8 @@ class RemoteKaraokeClient:
# Two-phase workflow (Batch 6)
prep_only: bool = False,
keep_brand_code: Optional[str] = None,
# Theme system
theme_id: Optional[str] = None,
) -> Dict[str, Any]:
"""
Submit a new karaoke generation job with optional style configuration.
@@ -456,6 +464,7 @@ class RemoteKaraokeClient:
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
theme_id: Theme ID from GCS themes (e.g., 'nomad', 'default')
"""
file_path = Path(filepath)
@@ -574,7 +583,10 @@ class RemoteKaraokeClient:
create_request['prep_only'] = prep_only
if keep_brand_code:
create_request['keep_brand_code'] = keep_brand_code
# Theme system
if theme_id:
create_request['theme_id'] = theme_id
response = self._request('POST', '/api/jobs/create-with-upload-urls', json=create_request)
if response.status_code != 200:
@@ -1195,6 +1207,8 @@ class RemoteKaraokeClient:
clean_instrumental_model: Optional[str] = None,
backing_vocals_models: Optional[list] = None,
other_stems_models: Optional[list] = None,
# Theme system
theme_id: Optional[str] = None,
) -> Dict[str, Any]:
"""
Search for audio by artist and title (Batch 5 - Flacfetch integration).
@@ -1247,7 +1261,10 @@ class RemoteKaraokeClient:
request_data['backing_vocals_models'] = backing_vocals_models
if other_stems_models:
request_data['other_stems_models'] = other_stems_models
# Theme system
if theme_id:
request_data['theme_id'] = theme_id
# Prepare style files for upload if provided
style_files = []
local_style_files: Dict[str, str] = {} # file_type -> local_path
@@ -3029,6 +3046,8 @@ def main():
logger.info(f"Searching for: {artist} - {title}")
if getattr(args, 'auto_download', False) or config.non_interactive:
logger.info(f"Auto-download: enabled (will auto-select best source)")
if getattr(args, 'theme', None):
logger.info(f"Theme: {args.theme}")
if args.style_params_json:
logger.info(f"Style: {args.style_params_json}")
logger.info(f"CDG: {args.enable_cdg}, TXT: {args.enable_txt}")
@@ -3070,6 +3089,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),
# Theme system
theme_id=getattr(args, 'theme', None),
)
job_id = result.get('job_id')
@@ -3106,6 +3127,8 @@ def main():
logger.info(f"Title: {title}")
if not artist and not title and is_url_input:
logger.info(f"Artist/Title: (will be auto-detected from URL)")
if getattr(args, 'theme', None):
logger.info(f"Theme: {args.theme}")
if args.style_params_json:
logger.info(f"Style: {args.style_params_json}")
logger.info(f"CDG: {args.enable_cdg}, TXT: {args.enable_txt}")
@@ -3206,6 +3229,8 @@ def main():
# Two-phase workflow (Batch 6)
prep_only=getattr(args, 'prep_only', False),
keep_brand_code=keep_brand_code_value,
# Theme system
theme_id=getattr(args, 'theme', None),
)
else:
# File-based job submission
@@ -3238,6 +3263,8 @@ def main():
# Two-phase workflow (Batch 6)
prep_only=getattr(args, 'prep_only', False),
keep_brand_code=keep_brand_code_value,
# Theme system
theme_id=getattr(args, 'theme', None),
)
job_id = result.get('job_id')
style_assets = result.get('style_assets_uploaded', [])
@@ -11,6 +11,13 @@ class TranscriberConfig:
runpod_api_key: Optional[str] = None
whisper_runpod_id: Optional[str] = None
# Local Whisper configuration
enable_local_whisper: bool = True # Enabled by default as fallback
local_whisper_model_size: str = "medium" # tiny, base, small, medium, large
local_whisper_device: Optional[str] = None # None for auto-detect, or cpu/cuda/mps
local_whisper_cache_dir: Optional[str] = None # Custom model download location
local_whisper_language: Optional[str] = None # None for auto-detect
@dataclass
class LyricsConfig:
@@ -7,6 +7,7 @@ from lyrics_transcriber.types import LyricsData, TranscriptionResult, Correction
from lyrics_transcriber.transcribers.base_transcriber import BaseTranscriber
from lyrics_transcriber.transcribers.audioshake import AudioShakeTranscriber, AudioShakeConfig
from lyrics_transcriber.transcribers.whisper import WhisperTranscriber, WhisperConfig
from lyrics_transcriber.transcribers.local_whisper import LocalWhisperTranscriber, LocalWhisperConfig
from lyrics_transcriber.lyrics.base_lyrics_provider import BaseLyricsProvider, LyricsProviderConfig
from lyrics_transcriber.lyrics.genius import GeniusProvider
from lyrics_transcriber.lyrics.spotify import SpotifyProvider
@@ -206,6 +207,34 @@ class LyricsTranscriber:
else:
self.logger.debug("Skipping Whisper transcriber - missing runpod_api_key or whisper_runpod_id")
# Local Whisper - lowest priority, fallback when cloud services unavailable
if self.transcriber_config.enable_local_whisper:
# Check if whisper-timestamped is available
try:
import whisper_timestamped # noqa: F401
self.logger.debug("Initializing LocalWhisper transcriber")
transcribers["local_whisper"] = {
"instance": LocalWhisperTranscriber(
cache_dir=self.output_config.cache_dir,
config=LocalWhisperConfig(
model_size=self.transcriber_config.local_whisper_model_size,
device=self.transcriber_config.local_whisper_device,
cache_dir=self.transcriber_config.local_whisper_cache_dir,
language=self.transcriber_config.local_whisper_language,
),
logger=self.logger,
),
"priority": 3, # Local Whisper has lowest priority (fallback)
}
except ImportError:
self.logger.debug(
"Skipping LocalWhisper transcriber - whisper-timestamped not installed. "
"Install with: pip install karaoke-gen[local-whisper]"
)
else:
self.logger.debug("Skipping LocalWhisper transcriber - disabled via enable_local_whisper=False")
return transcribers
def _initialize_lyrics_providers(self) -> Dict[str, BaseLyricsProvider]:
@@ -442,7 +471,7 @@ class LyricsTranscriber:
# Whisper/RunPod status
has_runpod_key = bool(self.transcriber_config.runpod_api_key)
has_whisper_id = bool(self.transcriber_config.whisper_runpod_id)
if has_runpod_key and has_whisper_id:
self.logger.debug(" - Whisper (RunPod): CONFIGURED (API key and endpoint ID provided)")
elif has_runpod_key:
@@ -452,6 +481,19 @@ class LyricsTranscriber:
else:
self.logger.debug(" - Whisper (RunPod): NOT CONFIGURED (missing RUNPOD_API_KEY and WHISPER_RUNPOD_ID)")
# Local Whisper status
if self.transcriber_config.enable_local_whisper:
try:
import whisper_timestamped # noqa: F401
self.logger.debug(
f" - LocalWhisper: AVAILABLE (model={self.transcriber_config.local_whisper_model_size}, "
f"device={self.transcriber_config.local_whisper_device or 'auto'})"
)
except ImportError:
self.logger.debug(" - LocalWhisper: ENABLED but whisper-timestamped not installed")
else:
self.logger.debug(" - LocalWhisper: DISABLED (enable_local_whisper=False)")
def correct_lyrics(self) -> None:
"""Run lyrics correction using transcription and internet lyrics."""
self.logger.info("Starting lyrics correction process")
@@ -52,7 +52,7 @@ class OutputGenerator:
self.logger.info(f"Initializing OutputGenerator with config: {self.config}")
# Load output styles from JSON if provided
# Load output styles from JSON if provided, otherwise use defaults
if self.config.output_styles_json and os.path.exists(self.config.output_styles_json):
try:
with open(self.config.output_styles_json, "r") as f:
@@ -67,9 +67,10 @@ class OutputGenerator:
self.logger.warning(f"Failed to load output styles file: {str(e)}")
self.config.styles = {}
else:
# No styles file provided or doesn't exist
# No styles file provided or doesn't exist - use defaults
if self.config.render_video or self.config.generate_cdg:
raise ValueError(f"Output styles file required for video/CDG generation but not found: {self.config.output_styles_json}")
self.logger.info("No output styles file provided, using default karaoke styles")
self.config.styles = self._get_default_styles()
else:
self.config.styles = {}
@@ -242,6 +243,52 @@ class OutputGenerator:
return resolution_dims, font_size, line_height
def _get_default_styles(self) -> dict:
"""Get default styles for video/CDG generation when no styles file is provided."""
return {
"karaoke": {
# Video background
"background_color": "#000000",
"background_image": None,
# Font settings
"font": "Arial",
"font_path": "", # Must be string, not None (for ASS generator)
"ass_name": "Default",
# Colors in "R, G, B, A" format (required by ASS)
"primary_color": "112, 112, 247, 255",
"secondary_color": "255, 255, 255, 255",
"outline_color": "26, 58, 235, 255",
"back_color": "0, 0, 0, 0",
# Boolean style options
"bold": False,
"italic": False,
"underline": False,
"strike_out": False,
# Numeric style options (all required for ASS)
"scale_x": 100,
"scale_y": 100,
"spacing": 0,
"angle": 0.0,
"border_style": 1,
"outline": 1,
"shadow": 0,
"margin_l": 0,
"margin_r": 0,
"margin_v": 0,
"encoding": 0,
# Layout settings
"max_line_length": 40,
"top_padding": 200,
"font_size": 100,
},
"cdg": {
"font_path": None,
"instrumental_background": None,
"title_screen_background": None,
"outro_background": None,
},
}
def write_corrections_data(self, correction_result: CorrectionResult, output_prefix: str) -> str:
"""Write corrections data to JSON file."""
self.logger.info("Writing corrections data JSON")
@@ -0,0 +1,260 @@
"""Local Whisper transcription service using whisper-timestamped for word-level timestamps."""
from dataclasses import dataclass
import os
import logging
from typing import Optional, Dict, Any, Union
from pathlib import Path
from lyrics_transcriber.types import TranscriptionData, LyricsSegment, Word
from lyrics_transcriber.transcribers.base_transcriber import BaseTranscriber, TranscriptionError
from lyrics_transcriber.utils.word_utils import WordUtils
@dataclass
class LocalWhisperConfig:
"""Configuration for local Whisper transcription service."""
model_size: str = "medium" # tiny, base, small, medium, large, large-v2, large-v3
device: Optional[str] = None # None for auto-detect, or "cpu", "cuda", "mps"
cache_dir: Optional[str] = None # Directory for model downloads (~/.cache/whisper by default)
language: Optional[str] = None # Language code for transcription, None for auto-detect
compute_type: str = "auto" # float16, float32, int8, auto
class LocalWhisperTranscriber(BaseTranscriber):
"""
Transcription service using local Whisper inference via whisper-timestamped.
This transcriber runs Whisper models locally on your machine, supporting
CPU, CUDA GPU, and Apple Silicon MPS acceleration. It uses the
whisper-timestamped library to get accurate word-level timestamps.
Requirements:
pip install karaoke-gen[local-whisper]
Configuration:
Set environment variables to customize behavior:
- WHISPER_MODEL_SIZE: Model size (tiny, base, small, medium, large)
- WHISPER_DEVICE: Device to use (cpu, cuda, mps, or auto)
- WHISPER_CACHE_DIR: Directory for model downloads
- WHISPER_LANGUAGE: Language code (en, es, fr, etc.) or auto-detect
"""
def __init__(
self,
cache_dir: Union[str, Path],
config: Optional[LocalWhisperConfig] = None,
logger: Optional[logging.Logger] = None,
):
"""
Initialize local Whisper transcriber.
Args:
cache_dir: Directory for caching transcription results
config: Configuration options for the transcriber
logger: Logger instance to use
"""
super().__init__(cache_dir=cache_dir, logger=logger)
# Initialize configuration from env vars or defaults
self.config = config or LocalWhisperConfig(
model_size=os.getenv("WHISPER_MODEL_SIZE", "medium"),
device=os.getenv("WHISPER_DEVICE"), # None for auto-detect
cache_dir=os.getenv("WHISPER_CACHE_DIR"),
language=os.getenv("WHISPER_LANGUAGE"), # None for auto-detect
)
# Lazy-loaded model instance (loaded on first use)
self._model = None
self._whisper_module = None
self.logger.debug(
f"LocalWhisperTranscriber initialized with model_size={self.config.model_size}, "
f"device={self.config.device or 'auto'}, language={self.config.language or 'auto-detect'}"
)
def get_name(self) -> str:
"""Return the name of this transcription service."""
return "LocalWhisper"
def _check_dependencies(self) -> None:
"""Check that required dependencies are installed."""
try:
import whisper_timestamped # noqa: F401
except ImportError:
raise TranscriptionError(
"whisper-timestamped is not installed. "
"Install it with: pip install karaoke-gen[local-whisper] "
"or: pip install whisper-timestamped"
)
def _get_device(self) -> str:
"""Determine the best device to use for inference."""
if self.config.device:
return self.config.device
# Auto-detect best available device
try:
import torch
if torch.cuda.is_available():
self.logger.info("Using CUDA GPU for Whisper inference")
return "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
self.logger.info("Using Apple Silicon MPS for Whisper inference")
return "cpu" # whisper-timestamped works better with CPU on MPS
else:
self.logger.info("Using CPU for Whisper inference (no GPU detected)")
return "cpu"
except ImportError:
self.logger.warning("PyTorch not available, defaulting to CPU")
return "cpu"
def _load_model(self):
"""Load the Whisper model (lazy loading on first use)."""
if self._model is not None:
return self._model
self._check_dependencies()
import whisper_timestamped as whisper
self._whisper_module = whisper
device = self._get_device()
self.logger.info(f"Loading Whisper model '{self.config.model_size}' on device '{device}'...")
try:
# Load model with optional custom cache directory
download_root = self.config.cache_dir
self._model = whisper.load_model(
self.config.model_size,
device=device,
download_root=download_root,
)
self.logger.info(f"Whisper model '{self.config.model_size}' loaded successfully")
return self._model
except RuntimeError as e:
if "out of memory" in str(e).lower() or "CUDA" in str(e):
raise TranscriptionError(
f"GPU out of memory loading model '{self.config.model_size}'. "
"Try using a smaller model (set WHISPER_MODEL_SIZE=small or tiny) "
"or force CPU mode (set WHISPER_DEVICE=cpu)"
) from e
raise TranscriptionError(f"Failed to load Whisper model: {e}") from e
except Exception as e:
raise TranscriptionError(f"Failed to load Whisper model: {e}") from e
def _perform_transcription(self, audio_filepath: str) -> Dict[str, Any]:
"""
Perform local Whisper transcription with word-level timestamps.
Args:
audio_filepath: Path to the audio file to transcribe
Returns:
Raw transcription result dictionary
"""
self.logger.info(f"Starting local Whisper transcription for {audio_filepath}")
# Load model if not already loaded
model = self._load_model()
try:
# Perform transcription with word-level timestamps
transcribe_kwargs = {
"verbose": False,
}
# Add language if specified
if self.config.language:
transcribe_kwargs["language"] = self.config.language
self.logger.debug(f"Transcribing with options: {transcribe_kwargs}")
result = self._whisper_module.transcribe_timestamped(
model,
audio_filepath,
**transcribe_kwargs,
)
self.logger.info("Local Whisper transcription completed successfully")
return result
except RuntimeError as e:
if "out of memory" in str(e).lower():
raise TranscriptionError(
f"GPU out of memory during transcription. "
"Try using a smaller model (WHISPER_MODEL_SIZE=small) "
"or force CPU mode (WHISPER_DEVICE=cpu)"
) from e
raise TranscriptionError(f"Transcription failed: {e}") from e
except Exception as e:
raise TranscriptionError(f"Transcription failed: {e}") from e
def _convert_result_format(self, raw_data: Dict[str, Any]) -> TranscriptionData:
"""
Convert whisper-timestamped output to standard TranscriptionData format.
The whisper-timestamped library returns results in this format:
{
"text": "Full transcription text",
"segments": [
{
"id": 0,
"text": "Segment text",
"start": 0.0,
"end": 2.5,
"words": [
{"text": "word", "start": 0.0, "end": 0.5, "confidence": 0.95},
...
]
},
...
],
"language": "en"
}
Args:
raw_data: Raw output from whisper_timestamped.transcribe_timestamped()
Returns:
TranscriptionData with segments, words, and metadata
"""
segments = []
all_words = []
for seg in raw_data.get("segments", []):
segment_words = []
for word_data in seg.get("words", []):
word = Word(
id=WordUtils.generate_id(),
text=word_data.get("text", "").strip(),
start_time=word_data.get("start", 0.0),
end_time=word_data.get("end", 0.0),
confidence=word_data.get("confidence"),
)
segment_words.append(word)
all_words.append(word)
# Create segment with its words
segment = LyricsSegment(
id=WordUtils.generate_id(),
text=seg.get("text", "").strip(),
words=segment_words,
start_time=seg.get("start", 0.0),
end_time=seg.get("end", 0.0),
)
segments.append(segment)
return TranscriptionData(
segments=segments,
words=all_words,
text=raw_data.get("text", "").strip(),
source=self.get_name(),
metadata={
"model_size": self.config.model_size,
"detected_language": raw_data.get("language", "unknown"),
"device": self._get_device(),
},
)
@@ -82,8 +82,8 @@ class TestOutputGenerator:
assert generator.font_size == 40
assert generator.line_height == 50
def test_initialization_missing_styles_file(self, tmp_path):
"""Test OutputGenerator initialization with missing styles file."""
def test_initialization_missing_styles_file_uses_defaults(self, tmp_path):
"""Test OutputGenerator uses default styles when styles file is missing."""
config = OutputConfig(
output_styles_json=str(tmp_path / "nonexistent.json"),
output_dir=str(tmp_path / "output"),
@@ -91,9 +91,33 @@ class TestOutputGenerator:
default_max_line_length=36,
render_video=True
)
with pytest.raises(ValueError, match="Output styles file required for video/CDG generation but not found"):
OutputGenerator(config=config)
# Should initialize successfully with default styles
generator = OutputGenerator(config=config)
# Verify default styles are loaded
assert "karaoke" in generator.config.styles
assert "cdg" in generator.config.styles
assert generator.config.styles["karaoke"]["font"] == "Arial"
assert generator.config.styles["karaoke"]["primary_color"] == "112, 112, 247, 255"
assert generator.config.styles["karaoke"]["max_line_length"] == 40
def test_initialization_no_styles_path_uses_defaults(self, tmp_path):
"""Test OutputGenerator uses default styles when styles path is None."""
config = OutputConfig(
output_styles_json=None,
output_dir=str(tmp_path / "output"),
cache_dir=str(tmp_path / "cache"),
default_max_line_length=36,
render_video=True
)
# Should initialize successfully with default styles
generator = OutputGenerator(config=config)
# Verify default styles are loaded
assert "karaoke" in generator.config.styles
assert generator.config.styles["karaoke"]["background_color"] == "#000000"
def test_initialization_invalid_styles_file(self, tmp_path):
"""Test OutputGenerator initialization with invalid JSON in styles file."""
@@ -0,0 +1,540 @@
"""Tests for LocalWhisperTranscriber."""
import pytest
import logging
import os
import shutil
import tempfile
from unittest.mock import Mock, patch, MagicMock
from lyrics_transcriber.types import TranscriptionData
from lyrics_transcriber.transcribers.local_whisper import (
LocalWhisperTranscriber,
LocalWhisperConfig,
)
from lyrics_transcriber.transcribers.base_transcriber import TranscriptionError
from tests.test_helpers import create_test_word, create_test_segment
@pytest.fixture
def mock_logger():
return Mock(spec=logging.Logger)
@pytest.fixture
def test_cache_dir():
"""Create and cleanup a test cache directory."""
cache_dir = tempfile.mkdtemp(prefix="test_local_whisper_cache_")
yield cache_dir
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
@pytest.fixture
def default_config():
"""Create a default LocalWhisperConfig for testing."""
return LocalWhisperConfig(
model_size="medium",
device="cpu",
cache_dir=None,
language=None,
)
@pytest.fixture
def mock_whisper_output():
"""Sample output from whisper-timestamped."""
return {
"text": "Hello world, this is a test.",
"segments": [
{
"id": 0,
"text": "Hello world,",
"start": 0.0,
"end": 1.5,
"words": [
{"text": "Hello", "start": 0.0, "end": 0.5, "confidence": 0.95},
{"text": "world,", "start": 0.6, "end": 1.5, "confidence": 0.92},
],
},
{
"id": 1,
"text": "this is a test.",
"start": 1.6,
"end": 3.0,
"words": [
{"text": "this", "start": 1.6, "end": 1.8, "confidence": 0.98},
{"text": "is", "start": 1.9, "end": 2.0, "confidence": 0.97},
{"text": "a", "start": 2.1, "end": 2.2, "confidence": 0.99},
{"text": "test.", "start": 2.3, "end": 3.0, "confidence": 0.96},
],
},
],
"language": "en",
}
class TestLocalWhisperConfig:
"""Tests for LocalWhisperConfig dataclass."""
def test_default_values(self):
"""Test that default values are set correctly."""
config = LocalWhisperConfig()
assert config.model_size == "medium"
assert config.device is None
assert config.cache_dir is None
assert config.language is None
def test_custom_values(self):
"""Test that custom values can be set."""
config = LocalWhisperConfig(
model_size="large",
device="cuda",
cache_dir="/custom/cache",
language="en",
)
assert config.model_size == "large"
assert config.device == "cuda"
assert config.cache_dir == "/custom/cache"
assert config.language == "en"
class TestLocalWhisperTranscriber:
"""Tests for LocalWhisperTranscriber class."""
def test_init_with_config(self, mock_logger, test_cache_dir, default_config):
"""Test initialization with explicit config."""
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=default_config,
logger=mock_logger,
)
assert transcriber.config == default_config
assert transcriber.logger == mock_logger
assert str(transcriber.cache_dir) == test_cache_dir
def test_init_without_config(self, mock_logger, test_cache_dir):
"""Test initialization without config uses environment variables."""
with patch.dict(
os.environ,
{
"WHISPER_MODEL_SIZE": "small",
"WHISPER_DEVICE": "cpu",
},
clear=False,
):
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
logger=mock_logger,
)
assert transcriber.config.model_size == "small"
assert transcriber.config.device == "cpu"
def test_get_name(self, mock_logger, test_cache_dir, default_config):
"""Test that get_name returns correct name."""
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=default_config,
logger=mock_logger,
)
assert transcriber.get_name() == "LocalWhisper"
def test_check_dependencies_not_installed(self, mock_logger, test_cache_dir, default_config):
"""Test that missing dependencies raise TranscriptionError."""
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=default_config,
logger=mock_logger,
)
# Mock import to fail
with patch.dict("sys.modules", {"whisper_timestamped": None}):
with pytest.raises(TranscriptionError, match="whisper-timestamped is not installed"):
transcriber._check_dependencies()
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._check_dependencies")
def test_get_device_explicit(self, mock_check, mock_logger, test_cache_dir):
"""Test device selection when explicitly set."""
config = LocalWhisperConfig(device="cuda")
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=config,
logger=mock_logger,
)
assert transcriber._get_device() == "cuda"
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._check_dependencies")
def test_get_device_auto_detect_cpu(self, mock_check, mock_logger, test_cache_dir, default_config):
"""Test auto-detection falls back to CPU."""
# Mock torch to simulate no GPU available
mock_torch = MagicMock()
mock_torch.cuda.is_available.return_value = False
mock_torch.backends.mps.is_available.return_value = False
with patch.dict("sys.modules", {"torch": mock_torch}):
config = LocalWhisperConfig(device=None) # Auto-detect
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=config,
logger=mock_logger,
)
device = transcriber._get_device()
assert device == "cpu"
class TestLocalWhisperTranscriberConversion:
"""Tests for result format conversion."""
@pytest.fixture
def transcriber(self, mock_logger, test_cache_dir, default_config):
"""Create transcriber instance for conversion tests."""
return LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=default_config,
logger=mock_logger,
)
def test_convert_result_format_basic(self, transcriber, mock_whisper_output):
"""Test basic conversion of whisper output."""
result = transcriber._convert_result_format(mock_whisper_output)
assert isinstance(result, TranscriptionData)
assert result.text == "Hello world, this is a test."
assert result.source == "LocalWhisper"
assert len(result.segments) == 2
assert len(result.words) == 6 # 2 + 4 words
def test_convert_result_format_segments(self, transcriber, mock_whisper_output):
"""Test that segments are correctly converted."""
result = transcriber._convert_result_format(mock_whisper_output)
# Check first segment
seg1 = result.segments[0]
assert seg1.text == "Hello world,"
assert seg1.start_time == 0.0
assert seg1.end_time == 1.5
assert len(seg1.words) == 2
# Check second segment
seg2 = result.segments[1]
assert seg2.text == "this is a test."
assert seg2.start_time == 1.6
assert seg2.end_time == 3.0
assert len(seg2.words) == 4
def test_convert_result_format_words(self, transcriber, mock_whisper_output):
"""Test that words are correctly converted with IDs."""
result = transcriber._convert_result_format(mock_whisper_output)
# Check first word
word1 = result.words[0]
assert word1.text == "Hello"
assert word1.start_time == 0.0
assert word1.end_time == 0.5
assert word1.confidence == 0.95
assert word1.id is not None # Should have generated ID
# Check that all words have unique IDs
word_ids = [w.id for w in result.words]
assert len(word_ids) == len(set(word_ids)) # All unique
def test_convert_result_format_metadata(self, transcriber, mock_whisper_output):
"""Test that metadata is correctly set."""
result = transcriber._convert_result_format(mock_whisper_output)
assert result.metadata["model_size"] == "medium"
assert result.metadata["detected_language"] == "en"
assert result.metadata["device"] == "cpu"
def test_convert_result_format_empty_segments(self, transcriber):
"""Test conversion with empty segments."""
empty_output = {
"text": "",
"segments": [],
"language": "en",
}
result = transcriber._convert_result_format(empty_output)
assert result.text == ""
assert len(result.segments) == 0
assert len(result.words) == 0
def test_convert_result_format_missing_confidence(self, transcriber):
"""Test conversion when words are missing confidence scores."""
output_no_confidence = {
"text": "Hello world",
"segments": [
{
"id": 0,
"text": "Hello world",
"start": 0.0,
"end": 1.0,
"words": [
{"text": "Hello", "start": 0.0, "end": 0.4},
{"text": "world", "start": 0.5, "end": 1.0},
],
}
],
"language": "en",
}
result = transcriber._convert_result_format(output_no_confidence)
# Words should have None confidence
assert result.words[0].confidence is None
assert result.words[1].confidence is None
def test_convert_result_format_special_characters(self, transcriber):
"""Test conversion with special characters in lyrics."""
output_special = {
"text": "Don't stop believin'!",
"segments": [
{
"id": 0,
"text": "Don't stop believin'!",
"start": 0.0,
"end": 2.0,
"words": [
{"text": "Don't", "start": 0.0, "end": 0.5, "confidence": 0.9},
{"text": "stop", "start": 0.6, "end": 1.0, "confidence": 0.95},
{"text": "believin'!", "start": 1.1, "end": 2.0, "confidence": 0.88},
],
}
],
"language": "en",
}
result = transcriber._convert_result_format(output_special)
assert result.text == "Don't stop believin'!"
assert result.words[0].text == "Don't"
assert result.words[2].text == "believin'!"
class TestLocalWhisperTranscriberTranscription:
"""Tests for actual transcription functionality."""
@pytest.fixture
def transcriber(self, mock_logger, test_cache_dir, default_config):
"""Create transcriber instance."""
return LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=default_config,
logger=mock_logger,
)
@pytest.fixture
def test_audio_file(self, test_cache_dir):
"""Create a dummy audio file for testing."""
audio_path = os.path.join(test_cache_dir, "test_audio.wav")
with open(audio_path, "wb") as f:
f.write(b"dummy audio content")
return audio_path
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._load_model")
def test_perform_transcription_calls_model(
self, mock_load_model, transcriber, test_audio_file, mock_whisper_output
):
"""Test that _perform_transcription calls the model correctly."""
# Setup mock
mock_model = MagicMock()
mock_load_model.return_value = mock_model
mock_whisper = MagicMock()
mock_whisper.transcribe_timestamped.return_value = mock_whisper_output
transcriber._whisper_module = mock_whisper
# Call transcription
result = transcriber._perform_transcription(test_audio_file)
# Verify model was loaded and transcription was called
mock_load_model.assert_called_once()
mock_whisper.transcribe_timestamped.assert_called_once()
# Verify the audio file path was passed
call_args = mock_whisper.transcribe_timestamped.call_args
assert call_args[0][1] == test_audio_file
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._load_model")
def test_perform_transcription_with_language(
self, mock_load_model, mock_logger, test_cache_dir, test_audio_file, mock_whisper_output
):
"""Test transcription with specified language."""
config = LocalWhisperConfig(language="en")
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=config,
logger=mock_logger,
)
mock_model = MagicMock()
mock_load_model.return_value = mock_model
mock_whisper = MagicMock()
mock_whisper.transcribe_timestamped.return_value = mock_whisper_output
transcriber._whisper_module = mock_whisper
transcriber._perform_transcription(test_audio_file)
# Verify language was passed
call_kwargs = mock_whisper.transcribe_timestamped.call_args[1]
assert call_kwargs["language"] == "en"
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._load_model")
def test_perform_transcription_oom_error(
self, mock_load_model, transcriber, test_audio_file
):
"""Test that OOM errors are handled gracefully."""
mock_model = MagicMock()
mock_load_model.return_value = mock_model
mock_whisper = MagicMock()
mock_whisper.transcribe_timestamped.side_effect = RuntimeError("CUDA out of memory")
transcriber._whisper_module = mock_whisper
with pytest.raises(TranscriptionError, match="out of memory"):
transcriber._perform_transcription(test_audio_file)
class TestLocalWhisperTranscriberModelLoading:
"""Tests for model loading functionality."""
@pytest.fixture
def transcriber(self, mock_logger, test_cache_dir, default_config):
"""Create transcriber instance."""
return LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=default_config,
logger=mock_logger,
)
def test_lazy_model_loading(self, transcriber):
"""Test that model is not loaded until first use."""
assert transcriber._model is None
assert transcriber._whisper_module is None
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._check_dependencies")
def test_model_caching(self, mock_check, transcriber):
"""Test that model is cached after first load."""
mock_whisper = MagicMock()
mock_model = MagicMock()
mock_whisper.load_model.return_value = mock_model
with patch.dict("sys.modules", {"whisper_timestamped": mock_whisper}):
# First load
model1 = transcriber._load_model()
# Second load should return cached model
model2 = transcriber._load_model()
assert model1 is model2
# load_model should only be called once
mock_whisper.load_model.assert_called_once()
@patch("lyrics_transcriber.transcribers.local_whisper.LocalWhisperTranscriber._check_dependencies")
def test_load_model_with_custom_cache_dir(self, mock_check, mock_logger, test_cache_dir):
"""Test model loading with custom cache directory."""
custom_cache = "/custom/whisper/cache"
config = LocalWhisperConfig(cache_dir=custom_cache)
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=config,
logger=mock_logger,
)
mock_whisper = MagicMock()
mock_model = MagicMock()
mock_whisper.load_model.return_value = mock_model
with patch.dict("sys.modules", {"whisper_timestamped": mock_whisper}):
transcriber._load_model()
# Verify custom cache dir was passed
call_kwargs = mock_whisper.load_model.call_args[1]
assert call_kwargs["download_root"] == custom_cache
class TestLocalWhisperTranscriberIntegration:
"""Integration tests for full transcription flow."""
@pytest.fixture(autouse=True)
def setup_teardown(self):
"""Clean up test environment."""
test_cache_dir = tempfile.mkdtemp(prefix="test_local_whisper_integration_")
os.environ["LYRICS_TRANSCRIBER_CACHE_DIR"] = test_cache_dir
yield test_cache_dir
if os.path.exists(test_cache_dir):
shutil.rmtree(test_cache_dir)
if "LYRICS_TRANSCRIBER_CACHE_DIR" in os.environ:
del os.environ["LYRICS_TRANSCRIBER_CACHE_DIR"]
def test_full_transcription_flow(self, setup_teardown, mock_logger, mock_whisper_output):
"""Test complete transcription flow from audio to TranscriptionData."""
test_cache_dir = setup_teardown
# Create test audio file
audio_path = os.path.join(test_cache_dir, "test.wav")
with open(audio_path, "wb") as f:
f.write(b"dummy audio content for testing")
config = LocalWhisperConfig(model_size="tiny", device="cpu")
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=config,
logger=mock_logger,
)
# Mock the model and transcription
mock_whisper = MagicMock()
mock_model = MagicMock()
mock_whisper.load_model.return_value = mock_model
mock_whisper.transcribe_timestamped.return_value = mock_whisper_output
with patch.dict("sys.modules", {"whisper_timestamped": mock_whisper}):
# Bypass dependency check
with patch.object(transcriber, "_check_dependencies"):
result = transcriber.transcribe(audio_path)
# Verify result
assert isinstance(result, TranscriptionData)
assert result.source == "LocalWhisper"
assert len(result.segments) == 2
assert len(result.words) == 6
# Verify caching
file_hash = transcriber._get_file_hash(audio_path)
raw_cache_path = transcriber._get_cache_path(file_hash, "raw")
converted_cache_path = transcriber._get_cache_path(file_hash, "converted")
assert os.path.exists(raw_cache_path)
assert os.path.exists(converted_cache_path)
def test_transcription_uses_cache(self, setup_teardown, mock_logger, mock_whisper_output):
"""Test that subsequent transcriptions use cache."""
test_cache_dir = setup_teardown
audio_path = os.path.join(test_cache_dir, "test.wav")
with open(audio_path, "wb") as f:
f.write(b"dummy audio content for testing")
config = LocalWhisperConfig(model_size="tiny", device="cpu")
transcriber = LocalWhisperTranscriber(
cache_dir=test_cache_dir,
config=config,
logger=mock_logger,
)
mock_whisper = MagicMock()
mock_model = MagicMock()
mock_whisper.load_model.return_value = mock_model
mock_whisper.transcribe_timestamped.return_value = mock_whisper_output
with patch.dict("sys.modules", {"whisper_timestamped": mock_whisper}):
with patch.object(transcriber, "_check_dependencies"):
# First transcription
result1 = transcriber.transcribe(audio_path)
# Second transcription should use cache
result2 = transcriber.transcribe(audio_path)
# Both results should match
assert result1.text == result2.text
assert len(result1.segments) == len(result2.segments)
# transcribe_timestamped should only be called once
mock_whisper.transcribe_timestamped.assert_called_once()
Generated
+114 -15
View File
@@ -1632,6 +1632,53 @@ requests = ">=2.16.2"
six = ">=1.12.0"
stone = ">=2,<3.3.3"
[[package]]
name = "dtw-python"
version = "1.7.2"
description = "A comprehensive implementation of dynamic time warping (DTW) algorithms."
optional = true
python-versions = ">=3.9"
groups = ["main"]
markers = "extra == \"local-whisper\""
files = [
{file = "dtw_python-1.7.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:ae97d9d1a34bf1267b5742800eebc83655d6edac7ef11eaa1da614fe15ecbf15"},
{file = "dtw_python-1.7.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:48de94c12f57bf524f70f4debdf46e4895c43e24abc71fa14f15dec8ecdce7a0"},
{file = "dtw_python-1.7.2-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4ca9ed4abdcc3a74e46d5cc8a08f892e28d1151969914839ac60ac5ff030dbfc"},
{file = "dtw_python-1.7.2-cp310-cp310-win_amd64.whl", hash = "sha256:8071c345f1dde18830d520690bb4a95dcaa50ea0bd4d260fc4368d592f1b214f"},
{file = "dtw_python-1.7.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:e5bbf17129365ee88aea98890332bb7d20e80fa78eb9c64a11175f07e4c699b3"},
{file = "dtw_python-1.7.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:0f1f075f90bc1faacfa0661cd29f070516b09ce1fc90ea353313aae8c19f5c2e"},
{file = "dtw_python-1.7.2-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:d81ddd8e03befc4041cdf66d569c0f8ba730fd7aebede0d38add7862a10f9779"},
{file = "dtw_python-1.7.2-cp311-cp311-win_amd64.whl", hash = "sha256:e5aecfee9abc5cb39f94ebb25bc74683575dda507836ab0563badfa0fbb11b2b"},
{file = "dtw_python-1.7.2-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:85ff43afd6adb64a25755f01e98ee21eb70a34c105231720e5f4104b5988741a"},
{file = "dtw_python-1.7.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:c507179cae0f38bc22b22fd63c53d99274e11aa2e3a65e6148108345da9df033"},
{file = "dtw_python-1.7.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:239cb6207f512dbd1b0a5da03269ec3ddb2dc177719fe9b6eb5149feeec1bd63"},
{file = "dtw_python-1.7.2-cp312-cp312-win_amd64.whl", hash = "sha256:3126abd69b18a2a4809790d71f5f42fcc2cb483921e17da8f11bac5ddd7d548d"},
{file = "dtw_python-1.7.2-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:7d36341701b6a049b9dc3b234fe80047c4cd7df438bb86c6236179f2ce79dcf6"},
{file = "dtw_python-1.7.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:129d860f875ded4c4e119450315eff27bcf4d7e6dee8831fd91e68c0e757219c"},
{file = "dtw_python-1.7.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1b0cf947bc5e71f4d7ad139deaa51ad02bba5458e28e5df92b94a90374f5067e"},
{file = "dtw_python-1.7.2-cp313-cp313-win_amd64.whl", hash = "sha256:61e5e4abaa7fd58ef3d63b064fa394770c095a3bbad991339b1fb934928cbc1b"},
{file = "dtw_python-1.7.2-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:1bcc6ace984ed893d688cd8b8a0676bf76f0f01647265d2a017c9f7d3bfcc71a"},
{file = "dtw_python-1.7.2-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:c77633dc1d8d9d0276aed0a782b508b415aab04ddc79bfc2c1abe647a8ccc151"},
{file = "dtw_python-1.7.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:217811332fc3b4e53290942abe45f11ae65a9ca91c77c475d38daa6b73e4d47f"},
{file = "dtw_python-1.7.2-cp314-cp314-win_amd64.whl", hash = "sha256:d7e6675e4b6179b80f8a9c1cd8db60d92f59019e8ea1f344f6b1c8c61142ed45"},
{file = "dtw_python-1.7.2-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:4d4395a97d20b84c665adef5873d3841f658885568c289d46a649484d994a6b6"},
{file = "dtw_python-1.7.2-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:e7ce66f2c72c28c657e03ba799d7a00a538ced00870b9801debeab2bf4168792"},
{file = "dtw_python-1.7.2-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f19ef9ad1ea09f373db003a191f5615c772039ff261b98e33aab186e62e13e1e"},
{file = "dtw_python-1.7.2-cp314-cp314t-win_amd64.whl", hash = "sha256:fcd8f8f484eb100362e2fe65ca27cb8b431964fceb33c64d97c4a841b5abaef8"},
{file = "dtw_python-1.7.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:d403cd4d72db13576fe318aacc792adaa895658465c28f5a92b66e845577c1e1"},
{file = "dtw_python-1.7.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:33ce34017855fecee57ef08ed03b858f6f520bee4eef744b171938ac0e5262b6"},
{file = "dtw_python-1.7.2-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:3d4438d9ba63355492d7c8ffd7455d0335dfb95f7e2ab50a7dc0ffe013d89b45"},
{file = "dtw_python-1.7.2-cp39-cp39-win_amd64.whl", hash = "sha256:97d243009705603c45c073f84abbb1d02a3fda2c9b18afabb92d012cba058509"},
{file = "dtw_python-1.7.2.tar.gz", hash = "sha256:ad832a96067c9ddc104307abcc7ed3787fc627b71aa48dd2a6a7cf208dbec020"},
]
[package.dependencies]
numpy = ">=2.0"
scipy = "*"
[package.extras]
test = ["numpy"]
[[package]]
name = "dulwich"
version = "0.24.10"
@@ -1772,14 +1819,14 @@ readthedocs = ["sphinx-rtd-theme"]
[[package]]
name = "fastapi"
version = "0.127.0"
version = "0.128.0"
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
optional = false
python-versions = ">=3.9"
groups = ["main"]
files = [
{file = "fastapi-0.127.0-py3-none-any.whl", hash = "sha256:725aa2bb904e2eff8031557cf4b9b77459bfedd63cae8427634744fd199f6a49"},
{file = "fastapi-0.127.0.tar.gz", hash = "sha256:5a9246e03dcd1fdb19f1396db30894867c1d630f5107dc167dcbc5ed1ea7d259"},
{file = "fastapi-0.128.0-py3-none-any.whl", hash = "sha256:aebd93f9716ee3b4f4fcfe13ffb7cf308d99c9f3ab5622d8877441072561582d"},
{file = "fastapi-0.128.0.tar.gz", hash = "sha256:1cc179e1cef10a6be60ffe429f79b829dce99d8de32d7acb7e6c8dfdf7f2645a"},
]
[package.dependencies]
@@ -1873,14 +1920,14 @@ platformdirs = ">=4.3.6"
[[package]]
name = "flacfetch"
version = "0.11.0"
version = "0.15.1"
description = "Search and download high-quality audio from multiple sources"
optional = false
python-versions = ">=3.8"
groups = ["main"]
files = [
{file = "flacfetch-0.11.0-py3-none-any.whl", hash = "sha256:56657cba85cf6e390b4213d3f44d9fd05603138735d01dd10ebc1f695b5f77f3"},
{file = "flacfetch-0.11.0.tar.gz", hash = "sha256:56bbc3a66f1241c831490a88c51fcbfd5d5026108daac9d41f5a23c66c1f79b3"},
{file = "flacfetch-0.15.1-py3-none-any.whl", hash = "sha256:d50beda83866a8d5567e50e6a2f714379a3c48fae23ccbe5f8153403a5979f09"},
{file = "flacfetch-0.15.1.tar.gz", hash = "sha256:d0953931e63d858d71f5e9ece61a82886b8104c2b30be0bdf7b53c97290e6610"},
]
[package.dependencies]
@@ -1889,11 +1936,11 @@ transmission-rpc = ">=7.0.0"
yt-dlp = ">=2023.0.0"
[package.extras]
all = ["fastapi (>=0.115.0)", "google-cloud-secret-manager (>=2.16.0)", "google-cloud-storage (>=2.13.0)", "httpx (>=0.28.0)", "mypy (>=1.0.0)", "pydantic (>=2.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.23.0)", "pytest-cov (>=4.0.0)", "pytest-mock (>=3.10.0)", "ruff (>=0.1.0)", "spotipy (>=2.25.1)", "types-requests (>=2.28.0)", "uvicorn[standard] (>=0.24.0)", "yt-dlp-ejs (>=0.3.0)"]
all = ["fastapi (>=0.115.0)", "google-cloud-secret-manager (>=2.16.0)", "google-cloud-storage (>=2.13.0)", "httpx (>=0.28.0)", "mypy (>=1.0.0)", "playwright (>=1.40.0)", "pydantic (>=2.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.23.0)", "pytest-cov (>=4.0.0)", "pytest-mock (>=3.10.0)", "ruff (>=0.1.0)", "spotipy (>=2.25.1)", "types-requests (>=2.28.0)", "uvicorn[standard] (>=0.24.0)", "yt-dlp-ejs (>=0.3.0)"]
api = ["fastapi (>=0.115.0)", "google-cloud-secret-manager (>=2.16.0)", "google-cloud-storage (>=2.13.0)", "httpx (>=0.28.0)", "pydantic (>=2.0.0)", "uvicorn[standard] (>=0.24.0)"]
dev = ["mypy (>=1.0.0)", "pytest (>=7.0.0)", "pytest-asyncio (>=0.23.0)", "pytest-cov (>=4.0.0)", "pytest-mock (>=3.10.0)", "ruff (>=0.1.0)", "types-requests (>=2.28.0)"]
ejs = ["yt-dlp-ejs (>=0.3.0)"]
remote = ["httpx (>=0.28.0)"]
remote = ["httpx (>=0.28.0)", "playwright (>=1.40.0)"]
spotify = ["spotipy (>=2.25.1)"]
[[package]]
@@ -4185,11 +4232,12 @@ version = "10.8.0"
description = "More routines for operating on iterables, beyond itertools"
optional = false
python-versions = ">=3.9"
groups = ["dev"]
groups = ["main", "dev"]
files = [
{file = "more_itertools-10.8.0-py3-none-any.whl", hash = "sha256:52d4362373dcf7c52546bc4af9a86ee7c4579df9a8dc268be0a2f949d376cc9b"},
{file = "more_itertools-10.8.0.tar.gz", hash = "sha256:f638ddf8a1a0d134181275fb5d58b086ead7c6a72429ad725c67503f13ba30bd"},
]
markers = {main = "extra == \"local-whisper\""}
[[package]]
name = "mpmath"
@@ -5101,6 +5149,30 @@ datalib = ["numpy (>=1)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)"]
realtime = ["websockets (>=13,<16)"]
voice-helpers = ["numpy (>=2.0.2)", "sounddevice (>=0.5.1)"]
[[package]]
name = "openai-whisper"
version = "20250625"
description = "Robust Speech Recognition via Large-Scale Weak Supervision"
optional = true
python-versions = ">=3.8"
groups = ["main"]
markers = "extra == \"local-whisper\""
files = [
{file = "openai_whisper-20250625.tar.gz", hash = "sha256:37a91a3921809d9f44748ffc73c0a55c9f366c85a3ef5c2ae0cc09540432eb96"},
]
[package.dependencies]
more-itertools = "*"
numba = "*"
numpy = "*"
tiktoken = "*"
torch = "*"
tqdm = "*"
triton = {version = ">=2", markers = "platform_machine == \"x86_64\" and sys_platform == \"linux\" or sys_platform == \"linux2\""}
[package.extras]
dev = ["black", "flake8", "isort", "pytest", "scipy"]
[[package]]
name = "opentelemetry-api"
version = "1.39.1"
@@ -5570,14 +5642,14 @@ win-unicode-console = {version = "*", markers = "platform_system == \"Windows\"
[[package]]
name = "pdfminer-six"
version = "20251107"
version = "20251227"
description = "PDF parser and analyzer"
optional = false
python-versions = ">=3.9"
python-versions = ">=3.10"
groups = ["main"]
files = [
{file = "pdfminer_six-20251107-py3-none-any.whl", hash = "sha256:c09df33e4cbe6b26b2a79248a4ffcccafaa5c5d39c9fff0e6e81567f165b5401"},
{file = "pdfminer_six-20251107.tar.gz", hash = "sha256:5fb0c553799c591777f22c0c72b77fc2522d7d10c70654e25f4c5f1fd996e008"},
{file = "pdfminer_six-20251227-py3-none-any.whl", hash = "sha256:7a41f668a74fdde9e3a6c9265e400992a8312224fb6dbc13ee8861111e330d36"},
{file = "pdfminer_six-20251227.tar.gz", hash = "sha256:1d98781cf884d7274c694e742a94ffe6326ee11ee7ea79e73e1e75c4c74d91eb"},
]
[package.dependencies]
@@ -8559,7 +8631,7 @@ description = "A language and compiler for custom Deep Learning operations"
optional = false
python-versions = "<3.15,>=3.10"
groups = ["main"]
markers = "platform_system == \"Linux\" and platform_machine == \"x86_64\""
markers = "(platform_machine == \"x86_64\" or sys_platform == \"linux2\") and (platform_system == \"Linux\" or sys_platform == \"linux\" or sys_platform == \"linux2\") and (platform_system == \"Linux\" or extra == \"local-whisper\") and (platform_machine == \"x86_64\" or extra == \"local-whisper\")"
files = [
{file = "triton-3.5.1-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:f63e34dcb32d7bd3a1d0195f60f30d2aee8b08a69a0424189b71017e23dfc3d2"},
{file = "triton-3.5.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5fc53d849f879911ea13f4a877243afc513187bc7ee92d1f2c0f1ba3169e3c94"},
@@ -9087,6 +9159,30 @@ files = [
{file = "websockets-15.0.1.tar.gz", hash = "sha256:82544de02076bafba038ce055ee6412d68da13ab47f0c60cab827346de828dee"},
]
[[package]]
name = "whisper-timestamped"
version = "1.15.9"
description = "Multi-lingual Automatic Speech Recognition (ASR) based on Whisper models, with accurate word timestamps, access to language detection confidence, several options for Voice Activity Detection (VAD), and more."
optional = true
python-versions = ">=3.7"
groups = ["main"]
markers = "extra == \"local-whisper\""
files = [
{file = "whisper_timestamped-1.15.9-py3-none-any.whl", hash = "sha256:7582e0cc45dfda78af3a35874485145c20cecb8cc4bfc4599ebb5eeb9fd2ffc2"},
{file = "whisper_timestamped-1.15.9.tar.gz", hash = "sha256:f3609d446ead6d8e0fc82021311b087efd8ef63b7fa255fa1b3a58a32b760a1e"},
]
[package.dependencies]
Cython = "*"
dtw-python = "*"
openai-whisper = "*"
[package.extras]
dev = ["matplotlib (==3.7.4)", "transformers"]
test = ["jsonschema"]
vad-auditok = ["auditok"]
vad-silero = ["onnxruntime", "torchaudio"]
[[package]]
name = "win-unicode-console"
version = "0.5"
@@ -9586,7 +9682,10 @@ files = [
[package.extras]
cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and python_version < \"3.14\"", "cffi (>=2.0.0b) ; platform_python_implementation != \"PyPy\" and python_version >= \"3.14\""]
[extras]
local-whisper = ["whisper-timestamped"]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
content-hash = "8639a26faea41348894a9f6491a95fa1a4a222e206f9d0afafd299ffc41cf5e1"
content-hash = "c14214372c6d52a86d32afc8638b2f1486aac7b4d06a444e8be7d14379bd4e25"
+10 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-gen"
version = "0.76.2"
version = "0.76.24"
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
@@ -101,6 +101,8 @@ opentelemetry-instrumentation-httpx = ">=0.41b0"
opentelemetry-instrumentation-logging = ">=0.41b0"
opentelemetry-exporter-gcp-trace = ">=1.6.0"
opentelemetry-resourcedetector-gcp = ">=1.6.0a0"
# Optional: Local Whisper transcription (install with: pip install karaoke-gen[local-whisper])
whisper-timestamped = { version = ">=1.15.0", optional = true }
[tool.poetry.group.dev.dependencies]
black = ">=23"
@@ -110,6 +112,13 @@ pytest-cov = ">=4.0"
pytest-mock = ">=3.10"
pytest-asyncio = "*"
[tool.poetry.extras]
# Local Whisper transcription support - optional, heavy dependencies
# Install with: pip install karaoke-gen[local-whisper]
# For CPU-only installation (recommended for systems without GPU):
# pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu
# pip install karaoke-gen[local-whisper]
local-whisper = ["whisper-timestamped"]
[tool.poetry.scripts]
karaoke-gen = 'karaoke_gen.utils.gen_cli:main'
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""
Fetch and display job data from Firestore.
Usage:
python scripts/get_job.py <job_id>
python scripts/get_job.py ffb0b8fa
Requires:
- google-cloud-firestore
- GOOGLE_APPLICATION_CREDENTIALS or gcloud auth application-default login
"""
import sys
import json
from google.cloud import firestore
def get_job(job_id: str) -> dict | None:
"""Fetch job document from Firestore."""
db = firestore.Client(project='nomadkaraoke')
doc = db.collection('jobs').document(job_id).get()
if doc.exists:
return doc.to_dict()
return None
def main():
if len(sys.argv) < 2:
print("Usage: python get_job.py <job_id> [--field <field_name>]")
print("\nExamples:")
print(" python get_job.py ffb0b8fa # Full job data")
print(" python get_job.py ffb0b8fa --field status # Just status")
print(" python get_job.py ffb0b8fa --field timeline # Just timeline")
sys.exit(1)
job_id = sys.argv[1]
field_filter = None
if len(sys.argv) >= 4 and sys.argv[2] == "--field":
field_filter = sys.argv[3]
data = get_job(job_id)
if data is None:
print(f"Job {job_id} not found", file=sys.stderr)
sys.exit(1)
if field_filter:
if field_filter in data:
output = data[field_filter]
else:
print(f"Field '{field_filter}' not found. Available fields:", file=sys.stderr)
print(", ".join(sorted(data.keys())), file=sys.stderr)
sys.exit(1)
else:
output = data
print(json.dumps(output, indent=2, default=str))
if __name__ == "__main__":
main()
+367
View File
@@ -0,0 +1,367 @@
#!/bin/bash
# Upload a theme to GCS
# Usage: BRANDING_DIR=/path/to/NomadBranding ./upload-theme.sh [theme_id]
set -e
THEME_ID="${1:-nomad}"
BUCKET="karaoke-gen-storage-nomadkaraoke"
THEMES_PATH="themes"
# BRANDING_DIR must be set via environment variable
BRANDING_DIR="${BRANDING_DIR:-}"
if [ -z "$BRANDING_DIR" ]; then
echo "Error: BRANDING_DIR environment variable must be set"
echo "Example: export BRANDING_DIR=/path/to/NomadBranding"
exit 1
fi
if [ ! -d "$BRANDING_DIR" ]; then
echo "Error: BRANDING_DIR does not exist: $BRANDING_DIR"
exit 1
fi
echo "=== Uploading theme: $THEME_ID to gs://$BUCKET/$THEMES_PATH/ ==="
echo "Using branding assets from: $BRANDING_DIR"
# Create temporary directory for processing
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR"' EXIT
# Function to upload the Nomad theme
upload_nomad_theme() {
echo ""
echo "=== Setting up Nomad theme ==="
THEME_PATH="$THEMES_PATH/nomad"
# Create style_params.json with GCS paths
cat > "$TEMP_DIR/style_params.json" << 'EOF'
{
"intro": {
"video_duration": 5,
"existing_image": null,
"background_color": "#000000",
"background_image": "karaoke-title-screen-background-nomad-4k.png",
"font": "AvenirNext-Bold.ttf",
"artist_color": "#ffdf6b",
"artist_text_transform": "uppercase",
"title_color": "#ffffff",
"title_gradient": null,
"title_text_transform": "uppercase",
"title_region": "370,980,3100,350",
"artist_region": "370,1400,3100,450",
"artist_gradient": null,
"extra_text": null,
"extra_text_color": "#ffffff",
"extra_text_gradient": null,
"extra_text_region": null,
"extra_text_text_transform": "uppercase"
},
"karaoke": {
"background_image": "karaoke-background-image-nomad-4k.png",
"background_color": "#000000",
"font_path": "AvenirNext-Bold.ttf",
"font": "Avenir Next Bold",
"ass_name": "Nomad",
"primary_color": "112, 112, 247, 255",
"secondary_color": "255, 255, 255, 255",
"outline_color": "26, 58, 235, 255",
"back_color": "0, 255, 0, 255",
"bold": false,
"italic": false,
"underline": false,
"strike_out": false,
"scale_x": 100,
"scale_y": 100,
"spacing": 0,
"angle": 0.0,
"border_style": 1,
"outline": 1,
"shadow": 0,
"margin_l": 0,
"margin_r": 0,
"margin_v": 0,
"encoding": 0
},
"end": {
"video_duration": 5,
"existing_image": null,
"background_color": "#000000",
"background_image": "karaoke-background-image-nomad-4k.png",
"font": "AvenirNext-Bold.ttf",
"artist_color": "#ffdf6b",
"artist_text_transform": "uppercase",
"title_color": "#ffffff",
"title_gradient": null,
"title_text_transform": "uppercase",
"title_region": "370,900,3100,350",
"artist_region": "370,1450,3100,200",
"artist_gradient": null,
"extra_text": "THANK YOU FOR SINGING!",
"extra_text_color": "#ff7acc",
"extra_text_region": "370,400,3100,400",
"extra_text_text_transform": "uppercase",
"extra_text_gradient": null
},
"cdg": {
"clear_mode": "eager",
"sync_offset": 0,
"lead_in_threshold": 300,
"lead_in_duration": 30,
"lead_in_total": 200,
"lead_in_symbols": ["/>", ">", ">", ">"],
"row": 4,
"line_tile_height": 3,
"lines_per_page": 4,
"title_color": "#ffffff",
"artist_color": "#ffdf6b",
"background_color": "#111427",
"border_color": "#111427",
"active_fill": "#7070F7",
"active_stroke": "#000000",
"inactive_fill": "#ff7acc",
"inactive_stroke": "#000000",
"instrumental_font_color": "#ffdf6b",
"font_path": "AvenirNext-Bold.ttf",
"font_size": 18,
"stroke_width": 0,
"stroke_style": "octagon",
"instrumental_gap_threshold": 1500,
"instrumental_text": "INSTRUMENTAL",
"instrumental_background": "cdg-instrumental-background-nomad-notes.png",
"instrumental_transition": "topleftmusicalnotes",
"title_screen_background": "cdg-title-screen-background-nomad-simple.png",
"title_screen_transition": "largecentertexttoplogo",
"title_artist_gap": 10,
"intro_duration_seconds": 5.0,
"first_syllable_buffer_seconds": 3.0,
"outro_background": "cdg-title-screen-background-nomad-simple.png",
"outro_transition": "largecentertexttoplogo",
"outro_text_line1": "THANK YOU FOR SINGING!",
"outro_text_line2": "nomadkaraoke.com",
"outro_line1_color": "#ffffff",
"outro_line2_color": "#ffdf6b",
"outro_line1_line2_gap": 30
}
}
EOF
echo "Uploading style_params.json..."
gsutil cp "$TEMP_DIR/style_params.json" "gs://$BUCKET/$THEME_PATH/style_params.json"
# Validate all required branding files exist before uploading
echo "Validating required branding files..."
REQUIRED_FILES=(
"$BRANDING_DIR/youtube-video-description.txt"
"$BRANDING_DIR/AvenirNext-Bold.ttf"
"$BRANDING_DIR/karaoke-title-screen-background-nomad-4k.png"
"$BRANDING_DIR/karaoke-background-image-nomad-4k.png"
"$BRANDING_DIR/cdg-instrumental-background-nomad-notes.png"
"$BRANDING_DIR/cdg-title-screen-background-nomad-simple.png"
)
for file in "${REQUIRED_FILES[@]}"; do
if [ ! -f "$file" ]; then
echo "Error: Required file not found: $file"
exit 1
fi
done
echo "All required files found."
echo "Uploading youtube_description.txt..."
gsutil cp "$BRANDING_DIR/youtube-video-description.txt" "gs://$BUCKET/$THEME_PATH/youtube_description.txt"
echo "Uploading assets..."
# Font
gsutil cp "$BRANDING_DIR/AvenirNext-Bold.ttf" "gs://$BUCKET/$THEME_PATH/assets/AvenirNext-Bold.ttf"
# Background images
gsutil cp "$BRANDING_DIR/karaoke-title-screen-background-nomad-4k.png" "gs://$BUCKET/$THEME_PATH/assets/karaoke-title-screen-background-nomad-4k.png"
gsutil cp "$BRANDING_DIR/karaoke-background-image-nomad-4k.png" "gs://$BUCKET/$THEME_PATH/assets/karaoke-background-image-nomad-4k.png"
# CDG assets
gsutil cp "$BRANDING_DIR/cdg-instrumental-background-nomad-notes.png" "gs://$BUCKET/$THEME_PATH/assets/cdg-instrumental-background-nomad-notes.png"
gsutil cp "$BRANDING_DIR/cdg-title-screen-background-nomad-simple.png" "gs://$BUCKET/$THEME_PATH/assets/cdg-title-screen-background-nomad-simple.png"
# Create preview image (use an existing image as preview for now)
echo "Uploading preview image..."
gsutil cp "$BRANDING_DIR/karaoke-background-image-nomad-4k.png" "gs://$BUCKET/$THEME_PATH/preview.png"
echo "Nomad theme uploaded successfully!"
}
# Function to upload the default theme (simple black background)
upload_default_theme() {
echo ""
echo "=== Setting up Default theme ==="
THEME_PATH="$THEMES_PATH/default"
# Create a simple default style_params.json (no custom assets)
cat > "$TEMP_DIR/default_style_params.json" << 'EOF'
{
"intro": {
"video_duration": 5,
"existing_image": null,
"background_color": "#000000",
"background_image": null,
"font": null,
"artist_color": "#ffffff",
"artist_text_transform": "uppercase",
"title_color": "#ffffff",
"title_gradient": null,
"title_text_transform": "uppercase",
"title_region": "370,980,3100,350",
"artist_region": "370,1400,3100,450"
},
"karaoke": {
"background_image": null,
"background_color": "#000000",
"font_path": null,
"font": null,
"ass_name": "Default",
"primary_color": "0, 255, 0, 255",
"secondary_color": "255, 255, 255, 255",
"outline_color": "0, 0, 0, 255",
"back_color": "0, 0, 0, 255",
"bold": false,
"italic": false,
"underline": false,
"strike_out": false,
"scale_x": 100,
"scale_y": 100,
"spacing": 0,
"angle": 0.0,
"border_style": 1,
"outline": 2,
"shadow": 0,
"margin_l": 0,
"margin_r": 0,
"margin_v": 0,
"encoding": 0
},
"end": {
"video_duration": 5,
"existing_image": null,
"background_color": "#000000",
"background_image": null,
"font": null,
"artist_color": "#ffffff",
"artist_text_transform": "uppercase",
"title_color": "#ffffff",
"title_gradient": null,
"title_text_transform": "uppercase",
"title_region": "370,900,3100,350",
"artist_region": "370,1450,3100,200",
"extra_text": null
},
"cdg": {
"clear_mode": "eager",
"sync_offset": 0,
"lead_in_threshold": 300,
"lead_in_duration": 30,
"lead_in_total": 200,
"lead_in_symbols": ["/>", ">", ">", ">"],
"row": 4,
"line_tile_height": 3,
"lines_per_page": 4,
"title_color": "#ffffff",
"artist_color": "#ffffff",
"background_color": "#000000",
"border_color": "#000000",
"active_fill": "#00ff00",
"active_stroke": "#000000",
"inactive_fill": "#ffffff",
"inactive_stroke": "#000000",
"instrumental_font_color": "#ffff00",
"font_path": null,
"font_size": 18,
"stroke_width": 1,
"stroke_style": "octagon",
"instrumental_gap_threshold": 1500,
"instrumental_text": "INSTRUMENTAL",
"instrumental_background": null,
"instrumental_transition": "default",
"title_screen_background": null,
"title_screen_transition": "default",
"title_artist_gap": 10,
"intro_duration_seconds": 5.0,
"first_syllable_buffer_seconds": 3.0,
"outro_background": null,
"outro_transition": "default",
"outro_text_line1": "THANK YOU FOR SINGING!",
"outro_text_line2": "",
"outro_line1_color": "#ffffff",
"outro_line2_color": "#ffffff",
"outro_line1_line2_gap": 30
}
}
EOF
echo "Uploading default style_params.json..."
gsutil cp "$TEMP_DIR/default_style_params.json" "gs://$BUCKET/$THEME_PATH/style_params.json"
echo "Default theme uploaded successfully!"
}
# Function to create/update the theme metadata
create_metadata() {
echo ""
echo "=== Creating theme metadata ==="
cat > "$TEMP_DIR/_metadata.json" << 'EOF'
{
"version": 1,
"themes": [
{
"id": "nomad",
"name": "Nomad Karaoke",
"description": "Professional karaoke style with golden artist text, purple sung lyrics, and beautiful backgrounds",
"is_default": true
},
{
"id": "default",
"name": "Classic",
"description": "Simple black background with white text and green highlighted lyrics",
"is_default": false
}
]
}
EOF
echo "Uploading _metadata.json..."
gsutil cp "$TEMP_DIR/_metadata.json" "gs://$BUCKET/$THEMES_PATH/_metadata.json"
echo "Metadata created successfully!"
}
# Main execution
case "$THEME_ID" in
"nomad")
upload_nomad_theme
create_metadata
;;
"default")
upload_default_theme
create_metadata
;;
"all")
upload_nomad_theme
upload_default_theme
create_metadata
;;
"metadata")
create_metadata
;;
*)
echo "Unknown theme: $THEME_ID"
echo "Usage: $0 [nomad|default|all|metadata]"
exit 1
;;
esac
echo ""
echo "=== Theme upload complete! ==="
echo "Verify with: gsutil ls -r gs://$BUCKET/$THEMES_PATH/"
+115 -2
View File
@@ -566,7 +566,120 @@ class TestAsync:
basic_karaoke_gen.input_media = None # Not a file
basic_karaoke_gen.artist = "Test Artist"
basic_karaoke_gen.title = None # Missing title
# Test that ValueError is raised
with pytest.raises(ValueError, match="Either a local file path or both artist and title must be provided"):
with pytest.raises(ValueError, match=r"Either a local file path.*or both artist and title must be provided"):
await basic_karaoke_gen.process()
@pytest.mark.asyncio
async def test_process_with_youtube_url(self, basic_karaoke_gen):
"""Test processing with a YouTube URL as input."""
# Setup - YouTube URL input
basic_karaoke_gen.input_media = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
basic_karaoke_gen.artist = "Rick Astley"
basic_karaoke_gen.title = "Never Gonna Give You Up"
# Mock dependencies
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
mock_prep_single_track.return_value = {"track": "result"}
result = await basic_karaoke_gen.process()
# Verify prep_single_track was called
mock_prep_single_track.assert_called_once()
# Verify URL mode was enabled
assert basic_karaoke_gen._use_audio_fetcher is True
assert basic_karaoke_gen._use_url_download is True
assert basic_karaoke_gen.extractor == "youtube"
assert basic_karaoke_gen.url == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Verify the result
assert len(result) == 1
assert result[0] == {"track": "result"}
@pytest.mark.asyncio
async def test_process_with_youtube_url_without_artist_title(self, basic_karaoke_gen):
"""Test processing with a YouTube URL but no artist/title."""
# Setup - YouTube URL input without artist/title
basic_karaoke_gen.input_media = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
basic_karaoke_gen.artist = None
basic_karaoke_gen.title = None
# Mock dependencies
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
mock_prep_single_track.return_value = {"track": "result"}
await basic_karaoke_gen.process()
# Verify prep_single_track was called
mock_prep_single_track.assert_called_once()
# Verify URL mode was enabled
assert basic_karaoke_gen._use_audio_fetcher is True
assert basic_karaoke_gen._use_url_download is True
# Verify extracted info has video ID as fallback
assert basic_karaoke_gen.extracted_info["id"] == "dQw4w9WgXcQ"
@pytest.mark.asyncio
async def test_process_with_youtu_be_short_url(self, basic_karaoke_gen):
"""Test processing with a youtu.be short URL."""
# Setup - Short URL input
basic_karaoke_gen.input_media = "https://youtu.be/dQw4w9WgXcQ"
basic_karaoke_gen.artist = "Rick Astley"
basic_karaoke_gen.title = "Never Gonna Give You Up"
# Mock dependencies
with patch.object(basic_karaoke_gen, 'prep_single_track', new_callable=AsyncMock) as mock_prep_single_track:
mock_prep_single_track.return_value = {"track": "result"}
await basic_karaoke_gen.process()
# Verify prep_single_track was called
mock_prep_single_track.assert_called_once()
# Verify URL mode was enabled
assert basic_karaoke_gen._use_audio_fetcher is True
assert basic_karaoke_gen._use_url_download is True
assert basic_karaoke_gen.url == "https://youtu.be/dQw4w9WgXcQ"
class TestKaraokePrepIsUrlHelper:
"""Tests for KaraokePrep._is_url() helper method."""
@pytest.fixture
def karaoke_prep(self, tmp_path):
"""Create a KaraokePrep instance for testing."""
return KaraokePrep(
artist="Test",
title="Test",
output_dir=str(tmp_path),
)
def test_is_url_http(self, karaoke_prep):
"""Test _is_url detects http URLs."""
assert karaoke_prep._is_url("http://example.com") is True
def test_is_url_https(self, karaoke_prep):
"""Test _is_url detects https URLs."""
assert karaoke_prep._is_url("https://www.youtube.com/watch?v=abc") is True
def test_is_url_youtube(self, karaoke_prep):
"""Test _is_url detects YouTube URLs."""
assert karaoke_prep._is_url("https://www.youtube.com/watch?v=dQw4w9WgXcQ") is True
assert karaoke_prep._is_url("https://youtu.be/dQw4w9WgXcQ") is True
def test_is_url_local_path(self, karaoke_prep):
"""Test _is_url returns False for local paths."""
assert karaoke_prep._is_url("/path/to/file.mp3") is False
assert karaoke_prep._is_url("./relative/path.wav") is False
assert karaoke_prep._is_url("file.flac") is False
def test_is_url_none(self, karaoke_prep):
"""Test _is_url returns False for None."""
assert karaoke_prep._is_url(None) is False
def test_is_url_empty_string(self, karaoke_prep):
"""Test _is_url returns False for empty string."""
assert karaoke_prep._is_url("") is False
+250
View File
@@ -2381,3 +2381,253 @@ class TestCreateAudioFetcherRemoteSelection:
assert isinstance(fetcher, RemoteFlacFetchAudioFetcher)
assert fetcher.api_url == "http://arg:9090"
assert fetcher.api_key == "arg_key"
class TestFlacFetchAudioFetcherDownloadFromUrl:
"""Tests for FlacFetchAudioFetcher.download_from_url() method."""
@pytest.fixture
def mock_logger(self):
"""Create a mock logger."""
return MagicMock(spec=logging.Logger)
@pytest.fixture
def fetcher(self, mock_logger):
"""Create a FlacFetchAudioFetcher instance with mocked dependencies."""
return FlacFetchAudioFetcher(logger=mock_logger)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_youtube_success(self, mock_get_manager, fetcher, tmp_path):
"""Test successful download from YouTube URL."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
output_filename="Rick Astley - Never Gonna Give You Up",
artist="Rick Astley",
title="Never Gonna Give You Up",
)
assert result.filepath == downloaded_path
assert result.artist == "Rick Astley"
assert result.title == "Never Gonna Give You Up"
assert result.provider == "YouTube"
mock_manager.download_by_id.assert_called_once_with(
source_name="YouTube",
source_id="dQw4w9WgXcQ",
output_path=str(tmp_path),
output_filename="Rick Astley - Never Gonna Give You Up",
download_url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_youtu_be_short_url(self, mock_get_manager, fetcher, tmp_path):
"""Test download from youtu.be short URL."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
result = fetcher.download_from_url(
url="https://youtu.be/dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert result.filepath == downloaded_path
assert result.provider == "YouTube"
# Should extract video ID from short URL
mock_manager.download_by_id.assert_called_once()
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["source_id"] == "dQw4w9WgXcQ"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_embed_url(self, mock_get_manager, fetcher, tmp_path):
"""Test download from YouTube embed URL."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
fetcher.download_from_url(
url="https://www.youtube.com/embed/dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["source_id"] == "dQw4w9WgXcQ"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_generates_filename_from_artist_title(self, mock_get_manager, fetcher, tmp_path):
"""Test that filename is generated from artist and title if not provided."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=test123test",
output_dir=str(tmp_path),
artist="Test Artist",
title="Test Song",
)
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["output_filename"] == "Test Artist - Test Song"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_generates_filename_from_video_id(self, mock_get_manager, fetcher, tmp_path):
"""Test that filename falls back to video ID if no artist/title."""
mock_manager = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager.download_by_id.return_value = downloaded_path
mock_get_manager.return_value = mock_manager
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
call_args = mock_manager.download_by_id.call_args
assert call_args.kwargs["output_filename"] == "dQw4w9WgXcQ"
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_raises_download_error_on_failure(self, mock_get_manager, fetcher, tmp_path):
"""Test that DownloadError is raised when download fails."""
mock_manager = MagicMock()
mock_manager.download_by_id.side_effect = Exception("Download failed")
mock_get_manager.return_value = mock_manager
with pytest.raises(DownloadError) as exc_info:
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert "Failed to download from URL" in str(exc_info.value)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_raises_on_empty_filepath(self, mock_get_manager, fetcher, tmp_path):
"""Test that DownloadError is raised when download returns no filepath."""
mock_manager = MagicMock()
mock_manager.download_by_id.return_value = None
mock_get_manager.return_value = mock_manager
with pytest.raises(DownloadError) as exc_info:
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert "Download returned no file path" in str(exc_info.value)
@patch('karaoke_gen.audio_fetcher.FlacFetchAudioFetcher._get_manager')
def test_download_from_url_creates_output_dir(self, mock_get_manager, fetcher, tmp_path):
"""Test that output directory is created if it doesn't exist."""
mock_manager = MagicMock()
mock_manager.download_by_id.return_value = str(tmp_path / "song.webm")
mock_get_manager.return_value = mock_manager
new_dir = tmp_path / "new_subdir"
assert not new_dir.exists()
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(new_dir),
)
assert new_dir.exists()
class TestRemoteFlacFetchAudioFetcherDownloadFromUrl:
"""Tests for RemoteFlacFetchAudioFetcher.download_from_url() method."""
@pytest.fixture
def mock_logger(self):
"""Create a mock logger."""
return MagicMock(spec=logging.Logger)
@pytest.fixture
def fetcher(self, mock_logger):
"""Create a RemoteFlacFetchAudioFetcher instance."""
return RemoteFlacFetchAudioFetcher(
api_url="http://localhost:8080",
api_key="test_key",
logger=mock_logger,
)
@patch('flacfetch.core.manager.FetchManager')
@patch('flacfetch.providers.youtube.YoutubeProvider')
@patch('flacfetch.downloaders.youtube.YoutubeDownloader')
def test_download_from_url_uses_local_flacfetch(
self, mock_yt_downloader, _mock_yt_provider, mock_fetch_manager, fetcher, tmp_path
):
"""Test that download_from_url uses local flacfetch (not remote API)."""
mock_manager_instance = MagicMock()
downloaded_path = str(tmp_path / "song.webm")
mock_manager_instance.download_by_id.return_value = downloaded_path
mock_fetch_manager.return_value = mock_manager_instance
result = fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
artist="Rick Astley",
title="Never Gonna Give You Up",
)
# Verify local flacfetch was used
mock_fetch_manager.assert_called_once()
mock_manager_instance.add_provider.assert_called_once()
mock_manager_instance.register_downloader.assert_called_once_with("YouTube", mock_yt_downloader.return_value)
mock_manager_instance.download_by_id.assert_called_once()
assert result.filepath == downloaded_path
assert result.artist == "Rick Astley"
assert result.title == "Never Gonna Give You Up"
assert result.provider == "YouTube"
@patch('flacfetch.core.manager.FetchManager')
@patch('flacfetch.providers.youtube.YoutubeProvider')
@patch('flacfetch.downloaders.youtube.YoutubeDownloader')
def test_download_from_url_extracts_video_id(
self, _mock_yt_downloader, _mock_yt_provider, mock_fetch_manager, fetcher, tmp_path
):
"""Test that video ID is extracted from URL correctly."""
mock_manager_instance = MagicMock()
mock_manager_instance.download_by_id.return_value = str(tmp_path / "song.webm")
mock_fetch_manager.return_value = mock_manager_instance
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=abc123xyz99",
output_dir=str(tmp_path),
)
call_args = mock_manager_instance.download_by_id.call_args
assert call_args.kwargs["source_id"] == "abc123xyz99"
@patch('flacfetch.core.manager.FetchManager')
@patch('flacfetch.providers.youtube.YoutubeProvider')
@patch('flacfetch.downloaders.youtube.YoutubeDownloader')
def test_download_from_url_raises_on_failure(
self, _mock_yt_downloader, _mock_yt_provider, mock_fetch_manager, fetcher, tmp_path
):
"""Test that DownloadError is raised on failure."""
mock_manager_instance = MagicMock()
mock_manager_instance.download_by_id.side_effect = Exception("Network error")
mock_fetch_manager.return_value = mock_manager_instance
with pytest.raises(DownloadError) as exc_info:
fetcher.download_from_url(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
output_dir=str(tmp_path),
)
assert "Failed to download from URL" in str(exc_info.value)
def test_download_from_url_method_exists(self, fetcher):
"""Test that download_from_url method exists on RemoteFlacFetchAudioFetcher."""
assert hasattr(fetcher, 'download_from_url')
assert callable(fetcher.download_from_url)