Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|