Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import sys
|
|
4 |
import tempfile
|
5 |
|
6 |
from fastapi import FastAPI, File, UploadFile
|
7 |
-
from fastapi.responses import RedirectResponse,
|
8 |
import gradio as gr
|
9 |
import requests
|
10 |
import uvicorn
|
@@ -90,24 +90,37 @@ async def search(query: str, k: int):
|
|
90 |
|
91 |
results = []
|
92 |
for idx in top_k_indices:
|
93 |
-
|
|
|
|
|
|
|
94 |
|
95 |
# Generate PDF
|
96 |
pdf_buffer = BytesIO()
|
97 |
c = canvas.Canvas(pdf_buffer, pagesize=letter)
|
98 |
width, height = letter
|
|
|
99 |
for result in results:
|
100 |
-
|
|
|
|
|
|
|
101 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
102 |
-
|
103 |
-
temp_file.
|
|
|
|
|
104 |
c.drawImage(temp_file.name, 0, 0, width, height)
|
105 |
c.showPage()
|
|
|
|
|
|
|
106 |
|
107 |
c.save()
|
108 |
pdf_buffer.seek(0)
|
109 |
|
110 |
-
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
4 |
import tempfile
|
5 |
|
6 |
from fastapi import FastAPI, File, UploadFile
|
7 |
+
from fastapi.responses import RedirectResponse, StreamingResponse
|
8 |
import gradio as gr
|
9 |
import requests
|
10 |
import uvicorn
|
|
|
90 |
|
91 |
results = []
|
92 |
for idx in top_k_indices:
|
93 |
+
img_byte_arr = BytesIO()
|
94 |
+
images[idx].save(img_byte_arr, format='PNG')
|
95 |
+
img_base64 = base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
|
96 |
+
results.append({"image": img_base64, "page": f"Page {idx}"})
|
97 |
|
98 |
# Generate PDF
|
99 |
pdf_buffer = BytesIO()
|
100 |
c = canvas.Canvas(pdf_buffer, pagesize=letter)
|
101 |
width, height = letter
|
102 |
+
|
103 |
for result in results:
|
104 |
+
img_base64 = result["image"]
|
105 |
+
img_data = base64.b64decode(img_base64)
|
106 |
+
|
107 |
+
# Create a temporary file to hold the image
|
108 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
109 |
+
temp_file.write(img_data)
|
110 |
+
temp_file.flush()
|
111 |
+
|
112 |
+
# Draw the image from the temporary file
|
113 |
c.drawImage(temp_file.name, 0, 0, width, height)
|
114 |
c.showPage()
|
115 |
+
|
116 |
+
# Clean up the temporary file
|
117 |
+
os.remove(temp_file.name)
|
118 |
|
119 |
c.save()
|
120 |
pdf_buffer.seek(0)
|
121 |
|
122 |
+
# Use StreamingResponse to handle in-memory file
|
123 |
+
return StreamingResponse(pdf_buffer, media_type='application/pdf', filename='search_results.pdf')
|
124 |
|
125 |
if __name__ == "__main__":
|
126 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|