Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,26 +4,10 @@ from PIL import Image
|
|
4 |
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
5 |
#pickle.load(open('energy_model.pkl', 'rb'))
|
6 |
#vocab = np.load('w2i.p', allow_pickle=True)
|
7 |
-
print("="*150)
|
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 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "CPU")
|
14 |
-
model.to(device)
|
15 |
-
max_length = 16
|
16 |
-
num_beams = 4
|
17 |
-
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
18 |
-
def predict_step(our_image):
|
19 |
-
if our_image.mode != "RGB":
|
20 |
-
our_image = our_image.convert(mode="RGB")
|
21 |
-
pixel_values = feature_extractor(images=our_image, return_tensors="pt").pixel_values
|
22 |
-
pixel_values = pixel_values.to(device)
|
23 |
-
output_ids = model.generate(pixel_values, **gen_kwargs)
|
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"
|
@@ -38,7 +22,20 @@ uploaded_photo = c2.file_uploader("Upload Image",type=['jpg','png','jpeg'], on_c
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if st.session_state["photo"]=="done":
|
43 |
if uploaded_photo:
|
44 |
our_image= load_image(uploaded_photo)
|
@@ -46,7 +43,7 @@ if st.checkbox("Generate_Caption"):
|
|
46 |
our_image= load_image(camera_photo)
|
47 |
elif uploaded_photo==None and camera_photo==None:
|
48 |
our_image= load_image('image.jpg')
|
49 |
-
|
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/)")
|
|
|
4 |
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
5 |
#pickle.load(open('energy_model.pkl', 'rb'))
|
6 |
#vocab = np.load('w2i.p', allow_pickle=True)
|
|
|
|
|
7 |
st.title("img_captioning_app")
|
8 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
9 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
10 |
tokenizer = AutoTokenizer.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"
|
|
|
22 |
camera_photo = c2.camera_input("Take a photo", on_change=change_photo_state)
|
23 |
#st.subheader("Detection")
|
24 |
if st.checkbox("Generate_Caption"):
|
25 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "CPU")
|
26 |
+
model.to(device)
|
27 |
+
max_length = 16
|
28 |
+
num_beams = 4
|
29 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
30 |
+
def predict_step(our_image):
|
31 |
+
if our_image.mode != "RGB":
|
32 |
+
our_image = our_image.convert(mode="RGB")
|
33 |
+
pixel_values = feature_extractor(images=our_image, return_tensors="pt").pixel_values
|
34 |
+
pixel_values = pixel_values.to(device)
|
35 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
36 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
37 |
+
preds = [pred.strip() for pred in preds]
|
38 |
+
return preds
|
39 |
if st.session_state["photo"]=="done":
|
40 |
if uploaded_photo:
|
41 |
our_image= load_image(uploaded_photo)
|
|
|
43 |
our_image= load_image(camera_photo)
|
44 |
elif uploaded_photo==None and camera_photo==None:
|
45 |
our_image= load_image('image.jpg')
|
46 |
+
st.success(predict_step(our_image))
|
47 |
elif st.checkbox("About"):
|
48 |
st.subheader("About Image Captioning App")
|
49 |
st.markdown("Built with Streamlit by [Soumen Sarker](https://soumen-sarker-personal-website.streamlit.app/)")
|