├── .dockerignore ├── .gitignore ├── Dockerfile ├── README.md ├── app ├── main.py └── model │ ├── model.py │ └── trained_pipeline-0.1.0.pkl ├── heroku.yml └── requirements.txt /.dockerignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | *.pyc 3 | *.pyo 4 | *.pyd 5 | .Python 6 | env/ 7 | venv/ 8 | pip-log.txt 9 | pip-delete-this-directory.txt 10 | .tox/ 11 | .coverage 12 | .coverage.* 13 | .cache 14 | nosetests.xml 15 | coverage.xml 16 | *.cover 17 | *.log 18 | .git 19 | .gitignore 20 | .mypy_cache 21 | .pytest_cache 22 | .hypothesis 23 | .idea -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9 2 | 3 | COPY ./requirements.txt /app/requirements.txt 4 | 5 | RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt 6 | 7 | COPY ./app /app/app 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deploy ML models with FastAPI, Docker, and Heroku 2 | 3 | ### 1. Develop and save the model with this Colab 4 | 5 | [Open Colab](https://colab.research.google.com/drive/1uaALcaatvxOu42IhQA4r0bahfdpw-Z7v?usp=sharing) 6 | 7 | ### 2. Create Docker container 8 | 9 | ```bash 10 | docker build -t app-name . 11 | 12 | docker run -p 80:80 app-name 13 | ``` 14 | 15 | ### 3. Create Git repo 16 | 17 | If you clone this repo this step is not needed. Or you can delete this git repo with `rm -rf .git` and start with a new one: 18 | 19 | ```bash 20 | git init 21 | git add . 22 | git commit -m "initial commit" 23 | git branch -M main 24 | ``` 25 | 26 | ### 4. Create Heroku project 27 | 28 | ```bash 29 | heroku login 30 | heroku create your-app-name 31 | heroku git:remote your-app-name 32 | heroku stack:set container 33 | git push heroku main 34 | ``` 35 | -------------------------------------------------------------------------------- /app/main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | from pydantic import BaseModel 3 | from app.model.model import predict_pipeline 4 | from app.model.model import __version__ as model_version 5 | 6 | 7 | app = FastAPI() 8 | 9 | 10 | class TextIn(BaseModel): 11 | text: str 12 | 13 | 14 | class PredictionOut(BaseModel): 15 | language: str 16 | 17 | 18 | @app.get("/") 19 | def home(): 20 | return {"health_check": "OK", "model_version": model_version} 21 | 22 | 23 | @app.post("/predict", response_model=PredictionOut) 24 | def predict(payload: TextIn): 25 | language = predict_pipeline(payload.text) 26 | return {"language": language} 27 | -------------------------------------------------------------------------------- /app/model/model.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | import re 3 | from pathlib import Path 4 | 5 | __version__ = "0.1.0" 6 | 7 | BASE_DIR = Path(__file__).resolve(strict=True).parent 8 | 9 | 10 | with open(f"{BASE_DIR}/trained_pipeline-{__version__}.pkl", "rb") as f: 11 | model = pickle.load(f) 12 | 13 | 14 | classes = [ 15 | "Arabic", 16 | "Danish", 17 | "Dutch", 18 | "English", 19 | "French", 20 | "German", 21 | "Greek", 22 | "Hindi", 23 | "Italian", 24 | "Kannada", 25 | "Malayalam", 26 | "Portugeese", 27 | "Russian", 28 | "Spanish", 29 | "Sweedish", 30 | "Tamil", 31 | "Turkish", 32 | ] 33 | 34 | 35 | def predict_pipeline(text): 36 | text = re.sub(r'[!@#$(),\n"%^*?\:;~`0-9]', " ", text) 37 | text = re.sub(r"[[]]", " ", text) 38 | text = text.lower() 39 | pred = model.predict([text]) 40 | return classes[pred[0]] 41 | -------------------------------------------------------------------------------- /app/model/trained_pipeline-0.1.0.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AssemblyAI-Community/ml-fastapi-docker-heroku/f36f43fe739fe8384e6fb41b754e3cc496e631ff/app/model/trained_pipeline-0.1.0.pkl -------------------------------------------------------------------------------- /heroku.yml: -------------------------------------------------------------------------------- 1 | build: 2 | docker: 3 | web: Dockerfile -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | scikit-learn==1.0.2 --------------------------------------------------------------------------------