# API Deployment โ€“ Air Paradis Project ๐Ÿ“˜ This documentation is also available in [French ๐Ÿ‡ซ๐Ÿ‡ท](./README.fr.md) This folder contains the code and configuration necessary to deploy the sentiment prediction API. --- ## ๐ŸŽฏ Objective Deploy a REST API that allows to: - Predict the sentiment of a tweet (positive or negative) using an LSTM model trained on embeddings (Word2Vec or GloVe), - Report prediction errors. The API uses the best-performing model registered on MLflow and serves it via FastAPI. --- ## ๐Ÿ“ฆ Overall Architecture Diagram: ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ End user (UI) โ”‚ โ”‚ (Streamlit interface) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ [HTTP call to /predict] โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ FastAPI API โ”‚ โ”‚ (server launched via โ”‚ โ”‚ Uvicorn) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ Model loading from MLflow โ”‚ Tweet preprocessing โ”‚ Prediction (inference) โ”‚ Sentiment response (JSON) ``` --- ## ๐Ÿงช API Workflow 1. **Model loading** - The best performing model is registered in MLflow under a fixed name (`SentimentAnalysisLSTM`) and a `Production` stage. - The API loads this model at startup using the URI: ``` models:/SentimentAnalysisLSTM/Production ``` 2. **Text preprocessing** - Text is cleaned, tokenized, lemmatized, and converted into vectors (embeddings). 3. **Prediction** - The LSTM model (loaded from MLflow) makes a prediction. - The result (`positive` or `negative`) is returned as JSON. 4. **User interface (optional)** - A Streamlit interface allows visual interaction with the API. --- ## ๐Ÿณ Deployment with Docker A `Dockerfile` is provided for easy deployment on any Docker-compatible host (NAS, cloud, etc.). This Docker image should be pushed to DockerHub with every validated version (see CI/CD below). For data sovereignty, deployment is preferably done on a **NAS with Intel N100** CPU (energy-efficient, sufficient for inference). Docker can be automatically redeployed using: - `cron` (simple), or - `Watchtower` in docker-compose/kubernetes setups. **Development command (local test):** ```bash docker build -t sentiment-api . docker run --rm --env-file ../../.env -p 8000:8000 -p 8080:8080 sentiment-api ``` In production (e.g. via docker-compose or cloud services), environment variables must be defined in the compose file or secret manager. --- ## ๐Ÿ›  Notes on AVX2 / Cloud Deployment **Update:** the NAS initially failed due to lack of AVX2 support. Some N100-based NAS motherboards disable AVX2 in BIOS. When not accessible, fallback deployment is done on **Google Cloud**. Issue: public cloud services do **not allow multiple ports** to be exposed easily. Two solutions: - Split into two containers: FastAPI + Streamlit, - Use a **reverse proxy** (e.g., Nginx) to map ports. โžก๏ธ We chose the second option to keep a **single Docker image**. **Update 2:** AVX2 was enabled via BIOS. Deployment is now active **both locally (NAS)** and **on cloud**, with continuous deployment on NAS (see section below). --- ## ๐Ÿ” CI/CD โ€“ GitHub Actions A pipeline is defined in `.github/workflows/` to: - Run unit tests on every push, - Check API health before deployment (local/cloud), - Build and push Docker image to DockerHub. Tests are defined in: ``` deployment/api/tests/ ``` Docker image auto-redeployment on NAS is handled by **Watchtower**, which checks DockerHub updates (like a cron job, with logs). ---