File size: 1,769 Bytes
aab0102
0b3a9ec
aab0102
0b3a9ec
 
 
 
 
 
 
 
aab0102
0b3a9ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
import vllm

# vLLMモデルの初期化
llm = vllm.LLM(
    model="pfnet/plamo-2-translate",
    trust_remote_code=True,
    max_model_len=8192,
    max_num_seqs=32,
    dtype="bfloat16",
)

def translate_text(text, direction):
    """テキスト翻訳(双方向対応)"""
    if not text.strip():
        return ""
    
    # 翻訳方向に応じてプロンプトを設定
    if direction == "英語→日本語":
        source_lang = "English"
        target_lang = "Japanese"
    else:  # "日本語→英語"
        source_lang = "Japanese"
        target_lang = "English"
    
    prompt = f'''<|plamo:op|>dataset
translation
<|plamo:op|>input lang={source_lang}
{text}
<|plamo:op|>output lang={target_lang}
'''

    responses = llm.generate(
        prompt, 
        sampling_params=vllm.SamplingParams(
            temperature=0,
            max_tokens=2048,
            stop=["<|plamo:op|>"]
        )
    )
    
    result = responses[0].outputs[0].text.strip()
    return result

# Gradioインターフェースの構築
demo = gr.Interface(
    fn=translate_text,
    inputs=[
        gr.Textbox(lines=10, max_lines=40, label="翻訳元テキスト"),
        gr.Radio(
            choices=["英語→日本語", "日本語→英語"],
            value="英語→日本語",
            label="翻訳方向"
        )
    ],           
    outputs=gr.Textbox(lines=10, max_lines=40, show_copy_button=True, label="翻訳結果"),                    
    title="双方向翻訳アプリ(Plamo-2)",
    description="テキストを入力し、翻訳方向を選択してください。英語⇔日本語の双方向翻訳が可能です。"
)

# アプリケーションの起動
if __name__ == "__main__":
    demo.launch()