Merge branch 'feature/add-prompt-translate'

# Conflicts:
#	requirements_versions.txt
#	webui.py
This commit is contained in:
Manuel Schmid 2023-11-26 00:14:12 +01:00
commit 298a807a1b
4 changed files with 28 additions and 3 deletions

View File

@ -44,6 +44,7 @@ def worker():
from modules.util import remove_empty_str, HWC3, resize_image, \
get_image_shape_ceil, set_image_shape_ceil, get_shape_ceil, resample_image
from modules.upscaler import perform_upscale
from modules.translator import translate2en
try:
async_gradio_app = shared.gradio_root
@ -128,6 +129,7 @@ def worker():
prompt = args.pop()
negative_prompt = args.pop()
translate_prompts = args.pop()
style_selections = args.pop()
performance_selection = args.pop()
aspect_ratios_selection = args.pop()
@ -203,6 +205,10 @@ def worker():
modules.patch.adm_scaler_end = advanced_parameters.adm_scaler_end = 0.0
steps = 8
if translate_prompts:
prompt = translate2en(prompt, 'prompt')
negative_prompt = translate2en(negative_prompt, 'negative prompt')
modules.patch.adaptive_cfg = advanced_parameters.adaptive_cfg
print(f'[Parameters] Adaptive CFG = {modules.patch.adaptive_cfg}')

15
modules/translator.py Normal file
View File

@ -0,0 +1,15 @@
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')
print(f'[Parameters] Translated {element}: {result}')
return result
except Exception as e:
print(f'[Parameters] Error during translation of {element}: {e}')
return text

View File

@ -6,7 +6,7 @@ accelerate==0.21.0
pyyaml==6.0
Pillow==9.2.0
scipy==1.9.3
tqdm==4.64.1
tqdm==4.65.0
psutil==5.9.5
numpy==1.23.5
pytorch_lightning==1.9.4
@ -15,4 +15,5 @@ gradio==3.41.2
pygit2==1.12.2
opencv-contrib-python==4.8.0.74
diffusers==0.21.4
httpx==0.24.1
httpx==0.24.1
translators==5.8.9

View File

@ -224,6 +224,9 @@ with shared.gradio_root:
info='Describing what you do not want to see.', lines=2,
elem_id='negative_prompt',
value=modules.config.default_prompt_negative)
translate_prompts = gr.Checkbox(label='Translate Prompts',
info='Uses the internet to translate prompts to English',
value=False)
seed_random = gr.Checkbox(label='Random', value=True)
image_seed = gr.Textbox(label='Seed', value=0, max_lines=1, visible=False) # workaround for https://github.com/gradio-app/gradio/issues/5354
@ -515,7 +518,7 @@ with shared.gradio_root:
], show_progress=False, queue=False)
ctrls = [
currentTask, prompt, negative_prompt, style_selections,
currentTask, prompt, negative_prompt, translate_prompts, style_selections,
performance_selection, aspect_ratios_selection, image_number, image_seed, sharpness, guidance_scale
]