File size: 1,322 Bytes
ce5cd48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
import io
import shutil
from PIL import Image
from pix2tex.cli import LatexOCR
from tempfile import NamedTemporaryFile

from fastapi.responses import JSONResponse
from fastapi import FastAPI, File, UploadFile

app = FastAPI()
latex_ocr_model = LatexOCR()


@app.get("/")
def read_root():
    return {"status": "ok"}


# Takes in an image and returns the latex code
@app.post("/latex/")
async def read_item(file: UploadFile = File(...)):
    # Create a temporary file to store the uploaded file's contents
    with NamedTemporaryFile(delete=False) as temp_file:
        # Copy the uploaded file's contents to the temporary file
        shutil.copyfileobj(file.file, temp_file)
        temp_file_path = temp_file.name

    # Ensure the file pointer is at the start
    file.file.seek(0)

    try:
        # Open the temporary file and pass its file object to the model
        with open(temp_file_path, "rb") as temp_file_obj:
            img_data = temp_file_obj.read()

        img_bytes = io.BytesIO(img_data)
        img = Image.open(img_bytes)
        latex_code = latex_ocr_model(img)
        return {"latex": latex_code}
    except Exception as e:
        return JSONResponse(status_code=400, content={"message": str(e)})
    finally:
        # Clean up: close and remove the temporary file
        file.file.close()