Spaces:
Runtime error
Runtime error
uncommented
Browse files
app.py
CHANGED
@@ -1,66 +1,66 @@
|
|
1 |
import streamlit as st
|
2 |
import io
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
|
45 |
-
|
46 |
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
|
57 |
-
from model import *
|
58 |
-
download_files()
|
59 |
-
sample_images = get_samples()
|
60 |
-
v, il = VirTexModel(), ImageLoader()
|
61 |
|
62 |
-
for s in sample_images:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import io
|
3 |
|
4 |
+
st.title("Image Captioning Demo from Redcaps")
|
5 |
+
st.sidebar.markdown(
|
6 |
+
"""
|
7 |
+
Image Captioning Model from VirTex trained on Redcaps
|
8 |
+
"""
|
9 |
+
)
|
10 |
|
11 |
+
with st.spinner("Loading Model"):
|
12 |
+
from model import *
|
13 |
+
sample_images = glob.glob("./samples/*.jpg")
|
14 |
+
download_files()
|
15 |
+
virtexModel = VirTexModel()
|
16 |
+
imageLoader = ImageLoader()
|
17 |
|
18 |
+
random_image = get_rand_img(sample_images)
|
19 |
|
20 |
+
st.sidebar.title("Select a sample image")
|
21 |
+
sample_image = st.sidebar.selectbox(
|
22 |
+
"",
|
23 |
+
sample_images
|
24 |
+
)
|
25 |
|
26 |
+
if st.sidebar.button("Random Sample Image"):
|
27 |
+
random_image = get_rand_img(sample_images)
|
28 |
+
sample_image = None
|
29 |
|
30 |
+
uploaded_image = None
|
31 |
+
with st.sidebar.form("file-uploader-form", clear_on_submit=True):
|
32 |
+
uploaded_file = st.file_uploader("Choose a file")
|
33 |
+
submitted = st.form_submit_button("Submit")
|
34 |
+
if uploaded_file is not None and submitted:
|
35 |
+
uploaded_image = Image.open(io.BytesIO(uploaded_file.get_values()))
|
36 |
|
37 |
+
if uploaded_image is None and submitted:
|
38 |
+
st.write("Please select a file to upload")
|
39 |
|
40 |
+
else:
|
41 |
+
image_file = sample_image if sample_image is not None else random_image
|
42 |
|
43 |
+
image = uploaded_image if uploaded_image is not None else Image.open()
|
44 |
|
45 |
+
image_dict = imageLoader.transform(image)
|
46 |
|
47 |
+
show.image(st.image(image_dict["image"]), "Target Image")
|
48 |
|
49 |
+
with st.spinner("Generating Caption"):
|
50 |
+
subreddit, caption = virtexModel.predict(image_dict)
|
51 |
+
st.header("Predicted Caption:\n\n")
|
52 |
+
st.subheader(f"Subreddit: {subreddit}\n")
|
53 |
+
st.subheader(f"Caption: {caption}\n")
|
54 |
|
55 |
+
image.close()
|
56 |
|
57 |
+
# from model import *
|
58 |
+
# download_files()
|
59 |
+
# sample_images = get_samples()
|
60 |
+
# v, il = VirTexModel(), ImageLoader()
|
61 |
|
62 |
+
# for s in sample_images:
|
63 |
+
# subreddit, caption = v.predict(il.load(s))
|
64 |
+
# print("=====================")
|
65 |
+
# print(subreddit)
|
66 |
+
# print(caption)
|