edugp commited on
Commit
75ecf13
1 Parent(s): 7dd21bb

Cache tokenizer and model loading

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -19,10 +19,14 @@ def save_file_to_disk(uplaoded_file):
19
  f.write(uploaded_file.getbuffer())
20
  return temp_file
21
 
22
- # load the saved model
23
- tokenizer = AutoTokenizer.from_pretrained("dccuchile/bert-base-spanish-wwm-cased")
24
- model = FlaxHybridCLIP.from_pretrained(LOCAL_PATH)
 
 
 
25
 
 
26
 
27
  st.title("Image-Caption Matching")
28
  uploaded_file = st.file_uploader("Choose an image...", type="jpg")
@@ -32,7 +36,7 @@ if uploaded_file is not None and text_input:
32
  local_image_path = None
33
  try:
34
  local_image_path = save_file_to_disk(uploaded_file)
35
- score = run_inference(local_image_path, text_input, model)[0][0].tolist()
36
  st.write('%s (%.2f%%)' % score)
37
  finally:
38
  if local_image_path:
 
19
  f.write(uploaded_file.getbuffer())
20
  return temp_file
21
 
22
+ @st.cache
23
+ def load_tokenizer_and_model():
24
+ # load the saved model
25
+ tokenizer = AutoTokenizer.from_pretrained("dccuchile/bert-base-spanish-wwm-cased")
26
+ model = FlaxHybridCLIP.from_pretrained(LOCAL_PATH)
27
+ return tokenizer, model
28
 
29
+ tokenizer, model = load_tokenizer_and_model()
30
 
31
  st.title("Image-Caption Matching")
32
  uploaded_file = st.file_uploader("Choose an image...", type="jpg")
 
36
  local_image_path = None
37
  try:
38
  local_image_path = save_file_to_disk(uploaded_file)
39
+ score = run_inference(local_image_path, text_input, model, tokenizer)[0][0].tolist()
40
  st.write('%s (%.2f%%)' % score)
41
  finally:
42
  if local_image_path: