Initial commit
This commit is contained in:
@@ -0,0 +1,715 @@
|
||||
"""Kommandozeilenschnittstelle von SjAudioSearch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
import sys
|
||||
from dataclasses import dataclass, replace
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import typer
|
||||
from rich.console import Console
|
||||
from rich.markup import escape
|
||||
from rich.panel import Panel
|
||||
from rich.progress import (
|
||||
BarColumn,
|
||||
MofNCompleteColumn,
|
||||
Progress,
|
||||
SpinnerColumn,
|
||||
TextColumn,
|
||||
TimeElapsedColumn,
|
||||
)
|
||||
from rich.table import Table
|
||||
|
||||
from . import __version__
|
||||
from .config import Config, load_config
|
||||
from .core.audio import PlaybackError, play as play_audio
|
||||
from .core.metadata import AudioMetadataError, read_metadata
|
||||
from .core.models import FileStatus
|
||||
from .core.scanner import scan as run_scan
|
||||
from .database.connection import check_fts5
|
||||
from .database.repository import Repository
|
||||
from .search.engine import SearchEngine, SearchError
|
||||
from .system.diagnostics import collect_report, render_report
|
||||
from .system.gpu import collect_runtime_info, detect_gpu, resolve_execution
|
||||
from .transcription.engine import FasterWhisperEngine, TranscriptionError
|
||||
from .transcription.worker import RunStats, TranscriptionWorker, WorkerEvent
|
||||
from .utils.formatting import (
|
||||
format_datetime,
|
||||
format_duration,
|
||||
format_rate,
|
||||
format_score,
|
||||
format_size,
|
||||
format_timestamp_verbose,
|
||||
shorten_path,
|
||||
)
|
||||
from .utils.logging import get_logger, setup_logging
|
||||
|
||||
app = typer.Typer(
|
||||
add_completion=False,
|
||||
no_args_is_help=True,
|
||||
help="Lokale Suchmaschine fuer transkribierte Hoerspiele und Hoerbuecher.",
|
||||
)
|
||||
|
||||
console = Console()
|
||||
logger = get_logger("cli")
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppContext:
|
||||
"""Globaler Zustand der CLI."""
|
||||
|
||||
config: Config
|
||||
console: Console
|
||||
_repository: Repository | None = None
|
||||
|
||||
def repository(self) -> Repository:
|
||||
if self._repository is None:
|
||||
self._repository = Repository.open(self.config.database_path)
|
||||
return self._repository
|
||||
|
||||
def close(self) -> None:
|
||||
if self._repository is not None:
|
||||
self._repository.close()
|
||||
self._repository = None
|
||||
|
||||
|
||||
def _fail(message: str, code: int = 1) -> None:
|
||||
console.print(f"[red]Fehler:[/red] {escape(message)}")
|
||||
raise typer.Exit(code)
|
||||
|
||||
|
||||
def _get_context(ctx: typer.Context) -> AppContext:
|
||||
if not isinstance(ctx.obj, AppContext):
|
||||
_fail("Interner Fehler: CLI-Kontext fehlt.")
|
||||
return ctx.obj
|
||||
|
||||
|
||||
@app.callback()
|
||||
def main(
|
||||
ctx: typer.Context,
|
||||
config: Optional[Path] = typer.Option(
|
||||
None, "--config", "-c", help="Pfad zur config.toml", show_default=False
|
||||
),
|
||||
version: bool = typer.Option(False, "--version", help="Version anzeigen", is_eager=True),
|
||||
) -> None:
|
||||
"""SjAudioSearch initialisieren."""
|
||||
if version:
|
||||
console.print(f"SjAudioSearch {__version__}")
|
||||
raise typer.Exit()
|
||||
|
||||
cfg = load_config(config)
|
||||
setup_logging(cfg.log_path, cfg.logging.level)
|
||||
app_context = AppContext(config=cfg, console=console)
|
||||
ctx.obj = app_context
|
||||
ctx.call_on_close(app_context.close)
|
||||
|
||||
for warning in cfg.load_warnings:
|
||||
console.print(f"[yellow]Hinweis:[/yellow] {escape(warning)}")
|
||||
for key in cfg.unknown_keys:
|
||||
console.print(f"[yellow]Hinweis:[/yellow] unbekannter Konfigurationsschluessel '{escape(key)}'")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# diagnose
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def diagnose(
|
||||
ctx: typer.Context,
|
||||
device: Optional[str] = typer.Option(None, help="Zu pruefendes Geraet: cuda oder cpu"),
|
||||
compute_type: Optional[str] = typer.Option(None, "--compute-type", help="Zu pruefender Compute Type"),
|
||||
) -> None:
|
||||
"""Zeigt einen vollstaendigen Systembericht (GPU, CUDA, Compute Types)."""
|
||||
app_context = _get_context(ctx)
|
||||
report = collect_report(
|
||||
app_context.config,
|
||||
requested_device=device,
|
||||
requested_compute_type=compute_type,
|
||||
)
|
||||
render_report(report, app_context.console)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# scan
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def scan(
|
||||
ctx: typer.Context,
|
||||
path: Path = typer.Argument(..., help="Ordner mit Audiodateien"),
|
||||
) -> None:
|
||||
"""Durchsucht einen Ordner rekursiv und nimmt neue Dateien in den Index auf."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
|
||||
with Progress(
|
||||
SpinnerColumn(),
|
||||
TextColumn("[bold]{task.description}"),
|
||||
BarColumn(),
|
||||
MofNCompleteColumn(),
|
||||
TimeElapsedColumn(),
|
||||
console=app_context.console,
|
||||
transient=True,
|
||||
) as progress:
|
||||
task = progress.add_task("Scanne Dateien", total=None)
|
||||
|
||||
def on_progress(current: int, current_path: Path) -> None:
|
||||
progress.update(
|
||||
task,
|
||||
total=max(current, progress.tasks[0].total or 0),
|
||||
description=f"Scanne {escape(current_path.name)}",
|
||||
)
|
||||
|
||||
try:
|
||||
result = run_scan(path, repository, app_context.config.processing, progress=on_progress)
|
||||
except (FileNotFoundError, NotADirectoryError) as exc:
|
||||
_fail(str(exc))
|
||||
|
||||
table = Table(title="Scan-Ergebnis", show_header=False, box=None)
|
||||
table.add_column(style="bold cyan")
|
||||
table.add_column()
|
||||
table.add_row("Ordner", result.root)
|
||||
table.add_row("Gefundene Audiodateien", str(result.files_found))
|
||||
table.add_row("Neu aufgenommen", str(result.added))
|
||||
table.add_row("Geaendert", str(result.changed))
|
||||
table.add_row("Unveraendert", str(result.unchanged))
|
||||
table.add_row("Duplikate", str(result.duplicates))
|
||||
table.add_row("Fehler", str(len(result.errors)))
|
||||
console.print(table)
|
||||
|
||||
if result.errors:
|
||||
error_table = Table(title="Dateien mit Problemen")
|
||||
error_table.add_column("Datei", overflow="fold")
|
||||
error_table.add_column("Grund", overflow="fold")
|
||||
for item in result.errors[:15]:
|
||||
error_table.add_row(shorten_path(item.path, 70), escape(item.message))
|
||||
console.print(error_table)
|
||||
console.print(
|
||||
"[yellow]Hinweis:[/yellow] Diese Dateien wurden mit Status 'error' gespeichert. "
|
||||
"Details stehen im Log."
|
||||
)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# transcribe
|
||||
# ----------------------------------------------------------------------
|
||||
def _resolve_transcription(ctx_app: AppContext, model: str | None, device: str | None,
|
||||
compute_type: str | None, language: str | None):
|
||||
config = ctx_app.config
|
||||
settings = config.transcription
|
||||
if model:
|
||||
settings = replace(settings, model=model)
|
||||
if language is not None:
|
||||
settings = replace(settings, language=language)
|
||||
|
||||
requested_device = device or settings.device
|
||||
requested_compute = compute_type or settings.compute_type
|
||||
gpu = detect_gpu()
|
||||
runtime = collect_runtime_info(check_fts=False)
|
||||
resolved = resolve_execution(requested_device, requested_compute, gpu, runtime)
|
||||
return settings, resolved, gpu, runtime
|
||||
|
||||
|
||||
@app.command()
|
||||
def transcribe(
|
||||
ctx: typer.Context,
|
||||
model: Optional[str] = typer.Option(None, help="Modell, z. B. large-v3, medium, small"),
|
||||
device: Optional[str] = typer.Option(None, help="cuda oder cpu"),
|
||||
compute_type: Optional[str] = typer.Option(None, "--compute-type", help="z. B. int8_float32, float32"),
|
||||
language: Optional[str] = typer.Option(None, help="Sprache, z. B. de oder auto"),
|
||||
limit: Optional[int] = typer.Option(None, help="Maximale Anzahl Dateien in diesem Lauf"),
|
||||
force: bool = typer.Option(False, "--force", help="Bereits transkribierte Dateien erneut verarbeiten"),
|
||||
) -> None:
|
||||
"""Arbeitet die Transkriptionswarteschlange ab."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
config = app_context.config
|
||||
|
||||
settings, resolved, gpu, runtime = _resolve_transcription(
|
||||
app_context, model, device, compute_type, language
|
||||
)
|
||||
|
||||
mode_table = Table(show_header=False, box=None, padding=(0, 1))
|
||||
mode_table.add_column(style="bold cyan", no_wrap=True)
|
||||
mode_table.add_column()
|
||||
mode_table.add_row("Modell", settings.model)
|
||||
mode_table.add_row("Geraet", f"{resolved.device}" + (" (GPU)" if resolved.device == "cuda" else " (CPU)"))
|
||||
mode_table.add_row("Compute Type", resolved.compute_type)
|
||||
mode_table.add_row("Sprache", settings.language_for_whisper or "automatisch")
|
||||
if gpu is not None:
|
||||
mode_table.add_row(
|
||||
"GPU", f"{gpu.name} (CC {gpu.compute_capability or '?'}, {gpu.memory_total_mb or '?'} MB)"
|
||||
)
|
||||
console.print(Panel(mode_table, title="Transkription", border_style="cyan"))
|
||||
|
||||
for note in resolved.notes:
|
||||
console.print(f"[yellow]Hinweis:[/yellow] {escape(note)}")
|
||||
if not runtime.cuda_runtime_usable and resolved.device == "cpu":
|
||||
console.print(
|
||||
"[yellow]Hinweis:[/yellow] CPU-Modus aktiv. Die Verarbeitung wird deutlich langsamer sein."
|
||||
)
|
||||
|
||||
queue = repository.transcription_queue(force=force)
|
||||
if limit is not None:
|
||||
queue = queue[: max(0, limit)]
|
||||
if not queue:
|
||||
console.print("Keine Dateien in der Warteschlange. Nutze zuerst 'sjaudiosearch scan <ordner>'.")
|
||||
return
|
||||
|
||||
engine = FasterWhisperEngine(
|
||||
settings,
|
||||
device=resolved.device,
|
||||
compute_type=resolved.compute_type,
|
||||
model_dir=config.model_path,
|
||||
)
|
||||
with console.status(f"Lade Modell {settings.model} ...", spinner="dots"):
|
||||
try:
|
||||
engine.load()
|
||||
except TranscriptionError as exc:
|
||||
if resolved.device == "cuda":
|
||||
console.print(f"[yellow]GPU-Modus fehlgeschlagen:[/yellow] {escape(str(exc))}")
|
||||
console.print("[yellow]Wechsle automatisch auf CPU (int8).[/yellow]")
|
||||
engine = FasterWhisperEngine(
|
||||
settings,
|
||||
device="cpu",
|
||||
compute_type="int8",
|
||||
model_dir=config.model_path,
|
||||
)
|
||||
resolved = replace(resolved, device="cpu", compute_type="int8")
|
||||
try:
|
||||
engine.load()
|
||||
except TranscriptionError as cpu_exc:
|
||||
_fail(f"Auch der CPU-Modus ist fehlgeschlagen: {cpu_exc}")
|
||||
else:
|
||||
_fail(str(exc))
|
||||
|
||||
worker = TranscriptionWorker(
|
||||
repository,
|
||||
engine,
|
||||
config.processing,
|
||||
device=resolved.device,
|
||||
compute_type=resolved.compute_type,
|
||||
)
|
||||
|
||||
columns = [
|
||||
SpinnerColumn(),
|
||||
TextColumn("[bold]{task.description}"),
|
||||
BarColumn(),
|
||||
MofNCompleteColumn(),
|
||||
TimeElapsedColumn(),
|
||||
TextColumn("{task.fields[rate]}"),
|
||||
]
|
||||
errors: list[tuple[str, str]] = []
|
||||
|
||||
with Progress(*columns, console=console) as progress:
|
||||
task = progress.add_task("Starte Transkription", total=len(queue), rate="")
|
||||
|
||||
def on_event(event: WorkerEvent) -> None:
|
||||
if event.kind == "start" and event.record:
|
||||
progress.update(
|
||||
task, description=escape(event.record.filename), rate=""
|
||||
)
|
||||
elif event.kind == "done":
|
||||
stats = worker.stats
|
||||
progress.update(
|
||||
task,
|
||||
advance=1,
|
||||
rate=format_rate(stats.audio_seconds, stats.elapsed) if stats.elapsed else "",
|
||||
)
|
||||
elif event.kind == "error" and event.record:
|
||||
errors.append((event.record.path, event.message or "unbekannter Fehler"))
|
||||
progress.update(task, advance=1, rate="[red]Fehler[/red]")
|
||||
|
||||
try:
|
||||
stats = worker.run(limit=limit, force=force, event_callback=on_event)
|
||||
except KeyboardInterrupt:
|
||||
console.print()
|
||||
console.print("[yellow]Abgebrochen.[/yellow] Der aktuelle Stand ist gespeichert, "
|
||||
"die Unterbrechung wird beim naechsten Lauf fortgesetzt.")
|
||||
raise typer.Exit(130)
|
||||
except TranscriptionError as exc:
|
||||
_fail(str(exc))
|
||||
|
||||
_render_run_summary(stats, settings.model, resolved.device, resolved.compute_type, errors)
|
||||
|
||||
|
||||
def _render_run_summary(
|
||||
stats: RunStats, model: str, device: str, compute_type: str, errors: list[tuple[str, str]]
|
||||
) -> None:
|
||||
table = Table(title="Transkription abgeschlossen", show_header=False, box=None)
|
||||
table.add_column(style="bold cyan", no_wrap=True)
|
||||
table.add_column()
|
||||
table.add_row("Modell", model)
|
||||
table.add_row("Ausfuehrung", f"{device} / {compute_type}")
|
||||
table.add_row("Warteschlange", str(stats.queued))
|
||||
table.add_row("Erfolgreich", str(stats.processed))
|
||||
table.add_row("Fehlgeschlagen", str(stats.failed))
|
||||
table.add_row("Verarbeitete Audiodauer", format_duration(stats.audio_seconds))
|
||||
table.add_row("Laufzeit", format_duration(stats.elapsed))
|
||||
table.add_row("Durchschnitt", f"{format_rate(stats.audio_seconds, stats.elapsed)}")
|
||||
console.print(table)
|
||||
|
||||
if errors:
|
||||
error_table = Table(title="Fehlerhafte Dateien")
|
||||
error_table.add_column("Datei", overflow="fold")
|
||||
error_table.add_column("Grund", overflow="fold")
|
||||
for path, message in errors[:15]:
|
||||
error_table.add_row(shorten_path(path, 70), escape(message))
|
||||
console.print(error_table)
|
||||
console.print("Mit [bold]sjaudiosearch retry[/bold] koennen fehlgeschlagene Dateien erneut versucht werden.")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# search
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def search(
|
||||
ctx: typer.Context,
|
||||
query: str = typer.Argument(..., help="Gesuchter Text (auch ungenau)"),
|
||||
limit: Optional[int] = typer.Option(None, help="Maximale Anzahl Treffer"),
|
||||
min_score: Optional[float] = typer.Option(None, "--min-score", help="Mindestaehnlichkeit 0-100"),
|
||||
context_words: Optional[int] = typer.Option(None, "--context-words", help="Woerter vor/nach dem Treffer"),
|
||||
language: Optional[str] = typer.Option(None, help="Nur Transkripte dieser Sprache"),
|
||||
json_output: bool = typer.Option(False, "--json", help="Ergebnisse als JSON ausgeben"),
|
||||
verbose: bool = typer.Option(False, "--verbose", help="Zusaetzliche Details anzeigen"),
|
||||
) -> None:
|
||||
"""Sucht in allen Transkripten - auch bei ungenauen Erinnerungen."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
config = app_context.config
|
||||
engine = SearchEngine(repository, config.search)
|
||||
|
||||
effective_limit = limit if limit is not None else config.search.default_limit
|
||||
effective_min = min_score if min_score is not None else config.search.default_min_score
|
||||
effective_context = context_words if context_words is not None else config.search.context_words
|
||||
|
||||
progress_state: dict[str, int] = {}
|
||||
|
||||
with console.status("[cyan]Suche ...[/cyan]", spinner="dots") as status:
|
||||
|
||||
def on_progress(current: int, total: int) -> None:
|
||||
progress_state["current"] = current
|
||||
progress_state["total"] = total
|
||||
status.update(f"[cyan]Suche ... {current}/{total} Dateien[/cyan]")
|
||||
|
||||
try:
|
||||
hits = engine.search(
|
||||
query,
|
||||
limit=effective_limit,
|
||||
min_score=effective_min,
|
||||
context_words=effective_context,
|
||||
language=language,
|
||||
progress=on_progress,
|
||||
)
|
||||
except SearchError as exc:
|
||||
_fail(str(exc))
|
||||
except sqlite3.Error as exc:
|
||||
_fail(f"Datenbankfehler bei der Suche: {exc}")
|
||||
|
||||
if json_output:
|
||||
payload = {
|
||||
"query": query,
|
||||
"limit": effective_limit,
|
||||
"min_score": effective_min,
|
||||
"context_words": effective_context,
|
||||
"count": len(hits),
|
||||
"results": [hit.to_dict(effective_context) for hit in hits],
|
||||
}
|
||||
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
||||
return
|
||||
|
||||
console.print(Panel.fit("[bold]SjAudioSearch[/bold]", border_style="cyan"))
|
||||
console.print(f'Query: [bold]"{escape(query)}"[/bold]')
|
||||
console.print(
|
||||
f"[bold]{len(hits)}[/bold] Treffer (Mindestaehnlichkeit {effective_min:.0f}%, "
|
||||
f"Kontext {effective_context} Woerter)"
|
||||
)
|
||||
if not hits:
|
||||
console.print()
|
||||
console.print(
|
||||
"[yellow]Keine Treffer.[/yellow] Versuche eine hoehere Kontextlaenge, "
|
||||
"eine niedrigere Mindestaehnlichkeit oder ein anderes Modell."
|
||||
)
|
||||
|
||||
for index, hit in enumerate(hits, start=1):
|
||||
header = f"[bold][{index}] {escape(hit.filename)}[/bold]"
|
||||
details = [
|
||||
f"Similarity: [bold green]{format_score(hit.score)}[/bold green]",
|
||||
f"Timestamp: [bold]{format_timestamp_verbose(hit.start_time)}[/bold] "
|
||||
f"(bis {format_timestamp_verbose(hit.end_time)}, "
|
||||
f"{'Wort' if hit.context.precision == 'word' else 'Segment'}-Genauigkeit)",
|
||||
]
|
||||
if verbose:
|
||||
details.append(
|
||||
f"Audio-ID: {hit.audio_id} | Sprache: {hit.language or '?'} | Modell: {escape(hit.model or '?')} | "
|
||||
f"Quelle: {hit.source}"
|
||||
)
|
||||
body = "\n".join(details)
|
||||
console.print()
|
||||
console.print(header)
|
||||
console.print(body)
|
||||
console.print()
|
||||
console.print(Panel(hit.context.as_block(), title="Context", border_style="dim"))
|
||||
console.print(f"Path: {escape(hit.path)}")
|
||||
console.print("[dim]" + "-" * 40 + "[/dim]")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# status
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def status(ctx: typer.Context) -> None:
|
||||
"""Zeigt Kennzahlen der Sammlung und den Datenbankzustand."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
stats = repository.stats(repository.databases_size())
|
||||
config = app_context.config
|
||||
|
||||
table = Table(title="SjAudioSearch Status", show_header=False, box=None)
|
||||
table.add_column(style="bold cyan", no_wrap=True)
|
||||
table.add_column()
|
||||
table.add_row("Audiodateien gesamt", str(stats.total_files))
|
||||
table.add_row("Transkribiert", str(stats.done))
|
||||
table.add_row("Ausstehend", str(stats.pending))
|
||||
table.add_row("In Bearbeitung", str(stats.processing))
|
||||
table.add_row("Fehlgeschlagen", str(stats.error))
|
||||
table.add_row("Duplikate", str(stats.duplicates))
|
||||
table.add_row("Gesamtspieldauer", format_duration(stats.total_duration))
|
||||
table.add_row("Transkripte", str(stats.transcripts))
|
||||
table.add_row("Segmente", str(stats.segment_count))
|
||||
table.add_row("Woerter", str(stats.word_count))
|
||||
table.add_row("Datenbank", f"{config.database_path} ({format_size(stats.database_size)})")
|
||||
model_text = ", ".join(stats.models) if stats.models else f"{config.transcription.model} (noch nicht verwendet)"
|
||||
table.add_row("Transkriptionsmodell", model_text)
|
||||
console.print(table)
|
||||
|
||||
if stats.error:
|
||||
console.print("[yellow]Hinweis:[/yellow] 'sjaudiosearch retry' setzt fehlgeschlagene Dateien zurueck.")
|
||||
if stats.pending or stats.processing:
|
||||
console.print("[yellow]Hinweis:[/yellow] 'sjaudiosearch transcribe' arbeitet die Warteschlange ab.")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# list
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command("list")
|
||||
def list_files(
|
||||
ctx: typer.Context,
|
||||
limit: Optional[int] = typer.Option(None, help="Maximale Anzahl Eintraege"),
|
||||
status_filter: Optional[str] = typer.Option(
|
||||
None, "--status", help="pending, processing, done oder error"
|
||||
),
|
||||
search: Optional[str] = typer.Option(None, "--search", help="Dateiname/Pfad filtern"),
|
||||
) -> None:
|
||||
"""Listet bekannte Audiodateien auf."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
if status_filter and status_filter not in FileStatus.values():
|
||||
_fail(f"Unbekannter Status '{status_filter}'. Erlaubt: {', '.join(FileStatus.values())}")
|
||||
|
||||
records = repository.list_audio(status=status_filter, limit=limit, search=search)
|
||||
if not records:
|
||||
console.print("Keine Dateien gefunden. Nutze 'sjaudiosearch scan <ordner>'.")
|
||||
return
|
||||
|
||||
table = Table(title=f"Audiodateien ({len(records)})", show_lines=False)
|
||||
table.add_column("ID", justify="right", style="cyan")
|
||||
table.add_column("Status")
|
||||
table.add_column("Dauer", justify="right")
|
||||
table.add_column("Groesse", justify="right")
|
||||
table.add_column("Datei", overflow="fold")
|
||||
table.add_column("Pfad", overflow="fold")
|
||||
|
||||
colors = {
|
||||
FileStatus.DONE.value: "green",
|
||||
FileStatus.PENDING.value: "yellow",
|
||||
FileStatus.PROCESSING.value: "cyan",
|
||||
FileStatus.ERROR.value: "red",
|
||||
}
|
||||
for record in records:
|
||||
status_text = record.status
|
||||
if record.duplicate_of:
|
||||
status_text = f"duplikat von #{record.duplicate_of}"
|
||||
color = colors.get(record.status, "white")
|
||||
table.add_row(
|
||||
str(record.id),
|
||||
f"[{color}]{status_text}[/{color}]",
|
||||
format_duration(record.duration),
|
||||
format_size(record.file_size),
|
||||
escape(record.filename),
|
||||
escape(shorten_path(record.path, 70)),
|
||||
)
|
||||
console.print(table)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# info
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def info(
|
||||
ctx: typer.Context,
|
||||
audio_id: int = typer.Argument(..., help="ID der Audiodatei"),
|
||||
) -> None:
|
||||
"""Zeigt Details zu einer Audiodatei und ihrem Transkript."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
record = repository.get_audio(audio_id)
|
||||
if record is None:
|
||||
_fail(f"Keine Audiodatei mit ID {audio_id} gefunden.")
|
||||
|
||||
table = Table(show_header=False, box=None)
|
||||
table.add_column(style="bold cyan", no_wrap=True)
|
||||
table.add_column(overflow="fold")
|
||||
table.add_row("ID", str(record.id))
|
||||
table.add_row("Dateiname", escape(record.filename))
|
||||
table.add_row("Pfad", escape(record.path))
|
||||
table.add_row("Status", record.status)
|
||||
table.add_row("Dauer", format_duration(record.duration))
|
||||
table.add_row("Groesse", format_size(record.file_size))
|
||||
table.add_row("Geaendert", format_datetime(record.updated_at))
|
||||
table.add_row("Index-Hash", record.content_hash or "-")
|
||||
if record.duplicate_of:
|
||||
table.add_row("Duplikat von", f"#{record.duplicate_of}")
|
||||
if record.error_message:
|
||||
table.add_row("Fehler", escape(record.error_message))
|
||||
|
||||
try:
|
||||
meta = read_metadata(record.path)
|
||||
if meta.tags:
|
||||
tags = ", ".join(f"{key}={value}" for key, value in list(meta.tags.items())[:6])
|
||||
table.add_row("Tags", escape(tags))
|
||||
except AudioMetadataError:
|
||||
pass
|
||||
|
||||
console.print(Panel(table, title=f"Audiodatei #{record.id}", border_style="cyan"))
|
||||
|
||||
transcript = repository.get_transcript(record.id)
|
||||
if transcript is None:
|
||||
console.print("[yellow]Kein Transkript vorhanden.[/yellow]")
|
||||
return
|
||||
|
||||
segments = repository.get_segments(record.id)
|
||||
console.print(
|
||||
f"Transkript: {len(segments)} Segmente, Modell [bold]{escape(transcript['model'])}[/bold], "
|
||||
f"Sprache {transcript['language'] or '?'}, erstellt {format_datetime(transcript['created_at'])}"
|
||||
)
|
||||
preview = Table(title="Segmente (Auszug)")
|
||||
preview.add_column("Start", justify="right")
|
||||
preview.add_column("Ende", justify="right")
|
||||
preview.add_column("Text", overflow="fold")
|
||||
for segment in segments[:20]:
|
||||
preview.add_row(
|
||||
format_timestamp_verbose(segment["start_time"]),
|
||||
format_timestamp_verbose(segment["end_time"]),
|
||||
escape(segment["text"]),
|
||||
)
|
||||
console.print(preview)
|
||||
if len(segments) > 20:
|
||||
console.print(f"[dim]... {len(segments) - 20} weitere Segmente[/dim]")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# play
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def play(
|
||||
ctx: typer.Context,
|
||||
audio_id: int = typer.Argument(..., help="ID der Audiodatei"),
|
||||
time: float = typer.Option(0.0, "--time", help="Startzeit in Sekunden"),
|
||||
) -> None:
|
||||
"""Spielt eine Audiodatei ab einer Zeitposition ab."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
record = repository.get_audio(audio_id)
|
||||
if record is None:
|
||||
_fail(f"Keine Audiodatei mit ID {audio_id} gefunden.")
|
||||
|
||||
console.print(
|
||||
f"Spiele [bold]{escape(record.filename)}[/bold] ab {format_timestamp_verbose(time)} ..."
|
||||
)
|
||||
try:
|
||||
play_audio(record.path, time)
|
||||
except PlaybackError as exc:
|
||||
_fail(str(exc), code=127)
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# retry / reindex / remove
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command()
|
||||
def retry(ctx: typer.Context) -> None:
|
||||
"""Setzt fehlgeschlagene Transkriptionen zurueck auf 'pending'."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
count = repository.reset_errors()
|
||||
console.print(f"{count} Datei(en) zurueckgesetzt. Starte 'sjaudiosearch transcribe'.")
|
||||
|
||||
|
||||
@app.command()
|
||||
def reindex(ctx: typer.Context) -> None:
|
||||
"""Baut den Suchindex aus den vorhandenen Transkripten neu auf."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
before = repository.stats().transcripts
|
||||
with console.status("Baue Suchindex neu ...", spinner="dots"):
|
||||
indexed = repository.rebuild_search_index()
|
||||
after = repository.stats().transcripts
|
||||
if before != after:
|
||||
_fail(f"Transkriptanzahl hat sich geaendert ({before} -> {after}) - bitte pruefen.")
|
||||
console.print(
|
||||
f"Suchindex neu aufgebaut: {indexed} Segmente indexiert. "
|
||||
f"Transkripte unveraendert ({after})."
|
||||
)
|
||||
|
||||
|
||||
@app.command()
|
||||
def remove(
|
||||
ctx: typer.Context,
|
||||
audio_id: int = typer.Argument(..., help="ID der Audiodatei"),
|
||||
) -> None:
|
||||
"""Entfernt eine Datei aus dem Index (die Audiodatei selbst bleibt erhalten)."""
|
||||
app_context = _get_context(ctx)
|
||||
repository = app_context.repository()
|
||||
record = repository.get_audio(audio_id)
|
||||
if record is None:
|
||||
_fail(f"Keine Audiodatei mit ID {audio_id} gefunden.")
|
||||
|
||||
if not repository.delete_audio(audio_id):
|
||||
_fail(f"Datei #{audio_id} konnte nicht entfernt werden.")
|
||||
|
||||
console.print(
|
||||
f"[green]Entfernt:[/green] #{audio_id} {escape(record.filename)} "
|
||||
"(Transkript und Suchindex geloescht)"
|
||||
)
|
||||
console.print(f"[dim]Die Originaldatei bleibt erhalten: {escape(record.path)}[/dim]")
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
# search-index Diagnose
|
||||
# ----------------------------------------------------------------------
|
||||
@app.command("check")
|
||||
def check(ctx: typer.Context) -> None:
|
||||
"""Prueft Datenbank, FTS5 und Trigramm-Unterstuetzung."""
|
||||
app_context = _get_context(ctx)
|
||||
config = app_context.config
|
||||
conn = app_context.repository().conn
|
||||
fts5, trigram = check_fts5(conn)
|
||||
console.print(f"Datenbank: {config.database_path}")
|
||||
console.print(f"FTS5: {'OK' if fts5 else 'nicht verfuegbar'}")
|
||||
console.print(f"Trigramm-Tokenizer: {'OK' if trigram else 'nicht verfuegbar'}")
|
||||
console.print(f"Logdatei: {config.log_path}")
|
||||
console.print(f"Konfiguration: {config.source_path or 'eingebaute Defaults'}")
|
||||
|
||||
|
||||
def run() -> None:
|
||||
"""Einstiegspunkt fuer ``python -m sjaudiosearch``."""
|
||||
try:
|
||||
app()
|
||||
except KeyboardInterrupt:
|
||||
console.print("\n[yellow]Abgebrochen.[/yellow]")
|
||||
sys.exit(130)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
run()
|
||||
Reference in New Issue
Block a user