|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +# ------------------------------------------------------------- |
| 21 | +from typing import List, Optional, Union |
| 22 | +from faster_whisper import WhisperModel |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +from systemds.scuro.dataloader.base_loader import BaseLoader |
| 26 | +from systemds.scuro.modality.type import ModalityType |
| 27 | + |
| 28 | + |
| 29 | +class TranscriptLoader(BaseLoader): |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + source_path: str, |
| 33 | + indices: List[str], |
| 34 | + data_type: Union[np.dtype, str] = np.float32, |
| 35 | + chunk_size: Optional[int] = None, |
| 36 | + normalize: bool = True, |
| 37 | + transcribe_model_size: str = "medium", |
| 38 | + load=True, |
| 39 | + ): |
| 40 | + super().__init__(source_path, indices, data_type, chunk_size, ModalityType.TEXT) |
| 41 | + self.model = WhisperModel( |
| 42 | + transcribe_model_size, device="cpu", compute_type="int8" |
| 43 | + ) |
| 44 | + self.normalize = normalize |
| 45 | + self.load_data_from_file = load |
| 46 | + |
| 47 | + def extract(self, file: str, index: Optional[Union[str, List[str]]] = None): |
| 48 | + self.file_sanity_check(file) |
| 49 | + segments, _ = self.model.transcribe(file, vad_filter=True) |
| 50 | + |
| 51 | + for i, seg in enumerate(segments): |
| 52 | + md = self.modality_type.create_metadata(len(seg.text.split()), seg.text) |
| 53 | + md["timestamp_start"] = seg.start |
| 54 | + md["timestamp_end"] = seg.end |
| 55 | + md["text"] = seg.text |
| 56 | + |
| 57 | + self.metadata.append(md) |
| 58 | + |
| 59 | + self.data.append(seg.text) |
0 commit comments