Spaces:
Sleeping
Sleeping
| 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() | |