feat: add dockerfile
Some checks failed
Sphinx: Render docs / build (push) Has been cancelled
Python Linting / lint (push) Has been cancelled
Run Tests with Pytest / test (push) Has been cancelled

This commit is contained in:
2025-12-15 22:31:13 +08:00
parent 9b88cec77b
commit ba0968b2da
8 changed files with 784 additions and 9 deletions

View File

@@ -31,7 +31,7 @@ from texteller.utils import get_device
"-p",
"--port",
type=int,
default=8000,
default=8001,
help="Port to run the server on",
)
@click.option(

View File

@@ -1,7 +1,11 @@
import numpy as np
import cv2
import base64
import requests
from io import BytesIO
from starlette.requests import Request
from starlette.responses import JSONResponse
from ray import serve
from ray.serve.handle import DeploymentHandle
@@ -57,13 +61,42 @@ class Ingress:
def __init__(self, rec_server: DeploymentHandle) -> None:
self.texteller_server = rec_server
async def __call__(self, request: Request) -> str:
form = await request.form()
img_rb = await form["img"].read()
async def __call__(self, request: Request):
try:
# Parse JSON body
body = await request.json()
img_nparray = np.frombuffer(img_rb, np.uint8)
img_nparray = cv2.imdecode(img_nparray, cv2.IMREAD_COLOR)
img_nparray = cv2.cvtColor(img_nparray, cv2.COLOR_BGR2RGB)
# Get image data from either base64 or URL
if "image_base64" in body:
# Decode base64 image
image_data = body["image_base64"]
# Remove data URL prefix if present (e.g., "data:image/png;base64,")
if "," in image_data:
image_data = image_data.split(",", 1)[1]
img_bytes = base64.b64decode(image_data)
img_nparray = np.frombuffer(img_bytes, np.uint8)
pred = await self.texteller_server.predict.remote(img_nparray)
return pred
elif "image_url" in body:
# Download image from URL
image_url = body["image_url"]
response = requests.get(image_url, timeout=30)
response.raise_for_status()
img_bytes = response.content
img_nparray = np.frombuffer(img_bytes, np.uint8)
else:
return JSONResponse({"error": "Either 'image_base64' or 'image_url' must be provided"}, status_code=400)
# Decode and convert image
img_nparray = cv2.imdecode(img_nparray, cv2.IMREAD_COLOR)
if img_nparray is None:
return JSONResponse({"error": "Failed to decode image"}, status_code=400)
img_nparray = cv2.cvtColor(img_nparray, cv2.COLOR_BGR2RGB)
# Get prediction
pred = await self.texteller_server.predict.remote(img_nparray)
return JSONResponse({"result": pred})
except Exception as e:
return JSONResponse({"error": str(e)}, status_code=500)