Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
| 5 |
+
import torch
|
| 6 |
+
from nltk.corpus import wordnet
|
| 7 |
+
import nltk
|
| 8 |
+
|
| 9 |
+
nltk.download('wordnet')
|
| 10 |
+
|
| 11 |
+
# Load the pre-trained model for image captioning
|
| 12 |
+
model_name = "nlpconnect/vit-gpt2-image-captioning"
|
| 13 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_name)
|
| 14 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
|
| 17 |
+
def generate_caption(image):
|
| 18 |
+
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
| 19 |
+
output_ids = model.generate(pixel_values)
|
| 20 |
+
caption = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 21 |
+
return caption
|
| 22 |
+
|
| 23 |
+
def get_synonyms(word):
|
| 24 |
+
synonyms = set()
|
| 25 |
+
for syn in wordnet.synsets(word):
|
| 26 |
+
for lemma in syn.lemmas():
|
| 27 |
+
synonyms.add(lemma.name())
|
| 28 |
+
return synonyms
|
| 29 |
+
|
| 30 |
+
def search_captions(query, captions):
|
| 31 |
+
query_words = query.split()
|
| 32 |
+
query_synonyms = set(query_words)
|
| 33 |
+
for word in query_words:
|
| 34 |
+
query_synonyms.update(get_synonyms(word))
|
| 35 |
+
|
| 36 |
+
results = []
|
| 37 |
+
for path, caption in captions.items():
|
| 38 |
+
if any(word in caption.split() for word in query_synonyms):
|
| 39 |
+
results.append((path, caption))
|
| 40 |
+
|
| 41 |
+
return results
|
| 42 |
+
|
| 43 |
+
def main():
|
| 44 |
+
st.title("Image Gallery with Captioning and Search")
|
| 45 |
+
|
| 46 |
+
folder_path = st.text_input("Enter the folder path containing images:")
|
| 47 |
+
|
| 48 |
+
if folder_path and os.path.isdir(folder_path):
|
| 49 |
+
image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(('png', 'jpg', 'jpeg'))]
|
| 50 |
+
captions = {}
|
| 51 |
+
|
| 52 |
+
for image_file in image_files:
|
| 53 |
+
image_path = os.path.join(folder_path, image_file)
|
| 54 |
+
image = Image.open(image_path)
|
| 55 |
+
caption = generate_caption(image)
|
| 56 |
+
captions[image_path] = caption
|
| 57 |
+
st.image(image, caption=caption)
|
| 58 |
+
|
| 59 |
+
query = st.text_input("Search images by caption:")
|
| 60 |
+
if query:
|
| 61 |
+
results = search_captions(query, captions)
|
| 62 |
+
for image_path, caption in results:
|
| 63 |
+
st.image(image_path, caption=caption)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|