File size: 10,035 Bytes
b5df735 |
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
"""
Model converters for transforming between different formats
"""
from typing import Dict, Any, List
from datetime import datetime
import os
from .transcription import (
TranscriptionResponse, TranscriptionFiles, TranscriptionSegment,
TranscriptionMetrics, SpeakerInfo, ModelSize, OutputFormat
)
from .download import DownloadResponse, PodcastPlatform
from .file_operations import (
FileInfoResponse, FileReadResponse, DirectoryListResponse,
FileMetadata, FileReadProgress
)
from .base import OperationStatus
class TranscriptionConverter:
"""Converter for transcription models"""
@staticmethod
def from_legacy_dict(data: Dict[str, Any]) -> TranscriptionResponse:
"""Convert legacy dictionary format to TranscriptionResponse"""
# Parse files
files = TranscriptionFiles(
txt_file_path=data.get("txt_file_path"),
srt_file_path=data.get("srt_file_path"),
json_file_path=data.get("json_file_path")
)
# Parse segments
segments = []
if "segments" in data:
for seg_data in data["segments"]:
segments.append(TranscriptionSegment(
start=seg_data.get("start", 0),
end=seg_data.get("end", 0),
text=seg_data.get("text", ""),
speaker=seg_data.get("speaker"),
confidence=seg_data.get("confidence")
))
# Parse metrics
metrics = TranscriptionMetrics(
audio_duration=data.get("audio_duration", 0),
processing_time=data.get("processing_time", 0),
segment_count=data.get("segment_count", 0),
model_used=data.get("model_used", ""),
language_detected=data.get("language_detected", "unknown")
)
# Parse speaker info
speaker_info = SpeakerInfo(
enabled=data.get("speaker_diarization_enabled", False),
global_speaker_count=data.get("global_speaker_count", 0),
speaker_mapping=data.get("speaker_summary", {}).get("speaker_mapping", {}),
speaker_summary=data.get("speaker_summary", {})
)
# Determine status
status = OperationStatus.SUCCESS if data.get("processing_status") == "success" else OperationStatus.FAILED
return TranscriptionResponse(
status=status,
message=data.get("error_message") if status == OperationStatus.FAILED else "转录完成",
audio_file=data.get("audio_file", ""),
files=files,
segments=segments,
speaker_info=speaker_info,
metrics=metrics
)
@staticmethod
def to_legacy_dict(response: TranscriptionResponse) -> Dict[str, Any]:
"""Convert TranscriptionResponse to legacy dictionary format"""
return {
"txt_file_path": response.files.txt_file_path,
"srt_file_path": response.files.srt_file_path,
"audio_file": response.audio_file,
"model_used": response.metrics.model_used,
"segment_count": response.metrics.segment_count,
"audio_duration": response.metrics.audio_duration,
"processing_status": response.status.value,
"processing_time": response.metrics.processing_time,
"saved_files": response.files.all_files,
"speaker_diarization_enabled": response.speaker_info.enabled,
"global_speaker_count": response.speaker_info.global_speaker_count,
"speaker_summary": response.speaker_info.speaker_summary,
"language_detected": response.metrics.language_detected,
"error_message": response.message if response.is_failed else None
}
class DownloadConverter:
"""Converter for download models"""
@staticmethod
def from_legacy_dict(data: Dict[str, Any]) -> DownloadResponse:
"""Convert legacy dictionary format to DownloadResponse"""
status = OperationStatus.SUCCESS if data.get("status") == "success" else OperationStatus.FAILED
# Calculate file size if available
file_size_mb = None
if data.get("audio_file_path") and os.path.exists(data["audio_file_path"]):
try:
file_size_bytes = os.path.getsize(data["audio_file_path"])
file_size_mb = file_size_bytes / (1024 * 1024)
except:
pass
return DownloadResponse(
status=status,
message=data.get("error_message") if status == OperationStatus.FAILED else "下载成功",
original_url=data.get("original_url", ""),
audio_file_path=data.get("audio_file_path"),
file_size_mb=file_size_mb
)
@staticmethod
def to_legacy_dict(response: DownloadResponse) -> Dict[str, Any]:
"""Convert DownloadResponse to legacy dictionary format"""
return {
"status": response.status.value,
"original_url": response.original_url,
"audio_file_path": response.audio_file_path,
"error_message": response.message if response.is_failed else None
}
class FileOperationConverter:
"""Converter for file operation models"""
@staticmethod
def from_legacy_file_info(data: Dict[str, Any]) -> FileInfoResponse:
"""Convert legacy file info format to FileInfoResponse"""
status = OperationStatus.SUCCESS if data.get("status") == "success" else OperationStatus.FAILED
metadata = None
if status == OperationStatus.SUCCESS:
metadata = FileMetadata(
filename=data.get("filename", ""),
full_path=data.get("file_path", ""),
file_size=data.get("file_size", 0),
file_size_mb=data.get("file_size_mb", 0.0),
created_time=datetime.fromtimestamp(data.get("created_time", 0)),
modified_time=datetime.fromtimestamp(data.get("modified_time", 0)),
file_extension=data.get("file_extension", ""),
is_audio_file=data.get("file_extension", "").lower() in ['.mp3', '.wav', '.m4a', '.flac']
)
return FileInfoResponse(
status=status,
message=data.get("error_message") if status == OperationStatus.FAILED else "文件信息获取成功",
file_path=data.get("file_path", ""),
file_exists=data.get("file_exists", False),
metadata=metadata
)
@staticmethod
def from_legacy_file_read(data: Dict[str, Any]) -> FileReadResponse:
"""Convert legacy file read format to FileReadResponse"""
status = OperationStatus.SUCCESS if data.get("status") == "success" else OperationStatus.FAILED
progress = None
if status == OperationStatus.SUCCESS:
progress = FileReadProgress(
current_position=data.get("current_position", 0),
file_size=data.get("file_size", 0),
bytes_read=data.get("bytes_read", 0),
content_length=data.get("content_length", 0),
progress_percentage=data.get("progress_percentage", 0.0),
end_of_file_reached=data.get("end_of_file_reached", False),
actual_boundary=data.get("actual_boundary", "")
)
return FileReadResponse(
status=status,
message=data.get("error_message") if status == OperationStatus.FAILED else "文件读取成功",
file_path=data.get("file_path", ""),
content=data.get("content", ""),
progress=progress
)
@staticmethod
def from_legacy_directory_list(data: Dict[str, Any]) -> DirectoryListResponse:
"""Convert legacy directory list format to DirectoryListResponse"""
status = OperationStatus.SUCCESS if not data.get("error_message") else OperationStatus.FAILED
file_list = []
if "file_list" in data:
for file_data in data["file_list"]:
# Handle time format conversion - original format is string, not ISO format
created_time_str = file_data.get("created_time", "")
modified_time_str = file_data.get("modified_time", "")
try:
# Try to parse as ISO format first
created_time = datetime.fromisoformat(created_time_str.replace("T", " ")) if created_time_str else datetime.fromtimestamp(0)
except:
# Fallback to default time
created_time = datetime.fromtimestamp(0)
try:
modified_time = datetime.fromisoformat(modified_time_str.replace("T", " ")) if modified_time_str else datetime.fromtimestamp(0)
except:
modified_time = datetime.fromtimestamp(0)
file_list.append(FileMetadata(
filename=file_data.get("filename", ""),
full_path=file_data.get("full_path", ""),
file_size=file_data.get("file_size", 0),
file_size_mb=file_data.get("file_size_mb", 0.0),
created_time=created_time,
modified_time=modified_time,
file_extension=os.path.splitext(file_data.get("filename", ""))[1],
is_audio_file=file_data.get("filename", "").lower().endswith(('.mp3', '.wav', '.m4a', '.flac'))
))
return DirectoryListResponse(
status=status,
message=data.get("error_message", "目录扫描成功"),
directory=data.get("scanned_directory", ""),
total_files=data.get("total_files", 0),
file_list=file_list
) |