From 3960d106c8328dc946b6ccbda4f975fba37d60c6 Mon Sep 17 00:00:00 2001 From: Mark Bailey Date: Fri, 25 Oct 2024 18:07:41 -0400 Subject: [PATCH] refactor: convert get route to post --- src/main.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main.py b/src/main.py index 977a153..d54f229 100644 --- a/src/main.py +++ b/src/main.py @@ -1,8 +1,10 @@ import os import pathlib -from fastapi.responses import JSONResponse import numpy as np +from base64 import b64decode +from fastapi.responses import JSONResponse +from pydantic import BaseModel from doctr.io import DocumentFile from doctr.models import ocr_predictor from fastapi import FastAPI, HTTPException, Response @@ -45,10 +47,16 @@ def root(): return Response(renderTemplate("main.html", {"results": result})) +class Image(BaseModel): + file_name: str + file_contents: str -@app.get("/detect/") -def file(file_name: str): - file_name = os.path.expanduser("~/Projects/sandbox/")+unquote(file_name).lstrip("/") +@app.post("/detect/") +def file(file: Image): + file_name = unquote(file.file_name) + + with open("data/"+file_name, "wb") as f: + f.write(b64decode(file.file_contents)) print("Working on: " + file_name) @@ -60,7 +68,7 @@ def file(file_name: str): assume_straight_pages=False, preserve_aspect_ratio=True, ) - doc = DocumentFile.from_images(file_name) + doc = DocumentFile.from_images("data/"+file_name) pred_res = predictor(doc) json_res = pred_res.export() converted = convert_dict_items_to_list(json_res)