Spaces:
Running on Zero
Running on Zero
Download src/lecture_clear/interfaces/cli.py from amirelrefai/audioenhancer: direct link, hf CLI and curl.
- Browser
- Download file 5.19 kB
-
https://huggingface.co/spaces/amirelrefai/audioenhancer/resolve/main/src/lecture_clear/interfaces/cli.py
- Command line
-
hf download hf://spaces/amirelrefai/audioenhancer/src/lecture_clear/interfaces/cli.py
-
curl -L -o cli.py https://huggingface.co/spaces/amirelrefai/audioenhancer/resolve/main/src/lecture_clear/interfaces/cli.py
5.19 kB
| """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()) | |