Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -22,35 +22,32 @@ print("RESNET MODEL LOADED")
|
|
22 |
def load_image(img):
|
23 |
im = Image.open(img)
|
24 |
return im
|
25 |
-
activities = ["Generate ","About"]
|
26 |
-
choice = st.sidebar.selectbox("Select Activty",activities)
|
27 |
uploaded_photo = c2.file_uploader("Upload Image",type=['jpg','png','jpeg'], on_change=change_photo_state)
|
28 |
camera_photo = c2.camera_input("Take a photo", on_change=change_photo_state)
|
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 |
-
st.markdown("Demo applicaton of the following model [credit](https://huggingface.co/nlpconnect/vit-gpt2-image-captioning/)")
|
|
|
22 |
def load_image(img):
|
23 |
im = Image.open(img)
|
24 |
return im
|
|
|
|
|
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 |
+
st.subheader("Caption is below!")
|
28 |
+
if st.session_state["photo"]=="done":
|
29 |
+
if uploaded_photo:
|
30 |
+
our_image= load_image(uploaded_photo)
|
31 |
+
elif camera_photo:
|
32 |
+
our_image= load_image(camera_photo)
|
33 |
+
elif uploaded_photo==None and camera_photo==None:
|
34 |
+
our_image= load_image('image.jpg')
|
35 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
36 |
+
model.to(device)
|
37 |
+
max_length = 16
|
38 |
+
num_beams = 4
|
39 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
40 |
+
def predict_step(our_image):
|
41 |
+
if our_image.mode != "RGB":
|
42 |
+
our_image = our_image.convert(mode="RGB")
|
43 |
+
pixel_values = feature_extractor(images=our_image, return_tensors="pt").pixel_values
|
44 |
+
pixel_values = pixel_values.to(device)
|
45 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
46 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
47 |
+
preds = [pred.strip() for pred in preds]
|
48 |
+
return preds
|
49 |
+
st.success(predict_step(our_image))
|
50 |
+
if st.checkbox('About'):
|
51 |
+
st.subheader("About Image Captioning App")
|
52 |
+
st.markdown("Built with Streamlit by [Soumen Sarker](https://soumen-sarker-personal-site.streamlit.app/)")
|
53 |
+
st.markdown("Demo applicaton of the following model [credit](https://huggingface.co/nlpconnect/vit-gpt2-image-captioning/)")
|
|