root-sajjan commited on
Commit
d037576
·
verified ·
1 Parent(s): 83dc34d
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -2,6 +2,7 @@ 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
6
  from io import BytesIO
7
  import requests
@@ -287,22 +288,25 @@ async def generate_excel(predictions: List[Prediction]):
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
 
308
  '''
 
2
  from fastapi.responses import JSONResponse, FileResponse, StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from PIL import Image
5
+ from datetime import datetime
6
  import io
7
  from io import BytesIO
8
  import requests
 
288
  # sheet.add_image(img, img_cell)
289
 
290
 
291
+ # Create a unique filename based on the current timestamp or index
292
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
293
+ excel_file_path = f"/predictions_with_images_{timestamp}.xlsx"
294
+
295
+ # Save the Excel file to the specified path
296
  workbook.save(excel_file_path)
297
 
298
+ # Check if the directory exists, if not, create it (to store multiple files)
299
+ if not os.path.exists("/predictions"):
300
+ os.makedirs("/predictions")
301
+
302
+ # Move the file to a new directory
303
+ os.rename(excel_file_path, f"/predictions/{os.path.basename(excel_file_path)}")
304
+ hf_path = "https://huggingface.co/spaces/root-sajjan/whatisit/resolve/main"
305
+ excel_file_path = hf_path + f"/predictions/{os.path.basename(excel_file_path)}"+"?download=True"
306
+
307
+ return JSONResponse(content={"download_link": excel_file_path})
308
+ # else:
309
+ # return JSONResponse(status_code=500, content={"error": "File upload failed"})
310
 
311
 
312
  '''