Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,162 +1,171 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
import zipfile
|
4 |
-
import tempfile
|
5 |
-
import base64
|
6 |
-
from PIL import Image
|
7 |
-
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
8 |
-
import pandas as pd
|
9 |
-
from nltk.corpus import wordnet
|
10 |
-
import spacy
|
11 |
-
import io
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
return
|
48 |
-
|
49 |
-
def
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
if
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
st.
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
st.
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
import tempfile
|
5 |
+
import base64
|
6 |
+
from PIL import Image
|
7 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
8 |
+
import pandas as pd
|
9 |
+
from nltk.corpus import wordnet
|
10 |
+
import spacy
|
11 |
+
import io
|
12 |
+
from spacy.cli import download
|
13 |
+
|
14 |
+
# Download the model if it is not already present
|
15 |
+
download("en_core_web_sm")
|
16 |
+
|
17 |
+
# Load the model
|
18 |
+
nlp = spacy.load("en_core_web_sm")
|
19 |
+
|
20 |
+
# Your existing code here
|
21 |
+
|
22 |
+
# Download NLTK WordNet data
|
23 |
+
import nltk
|
24 |
+
nltk.download('wordnet')
|
25 |
+
nltk.download('omw-1.4')
|
26 |
+
|
27 |
+
# Load spaCy model
|
28 |
+
nlp = spacy.load("en_core_web_sm")
|
29 |
+
|
30 |
+
# Load the pre-trained model for image captioning
|
31 |
+
model_name = "NourFakih/Vit-GPT2-COCO2017Flickr-85k-09"
|
32 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
33 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
34 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
35 |
+
|
36 |
+
def generate_caption(image):
|
37 |
+
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
38 |
+
output_ids = model.generate(pixel_values)
|
39 |
+
caption = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
40 |
+
return caption
|
41 |
+
|
42 |
+
def get_synonyms(word):
|
43 |
+
synonyms = set()
|
44 |
+
for syn in wordnet.synsets(word):
|
45 |
+
for lemma in syn.lemmas():
|
46 |
+
synonyms.add(lemma.name())
|
47 |
+
return synonyms
|
48 |
+
|
49 |
+
def preprocess_query(query):
|
50 |
+
doc = nlp(query)
|
51 |
+
tokens = set()
|
52 |
+
for token in doc:
|
53 |
+
tokens.add(token.text)
|
54 |
+
tokens.add(token.lemma_)
|
55 |
+
tokens.update(get_synonyms(token.text))
|
56 |
+
return tokens
|
57 |
+
|
58 |
+
def search_captions(query, captions):
|
59 |
+
query_tokens = preprocess_query(query)
|
60 |
+
|
61 |
+
results = []
|
62 |
+
for path, caption in captions.items():
|
63 |
+
caption_tokens = preprocess_query(caption)
|
64 |
+
if query_tokens & caption_tokens:
|
65 |
+
results.append((path, caption))
|
66 |
+
|
67 |
+
return results
|
68 |
+
|
69 |
+
st.title("Image Gallery with Captioning and Search")
|
70 |
+
|
71 |
+
# Sidebar for search functionality
|
72 |
+
with st.sidebar:
|
73 |
+
query = st.text_input("Search images by caption:")
|
74 |
+
|
75 |
+
# Right side for folder path input and displaying images
|
76 |
+
option = st.selectbox("Select input method:", ["Folder Path", "Upload Images"])
|
77 |
+
|
78 |
+
if option == "Folder Path":
|
79 |
+
folder_path = st.text_input("Enter the folder path containing images:")
|
80 |
+
image_files = []
|
81 |
+
if folder_path and os.path.isdir(folder_path):
|
82 |
+
image_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.lower().endswith(('png', 'jpg', 'jpeg'))]
|
83 |
+
else:
|
84 |
+
uploaded_files = st.file_uploader("Upload images or a zip file containing images:", type=['png', 'jpg', 'jpeg', 'zip'], accept_multiple_files=True)
|
85 |
+
image_files = []
|
86 |
+
if uploaded_files:
|
87 |
+
for uploaded_file in uploaded_files:
|
88 |
+
if uploaded_file.name.endswith('.zip'):
|
89 |
+
with zipfile.ZipFile(uploaded_file, 'r') as zip_ref:
|
90 |
+
zip_ref.extractall("uploaded_images")
|
91 |
+
for file in zip_ref.namelist():
|
92 |
+
if file.lower().endswith(('png', 'jpg', 'jpeg')):
|
93 |
+
image_files.append(os.path.join("uploaded_images", file))
|
94 |
+
else:
|
95 |
+
if uploaded_file.name.lower().endswith(('png', 'jpg', 'jpeg')):
|
96 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1])
|
97 |
+
temp_file.write(uploaded_file.read())
|
98 |
+
image_files.append(temp_file.name)
|
99 |
+
|
100 |
+
captions = {}
|
101 |
+
if st.button("Generate Captions", key='generate_captions'):
|
102 |
+
for image_file in image_files:
|
103 |
+
try:
|
104 |
+
image = Image.open(image_file)
|
105 |
+
caption = generate_caption(image)
|
106 |
+
if option == "Folder Path":
|
107 |
+
captions[os.path.join(folder_path, os.path.basename(image_file))] = caption
|
108 |
+
else:
|
109 |
+
if image_file.startswith("uploaded_images"):
|
110 |
+
captions[image_file.replace("uploaded_images/", "")] = caption
|
111 |
+
else:
|
112 |
+
captions[os.path.basename(image_file)] = caption
|
113 |
+
except Exception as e:
|
114 |
+
st.error(f"Error processing {image_file}: {e}")
|
115 |
+
|
116 |
+
# Display images in a grid
|
117 |
+
st.subheader("Images and Captions:")
|
118 |
+
cols = st.columns(4)
|
119 |
+
idx = 0
|
120 |
+
for image_path, caption in captions.items():
|
121 |
+
col = cols[idx % 4]
|
122 |
+
with col:
|
123 |
+
try:
|
124 |
+
with open(image_path, "rb") as img_file:
|
125 |
+
img_bytes = img_file.read()
|
126 |
+
encoded_image = base64.b64encode(img_bytes).decode()
|
127 |
+
st.markdown(
|
128 |
+
f"""
|
129 |
+
<div style='text-align: center;'>
|
130 |
+
<img src='data:image/jpeg;base64,{encoded_image}' width='100%'>
|
131 |
+
<p>{caption}</p>
|
132 |
+
<p style='font-size: small; font-style: italic;'>{image_path}</p>
|
133 |
+
</div>
|
134 |
+
""", unsafe_allow_html=True)
|
135 |
+
except Exception as e:
|
136 |
+
st.error(f"Error displaying {image_path}: {e}")
|
137 |
+
idx += 1
|
138 |
+
|
139 |
+
if query:
|
140 |
+
results = search_captions(query, captions)
|
141 |
+
st.write("Search Results:")
|
142 |
+
cols = st.columns(4)
|
143 |
+
idx = 0
|
144 |
+
for image_path, caption in results:
|
145 |
+
col = cols[idx % 4]
|
146 |
+
with col:
|
147 |
+
try:
|
148 |
+
with open(image_path, "rb") as img_file:
|
149 |
+
img_bytes = img_file.read()
|
150 |
+
encoded_image = base64.b64encode(img_bytes).decode()
|
151 |
+
st.markdown(
|
152 |
+
f"""
|
153 |
+
<div style='text-align: center;'>
|
154 |
+
<img src='data:image/jpeg;base64,{encoded_image}' width='100%'>
|
155 |
+
<p>{caption}</p>
|
156 |
+
<p style='font-size: small; font-style: italic;'>{image_path}</p>
|
157 |
+
</div>
|
158 |
+
""", unsafe_allow_html=True)
|
159 |
+
except Exception as e:
|
160 |
+
st.error(f"Error displaying search result {image_path}: {e}")
|
161 |
+
idx += 1
|
162 |
+
|
163 |
+
# Save captions to Excel and provide a download button
|
164 |
+
df = pd.DataFrame(list(captions.items()), columns=['Image', 'Caption'])
|
165 |
+
excel_file = io.BytesIO()
|
166 |
+
df.to_excel(excel_file, index=False)
|
167 |
+
excel_file.seek(0)
|
168 |
+
st.download_button(label="Download captions as Excel",
|
169 |
+
data=excel_file,
|
170 |
+
file_name="captions.xlsx",
|
171 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|