Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -5,49 +5,58 @@ import requests
|
|
5 |
from PIL import Image
|
6 |
from model import get_caption_model, generate_caption
|
7 |
|
8 |
-
|
9 |
-
@st.
|
10 |
def get_model():
|
11 |
return get_caption_model()
|
12 |
|
13 |
caption_model = get_model()
|
14 |
|
15 |
-
|
16 |
def predict():
|
17 |
captions = []
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
st.title('Image Captioner')
|
32 |
-
img_url = st.text_input(label='Enter Image URL')
|
33 |
|
34 |
-
|
35 |
-
|
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
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
from model import get_caption_model, generate_caption
|
7 |
|
8 |
+
# Use st.experimental_memo instead of st.cache_resource
|
9 |
+
@st.experimental_memo(allow_output_mutation=True)
|
10 |
def get_model():
|
11 |
return get_caption_model()
|
12 |
|
13 |
caption_model = get_model()
|
14 |
|
|
|
15 |
def predict():
|
16 |
captions = []
|
17 |
+
try:
|
18 |
+
pred_caption = generate_caption('tmp.jpg', caption_model)
|
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 |
+
except Exception as e:
|
30 |
+
st.error(f"An error occurred during prediction: {e}")
|
31 |
|
32 |
st.title('Image Captioner')
|
|
|
33 |
|
34 |
+
# Handle image URL input
|
35 |
+
img_url = st.text_input(label='Enter Image URL')
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
if img_url:
|
38 |
+
try:
|
39 |
+
img = Image.open(requests.get(img_url, stream=True).raw)
|
40 |
+
img = img.convert('RGB')
|
41 |
+
st.image(img)
|
42 |
+
img.save('tmp.jpg')
|
43 |
+
predict()
|
44 |
+
os.remove('tmp.jpg')
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"An error occurred with the image URL: {e}")
|
47 |
|
48 |
st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
|
49 |
+
|
50 |
+
# Handle image file upload
|
51 |
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
|
52 |
|
53 |
+
if img_upload:
|
54 |
+
try:
|
55 |
+
img = Image.open(io.BytesIO(img_upload.read()))
|
56 |
+
img = img.convert('RGB')
|
57 |
+
img.save('tmp.jpg')
|
58 |
+
st.image(img)
|
59 |
+
predict()
|
60 |
+
os.remove('tmp.jpg')
|
61 |
+
except Exception as e:
|
62 |
+
st.error(f"An error occurred with the image upload: {e}")
|