Upload 4 files
Browse files
modules/translation/nllb_inference.py
CHANGED
|
@@ -3,10 +3,10 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
from modules.utils.paths import TRANSLATION_OUTPUT_DIR, NLLB_MODELS_DIR
|
| 6 |
-
|
| 7 |
|
| 8 |
|
| 9 |
-
class NLLBInference(TranslationBase):
|
| 10 |
def __init__(self,
|
| 11 |
model_dir: str = NLLB_MODELS_DIR,
|
| 12 |
output_dir: str = TRANSLATION_OUTPUT_DIR
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
from modules.utils.paths import TRANSLATION_OUTPUT_DIR, NLLB_MODELS_DIR
|
| 6 |
+
import modules.translation.translation_base as base
|
| 7 |
|
| 8 |
|
| 9 |
+
class NLLBInference(base.TranslationBase):
|
| 10 |
def __init__(self,
|
| 11 |
model_dir: str = NLLB_MODELS_DIR,
|
| 12 |
output_dir: str = TRANSLATION_OUTPUT_DIR
|
modules/translation/translation_base.py
CHANGED
|
@@ -7,10 +7,12 @@ from typing import List
|
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
import modules.translation.nllb_inference as nllb
|
|
|
|
| 10 |
from modules.utils.subtitle_manager import *
|
| 11 |
from modules.utils.files_manager import load_yaml, save_yaml
|
| 12 |
from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH, NLLB_MODELS_DIR, TRANSLATION_OUTPUT_DIR
|
| 13 |
|
|
|
|
| 14 |
class TranslationBase(ABC):
|
| 15 |
def __init__(self,
|
| 16 |
model_dir: str = NLLB_MODELS_DIR,
|
|
@@ -127,64 +129,6 @@ class TranslationBase(ABC):
|
|
| 127 |
finally:
|
| 128 |
self.release_cuda_memory()
|
| 129 |
|
| 130 |
-
def translate_text(self,
|
| 131 |
-
input_list_dict: list,
|
| 132 |
-
model_size: str,
|
| 133 |
-
src_lang: str,
|
| 134 |
-
tgt_lang: str,
|
| 135 |
-
max_length: int = 200,
|
| 136 |
-
add_timestamp: bool = True,
|
| 137 |
-
progress=gr.Progress()) -> list:
|
| 138 |
-
"""
|
| 139 |
-
Translate text from source language to target language
|
| 140 |
-
Parameters
|
| 141 |
-
----------
|
| 142 |
-
str_text: str
|
| 143 |
-
List[dict] to translate
|
| 144 |
-
model_size: str
|
| 145 |
-
Whisper model size from gr.Dropdown()
|
| 146 |
-
src_lang: str
|
| 147 |
-
Source language of the file to translate from gr.Dropdown()
|
| 148 |
-
tgt_lang: str
|
| 149 |
-
Target language of the file to translate from gr.Dropdown()
|
| 150 |
-
max_length: int
|
| 151 |
-
Max length per line to translate
|
| 152 |
-
add_timestamp: bool
|
| 153 |
-
Boolean value that determines whether to add a timestamp
|
| 154 |
-
progress: gr.Progress
|
| 155 |
-
Indicator to show progress directly in gradio.
|
| 156 |
-
I use a forked version of whisper for this. To see more info : https://github.com/jhj0517/jhj0517-whisper/tree/add-progress-callback
|
| 157 |
-
Returns
|
| 158 |
-
----------
|
| 159 |
-
A List of
|
| 160 |
-
List[dict] with translation
|
| 161 |
-
"""
|
| 162 |
-
try:
|
| 163 |
-
self.cache_parameters(model_size=model_size,
|
| 164 |
-
src_lang=src_lang,
|
| 165 |
-
tgt_lang=tgt_lang,
|
| 166 |
-
max_length=max_length,
|
| 167 |
-
add_timestamp=add_timestamp)
|
| 168 |
-
|
| 169 |
-
self.update_model(model_size=model_size,
|
| 170 |
-
src_lang=src_lang,
|
| 171 |
-
tgt_lang=tgt_lang,
|
| 172 |
-
progress=progress)
|
| 173 |
-
|
| 174 |
-
total_progress = len(input_list_dict)
|
| 175 |
-
for index, dic in enumerate(input_list_dict):
|
| 176 |
-
progress(index / total_progress, desc="Translating..")
|
| 177 |
-
translated_text = self.translate(dic["text"], max_length=max_length)
|
| 178 |
-
dic["text"] = translated_text
|
| 179 |
-
|
| 180 |
-
return input_list_dict
|
| 181 |
-
|
| 182 |
-
except Exception as e:
|
| 183 |
-
print(f"Error translating file: {e}")
|
| 184 |
-
raise
|
| 185 |
-
finally:
|
| 186 |
-
self.release_cuda_memory()
|
| 187 |
-
|
| 188 |
def offload(self):
|
| 189 |
"""Offload the model and free up the memory"""
|
| 190 |
if self.model is not None:
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
import modules.translation.nllb_inference as nllb
|
| 10 |
+
from modules.whisper.data_classes import *
|
| 11 |
from modules.utils.subtitle_manager import *
|
| 12 |
from modules.utils.files_manager import load_yaml, save_yaml
|
| 13 |
from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH, NLLB_MODELS_DIR, TRANSLATION_OUTPUT_DIR
|
| 14 |
|
| 15 |
+
|
| 16 |
class TranslationBase(ABC):
|
| 17 |
def __init__(self,
|
| 18 |
model_dir: str = NLLB_MODELS_DIR,
|
|
|
|
| 129 |
finally:
|
| 130 |
self.release_cuda_memory()
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
def offload(self):
|
| 133 |
"""Offload the model and free up the memory"""
|
| 134 |
if self.model is not None:
|