Spaces:
Running
on
T4
Running
on
T4
Upload main.py with huggingface_hub
Browse files
main.py
CHANGED
@@ -131,7 +131,7 @@ def get():
|
|
131 |
|
132 |
|
133 |
@rt("/search")
|
134 |
-
def get(request):
|
135 |
# Extract the 'query' and 'ranking' parameters from the URL
|
136 |
query_value = request.query_params.get("query", "").strip()
|
137 |
ranking_value = request.query_params.get("ranking", "nn+colpali")
|
@@ -156,13 +156,9 @@ def get(request):
|
|
156 |
)
|
157 |
)
|
158 |
# Generate a unique query_id based on the query and ranking value
|
159 |
-
query_id = generate_query_id(query_value + ranking_value)
|
160 |
-
|
161 |
-
|
162 |
-
# print(f"Results for query_id {query_id} already in cache")
|
163 |
-
# result = result_cache.get(query_id)
|
164 |
-
# search_results = get_results_children(result)
|
165 |
-
# return Layout(Search(request, search_results))
|
166 |
# Show the loading message if a query is provided
|
167 |
return Layout(
|
168 |
Main(Search(request), data_overlayscrollbars_initialize=True, cls="border-t"),
|
@@ -174,7 +170,7 @@ def get(request):
|
|
174 |
|
175 |
|
176 |
@rt("/fetch_results")
|
177 |
-
async def get(request, query: str, nn: bool = True):
|
178 |
if "hx-request" not in request.headers:
|
179 |
return RedirectResponse("/search")
|
180 |
|
@@ -184,15 +180,9 @@ async def get(request, query: str, nn: bool = True):
|
|
184 |
f"/fetch_results: Fetching results for query: {query}, ranking: {ranking_value}"
|
185 |
)
|
186 |
# Generate a unique query_id based on the query and ranking value
|
187 |
-
query_id =
|
188 |
-
|
189 |
-
# if result_cache.get(query_id) is not None:
|
190 |
-
# print(f"Results for query_id {query_id} already in cache")
|
191 |
-
# result = result_cache.get(query_id)
|
192 |
-
# search_results = get_results_children(result)
|
193 |
-
# return SearchResult(search_results, query_id)
|
194 |
# Run the embedding and query against Vespa app
|
195 |
-
task_cache.set(query_id, False)
|
196 |
model = app.manager.model
|
197 |
processor = app.manager.processor
|
198 |
q_embs, token_to_idx = get_query_embeddings_and_token_map(processor, model, query)
|
@@ -209,7 +199,15 @@ async def get(request, query: str, nn: bool = True):
|
|
209 |
print(
|
210 |
f"Search results fetched in {end - start:.2f} seconds, Vespa says searchtime was {result['timing']['searchtime']} seconds"
|
211 |
)
|
212 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
result_cache.set(query_id, result)
|
214 |
# Start generating the similarity map in the background
|
215 |
asyncio.create_task(
|
@@ -217,15 +215,7 @@ async def get(request, query: str, nn: bool = True):
|
|
217 |
model, processor, query, q_embs, token_to_idx, result, query_id
|
218 |
)
|
219 |
)
|
220 |
-
fields_to_add = [
|
221 |
-
f"sim_map_{token}"
|
222 |
-
for token in token_to_idx.keys()
|
223 |
-
if not is_special_token(token)
|
224 |
-
]
|
225 |
search_results = get_results_children(result)
|
226 |
-
for result in search_results:
|
227 |
-
for sim_map_key in fields_to_add:
|
228 |
-
result["fields"][sim_map_key] = None
|
229 |
return SearchResult(search_results, query_id)
|
230 |
|
231 |
|
|
|
131 |
|
132 |
|
133 |
@rt("/search")
|
134 |
+
def get(session, request):
|
135 |
# Extract the 'query' and 'ranking' parameters from the URL
|
136 |
query_value = request.query_params.get("query", "").strip()
|
137 |
ranking_value = request.query_params.get("ranking", "nn+colpali")
|
|
|
156 |
)
|
157 |
)
|
158 |
# Generate a unique query_id based on the query and ranking value
|
159 |
+
session["query_id"] = generate_query_id(query_value + ranking_value)
|
160 |
+
query_id = session.get("query_id")
|
161 |
+
print(f"Query id in /search: {query_id}")
|
|
|
|
|
|
|
|
|
162 |
# Show the loading message if a query is provided
|
163 |
return Layout(
|
164 |
Main(Search(request), data_overlayscrollbars_initialize=True, cls="border-t"),
|
|
|
170 |
|
171 |
|
172 |
@rt("/fetch_results")
|
173 |
+
async def get(session, request, query: str, nn: bool = True):
|
174 |
if "hx-request" not in request.headers:
|
175 |
return RedirectResponse("/search")
|
176 |
|
|
|
180 |
f"/fetch_results: Fetching results for query: {query}, ranking: {ranking_value}"
|
181 |
)
|
182 |
# Generate a unique query_id based on the query and ranking value
|
183 |
+
query_id = session.get("query_id")
|
184 |
+
print(f"Query id in /fetch_results: {query_id}")
|
|
|
|
|
|
|
|
|
|
|
185 |
# Run the embedding and query against Vespa app
|
|
|
186 |
model = app.manager.model
|
187 |
processor = app.manager.processor
|
188 |
q_embs, token_to_idx = get_query_embeddings_and_token_map(processor, model, query)
|
|
|
199 |
print(
|
200 |
f"Search results fetched in {end - start:.2f} seconds, Vespa says searchtime was {result['timing']['searchtime']} seconds"
|
201 |
)
|
202 |
+
# Initialize sim_map_ fields in the result
|
203 |
+
fields_to_add = [
|
204 |
+
f"sim_map_{token}"
|
205 |
+
for token in token_to_idx.keys()
|
206 |
+
if not is_special_token(token)
|
207 |
+
]
|
208 |
+
for child in result["root"]["children"]:
|
209 |
+
for sim_map_key in fields_to_add:
|
210 |
+
child["fields"][sim_map_key] = None
|
211 |
result_cache.set(query_id, result)
|
212 |
# Start generating the similarity map in the background
|
213 |
asyncio.create_task(
|
|
|
215 |
model, processor, query, q_embs, token_to_idx, result, query_id
|
216 |
)
|
217 |
)
|
|
|
|
|
|
|
|
|
|
|
218 |
search_results = get_results_children(result)
|
|
|
|
|
|
|
219 |
return SearchResult(search_results, query_id)
|
220 |
|
221 |
|