- Train and predict text intent using a custom classifier
- MongoDB integration for storing predictions
- FastAPI endpoints with automatic Swagger docs
- Dockerized for easy deployment
- Public exposure via ngrok
intent-classifier/
ml-deploy-test/
├── Dockerfile # Docker container setup
├── docker-compose.yml # Compose file for API and MongoDB
├── README.md # This file
├── app/
│ └── app.py # FastAPI app entry point
├── db/
│ ├── models.py # MongoDB schemas
│ └── engine.py # MongoDB connection logic
├── tools/
│ ├── models/ # Trained models
│ │ └── ...
│ └── classifier_wrapper.py # IntentClassifier implementation
├── tests/
├── requirements.txt
└── .gitignore # Git ignore rules
git clone https://github.com/adaj/ml-deploy-test.git
cd ml-deploy-test
# If you are not in a Colab Notebook, create your conda env
conda create -n ml-deploy-test python=3.10
pip install -r requirements.txt
python tools/intent_classifier.py train \
--config="tools/confusion/confusion_config.yml" \
--examples_file="tools/confusion/confusion_examples.yml" \ --save_model="tools/confusion/confusion-clf-v1.keras"
Create a free MongoDB Atlas cluster:
- Sign up at https://www.mongodb.com/cloud/atlas
- Create a new Shared Cluster (M0)
- Add your IP to the access list (e.g., 0.0.0.0/0 for testing)
- Create a database user and password
- Copy the connection string (e.g., mongodb+srv://:@cluster.mongodb.net/dbname)
Set the connection string as an environment variable MONGO_URI in your .env
, taken from your MongoDB Atlas cluster.
Set the WANDB_API_KEY to keep track of your model's info.
If you set ENV=prod, create your API token (requires authenticated access to MongoDB cluster):
# Criar um novo token
python db/tokens.py create --owner="alguem" --expires_in_days=365
# Read the tokens created
python db/tokens.py read_all
This token has to be added in the header of API requests.
If you want to run the app locally:
uvicorn app.app:app --host 0.0.0.0 --port 8000
Check http://localhost:8000.
Install ngrok:
npm install -g ngrok # or follow instructions at https://ngrok.com/download
Authenticate ngrok (replace YOUR_AUTHTOKEN with your token):
ngrok config add-authtoken YOUR_AUTHTOKEN
Start ngrok tunnel:
ngrok http 8000
You’ll receive a public URL like `https://abc123.ngrok.io.
docker compose up --build --detach
Check the containers and logs:
docker ps -a
docker compose logs -f
After it's up, the API should be available at http://localhost:8000.
POST /confusion
Make a prediction:
curl -X POST http://localhost:8000/confusion \
-H "Content-Type: application/json" \
-d '{"text": "Não entendo como isso é possível"}'
Response:
{
"text": "Não entendo como isso é possível",
"prediction": "confusion",
"certainty": 0.97
}
Access the interactive Swagger UI at http://localhost:8000/docs
or via your ngrok URL.
Este projeto demonstra algumas práticas essenciais de MLOps para garantir reproduzibilidade e automação:
- Imagens Docker: a API e a interface Streamlit são distribuídas em containers isolados. O
Dockerfile.app
eDockerfile.streamlit
definem dependências e comandos. - Orquestração com Docker Compose: o arquivo
docker-compose.yml
permite iniciar a API, o Streamlit e o MongoDB de forma integrada. - Variáveis de ambiente: URIs, credenciais e o caminho do modelo são configurados via variáveis de ambiente ou
.env
. - Testes automatizados: os testes em
tests/
validam oIntentClassifier
e rodam no fluxo de CI. - Download automático de modelo:
fetch_model.sh
obtém o modelo armazenado no Weights & Biases sempre que o container inicia. - Logs em MongoDB: previsões são registradas para análise posterior.
A automação do ciclo de vida conta com dois workflows em .github/workflows
:
- ci.yml – constrói as imagens do
docker-compose.yml
e executa os testes unitários a cada push ou pull request paramain
, usando os segredos como variáveis de ambiente. - docker-publish.yml – publica a imagem da API no Docker Hub após push para
main
ou execução manual. A imagem é construída comdocker/build-push-action
e enviada usandoDOCKER_USERNAME
eDOCKER_PASSWORD
.
-
The IntentClassifier can be trained (
IntentClassifier(config, examples_file).train(save_model="model-0.1")
) and later, initialized withload_model="model-0.1"
to load a pre-trained model. -
Training data is loaded from examples_file specified in the config.
-
Predictions are stored in MongoDB for later analysis.
- FastAPI Docs: https://fastapi.tiangolo.com/
- MongoDB Atlas: https://www.mongodb.com/cloud/atlas
- ngrok Docs: https://ngrok.com/docs
📄 License This project is licensed under the MIT License.