Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import os
|
|
3 |
import sys
|
4 |
|
5 |
from fastapi import FastAPI, File, UploadFile
|
6 |
-
from fastapi.responses import RedirectResponse,
|
7 |
import gradio as gr
|
8 |
import requests
|
9 |
import uvicorn
|
@@ -14,6 +14,8 @@ from PIL import Image
|
|
14 |
from torch.utils.data import DataLoader
|
15 |
from transformers import AutoProcessor
|
16 |
import base64
|
|
|
|
|
17 |
|
18 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), './colpali-main')))
|
19 |
|
@@ -87,13 +89,25 @@ async def search(query: str, k: int):
|
|
87 |
|
88 |
results = []
|
89 |
for idx in top_k_indices:
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
img_byte_arr = img_byte_arr.getvalue()
|
93 |
img_base64 = base64.b64encode(img_byte_arr).decode('utf-8')
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
return
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
3 |
import sys
|
4 |
|
5 |
from fastapi import FastAPI, File, UploadFile
|
6 |
+
from fastapi.responses import RedirectResponse, FileResponse
|
7 |
import gradio as gr
|
8 |
import requests
|
9 |
import uvicorn
|
|
|
14 |
from torch.utils.data import DataLoader
|
15 |
from transformers import AutoProcessor
|
16 |
import base64
|
17 |
+
from reportlab.pdfgen import canvas
|
18 |
+
from reportlab.lib.pagesizes import letter
|
19 |
|
20 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), './colpali-main')))
|
21 |
|
|
|
89 |
|
90 |
results = []
|
91 |
for idx in top_k_indices:
|
92 |
+
results.append({"image": images[idx], "page": f"Page {idx}"})
|
93 |
+
|
94 |
+
# Generate PDF
|
95 |
+
pdf_buffer = BytesIO()
|
96 |
+
c = canvas.Canvas(pdf_buffer, pagesize=letter)
|
97 |
+
width, height = letter
|
98 |
+
for result in results:
|
99 |
+
img = result["image"]
|
100 |
+
img_byte_arr = BytesIO()
|
101 |
+
img.save(img_byte_arr, format='PNG')
|
102 |
img_byte_arr = img_byte_arr.getvalue()
|
103 |
img_base64 = base64.b64encode(img_byte_arr).decode('utf-8')
|
104 |
+
c.drawImage(BytesIO(base64.b64decode(img_base64)), 0, 0, width, height)
|
105 |
+
c.showPage()
|
106 |
+
|
107 |
+
c.save()
|
108 |
+
pdf_buffer.seek(0)
|
109 |
|
110 |
+
return FileResponse(pdf_buffer, media_type='application/pdf', filename='search_results.pdf')
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|