Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -80,7 +80,7 @@ class ImageSearchEngine:
|
|
80 |
embedding = F.normalize(embedding, p=2, dim=1)
|
81 |
return embedding.cpu()
|
82 |
|
83 |
-
def search(self, query_text: str, k: int =
|
84 |
"""Search for images matching the query text."""
|
85 |
if len(query_text) < 3: # avoid searching for very short queries
|
86 |
return []
|
@@ -125,20 +125,14 @@ class GalleryUI:
|
|
125 |
label="Search Query",
|
126 |
elem_classes="search-input",
|
127 |
autofocus=True,
|
128 |
-
container=False
|
|
|
129 |
)
|
130 |
-
|
131 |
-
with gr.Row(visible=False): # this is hidden for now, but you can show it if you want
|
132 |
-
self.number_of_results = gr.Dropdown(
|
133 |
-
choices=[4,8,12,16,24,30],
|
134 |
-
value=30,
|
135 |
-
label="Results per page",
|
136 |
-
elem_classes="dropdown"
|
137 |
-
)
|
138 |
|
139 |
self.gallery = gr.Gallery(
|
140 |
label="Search Results",
|
141 |
-
columns=
|
|
|
142 |
object_fit="cover",
|
143 |
elem_classes="gallery",
|
144 |
container=False,
|
@@ -154,25 +148,22 @@ class GalleryUI:
|
|
154 |
|
155 |
def _setup_callbacks(self, demo: gr.Blocks) -> None:
|
156 |
"""Setup the interface callbacks."""
|
157 |
-
self.query_text.
|
158 |
self.search_engine.search,
|
159 |
-
inputs=[self.query_text
|
160 |
outputs=self.gallery,
|
161 |
show_progress='hidden',
|
162 |
)
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
outputs=self.gallery
|
168 |
-
)
|
169 |
-
|
170 |
-
if __name__ == "__main__":
|
171 |
-
search_engine = ImageSearchEngine(
|
172 |
model_name = "sentence-transformers/all-MiniLM-L6-v2",
|
173 |
output_dim = 64,
|
174 |
gallery_folder = "photos",
|
175 |
)
|
176 |
-
|
177 |
-
|
|
|
|
|
178 |
demo.launch()
|
|
|
80 |
embedding = F.normalize(embedding, p=2, dim=1)
|
81 |
return embedding.cpu()
|
82 |
|
83 |
+
def search(self, query_text: str, k: int = 20) -> List[Tuple[str, Optional[str]]]:
|
84 |
"""Search for images matching the query text."""
|
85 |
if len(query_text) < 3: # avoid searching for very short queries
|
86 |
return []
|
|
|
125 |
label="Search Query",
|
126 |
elem_classes="search-input",
|
127 |
autofocus=True,
|
128 |
+
container=False,
|
129 |
+
interactive=True
|
130 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
self.gallery = gr.Gallery(
|
133 |
label="Search Results",
|
134 |
+
columns=[4],
|
135 |
+
height=600,
|
136 |
object_fit="cover",
|
137 |
elem_classes="gallery",
|
138 |
container=False,
|
|
|
148 |
|
149 |
def _setup_callbacks(self, demo: gr.Blocks) -> None:
|
150 |
"""Setup the interface callbacks."""
|
151 |
+
self.query_text.submit(
|
152 |
self.search_engine.search,
|
153 |
+
inputs=[self.query_text],#, self.number_of_results],
|
154 |
outputs=self.gallery,
|
155 |
show_progress='hidden',
|
156 |
)
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
search_engine = ImageSearchEngine(
|
|
|
|
|
|
|
|
|
|
|
161 |
model_name = "sentence-transformers/all-MiniLM-L6-v2",
|
162 |
output_dim = 64,
|
163 |
gallery_folder = "photos",
|
164 |
)
|
165 |
+
ui = GalleryUI(search_engine)
|
166 |
+
demo = ui.create_interface()
|
167 |
+
|
168 |
+
if __name__ == "__main__":
|
169 |
demo.launch()
|