Amite5h commited on
Commit
6643f07
·
1 Parent(s): b17dfaa
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+ import streamlit as st
4
+ import requests
5
+ from PIL import Image
6
+ from model import get_caption_model, generate_caption
7
+
8
+ @st.cache(allow_output_mutation=True)
9
+ def get_model():
10
+ return get_caption_model()
11
+
12
+ caption_model = get_model()
13
+
14
+
15
+ def predict():
16
+ captions = []
17
+ pred_caption = generate_caption('tmp.jpg', caption_model)
18
+
19
+ st.markdown('#### Predicted Captions:')
20
+ captions.append(pred_caption)
21
+
22
+ for _ in range(4):
23
+ pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
24
+ if pred_caption not in captions:
25
+ captions.append(pred_caption)
26
+
27
+ for c in captions:
28
+ st.write(c)
29
+
30
+
31
+ st.title('Image Captioner')
32
+ img_url = st.text_input(label='Enter Image URL')
33
+
34
+ if (img_url != "") and (img_url != None):
35
+ img = Image.open(requests.get(img_url, stream=True).raw)
36
+ img = img.convert('RGB')
37
+ st.image(img)
38
+ img.save('tmp.jpg')
39
+ predict()
40
+ os.remove('tmp.jpg')
41
+
42
+
43
+ st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
44
+ img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
45
+
46
+ if img_upload != None:
47
+ img = img_upload.read()
48
+ img = Image.open(io.BytesIO(img))
49
+ img = img.convert('RGB')
50
+ img.save('tmp.jpg')
51
+ st.image(img)
52
+ predict()
53
+ os.remove('tmp.jpg')