AItool commited on
Commit
fe496f4
·
verified ·
1 Parent(s): 3607789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -49,20 +49,29 @@ def home_page():
49
  </body>
50
  </html>
51
  """
52
-
53
  @app.post("/upload/")
54
- def upload_file(file: UploadFile = File(...)):
55
- contents = file.read()
56
- img = Image.open(BytesIO(contents)).convert("RGB")
57
- squared_img = fill_square_cropper(img)
 
 
 
 
 
 
 
58
 
59
- # Save the squared image
60
- output = BytesIO()
61
- squared_img.save(output, format="JPEG")
62
- output.seek(0)
 
 
 
 
 
63
 
64
- return HTMLResponse(content=f"<h3>Image successfully squared!</h3><img src='data:image/jpeg;base64,{output.getvalue().hex()}' />", media_type="text/html")
65
-
66
 
67
  if __name__ == "__main__":
68
  import uvicorn
 
49
  </body>
50
  </html>
51
  """
 
52
  @app.post("/upload/")
53
+ async def upload_file(file: UploadFile = File(...)): # Make the function asynchronous
54
+ try:
55
+ # Await the read method
56
+ contents = await file.read()
57
+ img = Image.open(BytesIO(contents)).convert("RGB")
58
+ squared_img = fill_square_cropper(img)
59
+
60
+ # Save the squared image
61
+ output = BytesIO()
62
+ squared_img.save(output, format="JPEG")
63
+ output.seek(0)
64
 
65
+ # Return base64-encoded image
66
+ import base64
67
+ encoded_img = base64.b64encode(output.getvalue()).decode("utf-8")
68
+ return HTMLResponse(
69
+ content=f"<h3>Image successfully squared!</h3><img src='data:image/jpeg;base64,{encoded_img}' />",
70
+ media_type="text/html"
71
+ )
72
+ except Exception as e:
73
+ return HTMLResponse(content=f"<h3>An error occurred: {e}</h3>", media_type="text/html")
74
 
 
 
75
 
76
  if __name__ == "__main__":
77
  import uvicorn