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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastmodel-0.1.0.post1.tar.gz.
File metadata
- Download URL: fastmodel-0.1.0.post1.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0a3ab7921e8f3bbf4edda5a34e424903ec6f7f5e90184303a4aafdee9706a19
|
|
| MD5 |
ddc20d415b08608db56fb5281d6d8d61
|
|
| BLAKE2b-256 |
462439d3f5ae0381a7b28c0302c63dcd74edff62b5599a745af6e5a0f96410a4
|
File details
Details for the file fastmodel-0.1.0.post1-py3-none-any.whl.
File metadata
- Download URL: fastmodel-0.1.0.post1-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b09b09f49649fc4f8d10796d2c7c703cdd0e8eb4726ddd14083c435e62d3c17f
|
|
| MD5 |
33cf847ee949c649dbee02143963f598
|
|
| BLAKE2b-256 |
9f25b8b07effb35f916ae60878e1d9f07bf65627cf1afc4ffafabbf411e7a3e9
|