feat: round output JSON floats to 2dp + unify ensure_ascii for asr/asrx
This commit is contained in:
+33
-10
@@ -18,6 +18,7 @@ Configuration:
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
import math
|
||||
import time
|
||||
import argparse
|
||||
import signal
|
||||
@@ -28,6 +29,28 @@ from faster_whisper import WhisperModel
|
||||
PROCESSOR_VERSION = "2.1"
|
||||
MODEL_SIZE = "small"
|
||||
DEVICE = "cpu"
|
||||
|
||||
|
||||
def segment_confidence(segment) -> float:
|
||||
"""Derive a 0-1 confidence from faster-whisper's avg_logprob.
|
||||
|
||||
faster-whisper segments do NOT expose a `confidence` attribute, so the old
|
||||
getattr(segment, "confidence", 0.0) always returned 0. avg_logprob is the
|
||||
average log-probability per token (negative); exp() maps it to (0, 1].
|
||||
"""
|
||||
logprob = getattr(segment, "avg_logprob", -0.1)
|
||||
return min(1.0, math.exp(logprob))
|
||||
|
||||
|
||||
def round_json_numbers(obj):
|
||||
"""Recursively round all float values to 2 decimal places (JSON output only)."""
|
||||
if isinstance(obj, float):
|
||||
return round(obj, 2)
|
||||
if isinstance(obj, dict):
|
||||
return {k: round_json_numbers(v) for k, v in obj.items()}
|
||||
if isinstance(obj, list):
|
||||
return [round_json_numbers(v) for v in obj]
|
||||
return obj
|
||||
COMPUTE_TYPE = "int8"
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
@@ -213,7 +236,7 @@ def run_asr(video_path, output_path, uuid: str = "", fps: float = None):
|
||||
"segments": []
|
||||
}
|
||||
with open(output_path, "w") as f:
|
||||
json.dump(output, f, indent=2)
|
||||
json.dump(round_json_numbers(output), f, indent=2, ensure_ascii=False)
|
||||
if publisher:
|
||||
publisher.complete("asr", "0 segments (no audio)")
|
||||
sys.stderr.write("ASR: No audio stream, skipping transcription\n")
|
||||
@@ -347,21 +370,21 @@ def run_asr(video_path, output_path, uuid: str = "", fps: float = None):
|
||||
seg_end = start_t + segment.end
|
||||
scene_idx = find_scene_idx((seg_start + seg_end) / 2)
|
||||
scene_segments.append({
|
||||
"start_time": seg_start,
|
||||
"end_time": seg_end,
|
||||
"start_time": round(seg_start, 2),
|
||||
"end_time": round(seg_end, 2),
|
||||
"start_frame": int(round(seg_start * fps)),
|
||||
"end_frame": int(round(seg_end * fps)),
|
||||
"text": segment.text.strip(),
|
||||
"scene_number": scene_idx + 1,
|
||||
"language": seg_language,
|
||||
"confidence": getattr(segment, "confidence", 0.0),
|
||||
"confidence": round(segment_confidence(segment), 2),
|
||||
})
|
||||
total_segments += 1
|
||||
|
||||
# 當前 scene 結果寫入 .asr.tmp
|
||||
all_segments.extend(scene_segments)
|
||||
with open(tmp_path, "w") as f:
|
||||
json.dump({"language": transcript_language or "", "segments": all_segments}, f)
|
||||
json.dump(round_json_numbers({"language": transcript_language or "", "segments": all_segments}), f, ensure_ascii=False)
|
||||
|
||||
if total_segments % 100 == 0:
|
||||
if publisher:
|
||||
@@ -387,7 +410,7 @@ def run_asr(video_path, output_path, uuid: str = "", fps: float = None):
|
||||
|
||||
# Write final output with status
|
||||
with open(tmp_path, "w") as f:
|
||||
json.dump({"status": status, "language": info_language, "segments": all_segments}, f)
|
||||
json.dump(round_json_numbers({"status": status, "language": info_language, "segments": all_segments}), f, ensure_ascii=False)
|
||||
else:
|
||||
# 無 CUT 資料,直接轉錄(原有流程)
|
||||
segments, info = transcribe_with_fallback(model, video_path, publisher)
|
||||
@@ -397,12 +420,12 @@ def run_asr(video_path, output_path, uuid: str = "", fps: float = None):
|
||||
all_segments = []
|
||||
for segment in segments:
|
||||
all_segments.append({
|
||||
"start_time": segment.start,
|
||||
"end_time": segment.end,
|
||||
"start_time": round(segment.start, 2),
|
||||
"end_time": round(segment.end, 2),
|
||||
"start_frame": int(round(segment.start * fps)),
|
||||
"end_frame": int(round(segment.end * fps)),
|
||||
"text": segment.text.strip(),
|
||||
"confidence": getattr(segment, "confidence", 0.0),
|
||||
"confidence": round(segment_confidence(segment), 2),
|
||||
})
|
||||
total_segments += 1
|
||||
if total_segments % 100 == 0:
|
||||
@@ -416,7 +439,7 @@ def run_asr(video_path, output_path, uuid: str = "", fps: float = None):
|
||||
status = "silent_audio"
|
||||
|
||||
with open(tmp_path, "w") as f:
|
||||
json.dump({"status": status, "language": info_language, "segments": all_segments}, f)
|
||||
json.dump(round_json_numbers({"status": status, "language": info_language, "segments": all_segments}), f, ensure_ascii=False)
|
||||
|
||||
if publisher:
|
||||
publisher.info("asr", f"ASR_LANGUAGE:{info_language}")
|
||||
|
||||
Reference in New Issue
Block a user