edugp commited on
Commit
18ca2a9
1 Parent(s): 6b1177a

Add Image-Caption matching app

Browse files
Files changed (1) hide show
  1. app.py +37 -2
app.py CHANGED
@@ -1,4 +1,39 @@
 
 
 
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
  import streamlit as st
5
+ from huggingface_hub import snapshot_download
6
+ from transformers import AutoTokenizer
7
+
8
+
9
+ LOCAL_PATH = snapshot_download("flax-community/clip-spanish")
10
+ sys.path.append(LOCAL_PATH)
11
+
12
+ from modeling_hybrid_clip import FlaxHybridCLIP
13
+ from test_on_image import run_inference
14
+
15
+
16
+ def save_file_to_disk(uplaoded_file):
17
+ temp_file = os.path.join("/tmp", uplaoded_file.name)
18
+ with open(temp_file,"wb") as f:
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")
29
+ text_input = st.text_input("Type a caption")
30
 
31
+ 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:
39
+ os.remove(local_image_path)