Python & FastAPI Tutorial: Create an ai microservice to extract text from images
Learn how to deploy an AI microservice REST API endpoint using FastAPI, pytesseract, streamlit cloud Platform
Checkout the application live here 🚀
Screenshot 📸
- Setup Requirements.txt
- Setup Environment
- Setup FastAPI App
- FastAPI & Jinja Templates
- FastAPI & PyTest
- FastAPI Git & pre-commit
- Deploy to DigitalOcean
- Deploy Docker App to DigitalOcean App Platform
- FastAPI Settings & Environment Variables & dotenv
- Handling File Uploads
- Automated Testing File Uploads
- Image Upload Validation & Tests
- Implementing Tesseract & pytesseract
- Authorization Headers
- Production Endpoint & Authorization Tests
- One-Click Deploy on DigitalOcean App Platform
fastapi
gunicorn
uvicorn
jinja2
pytest
requests
pre-commit
python-dotenv
python-multipart
aiofiles
pillow
- First, pip install pre-commit
- Make a .pre-commit-config.yaml file
- In the Terminal use : pre-commit-install
- Next run the following command : pre-commit run --all-files
- Install pytest using pip
- add pytest.ini file to exclude the directories pytest looks for testing code
- Using pytest -s to stdout the test responses from endpoints as well.
Reference - Tesseract OCR GitHub
Use following command to install wrapper class for Google Tesseract OCR Engine:
pip install pytesseract
-
Install tesseract using windows installer available at: https://github.com/UB-Mannheim/tesseract/wiki
-
Note the tesseract path from the installation. Default installation path at the time of this edit was: C:\Users\USER\AppData\Local\Tesseract-OCR. It may change so please check the installation path.
-
pip install pytesseract
-
Set the tesseract path in the script before calling image_to_string:
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USER\AppData\Local\Tesseract-OCR\tesseract.exe'
- Generate a random token using Python secrets library
import secrets
secrets.token_urlsafe(32)
- Save the generated tokens in a .env file