File size: 1,265 Bytes
6c20b9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

# model ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
model_name = "gj5520/kkachi60_en2ko"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device).eval()

# ๋ฒˆ์—ญ ํ•จ์ˆ˜
def translate(text: str):
    prefix = "translate English to Korean: "
    inputs = tokenizer(prefix + text,
                       return_tensors="pt",
                       truncation=True,
                       padding="longest").to(device)
    outputs = model.generate(
        **inputs,
        max_new_tokens=100,
        num_beams=5,
        no_repeat_ngram_size=3,
        early_stopping=True,
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
interface = gr.Interface(
    fn=translate,
    inputs=gr.Textbox(
        lines=3,
        placeholder="Enter English text here..."
    ),
    outputs="text",
    title="์˜์–ดโ†’ํ•œ๊ตญ์–ด ๋ฒˆ์—ญ๊ธฐ (kkachi60_en2ko)",
    description="Hugging Face kkachi60_en2ko ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ์‹ค์‹œ๊ฐ„ ๋ฒˆ์—ญ"
)

# Spaces์—์„œ ์ž๋™ ์‹คํ–‰
if __name__ == "__main__":
    interface.launch()