File size: 1,690 Bytes
eda2d42 f6cca0e eda2d42 f6cca0e eda2d42 c16251c f3c2cd6 f6cca0e 9a957b6 b613b7a f6cca0e b613b7a f8ab01b eda2d42 f6cca0e c476f0a eda2d42 f6cca0e | 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 61 62 63 64 65 66 | import io
import os
import streamlit as st
import requests
from PIL import Image
from model import get_caption_model, generate_caption
import gradio as gr
@st.cache(allow_output_mutation=True)
def get_model():
return get_caption_model()
caption_model = get_model()
def predict():
captions = []
pred_caption = generate_caption('tmp.jpg', caption_model)
captions.append(pred_caption)
for _ in range(4):
pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
if pred_caption not in captions:
captions.append(pred_caption)
#finalc = ' '.join([str(elem) for elem in captions])
return captions;
def launch(inputs):
img = Image.open(requests.get(inputs, stream=True).raw)
img = img.convert('RGB')
st.image(img)
img.save('tmp.jpg')
o=predict()
str1=""
for ele in o:
str1 += "\n"+ele
os.remove('tmp.jpg')
return str1
iface = gr.Interface(launch, inputs="text", outputs="text")
iface.launch(debug=True)
'''st.title('Image Captioner')
img_url = st.text_input(label='Enter Image URL')
if (img_url != "") and (img_url != None):
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')
st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
if img_upload != None:
img = img_upload.read()
img = Image.open(io.BytesIO(img))
img = img.convert('RGB')
img.save('tmp.jpg')
st.image(img)
predict()
os.remove('tmp.jpg')''' |