blob: 8c565af4e35bf31830657100ca6b6e5c5325951d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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}
|