mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
## Summary Moves **every** Gemini call in gen off **Vertex AI** (billed to the `nomadkaraoke` GCP project, ~$85/mo) onto the **Gemini Developer API** using the `gemini-api-key` Secret Manager secret (an AI Studio key billed outside this project). Once deployed and verified, `aiplatform.googleapis.com` / `generativelanguage.googleapis.com` get disabled in nomadkaraoke. ### Key injection - `backend/services/gemini_client.get_genai_client(timeout_ms)` — key from `GEMINI_API_KEY`, else Secret Manager `gemini-api-key` via `settings.get_secret` (runtime SA already has project-level `secretAccessor`). No `project`/`location`. - Service: CI `--set-secrets ... GEMINI_API_KEY=gemini-api-key:latest`. - Jobs: Pulumi secret env on `lyrics-transcription-job`, `video-encoding-job` (renders + translations), `nomad-error-monitor` — **already applied** (targeted `pulumi up`, only that env added). Other jobs fall back to Secret Manager. - Local scripts (`frontend/scripts/translate.py`, `scripts/measure_autocorrect_cost.py`, `scripts/test_agentic_correction.py`): env, else `gcloud secrets versions access` (in-process only). - **Rotation:** add a new secret version; new instances / job executions pick it up. No code change. ### Degradation (quota / billing / invalid key; per-minute 429s stay retryable) | Caller | Behaviour | |---|---| | auto-correct | non-retryable 503 → compare mode drops the Gemini leg, keeps Opus | | match-judge / free-text / parse-titles / tenant bulk | existing fallbacks (no suggestion / blanks / regex) | | credit eval | `pending_review` (fail-closed, unchanged) | | lyrics translation | no retry; render proceeds without translation, status recorded | | custom lyrics | clean 503 "temporarily unavailable…" (+ 5-min timeout so it can't hang) | | error-monitor LLM | no retry; alerts sent without AI summary | | translate.py | exit 2 "Gemini quota/credit exhausted — top up AI Studio or rotate gemini-api-key" | `note_gemini_failure()` also sends one Discord ops alert per 6h (Firestore-throttled across instances via new `ops_alerts.send_throttled_ops_alert`). ### Infra - Pulumi: removes `roles/aiplatform.user` from `karaoke-backend` — **will be applied after this deploys and Vertex traffic is confirmed at zero**. - Agentic correction default model `gemini/gemini-3.8-flash` (`vertexai/` prefix still accepted, routed to the Developer API). - Version 0.261.1 → 0.262.0. Docs: CLAUDE.md, LESSONS-LEARNED, README, ARCHITECTURE, API, etc. ## Testing - New: `backend/tests/services/test_backend_gemini_client.py` (classifier, key resolution, no-Vertex client kwargs, alert throttle, per-caller degradation), `tests/unit/lyrics_transcriber/test_agentic_gemini_provider.py`, `frontend/scripts/test_gemini_client.py`, lyrics-translation quota test. - `pytest backend/tests --ignore=backend/tests/emulator` — all pass; `pytest tests/unit` — 2021 passed. - Verified `gemini-3.8-flash` is listed on the Developer API and answered a 5-token test call with the key. - `translate.py --dry-run` on `frontend/messages` and `backend/translations` OK. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Updates** * AI-powered lyric translation, custom lyrics, auto-correction, title parsing, match judging, and related features now use the Gemini Developer API. * AI features use the configured Gemini API key. Quota, billing, or key issues may show an unavailable message or use an existing fallback instead of repeated attempts. * **Documentation** * Updated product, API, setup, and recovery guidance to reflect Gemini API key configuration and usage. * Updated guidance for handling AI service availability and quota issues. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
import os
|
|
import pytest
|
|
|
|
# Gemini Developer API key: never let a test fall through to Secret Manager.
|
|
os.environ.setdefault("GEMINI_API_KEY", "test-gemini-key")
|
|
import tempfile
|
|
import logging
|
|
from unittest.mock import MagicMock
|
|
from karaoke_gen.karaoke_gen import KaraokePrep
|
|
from karaoke_gen.style_loader import (
|
|
DEFAULT_INTRO_STYLE,
|
|
DEFAULT_END_STYLE,
|
|
DEFAULT_KARAOKE_STYLE,
|
|
DEFAULT_CDG_STYLE,
|
|
)
|
|
import inspect
|
|
|
|
@pytest.fixture
|
|
def mock_logger():
|
|
"""Return a mock logger for testing."""
|
|
return MagicMock(spec=logging.Logger)
|
|
|
|
@pytest.fixture
|
|
def mock_ffmpeg():
|
|
"""Return a mock for os.system to avoid executing ffmpeg commands."""
|
|
return MagicMock()
|
|
|
|
@pytest.fixture
|
|
def temp_dir():
|
|
"""Create a temporary directory for test files."""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
yield temp_dir
|
|
|
|
@pytest.fixture
|
|
def complete_theme():
|
|
"""
|
|
Return a complete theme with all required sections and fields.
|
|
|
|
This is the standard fixture for testing code that requires themes.
|
|
Use this instead of relying on DEFAULT_* constants with fallback logic.
|
|
"""
|
|
return {
|
|
"intro": DEFAULT_INTRO_STYLE.copy(),
|
|
"end": DEFAULT_END_STYLE.copy(),
|
|
"karaoke": DEFAULT_KARAOKE_STYLE.copy(),
|
|
"cdg": DEFAULT_CDG_STYLE.copy(),
|
|
}
|
|
|
|
@pytest.fixture
|
|
def basic_karaoke_gen(mock_logger, mock_ffmpeg, temp_dir):
|
|
"""Return a basic KaraokePrep instance for testing."""
|
|
with MagicMock() as mock_os_system:
|
|
# Create a KaraokePrep instance with required artist/title and a mock logger
|
|
karaoke_gen = KaraokePrep(
|
|
logger=mock_logger,
|
|
artist="Test Artist",
|
|
title="Test Title",
|
|
output_dir=temp_dir,
|
|
)
|
|
|
|
# Replace the ffmpeg_base_command with a mock
|
|
karaoke_gen.ffmpeg_base_command = "mock_ffmpeg"
|
|
|
|
# Mock os.system to avoid executing commands
|
|
karaoke_gen._os_system = mock_os_system
|
|
|
|
yield karaoke_gen
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
"""Mark async tests with asyncio marker."""
|
|
for item in items:
|
|
if inspect.iscoroutinefunction(item.obj):
|
|
item.add_marker(pytest.mark.asyncio)
|