fix: only import translator when needed

This commit is contained in:
Manuel Schmid 2024-07-16 19:21:31 +02:00
parent 02b06ccb33
commit 27cbca2d90
No known key found for this signature in database
GPG Key ID: 32C4F7569B40B84B
2 changed files with 7 additions and 3 deletions

View File

@ -198,7 +198,6 @@ def worker():
from modules.upscaler import perform_upscale
from modules.flags import Performance
from modules.meta_parser import get_metadata_parser
from modules.translator import translate2en
pid = os.getpid()
print(f'Started worker with PID {pid}')
@ -949,6 +948,8 @@ def worker():
prompt = fallback_prompt
else:
if translate:
# only initialize when needed, requires an internet connection
from modules.translator import translate2en
prompt = translate2en(prompt, prompt_type)
return prompt
@ -1093,6 +1094,8 @@ def worker():
set_hyper_sd_defaults(async_task, current_progress, advance_progress=True)
if async_task.translate_prompts:
# only initialize when needed, requires an internet connection
from modules.translator import translate2en
async_task.prompt = translate2en(async_task.prompt, 'prompt')
async_task.negative_prompt = translate2en(async_task.negative_prompt, 'negative prompt')

View File

@ -1,15 +1,16 @@
import translators
from functools import lru_cache
@lru_cache(maxsize=32, typed=False)
def translate2en(text, element):
if not text:
return text
try:
result = translators.translate_text(text,to_language='en')
result = translators.translate_text(text, to_language='en')
print(f'[Parameters] Translated {element}: {result}')
return result
except Exception as e:
print(f'[Parameters] Error during translation of {element}: {e}')
return text
return text