Spaces:
Running on Zero
Running on Zero
File size: 5,187 Bytes
5c225d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | """Command-line interface (CLI) for LectureClear audio pipeline and quality evaluation."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from lecture_clear.application.use_cases import ReportProgress
from lecture_clear.container import build_container
from lecture_clear.domain.entities import (
EnhancementLevel,
OutputFormat,
ProcessingStage,
)
from lecture_clear.domain.exceptions import LectureClearError
from lecture_clear.interfaces.evaluation import evaluate_enhancement
class CliProgressReporter:
"""Prints human-readable progress updates to stderr."""
def report(
self, stage: ProcessingStage, percent: float, message: str = ""
) -> None:
"""Write progress line to stderr."""
sys.stderr.write(f"[{percent:5.1f}%] ({stage.value}) {message}\n")
sys.stderr.flush()
def build_parser() -> argparse.ArgumentParser:
"""Create the argument parser for lecture-clear CLI."""
parser = argparse.ArgumentParser(
prog="lecture-clear",
description="LectureClear — تنقية صوت المحاضرات (CLI)",
)
subparsers = parser.add_subparsers(dest="command", required=True)
enhance_p = subparsers.add_parser("enhance", help="تنقية ملف صوتي أو فيديو لمحاضرة")
enhance_p.add_argument("input", type=Path, help="مسار ملف المحاضرة الأصلي")
enhance_p.add_argument("output", type=Path, help="مسار حفظ الملف المنقى")
enhance_p.add_argument(
"--level",
choices=[e.value for e in EnhancementLevel],
default=EnhancementLevel.MEDIUM.value,
help="مستوى المعالجة: light, medium, strong",
)
enhance_p.add_argument(
"--format",
choices=[f.value for f in OutputFormat],
default=OutputFormat.MP3.value,
help="صيغة الملف الناتج: mp3 أو wav",
)
eval_p = subparsers.add_parser("evaluate", help="تقييم جودة الصوت قبل وبعد التنقية")
eval_p.add_argument("original", type=Path, help="مسار الملف الأصلي")
eval_p.add_argument("enhanced", type=Path, help="مسار الملف المنقى")
eval_p.add_argument(
"--reference-text",
type=str,
default=None,
help="النص المرجعي (Ground Truth)",
)
eval_p.add_argument("--original-text", type=str, default=None, help="تفريغ الصوت الأصلي")
eval_p.add_argument("--enhanced-text", type=str, default=None, help="تفريغ الصوت المنقى")
return parser
def main(argv: list[str] | None = None) -> int:
"""CLI entrypoint returning process exit code."""
parser = build_parser()
args = parser.parse_args(argv)
try:
if args.command == "enhance":
container = build_container()
progress = ReportProgress(CliProgressReporter())
result = container.enhance_audio.execute(
input_path=args.input,
output_path=args.output,
level=EnhancementLevel(args.level),
output_format=OutputFormat(args.format),
progress=progress,
)
summary = {
"job_id": result.job_id,
"output_path": str(result.output_file.path),
"level": result.profile.level.value,
"output_format": result.output_format.value,
"input_duration_seconds": round(result.input_file.duration_seconds, 2),
"total_duration_ms": result.total_duration_ms,
"real_time_factor": round(result.real_time_factor, 4),
"chunks_count": result.chunks_count,
}
sys.stdout.write(json.dumps(summary, ensure_ascii=False, indent=2) + "\n")
return 0
if args.command == "evaluate":
report = evaluate_enhancement(
original_path=args.original,
enhanced_path=args.enhanced,
reference_transcript=args.reference_text,
original_hypothesis=args.original_text,
enhanced_hypothesis=args.enhanced_text,
)
payload = {
"original_mean_volume_db": report.original_stats.mean_volume_db,
"original_max_volume_db": report.original_stats.max_volume_db,
"enhanced_mean_volume_db": report.enhanced_stats.mean_volume_db,
"enhanced_max_volume_db": report.enhanced_stats.max_volume_db,
"mean_gain_db": report.mean_gain_db,
"original_wer": report.original_wer,
"enhanced_wer": report.enhanced_wer,
"passed_quality_gate": report.passed_quality_gate,
}
sys.stdout.write(json.dumps(payload, ensure_ascii=False, indent=2) + "\n")
return 0 if report.passed_quality_gate else 2
except LectureClearError as err:
sys.stderr.write(json.dumps(err.to_dict(), ensure_ascii=False) + "\n")
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())
|