jhj0517 commited on
Commit
c1e582f
·
1 Parent(s): 0669fbf

Cache parameters

Browse files
modules/translation/deepl_api.py CHANGED
@@ -4,8 +4,9 @@ import os
4
  from datetime import datetime
5
  import gradio as gr
6
 
7
- from modules.utils.paths import TRANSLATION_OUTPUT_DIR
8
  from modules.utils.subtitle_manager import *
 
9
 
10
  """
11
  This is written with reference to the DeepL API documentation.
@@ -125,6 +126,12 @@ class DeepLAPI:
125
  String to return to gr.Textbox()
126
  Files to return to gr.Files()
127
  """
 
 
 
 
 
 
128
 
129
  files_info = {}
130
  for fileobj in fileobjs:
@@ -199,4 +206,18 @@ class DeepLAPI:
199
  }
200
  response = requests.post(url, headers=headers, data=data).json()
201
  time.sleep(self.api_interval)
202
- return response["translations"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from datetime import datetime
5
  import gradio as gr
6
 
7
+ from modules.utils.paths import TRANSLATION_OUTPUT_DIR, DEFAULT_PARAMETERS_CONFIG_PATH
8
  from modules.utils.subtitle_manager import *
9
+ from modules.utils.files_manager import load_yaml, save_yaml
10
 
11
  """
12
  This is written with reference to the DeepL API documentation.
 
126
  String to return to gr.Textbox()
127
  Files to return to gr.Files()
128
  """
129
+ self.cache_parameters(
130
+ api_key=auth_key,
131
+ is_pro=is_pro,
132
+ source_lang=source_lang,
133
+ target_lang=target_lang
134
+ )
135
 
136
  files_info = {}
137
  for fileobj in fileobjs:
 
206
  }
207
  response = requests.post(url, headers=headers, data=data).json()
208
  time.sleep(self.api_interval)
209
+ return response["translations"]
210
+
211
+ @staticmethod
212
+ def cache_parameters(api_key: str,
213
+ is_pro: bool,
214
+ source_lang: str,
215
+ target_lang: str):
216
+ cached_params = load_yaml(DEFAULT_PARAMETERS_CONFIG_PATH)
217
+ cached_params["translation"]["deepl"] = {
218
+ "api_key": api_key,
219
+ "is_pro": is_pro,
220
+ "source_lang": source_lang,
221
+ "target_lang": target_lang
222
+ }
223
+ save_yaml(cached_params, DEFAULT_PARAMETERS_CONFIG_PATH)
modules/translation/translation_base.py CHANGED
@@ -7,6 +7,8 @@ from datetime import datetime
7
 
8
  from modules.whisper.whisper_parameter import *
9
  from modules.utils.subtitle_manager import *
 
 
10
 
11
 
12
  class TranslationBase(ABC):
@@ -75,6 +77,11 @@ class TranslationBase(ABC):
75
  Files to return to gr.Files()
76
  """
77
  try:
 
 
 
 
 
78
  self.update_model(model_size=model_size,
79
  src_lang=src_lang,
80
  tgt_lang=tgt_lang,
@@ -149,3 +156,17 @@ class TranslationBase(ABC):
149
  for file_path in file_paths:
150
  if file_path and os.path.exists(file_path):
151
  os.remove(file_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  from modules.whisper.whisper_parameter import *
9
  from modules.utils.subtitle_manager import *
10
+ from modules.utils.files_manager import load_yaml, save_yaml
11
+ from modules.utils.paths import DEFAULT_PARAMETERS_CONFIG_PATH
12
 
13
 
14
  class TranslationBase(ABC):
 
77
  Files to return to gr.Files()
78
  """
79
  try:
80
+ self.cache_parameters(model_size=model_size,
81
+ src_lang=src_lang,
82
+ tgt_lang=tgt_lang,
83
+ max_length=max_length)
84
+
85
  self.update_model(model_size=model_size,
86
  src_lang=src_lang,
87
  tgt_lang=tgt_lang,
 
156
  for file_path in file_paths:
157
  if file_path and os.path.exists(file_path):
158
  os.remove(file_path)
159
+
160
+ @staticmethod
161
+ def cache_parameters(model_size: str,
162
+ src_lang: str,
163
+ tgt_lang: str,
164
+ max_length: int):
165
+ cached_params = load_yaml(DEFAULT_PARAMETERS_CONFIG_PATH)
166
+ cached_params["translation"]["nllb"] = {
167
+ "model_size": model_size,
168
+ "src_lang": src_lang,
169
+ "tgt_lang": tgt_lang,
170
+ "max_length": max_length
171
+ }
172
+ save_yaml(cached_params, DEFAULT_PARAMETERS_CONFIG_PATH)