File size: 1,127 Bytes
f9230a6
8e0bff9
f9230a6
8e0bff9
f665421
8e0bff9
 
 
 
 
f665421
 
8e0bff9
 
 
 
 
f9230a6
 
8e0bff9
 
 
 
 
 
 
 
 
 
f9230a6
f156c39
 
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
import gradio as gr
from transformers import pipeline

MODEL_ID = "hasmar03/mt5_id2md"

pipe = pipeline(
    "text2text-generation",
    model=MODEL_ID,
    tokenizer=MODEL_ID,
    device=-1  # CPU
)

def translate(direction, text, max_new_tokens=64):
    if not text.strip():
        return ""
    prompt = f"translate {direction}: {text}"
    out = pipe(prompt, num_beams=4, do_sample=False, max_new_tokens=int(max_new_tokens))[0]["generated_text"]
    return out

with gr.Blocks() as demo:
    gr.Markdown("# mT5 id↔md Translator (HF Space API)")
    direction = gr.Dropdown(["id2md", "md2id"], value="id2md", label="Arah")
    inp       = gr.Textbox(label="Teks sumber", lines=3)
    max_tok   = gr.Slider(16, 128, value=64, step=1, label="max_new_tokens")
    out       = gr.Textbox(label="Terjemahan", lines=3)
    btn       = gr.Button("Terjemahkan")
    btn.click(translate, [direction, inp, max_tok], [out], api_name="translate")
    gr.Examples([["id2md","ia terus pulang begitu saja",64],
                 ["md2id","tarrus i pole tia",64]], [direction, inp, max_tok])

if __name__ == "__main__":
    demo.launch()