Update app.py
Browse files
app.py
CHANGED
|
@@ -819,4 +819,70 @@ async def cancel_job(job_id: str, body: CancelRequest):
|
|
| 819 |
"job_id": job_id,
|
| 820 |
"restarting": False, # no subprocess — field kept for contract compat
|
| 821 |
"message": "Job cancelled.",
|
| 822 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
"job_id": job_id,
|
| 820 |
"restarting": False, # no subprocess — field kept for contract compat
|
| 821 |
"message": "Job cancelled.",
|
| 822 |
+
}
|
| 823 |
+
|
| 824 |
+
@app.post("/benchmark")
|
| 825 |
+
async def benchmark_align(req: AlignRequest):
|
| 826 |
+
"""
|
| 827 |
+
Runs alignment synchronously and returns timing + input stats.
|
| 828 |
+
Used for calibrating the estimator — not for production use.
|
| 829 |
+
"""
|
| 830 |
+
if _aligner is None:
|
| 831 |
+
raise HTTPException(503, {"error": "Model not loaded yet"})
|
| 832 |
+
|
| 833 |
+
cleaned_text = _clean_text(req.text)
|
| 834 |
+
if not cleaned_text:
|
| 835 |
+
raise HTTPException(400, {"error": "Empty text"})
|
| 836 |
+
|
| 837 |
+
wav_normalised, audio_duration = await asyncio.get_running_loop().run_in_executor(
|
| 838 |
+
None, _validate_audio, req.audio_base64
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
word_count = len(cleaned_text.split())
|
| 842 |
+
|
| 843 |
+
tmp_path = None
|
| 844 |
+
try:
|
| 845 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 846 |
+
tmp.write(wav_normalised)
|
| 847 |
+
tmp_path = tmp.name
|
| 848 |
+
|
| 849 |
+
t_start = time.monotonic()
|
| 850 |
+
|
| 851 |
+
loop = asyncio.get_running_loop()
|
| 852 |
+
results = await loop.run_in_executor(
|
| 853 |
+
None,
|
| 854 |
+
lambda: _aligner.align(
|
| 855 |
+
audio=tmp_path,
|
| 856 |
+
text=cleaned_text,
|
| 857 |
+
language=req.language,
|
| 858 |
+
)
|
| 859 |
+
)
|
| 860 |
+
|
| 861 |
+
actual_duration_s = round(time.monotonic() - t_start, 3)
|
| 862 |
+
|
| 863 |
+
word_timestamps = []
|
| 864 |
+
if results and results[0]:
|
| 865 |
+
for token in results[0]:
|
| 866 |
+
word = (token.text or "").strip()
|
| 867 |
+
if not word:
|
| 868 |
+
continue
|
| 869 |
+
word_timestamps.append({
|
| 870 |
+
"word": word,
|
| 871 |
+
"start": round(float(token.start_time), 3),
|
| 872 |
+
"end": round(float(token.end_time), 3),
|
| 873 |
+
})
|
| 874 |
+
|
| 875 |
+
return {
|
| 876 |
+
"audio_duration_s": round(audio_duration, 3),
|
| 877 |
+
"word_count": word_count,
|
| 878 |
+
"actual_align_s": actual_duration_s,
|
| 879 |
+
"realtime_factor": round(actual_duration_s / audio_duration, 3),
|
| 880 |
+
"ms_per_word": round((actual_duration_s / word_count) * 1000, 1) if word_count else None,
|
| 881 |
+
"words_aligned": len(word_timestamps),
|
| 882 |
+
"language": req.language,
|
| 883 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 884 |
+
}
|
| 885 |
+
|
| 886 |
+
finally:
|
| 887 |
+
if tmp_path and os.path.exists(tmp_path):
|
| 888 |
+
os.unlink(tmp_path)
|