Cette application fournit une API REST et une page web de test pour analyser le sentiment (positif/négatif) d’un texte en utilisant un modèle Hugging Face (distilbert-base-uncased-finetuned-sst-2-english).
├── app/
│ └── main.py # Application FastAPI
│── static/
│ └── index.html # Page test frontend
├── requirements.txt # Dépendances Python
├── Dockerfile # Image Docker
└── README.md # Documentation
python3 -m venv venv
source venv/bin/activatepip install --upgrade pip
pip install -r requirements.txtuvicorn app.main:app --host 0.0.0.0 --port 8000 --reloaddocker build -t sentiment-api .docker run -p 8000:8000 sentiment-apiL’API sera accessible sur :
👉 http://localhost:8000
- Aller sur Hugging Face Spaces
- Créer un espace type Docker
- Uploader les fichiers du projet (
app/,requirements.txt,Dockerfile,README.md)
Le code utilise un dossier accessible en écriture pour le cache :
CACHE_DIR = "/tmp/hf_cache"
os.environ["TRANSFORMERS_CACHE"] = CACHE_DIR
os.makedirs(CACHE_DIR, exist_ok=True)Cela évite l’erreur PermissionError: /.cache.
Une fois le Space lancé, Hugging Face va automatiquement :
- Construire l’image Docker
- Démarrer l’API FastAPI
- Fournir un endpoint public
GET /health
Réponse :
{ "status": "ok", "model": "distilbert-base-uncased-finetuned-sst-2-english" }POST /predict
Entrée :
{ "text": "I love FastAPI and Hugging Face!" }Sortie :
{
"label": "positive",
"confidence": 0.9998,
"model": "distilbert-base-uncased-finetuned-sst-2-english"
}L’application expose aussi une page HTML minimaliste :
👉 http://localhost:8000/static/index.html
- Ajouter un modèle multilingue (
nlptown/bert-base-multilingual-uncased-sentiment) - Ajouter une UI Gradio pour la démo
- Intégrer des tests unitaires
Ce projet est sous licence MIT.
Le modèle appartient à Hugging Face et ses contributeurs.