Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,16 +8,14 @@ print("="*150)
|
|
8 |
print("MODEL LOADED")
|
9 |
st.title("img_captioning_app")
|
10 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
|
|
|
|
11 |
#st.text("Build with Streamlit and OpenCV")
|
12 |
if "photo" not in st.session_state:
|
13 |
st.session_state["photo"]="not done"
|
14 |
-
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
15 |
c2, c3 = st.columns([2,1])
|
16 |
def change_photo_state():
|
17 |
st.session_state["photo"]="done"
|
18 |
-
print("="*150)
|
19 |
-
print("RESNET MODEL LOADED")
|
20 |
-
|
21 |
@st.cache
|
22 |
def load_image(img):
|
23 |
im = Image.open(img)
|
@@ -25,7 +23,6 @@ def load_image(img):
|
|
25 |
activities = ["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 |
-
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
29 |
camera_photo = c2.camera_input("Take a photo", on_change=change_photo_state)
|
30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
31 |
model.to(device)
|
@@ -41,16 +38,17 @@ def predict_step(our_image):
|
|
41 |
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
42 |
preds = [pred.strip() for pred in preds]
|
43 |
return preds
|
44 |
-
#st.subheader("Detection")
|
45 |
-
if st.
|
46 |
-
if
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
st.subheader("About Image Captioning App")
|
55 |
st.markdown("Built with Streamlit by [Soumen Sarker](https://soumen-sarker-personal-website.streamlit.app/)")
|
56 |
st.markdown("Demo applicaton of the following model [credit](https://huggingface.co/nlpconnect/vit-gpt2-image-captioning/)")
|
|
|
8 |
print("MODEL LOADED")
|
9 |
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 |
#st.text("Build with Streamlit and OpenCV")
|
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)
|
|
|
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)
|
|
|
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)
|
46 |
+
elif camera_photo:
|
47 |
+
our_image= load_image(camera_photo)
|
48 |
+
elif uploaded_photo==None and camera_photo==None:
|
49 |
+
our_image= load_image('image.jpg')
|
50 |
+
st.success(predict_step(our_image))
|
51 |
+
elif choice == 'About':
|
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/)")
|