| 285 | |
| 286 | @dataclass() |
| 287 | class TranscriptionModel: |
| 288 | def __init__( |
| 289 | self, |
| 290 | model_type: ModelType = ModelType.WHISPER, |
| 291 | whisper_model_size: Optional[WhisperModelSize] = WhisperModelSize.TINY, |
| 292 | hugging_face_model_id: Optional[str] = "" |
| 293 | ): |
| 294 | self.model_type = model_type |
| 295 | self.whisper_model_size = whisper_model_size |
| 296 | self.hugging_face_model_id = hugging_face_model_id |
| 297 | |
| 298 | def __str__(self): |
| 299 | match self.model_type: |
| 300 | case ModelType.WHISPER: |
| 301 | return f"Whisper ({self.whisper_model_size})" |
| 302 | case ModelType.WHISPER_CPP: |
| 303 | return f"Whisper.cpp ({self.whisper_model_size})" |
| 304 | case ModelType.HUGGING_FACE: |
| 305 | return f"Hugging Face ({self.hugging_face_model_id})" |
| 306 | case ModelType.FASTER_WHISPER: |
| 307 | return f"Faster Whisper ({self.whisper_model_size})" |
| 308 | case ModelType.OPEN_AI_WHISPER_API: |
| 309 | return "OpenAI Whisper API" |
| 310 | case _: |
| 311 | raise Exception("Unknown model type") |
| 312 | |
| 313 | def is_deletable(self): |
| 314 | return ( |
| 315 | self.model_type == ModelType.WHISPER |
| 316 | or self.model_type == ModelType.WHISPER_CPP |
| 317 | or self.model_type == ModelType.FASTER_WHISPER |
| 318 | ) and self.get_local_model_path() is not None |
| 319 | |
| 320 | def open_file_location(self): |
| 321 | model_path = self.get_local_model_path() |
| 322 | |
| 323 | if (self.model_type == ModelType.HUGGING_FACE |
| 324 | or self.model_type == ModelType.FASTER_WHISPER): |
| 325 | model_path = os.path.dirname(model_path) |
| 326 | |
| 327 | if model_path is None: |
| 328 | return |
| 329 | self.open_path(path=os.path.dirname(model_path)) |
| 330 | |
| 331 | @staticmethod |
| 332 | def default(): |
| 333 | model_type = next( |
| 334 | model_type for model_type in ModelType if model_type.is_available() |
| 335 | ) |
| 336 | return TranscriptionModel(model_type=model_type) |
| 337 | |
| 338 | @staticmethod |
| 339 | def open_path(path: str): |
| 340 | if sys.platform == "win32": |
| 341 | os.startfile(path) |
| 342 | else: |
| 343 | opener = "open" if sys.platform == "darwin" else "xdg-open" |
| 344 | subprocess.call([opener, path]) |
no outgoing calls