Skip to main content

FastModelAPI

A lightweight Python package that turns any Ai inference class with a __call__ method into a web service, as long as its inputs and outputs use Pydantic models. Designed for ML inference, it simplifies exposing models without manually defining request and response schemas.

Features

  • Converts a callable class into a FastAPI-based web service
  • Accepts requests as multipart/form-data
  • Supports JSON or streaming responses based on output serializability
  • Built on FastAPI, Pydantic, and Uvicorn

Installation

Using pip

pip install fastmodel

From sources

git clone git@github.com:Iito/fastmodel.git
cd fastmodel
pip install .

Usage

Here's an example of exposing an OCR model using pytesseract:

import pytesseract
from PIL.Image import Image
from pydantic import BaseModel, ConfigDict, SkipValidation

class OCRModelInput(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    image: SkipValidation[Image]
    timeout: int = 10

class OCRModelOutput(BaseModel):
    text: str

class OCRModel:
    def __init__(self):
        self.ocr = pytesseract

    def __call__(self, input: OCRModelInput) -> OCRModelOutput:
        pred = self.ocr.image_to_string(input.image)
        return OCRModelOutput(text=pred)

    @staticmethod
    def version():
        return str(pytesseract.get_tesseract_version())

You can try this examples as follow:

  • tesseract and pytesseract must be installed on the host machine.
export PYTHONPATH=`pwd`/examples:$PYTHONPATH
fastmodel serve ocr.OCRModel

Request Example

Send an image for OCR processing using curl:

curl -X POST 'http://localhost:8000/' \
--form 'image=@"/path/to/image"'
--form 'timeout="20"'

Response Example

{
  "status": int,
  "message": str,
  "version": str,
  "text": str
}

Limitations

  • Only works with Uvicorn (Gunicorn is not supported).
  • Single worker only due to the way the server is handled.
  • No customization options yet.

Why This Exists

FastAPI is great with native python types and Pydantic models, but manually defining request and response models is tedious. This package automates that, making it easier to serve ML models without extra boilerplate.

Release files for fastmodel 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastmodel 1.0.0
File Size Uploaded
fastmodel-1.0.0.tar.gz 21.2 kB Details

Release files / fastmodel-1.0.0.tar.gz

Download URL fastmodel-1.0.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
390c8b00e7e1144de60cdfeed3c247f9945483aeffcbf0d32503c352020fd229
BLAKE2b-256 checksum
How to use checksums
8a0bfe27bbfd9a1aa667e84ed9aac64aa04fffe3de1cc6d14240bc5ee78feb34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page