hasmar03 commited on
Commit
5271b46
·
verified ·
1 Parent(s): 1dc2e2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -15
app.py CHANGED
@@ -6,41 +6,74 @@ MODEL_ID = "hasmar03/mt5_id2md"
6
 
7
  @lru_cache(maxsize=1)
8
  def get_pipe():
 
 
 
 
9
  return pipeline(
10
- "text2text-generation",
11
  model=MODEL_ID,
12
  tokenizer=MODEL_ID,
13
- device=-1, # CPU
 
14
  )
15
 
16
- def translate(direction, text, max_new_tokens=64):
17
- if not text.strip():
18
  return ""
 
 
19
  prompt = f"translate {direction}: {text}"
 
 
 
 
 
20
  out = get_pipe()(
21
  prompt,
22
- num_beams=4,
23
- do_sample=False,
24
  max_new_tokens=int(max_new_tokens),
 
 
 
 
25
  )[0]["generated_text"]
 
26
  return out
27
 
28
  with gr.Blocks(title="mT5 id↔md Translator (HF Space API)") as demo:
29
  gr.Markdown("# mT5 id↔md Translator (HF Space API)")
30
- direction = gr.Dropdown(["id2md", "md2id"], value="id2md", label="Arah")
31
- inp = gr.Textbox(label="Teks sumber", lines=3)
32
- max_tok = gr.Slider(16, 128, value=64, step=1, label="max_new_tokens")
 
 
 
33
  out = gr.Textbox(label="Terjemahan", lines=3)
34
- btn = gr.Button("Terjemahkan")
 
35
  btn.click(translate, [direction, inp, max_tok], [out], api_name="translate")
36
 
37
  gr.Examples(
38
- [["id2md", "ia terus pulang begitu saja", 64],
39
- ["md2id", "tarrus i pole tia", 64]],
40
- [direction, inp, max_tok],
41
- [out]
 
 
 
42
  )
43
 
44
- demo.queue(concurrency=1, max_size=12)
 
 
 
 
 
 
 
 
 
 
 
45
  if __name__ == "__main__":
46
  demo.launch()
 
6
 
7
  @lru_cache(maxsize=1)
8
  def get_pipe():
9
+ """
10
+ Cache pipeline supaya model hanya dimuat 1x.
11
+ Pakai CPU (device=-1) agar gratisan di Spaces jalan.
12
+ """
13
  return pipeline(
14
+ task="text2text-generation",
15
  model=MODEL_ID,
16
  tokenizer=MODEL_ID,
17
+ device=-1, # CPU
18
+ # framework & dtype otomatis oleh HF/Transformers
19
  )
20
 
21
+ def translate(direction: str, text: str, max_new_tokens: int = 64) -> str:
22
+ if not text or not text.strip():
23
  return ""
24
+
25
+ # Prefix sesuai format training mT5 kamu
26
  prompt = f"translate {direction}: {text}"
27
+
28
+ # Setting decoding yang umum untuk terjemahan
29
+ # - num_beams memperbaiki kualitas (sedikit lebih lambat tapi OK di CPU kecil)
30
+ # - no_repeat_ngram_size mengurangi pengulangan
31
+ # - early_stopping untuk hentikan beam kalau sudah cukup baik
32
  out = get_pipe()(
33
  prompt,
 
 
34
  max_new_tokens=int(max_new_tokens),
35
+ num_beams=5,
36
+ do_sample=False,
37
+ no_repeat_ngram_size=3,
38
+ early_stopping=True,
39
  )[0]["generated_text"]
40
+
41
  return out
42
 
43
  with gr.Blocks(title="mT5 id↔md Translator (HF Space API)") as demo:
44
  gr.Markdown("# mT5 id↔md Translator (HF Space API)")
45
+
46
+ with gr.Row():
47
+ direction = gr.Dropdown(["id2md", "md2id"], value="id2md", label="Arah")
48
+ max_tok = gr.Slider(16, 128, value=64, step=1, label="max_new_tokens")
49
+
50
+ inp = gr.Textbox(label="Teks sumber", lines=3, placeholder="Ketik kalimat...")
51
  out = gr.Textbox(label="Terjemahan", lines=3)
52
+ btn = gr.Button("Terjemahkan", variant="primary")
53
+
54
  btn.click(translate, [direction, inp, max_tok], [out], api_name="translate")
55
 
56
  gr.Examples(
57
+ examples=[
58
+ ["id2md", "ia terus pulang begitu saja", 64],
59
+ ["md2id", "tarrus i pole tia", 64],
60
+ ],
61
+ inputs=[direction, inp, max_tok],
62
+ outputs=[out],
63
+ label="Contoh cepat",
64
  )
65
 
66
+ # ==== Queue versi-agnostik (jalan di Gradio 4.x & 5.x) ====
67
+ try:
68
+ # Gradio 5.x (beberapa build menolak argumen apa pun)
69
+ demo.queue()
70
+ except TypeError:
71
+ try:
72
+ # Gradio 5.x lain (punya max_size)
73
+ demo.queue(max_size=12)
74
+ except TypeError:
75
+ # Gradio 4.x (pakai concurrency_count)
76
+ demo.queue(concurrency_count=1, max_size=12)
77
+
78
  if __name__ == "__main__":
79
  demo.launch()