NourFakih commited on
Commit
ff8c120
·
verified ·
1 Parent(s): 62648bb

Update app.py

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