root-sajjan commited on
Commit
f50806b
·
verified ·
1 Parent(s): d0af67b

url download

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py CHANGED
@@ -3,6 +3,7 @@ from fastapi.responses import JSONResponse, FileResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from PIL import Image
5
  import io
 
6
  import requests
7
  import sqlite3
8
  from pydantic import BaseModel, EmailStr
@@ -220,6 +221,84 @@ async def generate_excel(predictions: List[Prediction]):
220
  sheet = workbook.active
221
  sheet.title = "Predictions"
222
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  # Add headers
224
  headers = ["Category", "Confidence", "Predicted Brand", "Price", "Image URL", "Details", "Detected Text", ]
225
  sheet.append(headers)
 
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from PIL import Image
5
  import io
6
+ from io import BytesIO
7
  import requests
8
  import sqlite3
9
  from pydantic import BaseModel, EmailStr
 
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
+ # Set header style and alignment
229
+ for cell in sheet[1]:
230
+ cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
231
+ sheet.row_dimensions[1].height = 30 # Adjust header row height
232
+
233
+ # Set column widths based on data type
234
+ column_widths = {
235
+ "A": 20, # Category
236
+ "B": 15, # Confidence
237
+ "C": 40, # Predicted Brand
238
+ "D": 15, # Price
239
+ "E": 50, # Image URL
240
+ "F": 30, # Details
241
+ "G": 30 # Detected Text
242
+ }
243
+ for col, width in column_widths.items():
244
+ sheet.column_dimensions[col].width = width
245
+
246
+ # Add prediction rows
247
+ for idx, prediction in enumerate(predictions):
248
+ row_index = idx + 2 # Start from the second row
249
+
250
+ # Add data to the row
251
+ sheet.append([
252
+ prediction.category,
253
+ prediction.confidence,
254
+ prediction.predicted_brand,
255
+ prediction.price,
256
+ prediction.image_url,
257
+ prediction.details,
258
+ prediction.detected_text,
259
+ ])
260
+
261
+ # Adjust row height for multiline text
262
+ sheet.row_dimensions[row_index].height = 180 # Default height for rows
263
+
264
+ # Wrap text in all cells of the row
265
+ for col_idx in range(1, 8): # Columns A to G
266
+ cell = sheet.cell(row=row_index, column=col_idx)
267
+ cell.alignment = Alignment(wrap_text=True, vertical="top")
268
+
269
+ # If image URL is provided, download it
270
+ if prediction.image_url:
271
+ try:
272
+ response = requests.get(prediction.image_url)
273
+ img = ExcelImage(BytesIO(response.content))
274
+ img.width, img.height = 160, 160 # Resize image to fit into the cell
275
+ img_cell = f"G{row_index}" # Image column
276
+ sheet.add_image(img, img_cell)
277
+ except requests.exceptions.RequestException as e:
278
+ print(f"Error downloading image: {e}")
279
+ # Optionally add a placeholder image or text
280
+
281
+ # Save the Excel file
282
+ excel_file_path = "/tmp/predictions_with_images.xlsx"
283
+ workbook.save(excel_file_path)
284
+
285
+ # Serve the Excel file as a response
286
+ return FileResponse(
287
+ excel_file_path,
288
+ media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
289
+ filename="predictions_with_images.xlsx"
290
+ )
291
+
292
+
293
+ @app.post("/generate-excel2/")
294
+ async def generate_excel(predictions: List[Prediction]):
295
+ print('Generate excel called')
296
+
297
+ # Create an Excel workbook
298
+ workbook = Workbook()
299
+ sheet = workbook.active
300
+ sheet.title = "Predictions"
301
+
302
  # Add headers
303
  headers = ["Category", "Confidence", "Predicted Brand", "Price", "Image URL", "Details", "Detected Text", ]
304
  sheet.append(headers)