hasmar03 commited on
Commit
6cb1404
·
verified ·
1 Parent(s): fae674a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -1,32 +1,46 @@
1
  import gradio as gr
 
2
  from transformers import pipeline
3
 
4
  MODEL_ID = "hasmar03/mt5_id2md"
5
 
6
- pipe = pipeline(
7
- "text2text-generation",
8
- model=MODEL_ID,
9
- tokenizer=MODEL_ID,
10
- device=-1 # CPU
11
- )
 
 
12
 
13
  def translate(direction, text, max_new_tokens=64):
14
  if not text.strip():
15
  return ""
16
  prompt = f"translate {direction}: {text}"
17
- out = pipe(prompt, num_beams=4, do_sample=False, max_new_tokens=int(max_new_tokens))[0]["generated_text"]
 
 
 
 
 
18
  return out
19
 
20
- with gr.Blocks() as demo:
21
  gr.Markdown("# mT5 id↔md Translator (HF Space API)")
22
  direction = gr.Dropdown(["id2md", "md2id"], value="id2md", label="Arah")
23
- inp = gr.Textbox(label="Teks sumber", lines=3)
24
- max_tok = gr.Slider(16, 128, value=64, step=1, label="max_new_tokens")
25
- out = gr.Textbox(label="Terjemahan", lines=3)
26
- btn = gr.Button("Terjemahkan")
27
  btn.click(translate, [direction, inp, max_tok], [out], api_name="translate")
28
- gr.Examples([["id2md","ia terus pulang begitu saja",64],
29
- ["md2id","tarrus i pole tia",64]], [direction, inp, max_tok])
30
 
 
 
 
 
 
 
 
 
31
  if __name__ == "__main__":
32
  demo.launch()
 
1
  import gradio as gr
2
+ from functools import lru_cache
3
  from transformers import pipeline
4
 
5
  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_count=1, max_size=12)
45
  if __name__ == "__main__":
46
  demo.launch()