diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2024-10-23 10:49:35 -0600 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2024-10-23 10:49:35 -0600 |
commit | 33240ea56ea7e94eb39c3c302feb0409387e436d (patch) | |
tree | aa3f710652965715ae0aaa60fac006c8dc78e83b /api.py | |
parent | cc7b6ba6de4bd9abb5d1194469e57bcb340acf68 (diff) |
added basic ratings endpoint
Diffstat (limited to 'api.py')
-rw-r--r-- | api.py | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -0,0 +1,34 @@ +import hashlib +from fastapi import FastAPI, UploadFile, File, Form, HTTPException +from pathlib import Path +from PIL import Image +import io + +app = FastAPI() + +images_dir = Path("images") +images_dir.mkdir(exist_ok=True) + +MAX_FILE_SIZE = 25 * 1024 * 1024 + + +@app.post("/rate_snippet") +async def rate_snippet(good: bool = False, image: UploadFile = File(...)): + image_content = await image.read() + image_size = len(image_content) + + if image_size > MAX_FILE_SIZE: + raise HTTPException( + status_code=400, detail="File too large. Maximum file size is 25MB." + ) + + img = Image.open(io.BytesIO(image_content)) + sha256_hash = hashlib.sha256(image_content).hexdigest() + + if good: + fname = f"{sha256_hash}_good.png" + else: + fname = f"{sha256_hash}_bad.png" + + img.save(f"{images_dir}/{fname}", format="PNG") + return {"message": "Thank you for rating the snippet!", "image_size": image_size} |