Spaces:
Build error
Build error
File size: 1,766 Bytes
e7681d0 73f387e e7681d0 73f387e 9aec085 215e3fc 9aec085 73f387e b5e8600 215e3fc b5e8600 215e3fc b5e8600 73f387e b5e8600 73f387e b5e8600 73f387e b5e8600 73f387e b5e8600 |
1 2 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 58 59 60 |
import io
import os
import streamlit as st
import requests
from PIL import Image
from model import get_caption_model, generate_caption
# Initialize session state for the model
if 'caption_model' not in st.session_state:
st.session_state.caption_model = get_caption_model()
def predict():
captions = []
try:
pred_caption = generate_caption('tmp.jpg', st.session_state.caption_model)
st.markdown('#### Predicted Captions:')
captions.append(pred_caption)
for _ in range(4):
pred_caption = generate_caption('tmp.jpg', st.session_state.caption_model, add_noise=True)
if pred_caption not in captions:
captions.append(pred_caption)
for c in captions:
st.write(c)
except Exception as e:
st.error(f"An error occurred during prediction: {e}")
st.title('Image Captioner')
# Handle image URL input
img_url = st.text_input(label='Enter Image URL')
if img_url:
try:
img = Image.open(requests.get(img_url, stream=True).raw)
img = img.convert('RGB')
st.image(img)
img.save('tmp.jpg')
predict()
os.remove('tmp.jpg')
except Exception as e:
st.error(f"An error occurred with the image URL: {e}")
st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
# Handle image file upload
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
if img_upload:
try:
img = Image.open(io.BytesIO(img_upload.read()))
img = img.convert('RGB')
img.save('tmp.jpg')
st.image(img)
predict()
os.remove('tmp.jpg')
except Exception as e:
st.error(f"An error occurred with the image upload: {e}")
|