Spaces:
Sleeping
Sleeping
Commit
Β·
37bed6c
1
Parent(s):
1e1a418
Update pages/Captionize.py
Browse files- pages/Captionize.py +34 -22
pages/Captionize.py
CHANGED
@@ -1,8 +1,30 @@
|
|
1 |
import torch
|
2 |
import re
|
3 |
-
import
|
4 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
device='cpu'
|
7 |
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
8 |
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
@@ -18,24 +40,14 @@ def predict(image,max_length=64, num_beams=4):
|
|
18 |
clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
|
19 |
caption_ids = model.generate(image, max_length = max_length)[0]
|
20 |
caption_text = clean_text(tokenizer.decode(caption_ids))
|
21 |
-
return caption_text
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
fn=predict,
|
34 |
-
description=description,
|
35 |
-
inputs = input,
|
36 |
-
theme="grass",
|
37 |
-
outputs=output,
|
38 |
-
examples = examples,
|
39 |
-
title=title,
|
40 |
-
)
|
41 |
-
interface.launch(debug=True)
|
|
|
1 |
import torch
|
2 |
import re
|
3 |
+
import streamlit as st
|
4 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
5 |
|
6 |
+
|
7 |
+
st.set_page_config(page_title="Captionize")
|
8 |
+
|
9 |
+
st.title("π€ Captionize")
|
10 |
+
st.subheader("Generate Captions for your Image...")
|
11 |
+
|
12 |
+
st.sidebar.image('./csv_analysis.png',width=300, use_column_width=True)
|
13 |
+
|
14 |
+
# Applying Styling
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
div.stButton > button:first-child {
|
18 |
+
background-color: #0099ff;
|
19 |
+
color:#ffffff;
|
20 |
+
}
|
21 |
+
div.stButton > button:hover {
|
22 |
+
background-color: #00ff00;
|
23 |
+
color:#FFFFFF;
|
24 |
+
}
|
25 |
+
</style>""", unsafe_allow_html=True)
|
26 |
+
|
27 |
+
|
28 |
device='cpu'
|
29 |
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
30 |
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
|
|
40 |
clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
|
41 |
caption_ids = model.generate(image, max_length = max_length)[0]
|
42 |
caption_text = clean_text(tokenizer.decode(caption_ids))
|
43 |
+
return caption_text
|
44 |
+
|
45 |
+
pic = st.file_uploader(label="Please upload any Image here π",type=['png', 'jpeg', 'jpg'], help="Only 'png', 'jpeg' or 'jpg' formats allowed")
|
46 |
+
|
47 |
+
|
48 |
+
button = st.button("Generate Caption")
|
49 |
+
|
50 |
+
if button:
|
51 |
+
# Get Response
|
52 |
+
caption = predict(pic)
|
53 |
+
st.write(caption)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|