Spaces:
Sleeping
Sleeping
| # app.py | |
| import gradio as gr | |
| from functools import lru_cache | |
| from transformers import pipeline | |
| # Ganti sesuai nama repo-mu di HF | |
| ID2MD = "hasmar03/mt5_id2md" | |
| MD2ID = "hasmar03/mt5_md2id" # <- model baru hasil training md→id | |
| def get_pipe(model_id: str): | |
| """ | |
| Cache per-model supaya load cuma sekali (jalan di CPU Spaces). | |
| """ | |
| return pipeline( | |
| task="text2text-generation", | |
| model=model_id, | |
| tokenizer=model_id, | |
| device=-1, # CPU (gratis) | |
| ) | |
| def translate(direction: str, text: str, max_new_tokens: int = 64) -> str: | |
| text = (text or "").strip() | |
| if not text: | |
| return "" | |
| # pilih model berdasar arah | |
| model_id = ID2MD if direction == "id2md" else MD2ID | |
| # prefix sesuai format training | |
| prompt = f"translate {direction}: {text}" | |
| out = get_pipe(model_id)( | |
| prompt, | |
| max_new_tokens=int(max_new_tokens), | |
| num_beams=5, | |
| do_sample=False, | |
| no_repeat_ngram_size=3, | |
| early_stopping=True, | |
| )[0]["generated_text"] | |
| return out | |
| with gr.Blocks(title="mT5 id↔md Translator (HF Space API)") as demo: | |
| gr.Markdown("# mT5 id↔md Translator (HF Space API)") | |
| with gr.Row(): | |
| direction = gr.Dropdown(["id2md", "md2id"], value="id2md", label="Arah") | |
| max_tok = gr.Slider(16, 128, value=64, step=1, label="max_new_tokens") | |
| inp = gr.Textbox(label="Teks sumber", lines=3, placeholder="Ketik kalimat...") | |
| out = gr.Textbox(label="Terjemahan", lines=3) | |
| btn = gr.Button("Terjemahkan", variant="primary") | |
| btn.click(translate, [direction, inp, max_tok], [out], api_name="translate") | |
| gr.Examples( | |
| examples=[ | |
| ["id2md", "ia terus pulang begitu saja", 64], | |
| ["md2id", "tarrus i pole tia", 64], | |
| ], | |
| inputs=[direction, inp, max_tok], | |
| outputs=[out], | |
| label="Contoh cepat", | |
| ) | |
| # kompatibel Gradio 4.x/5.x | |
| try: | |
| demo.queue() | |
| except TypeError: | |
| try: | |
| demo.queue(max_size=12) | |
| except TypeError: | |
| demo.queue(concurrency_count=1, max_size=12) | |
| if __name__ == "__main__": | |
| demo.launch() | |