Spaces:
Sleeping
Sleeping
byaldi
Browse files
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
|
|
|
|
1 |
import json
|
2 |
import os
|
3 |
from datetime import datetime
|
|
|
4 |
|
5 |
import dotenv
|
6 |
import lancedb
|
@@ -8,6 +11,7 @@ import requests
|
|
8 |
from datasets import load_dataset
|
9 |
from fasthtml.common import * # noqa
|
10 |
from huggingface_hub import login, whoami
|
|
|
11 |
from rerankers import Reranker
|
12 |
|
13 |
|
@@ -179,7 +183,7 @@ style = Style("""
|
|
179 |
margin-top: 20px;
|
180 |
}
|
181 |
.image-result {
|
182 |
-
width: calc(
|
183 |
text-align: center;
|
184 |
}
|
185 |
.image-result img {
|
@@ -253,9 +257,38 @@ def SearchResult(result):
|
|
253 |
)
|
254 |
|
255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 |
def ImageResult(image):
|
257 |
return Div(
|
258 |
-
Img(src=
|
259 |
cls="image-result",
|
260 |
)
|
261 |
|
@@ -277,8 +310,10 @@ async def post(query: str):
|
|
277 |
log_query_and_results(query, results)
|
278 |
|
279 |
return Div(
|
|
|
280 |
H3("Byaldi Results"),
|
281 |
Div(*[ImageResult(img) for img in image_results], cls="image-results"),
|
|
|
282 |
H3("Text Results"),
|
283 |
Div(*[SearchResult(r) for r in results], id="text-results"),
|
284 |
id="search-results",
|
|
|
1 |
+
import base64
|
2 |
+
import io
|
3 |
import json
|
4 |
import os
|
5 |
from datetime import datetime
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
import dotenv
|
9 |
import lancedb
|
|
|
11 |
from datasets import load_dataset
|
12 |
from fasthtml.common import * # noqa
|
13 |
from huggingface_hub import login, whoami
|
14 |
+
from PIL import Image
|
15 |
from rerankers import Reranker
|
16 |
|
17 |
|
|
|
183 |
margin-top: 20px;
|
184 |
}
|
185 |
.image-result {
|
186 |
+
width: calc(33% - 10px);
|
187 |
text-align: center;
|
188 |
}
|
189 |
.image-result img {
|
|
|
257 |
)
|
258 |
|
259 |
|
260 |
+
def base64_to_pil(base64_string):
|
261 |
+
# Remove the "data:image/png;base64," part if it exists
|
262 |
+
if "base64," in base64_string:
|
263 |
+
base64_string = base64_string.split("base64,")[1]
|
264 |
+
|
265 |
+
# Decode the base64 string
|
266 |
+
img_data = base64.b64decode(base64_string)
|
267 |
+
|
268 |
+
# Open the image using PIL
|
269 |
+
img = Image.open(BytesIO(img_data))
|
270 |
+
|
271 |
+
return img
|
272 |
+
|
273 |
+
|
274 |
+
def process_image(image, max_size=(500, 500), quality=85):
|
275 |
+
pil_image = base64_to_pil(image)
|
276 |
+
img_byte_arr = io.BytesIO()
|
277 |
+
pil_image.thumbnail(max_size)
|
278 |
+
pil_image.save(img_byte_arr, format="JPEG", quality=quality, optimize=True)
|
279 |
+
return f"data:image/jpeg;base64,{base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')}"
|
280 |
+
|
281 |
+
|
282 |
+
# def ImageResult(image):
|
283 |
+
# return Div(
|
284 |
+
# Img(src=f"data:image/jpeg;base64,{image}", alt="arxiv image"),
|
285 |
+
# cls="image-result",
|
286 |
+
# )
|
287 |
+
|
288 |
+
|
289 |
def ImageResult(image):
|
290 |
return Div(
|
291 |
+
Img(src=process_image(image), alt="arxiv image"),
|
292 |
cls="image-result",
|
293 |
)
|
294 |
|
|
|
310 |
log_query_and_results(query, results)
|
311 |
|
312 |
return Div(
|
313 |
+
Br(),
|
314 |
H3("Byaldi Results"),
|
315 |
Div(*[ImageResult(img) for img in image_results], cls="image-results"),
|
316 |
+
Br(),
|
317 |
H3("Text Results"),
|
318 |
Div(*[SearchResult(r) for r in results], id="text-results"),
|
319 |
id="search-results",
|