CoolFace
Apppublic

JohnathyBoi/math-ocr-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.py29 linesDownload Raw Back to root
1from fastapi import FastAPI, UploadFile, File
2from PIL import Image
3import io
4from pix2tex.cli import LatexOCR
5
6app = FastAPI()
7model = LatexOCR()
8
9@app.get("/")
10async def read_root():
11    return {"message": "Math OCR API is running. Send POST requests to /predict"}
12
13@app.post("/predict")
14async def predict(file: UploadFile = File(...)):
15    try:
16        # Read the image sent from the Android app
17        image_data = await file.read()
18        img = Image.open(io.BytesIO(image_data))
19        
20        # Ensure image is in RGB format for the model
21        if img.mode != 'RGB':
22            img = img.convert('RGB')
23            
24        # Run the LaTeX OCR model
25        latex_result = model(img)
26        
27        return {"success": True, "latex": latex_result}
28    except Exception as e:
29        return {"success": False, "error": str(e)}