davidmezzetti commited on
Commit
b462137
1 Parent(s): b8ee19c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -42
app.py CHANGED
@@ -15,65 +15,84 @@ from PIL import Image
15
  from txtai.embeddings import Embeddings
16
 
17
 
18
- def images(directory):
19
  """
20
- Generator that loops over each image in a directory.
21
-
22
- Args:
23
- directory: directory with images
24
  """
25
 
26
- for path in glob.glob(directory + "/*jpg") + glob.glob(directory + "/*png"):
27
- yield (path, Image.open(path), None)
 
28
 
 
 
 
29
 
30
- @st.cache(allow_output_mutation=True)
31
- def build(directory):
32
- """
33
- Builds an image embeddings index.
34
 
35
- Args:
36
- directory: directory with images
 
37
 
38
- Returns:
39
- Embeddings index
40
- """
41
 
42
- embeddings = Embeddings({"method": "sentence-transformers", "path": "clip-ViT-B-32"})
43
- embeddings.index(images(directory))
 
44
 
45
- # Update model to support multilingual queries
46
- embeddings.config["path"] = "sentence-transformers/clip-ViT-B-32-multilingual-v1"
47
- embeddings.model = embeddings.loadVectors()
48
 
49
- return embeddings
 
 
50
 
 
51
 
52
- def app(directory):
53
- """
54
- Streamlit application that runs searches against an image embeddings index.
55
 
56
- Args:
57
- directory: directory with images
58
- """
59
 
60
- # Build embeddings index
61
- embeddings = build(directory)
62
 
63
- st.title("Image search")
 
 
 
64
 
65
- st.markdown("This application shows how images and text can be embedded into the same space to support similarity search. ")
66
- st.markdown(
67
- "[sentence-transformers](https://github.com/UKPLab/sentence-transformers/tree/master/examples/applications/image-search) "
68
- + "recently added support for the [OpenAI CLIP model](https://github.com/openai/CLIP). This model embeds text and images into "
69
- + "the same space, enabling image similarity search. txtai can directly utilize these models."
70
- )
71
 
72
- query = st.text_input("Search query:")
73
- if query:
74
- index, _ = embeddings.search(query, 1)[0]
75
- st.image(Image.open(index))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
 
77
 
78
  if __name__ == "__main__":
79
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
@@ -86,4 +105,6 @@ if __name__ == "__main__":
86
  f = tarfile.open(fileobj=response.raw, mode="r|gz")
87
  f.extractall(path="/tmp")
88
 
89
- app(files)
 
 
 
15
  from txtai.embeddings import Embeddings
16
 
17
 
18
+ class Application:
19
  """
20
+ Main application
 
 
 
21
  """
22
 
23
+ def __init__(self, directory):
24
+ """
25
+ Creates a new application.
26
 
27
+ Args:
28
+ directory: directory of images
29
+ """
30
 
31
+ self.embeddings = self.build(directory)
 
 
 
32
 
33
+ def build(self, directory):
34
+ """
35
+ Builds an image embeddings index.
36
 
37
+ Args:
38
+ directory: directory with images
 
39
 
40
+ Returns:
41
+ Embeddings index
42
+ """
43
 
44
+ embeddings = Embeddings({"method": "sentence-transformers", "path": "clip-ViT-B-32"})
45
+ embeddings.index(self.images(directory))
 
46
 
47
+ # Update model to support multilingual queries
48
+ embeddings.config["path"] = "sentence-transformers/clip-ViT-B-32-multilingual-v1"
49
+ embeddings.model = embeddings.loadVectors()
50
 
51
+ return embeddings
52
 
53
+ def images(self, directory):
54
+ """
55
+ Generator that loops over each image in a directory.
56
 
57
+ Args:
58
+ directory: directory with images
59
+ """
60
 
61
+ for path in glob.glob(directory + "/*jpg") + glob.glob(directory + "/*png"):
62
+ yield (path, Image.open(path), None)
63
 
64
+ def run(self):
65
+ """
66
+ Runs a Streamlit application.
67
+ """
68
 
69
+ st.title("Image search")
 
 
 
 
 
70
 
71
+ st.markdown("This application shows how images and text can be embedded into the same space to support similarity search. ")
72
+ st.markdown(
73
+ "[sentence-transformers](https://github.com/UKPLab/sentence-transformers/tree/master/examples/applications/image-search) "
74
+ + "recently added support for the [OpenAI CLIP model](https://github.com/openai/CLIP). This model embeds text and images into "
75
+ + "the same space, enabling image similarity search. txtai can directly utilize these models."
76
+ )
77
+
78
+ query = st.text_input("Search query:")
79
+ if query:
80
+ index, _ = self.embeddings.search(query, 1)[0]
81
+ st.image(Image.open(index))
82
+
83
+ @st.cache(allow_output_mutation=True)
84
+ def create(directory):
85
+ """
86
+ Creates and caches a Streamlit application.
87
+
88
+ Args:
89
+ directory: directory of images to index
90
+
91
+ Returns:
92
+ Application
93
+ """
94
 
95
+ return Application(directory)
96
 
97
  if __name__ == "__main__":
98
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
 
105
  f = tarfile.open(fileobj=response.raw, mode="r|gz")
106
  f.extractall(path="/tmp")
107
 
108
+ # Create and run application
109
+ app = create(files)
110
+ app.run()