Spaces:
Running
Running
Kyudan
commited on
Commit
ยท
f7dcae3
1
Parent(s):
624dfff
update
Browse files- translate_utils.py +16 -0
translate_utils.py
CHANGED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import MarianMTModel, MarianTokenizer
|
2 |
+
|
3 |
+
# 1) MarianMT ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ถ๋ฌ์ค๊ธฐ
|
4 |
+
model_name = "Helsinki-NLP/opus-mt-ko-en"
|
5 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
6 |
+
model = MarianMTModel.from_pretrained(model_name)
|
7 |
+
|
8 |
+
# 2) ๋ฒ์ญ ํจ์ ์์
|
9 |
+
def translate_ko_to_en(text: str) -> str:
|
10 |
+
# ์
๋ ฅ ๋ฌธ์ฅ์ ํ ํฐํ
|
11 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
12 |
+
# ๋ชจ๋ธ๋ก ์ถ๋ก
|
13 |
+
translated = model.generate(**inputs)
|
14 |
+
# ํ ํฐ์ ๋ค์ ๋ฌธ์ฅ์ผ๋ก ๋์ฝ๋ฉ
|
15 |
+
translation = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
16 |
+
return translation[0]
|