Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,21 +10,7 @@ st.title("img_captioning_app")
|
|
| 10 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 11 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 13 |
-
|
| 14 |
-
if "photo" not in st.session_state:
|
| 15 |
-
st.session_state["photo"]="not done"
|
| 16 |
-
c2, c3 = st.columns([2,1])
|
| 17 |
-
def change_photo_state():
|
| 18 |
-
st.session_state["photo"]="done"
|
| 19 |
-
@st.cache
|
| 20 |
-
def load_image(img):
|
| 21 |
-
im = Image.open(img)
|
| 22 |
-
return im
|
| 23 |
-
activities = ["About"]
|
| 24 |
-
choice = st.sidebar.selectbox("Select Activty",activities)
|
| 25 |
-
uploaded_photo = c2.file_uploader("Upload Image",type=['jpg','png','jpeg'], on_change=change_photo_state)
|
| 26 |
-
camera_photo = c2.camera_input("Take a photo", on_change=change_photo_state)
|
| 27 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 28 |
model.to(device)
|
| 29 |
max_length = 16
|
| 30 |
num_beams = 4
|
|
@@ -38,8 +24,21 @@ def predict_step(our_image):
|
|
| 38 |
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 39 |
preds = [pred.strip() for pred in preds]
|
| 40 |
return preds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
#st.subheader("Detection")
|
| 42 |
if st.checkbox("Generate_Caption"):
|
|
|
|
| 43 |
if st.session_state["photo"]=="done":
|
| 44 |
if uploaded_photo:
|
| 45 |
our_image= load_image(uploaded_photo)
|
|
@@ -47,8 +46,8 @@ if st.checkbox("Generate_Caption"):
|
|
| 47 |
our_image= load_image(camera_photo)
|
| 48 |
elif uploaded_photo==None and camera_photo==None:
|
| 49 |
our_image= load_image('image.jpg')
|
| 50 |
-
|
| 51 |
-
elif
|
| 52 |
st.subheader("About Image Captioning App")
|
| 53 |
st.markdown("Built with Streamlit by [Soumen Sarker](https://soumen-sarker-personal-website.streamlit.app/)")
|
| 54 |
st.markdown("Demo applicaton of the following model [credit](https://huggingface.co/nlpconnect/vit-gpt2-image-captioning/)")
|
|
|
|
| 10 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 11 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "CPU")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
model.to(device)
|
| 15 |
max_length = 16
|
| 16 |
num_beams = 4
|
|
|
|
| 24 |
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 25 |
preds = [pred.strip() for pred in preds]
|
| 26 |
return preds
|
| 27 |
+
#st.text("Build with Streamlit and OpenCV")
|
| 28 |
+
if "photo" not in st.session_state:
|
| 29 |
+
st.session_state["photo"]="not done"
|
| 30 |
+
c2, c3 = st.columns([2,1])
|
| 31 |
+
def change_photo_state():
|
| 32 |
+
st.session_state["photo"]="done"
|
| 33 |
+
@st.cache
|
| 34 |
+
def load_image(img):
|
| 35 |
+
im = Image.open(img)
|
| 36 |
+
return im
|
| 37 |
+
uploaded_photo = c2.file_uploader("Upload Image",type=['jpg','png','jpeg'], on_change=change_photo_state)
|
| 38 |
+
camera_photo = c2.camera_input("Take a photo", on_change=change_photo_state)
|
| 39 |
#st.subheader("Detection")
|
| 40 |
if st.checkbox("Generate_Caption"):
|
| 41 |
+
our_image=None
|
| 42 |
if st.session_state["photo"]=="done":
|
| 43 |
if uploaded_photo:
|
| 44 |
our_image= load_image(uploaded_photo)
|
|
|
|
| 46 |
our_image= load_image(camera_photo)
|
| 47 |
elif uploaded_photo==None and camera_photo==None:
|
| 48 |
our_image= load_image('image.jpg')
|
| 49 |
+
st.success(predict_step(our_image))
|
| 50 |
+
elif st.checkbox("About"):
|
| 51 |
st.subheader("About Image Captioning App")
|
| 52 |
st.markdown("Built with Streamlit by [Soumen Sarker](https://soumen-sarker-personal-website.streamlit.app/)")
|
| 53 |
st.markdown("Demo applicaton of the following model [credit](https://huggingface.co/nlpconnect/vit-gpt2-image-captioning/)")
|