Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -753,8 +753,56 @@ ko_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
|
753 |
ja_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ja-en")
|
754 |
zh_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
755 |
|
756 |
-
from transformers import MarianMTModel, MarianTokenizer
|
757 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
758 |
def translate_text(text, src_lang, model_name):
|
759 |
try:
|
760 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
@@ -778,46 +826,6 @@ def translate_if_needed(prompt):
|
|
778 |
return translate_text(prompt, 'zh', 'Helsinki-NLP/opus-mt-zh-en')
|
779 |
return prompt
|
780 |
|
781 |
-
@spaces.GPU
|
782 |
-
@torch.no_grad()
|
783 |
-
def generate_image(
|
784 |
-
prompt, width, height, guidance, inference_steps, seed,
|
785 |
-
do_img2img, init_image, image2image_strength, resize_img,
|
786 |
-
progress=gr.Progress(track_tqdm=True),
|
787 |
-
):
|
788 |
-
translated_prompt = translate_if_needed(prompt)
|
789 |
-
if translated_prompt != prompt:
|
790 |
-
print(f"Translated prompt: {translated_prompt}")
|
791 |
-
prompt = translated_prompt
|
792 |
-
|
793 |
-
|
794 |
-
# 한글, 일본어, 중국어 문자 감지
|
795 |
-
def contains_korean(text):
|
796 |
-
return any('\u3131' <= c <= '\u318E' or '\uAC00' <= c <= '\uD7A3' for c in text)
|
797 |
-
|
798 |
-
def contains_japanese(text):
|
799 |
-
return any('\u3040' <= c <= '\u309F' or '\u30A0' <= c <= '\u30FF' or '\u4E00' <= c <= '\u9FFF' for c in text)
|
800 |
-
|
801 |
-
def contains_chinese(text):
|
802 |
-
return any('\u4e00' <= c <= '\u9fff' for c in text)
|
803 |
-
|
804 |
-
# 한글, 일본어, 중국어가 있으면 번역
|
805 |
-
if contains_korean(prompt):
|
806 |
-
translated_prompt = ko_translator(prompt, max_length=512)[0]['translation_text']
|
807 |
-
print(f"Translated Korean prompt: {translated_prompt}")
|
808 |
-
prompt = translated_prompt
|
809 |
-
elif contains_japanese(prompt):
|
810 |
-
translated_prompt = ja_translator(prompt, max_length=512)[0]['translation_text']
|
811 |
-
print(f"Translated Japanese prompt: {translated_prompt}")
|
812 |
-
prompt = translated_prompt
|
813 |
-
elif contains_chinese(prompt):
|
814 |
-
translated_prompt = zh_translator(prompt, max_length=512)[0]['translation_text']
|
815 |
-
print(f"Translated Chinese prompt: {translated_prompt}")
|
816 |
-
prompt = translated_prompt
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
|
822 |
if seed == 0:
|
823 |
seed = int(random.random() * 1000000)
|
|
|
753 |
ja_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ja-en")
|
754 |
zh_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
|
755 |
|
|
|
756 |
|
757 |
+
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
758 |
+
|
759 |
+
def translate_text(text):
|
760 |
+
try:
|
761 |
+
# M2M100은 다국어 번역을 한 모델로 처리할 수 있습니다
|
762 |
+
model_name = "facebook/m2m100_418M"
|
763 |
+
tokenizer = M2M100Tokenizer.from_pretrained(model_name)
|
764 |
+
model = M2M100ForConditionalGeneration.from_pretrained(model_name).to(device)
|
765 |
+
|
766 |
+
# 언어 감지
|
767 |
+
def detect_language(text):
|
768 |
+
if any('\u3131' <= c <= '\u318E' or '\uAC00' <= c <= '\uD7A3' for c in text):
|
769 |
+
return 'ko'
|
770 |
+
elif any('\u3040' <= c <= '\u309F' or '\u30A0' <= c <= '\u30FF' for c in text):
|
771 |
+
return 'ja'
|
772 |
+
elif any('\u4e00' <= c <= '\u9fff' for c in text):
|
773 |
+
return 'zh'
|
774 |
+
return None
|
775 |
+
|
776 |
+
src_lang = detect_language(text)
|
777 |
+
if src_lang is None:
|
778 |
+
return text
|
779 |
+
|
780 |
+
tokenizer.src_lang = src_lang
|
781 |
+
encoded = tokenizer(text, return_tensors="pt").to(device)
|
782 |
+
generated_tokens = model.generate(
|
783 |
+
**encoded,
|
784 |
+
forced_bos_token_id=tokenizer.get_lang_id("en"),
|
785 |
+
max_length=128
|
786 |
+
)
|
787 |
+
return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
788 |
+
except Exception as e:
|
789 |
+
print(f"Translation error: {e}")
|
790 |
+
return text
|
791 |
+
|
792 |
+
@spaces.GPU
|
793 |
+
@torch.no_grad()
|
794 |
+
def generate_image(
|
795 |
+
prompt, width, height, guidance, inference_steps, seed,
|
796 |
+
do_img2img, init_image, image2image_strength, resize_img,
|
797 |
+
progress=gr.Progress(track_tqdm=True),
|
798 |
+
):
|
799 |
+
translated_prompt = translate_text(prompt)
|
800 |
+
if translated_prompt != prompt:
|
801 |
+
print(f"Translated prompt: {translated_prompt}")
|
802 |
+
prompt = translated_prompt
|
803 |
+
|
804 |
+
if seed == 0:
|
805 |
+
seed = int(random.random() * 1000000)
|
806 |
def translate_text(text, src_lang, model_name):
|
807 |
try:
|
808 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
|
|
826 |
return translate_text(prompt, 'zh', 'Helsinki-NLP/opus-mt-zh-en')
|
827 |
return prompt
|
828 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
829 |
|
830 |
if seed == 0:
|
831 |
seed = int(random.random() * 1000000)
|