Skip to main content

a FastAPI model generator and Ai model serving

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastmodel-1.0.0.tar.gz (21.2 kB view details)

Uploaded Source

File details

Details for the file fastmodel-1.0.0.tar.gz.

File metadata

  • Download URL: fastmodel-1.0.0.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for fastmodel-1.0.0.tar.gz
Algorithm Hash digest
SHA256 390c8b00e7e1144de60cdfeed3c247f9945483aeffcbf0d32503c352020fd229
MD5 f5311148968f9fa668893fba1d4a08fe
BLAKE2b-256 8a0bfe27bbfd9a1aa667e84ed9aac64aa04fffe3de1cc6d14240bc5ee78feb34

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page