davanstrien HF staff commited on
Commit
6634f63
1 Parent(s): 61e0458

add and cache examples (#10)

Browse files

- add and cache examples (ceb80b558f42abc6be7c752889a23e717fe54899)

Files changed (1) hide show
  1. app.py +12 -1
app.py CHANGED
@@ -5,6 +5,7 @@ from flores import code_mapping
5
  import platform
6
  import torch
7
  import nltk
 
8
 
9
  nltk.download("punkt_tab")
10
 
@@ -34,8 +35,14 @@ def load_tokenizer(src_lang, tgt_lang):
34
  return tokenizer
35
 
36
 
37
- @spaces.GPU
 
38
  def translate(text: str, src_lang: str, tgt_lang: str):
 
 
 
 
 
39
  tokenizer = load_tokenizer(src_lang, tgt_lang)
40
 
41
  paragraphs = text.split("\n")
@@ -98,6 +105,9 @@ By using this translation tool, you agree to these terms and acknowledge that th
98
  For any feedback or support, please contact UNESCO World Atlas of Languages Team: WAL.Data@unesco.org.
99
  """
100
 
 
 
 
101
  with gr.Blocks() as demo:
102
  gr.Markdown(description)
103
  with gr.Row():
@@ -114,6 +124,7 @@ with gr.Blocks() as demo:
114
  inputs=[input_text, src_lang, target_lang],
115
  outputs=output,
116
  )
 
117
  with gr.Row():
118
  gr.Markdown(disclaimer)
119
  demo.launch()
 
5
  import platform
6
  import torch
7
  import nltk
8
+ from functools import lru_cache
9
 
10
  nltk.download("punkt_tab")
11
 
 
35
  return tokenizer
36
 
37
 
38
+ # cache function
39
+ @lru_cache(maxsize=100)
40
  def translate(text: str, src_lang: str, tgt_lang: str):
41
+ return _translate(text, src_lang,tgt_lang )
42
+
43
+ # Only assign GPU if cache not used
44
+ @spaces.GPU
45
+ def _translate(text: str, src_lang: str, tgt_lang: str):
46
  tokenizer = load_tokenizer(src_lang, tgt_lang)
47
 
48
  paragraphs = text.split("\n")
 
105
  For any feedback or support, please contact UNESCO World Atlas of Languages Team: WAL.Data@unesco.org.
106
  """
107
 
108
+
109
+ examples_inputs = [["The United Nations Educational, Scientific and Cultural Organization is a specialized agency of the United Nations with the aim of promoting world peace and security through international cooperation in education, arts, sciences and culture. ","English","Ayacucho Quechua"],]
110
+
111
  with gr.Blocks() as demo:
112
  gr.Markdown(description)
113
  with gr.Row():
 
124
  inputs=[input_text, src_lang, target_lang],
125
  outputs=output,
126
  )
127
+ examples = gr.Examples(examples=examples_inputs,inputs=[input_text, src_lang,target_lang], fn=translate, outputs=output, cache_examples=True)
128
  with gr.Row():
129
  gr.Markdown(disclaimer)
130
  demo.launch()