Skip to content

Commit

Permalink
fix: only import translator when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mashb1t committed Jul 16, 2024
1 parent 02b06cc commit 27cbca2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion modules/async_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down Expand Up @@ -949,6 +948,8 @@ def prepare_enhance_prompt(prompt: str, fallback_prompt: str, translate: bool, p
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

Expand Down Expand Up @@ -1093,6 +1094,8 @@ def handler(async_task: AsyncTask):
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')

Expand Down
5 changes: 3 additions & 2 deletions modules/translator.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 27cbca2

Please sign in to comment.