Spaces:
Sleeping
Sleeping
File size: 2,140 Bytes
0c3f13f f9230a6 6cb1404 8e0bff9 f9230a6 0c3f13f f665421 0c3f13f 5271b46 0c3f13f 5271b46 6cb1404 5271b46 0c3f13f 6cb1404 f665421 5271b46 0c3f13f 8e0bff9 5271b46 0c3f13f 8e0bff9 5271b46 0c3f13f 6cb1404 5271b46 6cb1404 f9230a6 6cb1404 8e0bff9 5271b46 6cb1404 5271b46 8e0bff9 f9230a6 6cb1404 5271b46 6cb1404 0c3f13f 5271b46 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 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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
@lru_cache(maxsize=2)
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()
|