Spaces:
Sleeping
Sleeping
downloadable excel
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile, Response, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse, FileResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from PIL import Image
|
5 |
import io
|
@@ -210,8 +210,115 @@ class Prediction(BaseModel):
|
|
210 |
detected_text: Optional[str]
|
211 |
image_url: Optional[str]
|
212 |
image_path: Optional[str]
|
213 |
-
|
214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
@app.post("/generate-excel/")
|
216 |
async def generate_excel(predictions: List[Prediction]):
|
217 |
print('Generate excel called')
|
@@ -363,7 +470,7 @@ async def generate_excel(predictions: List[Prediction]):
|
|
363 |
filename="predictions_with_images.xlsx"
|
364 |
)
|
365 |
'''
|
366 |
-
|
367 |
@app.post("/generate-excel/")
|
368 |
async def generate_excel(predictions: list):
|
369 |
print('Generate excel called')
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile, Response, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse, FileResponse, StreamingResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from PIL import Image
|
5 |
import io
|
|
|
210 |
detected_text: Optional[str]
|
211 |
image_url: Optional[str]
|
212 |
image_path: Optional[str]
|
213 |
+
|
214 |
|
215 |
+
@app.post("/generate-excel/")
|
216 |
+
async def generate_excel(predictions: List[Prediction]):
|
217 |
+
print('Generate excel called')
|
218 |
+
|
219 |
+
# Create an Excel workbook
|
220 |
+
workbook = Workbook()
|
221 |
+
sheet = workbook.active
|
222 |
+
sheet.title = "Predictions"
|
223 |
+
|
224 |
+
# Add headers
|
225 |
+
headers = ["Category", "Confidence", "Predicted Brand", "Price", "Image URL", "Details", "Detected Text"]
|
226 |
+
sheet.append(headers)
|
227 |
+
|
228 |
+
# Add prediction rows (skipping for brevity)
|
229 |
+
|
230 |
+
# Set header style and alignment
|
231 |
+
for cell in sheet[1]:
|
232 |
+
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
|
233 |
+
sheet.row_dimensions[1].height = 30 # Adjust header row height
|
234 |
+
|
235 |
+
# Set column widths based on data type
|
236 |
+
column_widths = {
|
237 |
+
"A": 20, # Category
|
238 |
+
"B": 15, # Confidence
|
239 |
+
"C": 40, # Predicted Brand
|
240 |
+
"D": 15, # Price
|
241 |
+
"E": 50, # Image URL
|
242 |
+
"F": 30, # Details
|
243 |
+
"G": 30 # Detected Text
|
244 |
+
}
|
245 |
+
for col, width in column_widths.items():
|
246 |
+
sheet.column_dimensions[col].width = width
|
247 |
+
|
248 |
+
# Add prediction rows
|
249 |
+
for idx, prediction in enumerate(predictions):
|
250 |
+
row_index = idx + 2 # Start from the second row
|
251 |
+
|
252 |
+
# Add data to the row
|
253 |
+
sheet.append([
|
254 |
+
prediction.category,
|
255 |
+
prediction.confidence,
|
256 |
+
prediction.predicted_brand,
|
257 |
+
prediction.price,
|
258 |
+
prediction.image_url,
|
259 |
+
prediction.details,
|
260 |
+
prediction.detected_text,
|
261 |
+
|
262 |
+
])
|
263 |
+
|
264 |
+
# Adjust row height for multiline text
|
265 |
+
sheet.row_dimensions[row_index].height = 180 # Default height for rows
|
266 |
+
|
267 |
+
# Wrap text in all cells of the row
|
268 |
+
for col_idx in range(1, 8): # Columns A to G
|
269 |
+
cell = sheet.cell(row=row_index, column=col_idx)
|
270 |
+
cell.alignment = Alignment(wrap_text=True, vertical="top")
|
271 |
+
|
272 |
+
if prediction.image_url:
|
273 |
+
try:
|
274 |
+
response = requests.get(prediction.image_url)
|
275 |
+
img = ExcelImage(BytesIO(response.content))
|
276 |
+
img.width, img.height = 160, 160 # Resize image to fit into the cell
|
277 |
+
img_cell = f"G{row_index}" # Image column
|
278 |
+
sheet.add_image(img, img_cell)
|
279 |
+
except requests.exceptions.RequestException as e:
|
280 |
+
print(f"Error downloading image: {e}")
|
281 |
+
|
282 |
+
# # Add image if the path exists
|
283 |
+
# if os.path.exists(prediction.image_path):
|
284 |
+
# img = ExcelImage(prediction.image_path)
|
285 |
+
# img.width, img.height = 160, 160 # Resize image to fit into the cell
|
286 |
+
# img_cell = f"G{row_index}" # Image column
|
287 |
+
# sheet.add_image(img, img_cell)
|
288 |
+
|
289 |
+
|
290 |
+
# Save the Excel file to a temporary path
|
291 |
+
excel_file_path = "/predictions_with_images.xlsx"
|
292 |
+
workbook.save(excel_file_path)
|
293 |
+
|
294 |
+
# Upload the Excel file to File.io
|
295 |
+
with open(excel_file_path, 'rb') as file:
|
296 |
+
response = requests.post('https://file.io/', files={'file': file})
|
297 |
+
response_data = response.json()
|
298 |
+
|
299 |
+
# Check if upload was successful
|
300 |
+
if response.status_code == 200:
|
301 |
+
file_url = response_data['link']
|
302 |
+
print(file_url)
|
303 |
+
return JSONResponse(content={"download_link": file_url})
|
304 |
+
else:
|
305 |
+
return JSONResponse(status_code=500, content={"error": "File upload failed"})
|
306 |
+
|
307 |
+
@app.get("/download-excel/")
|
308 |
+
async def download_excel(url: str):
|
309 |
+
try:
|
310 |
+
# Download the file from File.io using the provided URL
|
311 |
+
response = requests.get(url)
|
312 |
+
response.raise_for_status() # Check for request errors
|
313 |
+
|
314 |
+
# Return the file as a streaming response
|
315 |
+
return StreamingResponse(BytesIO(response.content), media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
316 |
+
|
317 |
+
except requests.exceptions.RequestException as e:
|
318 |
+
print(f"Error downloading the file: {e}")
|
319 |
+
return {"error": "File download failed"}
|
320 |
+
|
321 |
+
'''
|
322 |
@app.post("/generate-excel/")
|
323 |
async def generate_excel(predictions: List[Prediction]):
|
324 |
print('Generate excel called')
|
|
|
470 |
filename="predictions_with_images.xlsx"
|
471 |
)
|
472 |
'''
|
473 |
+
'''
|
474 |
@app.post("/generate-excel/")
|
475 |
async def generate_excel(predictions: list):
|
476 |
print('Generate excel called')
|