shivangibithel commited on
Commit
b54da20
1 Parent(s): 1f8b2c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -9,8 +9,12 @@ from PIL import Image
9
  from io import BytesIO
10
  from sentence_transformers import SentenceTransformer
11
  import json
12
- from huggingface_hub import hf_hub_download
13
- hf_hub_download(repo_id="shivangibithel/Flickr8k", filename="Images.zip")
 
 
 
 
14
 
15
  # Load the pre-trained sentence encoder
16
  model_name = "sentence-transformers/all-distilroberta-v1"
@@ -23,6 +27,18 @@ model = SentenceTransformer(model_name)
23
  # wget.download(index_url, index_name)
24
  # index = faiss.read_index(faiss_flickr8k.index)
25
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  vectors = np.load("./sbert_text_features.npy")
27
  vector_dimension = vectors.shape[1]
28
  index = faiss.IndexFlatL2(vector_dimension)
@@ -53,20 +69,23 @@ def search(query, k=5):
53
  for i in I[0]:
54
  text_id = i
55
  image_id = str(image_list[i])
56
- image_url = "./Images/" + image_id
57
- image_urls.append(image_url)
 
 
 
58
 
59
- return image_urls
60
 
61
  st.title("Image Search App")
62
 
63
  query = st.text_input("Enter your search query here:")
64
  if st.button("Search"):
65
  if query:
66
- image_urls = search(query)
67
 
68
  # Display the images
69
- st.image(image_urls, width=200)
70
 
71
  if __name__ == '__main__':
72
  st.cache(allow_output_mutation=True)
 
9
  from io import BytesIO
10
  from sentence_transformers import SentenceTransformer
11
  import json
12
+ from zipfile import ZipFile
13
+ import zipfile
14
+ from io import BytesIO
15
+ from PIL import Image
16
+ # from huggingface_hub import hf_hub_download
17
+ # hf_hub_download(repo_id="shivangibithel/Flickr8k", filename="Images.zip")
18
 
19
  # Load the pre-trained sentence encoder
20
  model_name = "sentence-transformers/all-distilroberta-v1"
 
27
  # wget.download(index_url, index_name)
28
  # index = faiss.read_index(faiss_flickr8k.index)
29
 
30
+ # Define the path to the zip folder containing the images
31
+ zip_path = "Images.zip"
32
+
33
+ # Open the zip folder
34
+ zip_file = zipfile.ZipFile(zip_path)
35
+
36
+ # Iterate over the images in the zip folder and display them using Streamlit
37
+ for image_name in zip_file.namelist():
38
+ image_data = zip_file.read(image_name)
39
+ image = Image.open(io.BytesIO(image_data))
40
+ st.image(image, caption=image_name)
41
+
42
  vectors = np.load("./sbert_text_features.npy")
43
  vector_dimension = vectors.shape[1]
44
  index = faiss.IndexFlatL2(vector_dimension)
 
69
  for i in I[0]:
70
  text_id = i
71
  image_id = str(image_list[i])
72
+ image_data = zip_file.read(image_id)
73
+ image = Image.open(io.BytesIO(image_data))
74
+ st.image(image, caption=image_name, width=200)
75
+ # image_url = "./Images/" + image_id
76
+ # image_urls.append(image_url)
77
 
78
+ # return image_urls
79
 
80
  st.title("Image Search App")
81
 
82
  query = st.text_input("Enter your search query here:")
83
  if st.button("Search"):
84
  if query:
85
+ search(query)
86
 
87
  # Display the images
88
+ # st.image(image_urls, width=200)
89
 
90
  if __name__ == '__main__':
91
  st.cache(allow_output_mutation=True)