Spaces:
Running
Running
| # Dockerfile_api pour Hugging Face Spaces | |
| FROM python:3.11-slim AS builder | |
| # Créer un utilisateur non-root (requis par HF Spaces) | |
| RUN useradd -m -u 1000 user | |
| # Définir le répertoire de travail | |
| WORKDIR /app | |
| # Copier les fichiers de dépendances | |
| COPY --chown=user requirements_api.txt . | |
| # Installer les dépendances | |
| RUN pip install --no-cache-dir --upgrade pip && \ | |
| pip install --no-cache-dir -r requirements_api.txt | |
| # Copier le code de l'application | |
| COPY --chown=user main.py . | |
| COPY --chown=user config.py . | |
| COPY --chown=user 2_Data_transformed/crop_yield_2_train_set_simplified.csv ./2_Data_transformed/ | |
| COPY --chown=user 5_Notebooks/mlartifacts/1/models/m-c06ca7184d99459a87df34d662e183c5/artifacts/model.pkl ./5_Notebooks/mlartifacts/1/models/m-c06ca7184d99459a87df34d662e183c5/artifacts/ | |
| COPY --chown=user 8_Tests/test_functional.py ./8_Tests/ | |
| # ============================================ | |
| # Stage de test : exécution des tests fonctionnels | |
| # ============================================ | |
| FROM builder AS test | |
| # Installer les dépendances de test | |
| RUN pip install --no-cache-dir httpx | |
| # Copier le fichier de tests fonctionnels | |
| COPY --chown=user 8_Tests/test_functional.py . | |
| # Exécuter le test fonctionnel sur /health (démarre le serveur et fait une vraie requête HTTP) | |
| RUN python test_functional.py | |
| # ============================================ | |
| # Stage de production | |
| # ============================================ | |
| FROM builder AS production | |
| # Changer vers l'utilisateur non-root | |
| USER user | |
| # Exposer le port 7860 (port par défaut de HF Spaces) | |
| EXPOSE 7860 | |
| # Variables d'environnement | |
| ENV PYTHONUNBUFFERED=1 | |
| # Commande de démarrage | |
| CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"] | |