From 7f92a27b4948a722780fde57031e0ba40b04242c Mon Sep 17 00:00:00 2001 From: German Mikheev Date: Tue, 4 Nov 2025 15:57:52 +0300 Subject: [PATCH] Minor improvements & docs added --- .gitignore | 1 - env_cpu.yml | 19 --------- env_cuda.yml | 15 ------- env_mps.yml | 19 --------- transcription/audio_transcription.py | 57 +++++++++++++++++++++++++-- transcription/device_configuration.py | 5 ++- ui/ui.py | 4 +- 7 files changed, 58 insertions(+), 62 deletions(-) delete mode 100644 env_cpu.yml delete mode 100644 env_cuda.yml delete mode 100644 env_mps.yml diff --git a/.gitignore b/.gitignore index 7ca39b3..a93dccc 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,4 @@ main.todo sample.mp3 *.spec -# лютый завоз ui/assets/question_mark.png \ No newline at end of file diff --git a/env_cpu.yml b/env_cpu.yml deleted file mode 100644 index 314f139..0000000 --- a/env_cpu.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: notecast -channels: - - pytorch - - conda-forge - - defaults -dependencies: - - python=3.10 - - pytorch::pytorch=2.0.1 - - pytorch::torchvision=0.15.2 - - pytorch::torchaudio=2.0.2 - - transformers=4.30.0 - - accelerate=0.20.0 - - ffmpeg - - pyinstaller - - customtkinter - - isort - - mypy - - black - - tqdm \ No newline at end of file diff --git a/env_cuda.yml b/env_cuda.yml deleted file mode 100644 index 5879e5b..0000000 --- a/env_cuda.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: notecast -channels: - - pytorch - - nvidia - - conda-forge - - bioconda -dependencies: - - pytorch - - ffmpeg - - torchvision - - torchaudio - - transformers - - python=3.12 - - customtkinter - - openai \ No newline at end of file diff --git a/env_mps.yml b/env_mps.yml deleted file mode 100644 index 314f139..0000000 --- a/env_mps.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: notecast -channels: - - pytorch - - conda-forge - - defaults -dependencies: - - python=3.10 - - pytorch::pytorch=2.0.1 - - pytorch::torchvision=0.15.2 - - pytorch::torchaudio=2.0.2 - - transformers=4.30.0 - - accelerate=0.20.0 - - ffmpeg - - pyinstaller - - customtkinter - - isort - - mypy - - black - - tqdm \ No newline at end of file diff --git a/transcription/audio_transcription.py b/transcription/audio_transcription.py index 8415b49..1c6745b 100644 --- a/transcription/audio_transcription.py +++ b/transcription/audio_transcription.py @@ -15,7 +15,54 @@ from ui.ui_log_handler import UILogHandler # TODO: implement transcription with shift class AudioTranscription: - model_name = "openai/whisper-large-v2" + """ + Class for automatical audio transcription using Whisper. + + Provides audio file loading, resampling, conversion to mono, + splitting into chunks and batches, running the model, and compiling the final text. + + Attributes: + model_name (str): Whisper model name in HuggingFace. + filepath (str): Input filepath. + waveform (torch.Tensor): Audiosignal in tensor form. + sampling_rate (int): Input file's sampling frequency. + chunks (list): Audio's chunks list. + batches (list): Batches combined from chunks. + chunk_size (int): Chunk size in samples. + custom_chunk_length (int): Custom chunk length (in seconds). + custom_batch_length (int): Custom batch length (in chunks). + device (str): Inference device ("cuda", "cpu", "mps"). + processor (WhisperProcessor | None): Tokenizator/preprocessor. + model (WhisperForConditionalGeneration | None): Whisper model. + logger (logging.Logger): Logger. + torch_dtype (torch.dtype): Data type for calculations (fp16/fp32). + language (str): Transcription language (default "ru"). + all_transcription (list): List of strings with transcription. + + Args: + filepath (str): Input filepath. + device_configuration (DeviceConfiguration): Device configuration + (GPU/CPU/MPS, model, chunk length, batch etc.). + logger (logging.Logger): Logger. + language (str, optional): Transcription language. Default "ru". + + Example: + >>> from transcription.device_configuration import DeviceConfiguration + >>> import logging + >>> config = DeviceConfiguration(device="cuda", model_name="openai/whisper-large-v3-turbo") # recheck this, not true i think + >>> logger = logging.getLogger("transcription") + >>> transcriber = AudioTranscription("audio.wav", config, logger, language="en") + >>> text = transcriber.transcribe_audio() + >>> print(text) + "This is a test audio transcription." + + Methods: + transcribe_audio() -> str: + Starts full transcription pipeline: model loading, + file loading and preprocessing, splitting into chunks/batches, + inference and model unloading. Returns the final transcription. + """ + model_name = "openai/whisper-large-v3-turbo" filepath: str waveform: torch.Tensor @@ -93,12 +140,14 @@ class AudioTranscription: def _load_file(self) -> None: self.logger.info(f"Loading file {self.filepath}") try: - self.waveform, self.sampling_rate = torchaudio.load( - self.filepath, format=self.file_format, backend="ffmpeg" - ) + # self.waveform, self.sampling_rate = torchaudio.load( + # self.filepath, format=self.file_format, backend="ffmpeg" + # ) + self.waveform, self.sampling_rate = torchaudio.load(self.filepath) self.logger.info(f"Successfully loaded file {self.filepath}.") except Exception as e: self.logger.error(f"Unable to load file {self.filepath}: {e}") + # raise RuntimeError(f"Failed to load file {self.filepath}: {e}") def _resample(self) -> None: self.waveform = torchaudio.functional.resample( diff --git a/transcription/device_configuration.py b/transcription/device_configuration.py index 4e5edba..d987ba2 100644 --- a/transcription/device_configuration.py +++ b/transcription/device_configuration.py @@ -17,6 +17,7 @@ class DeviceConfiguration: - "openai/whisper-medium" - "openai/whisper-large" - "openai/whisper-large-v2" + - "openai/whisper-large-v3-turbo" batch_size (int): Chunks in one batch. Selected for VRAM. @@ -24,8 +25,8 @@ class DeviceConfiguration: data_type (str): custom data type of model. Variants: - torch.float16 - for GPUs - - torch.float32 - for CPU / weak GPU - - torch.bfloat16 - for GPUs which has BF16 support + - torch.float32 - for CPU + - torch.bfloat16 - for GPUs which has BF16 support (usually RTX 40XX+) """ device: str = "cuda" diff --git a/ui/ui.py b/ui/ui.py index d20808e..e9c44cb 100644 --- a/ui/ui.py +++ b/ui/ui.py @@ -94,6 +94,7 @@ class TranscriberApp(ctk.CTk): self.device_var = tk.StringVar(value=device_opts[0]) self.device_opts = device_opts + # input & output file variables self.input_file_var = tk.StringVar() self.output_file_var = tk.StringVar() @@ -218,7 +219,6 @@ class TranscriberApp(ctk.CTk): tooltip="Choose model for speed recognition", variable=self.model_var, values=[ - # maybe delete this option? "openai/whisper-large-v3-turbo", "openai/whisper-large-v2", "openai/whisper-large", @@ -257,7 +257,7 @@ class TranscriberApp(ctk.CTk): text="Chunk length (s):", tooltip="Maximum length of processing audio fragment", variable=self.chunk_var, - values=["30", "25", "20", "15", "10", "5"], + values=["30", "24", "20", "14", "10", "6"], ) # third row