Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,16 +1,12 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
App pour Hugging Face Spaces.
|
| 4 |
|
| 5 |
-
Lance
|
| 6 |
"""
|
| 7 |
import sys
|
| 8 |
import os
|
| 9 |
import logging
|
| 10 |
-
import subprocess
|
| 11 |
-
import time
|
| 12 |
-
import signal
|
| 13 |
-
from threading import Thread
|
| 14 |
|
| 15 |
# Configurer le logging avant toute chose
|
| 16 |
logging.basicConfig(
|
|
@@ -26,116 +22,20 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
|
|
| 26 |
|
| 27 |
from src.config import get_settings # noqa: E402
|
| 28 |
|
| 29 |
-
|
| 30 |
-
# Variables globales pour les processus
|
| 31 |
-
fastapi_process = None
|
| 32 |
-
gradio_thread = None
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def start_fastapi():
|
| 36 |
-
"""Lance le serveur FastAPI en subprocess."""
|
| 37 |
-
global fastapi_process
|
| 38 |
-
logger.info("🚀 Démarrage de FastAPI sur port 8000...")
|
| 39 |
-
|
| 40 |
-
try:
|
| 41 |
-
fastapi_process = subprocess.Popen(
|
| 42 |
-
[
|
| 43 |
-
sys.executable,
|
| 44 |
-
"-m",
|
| 45 |
-
"uvicorn",
|
| 46 |
-
"api:app",
|
| 47 |
-
"--host",
|
| 48 |
-
"0.0.0.0",
|
| 49 |
-
"--port",
|
| 50 |
-
"8000",
|
| 51 |
-
],
|
| 52 |
-
stdout=subprocess.PIPE,
|
| 53 |
-
stderr=subprocess.STDOUT,
|
| 54 |
-
text=True,
|
| 55 |
-
bufsize=1,
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
# Logger la sortie de FastAPI
|
| 59 |
-
for line in iter(fastapi_process.stdout.readline, ""):
|
| 60 |
-
if line:
|
| 61 |
-
logger.info(f"[FastAPI] {line.rstrip()}")
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
logger.error(f"❌ Erreur démarrage FastAPI: {e}", exc_info=True)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def start_gradio():
|
| 68 |
-
"""Lance l'interface Gradio."""
|
| 69 |
-
logger.info("🎨 Démarrage de Gradio sur port 7860...")
|
| 70 |
-
try:
|
| 71 |
-
from src.gradio_ui import launch_standalone
|
| 72 |
-
|
| 73 |
-
launch_standalone()
|
| 74 |
-
except Exception as e:
|
| 75 |
-
logger.error(f"❌ Erreur démarrage Gradio: {e}", exc_info=True)
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
def cleanup(signum=None, frame=None):
|
| 79 |
-
"""Nettoie les processus avant de quitter."""
|
| 80 |
-
logger.info("🛑 Arrêt des services...")
|
| 81 |
-
|
| 82 |
-
if fastapi_process:
|
| 83 |
-
logger.info("Arrêt de FastAPI...")
|
| 84 |
-
fastapi_process.terminate()
|
| 85 |
-
try:
|
| 86 |
-
fastapi_process.wait(timeout=5)
|
| 87 |
-
except subprocess.TimeoutExpired:
|
| 88 |
-
logger.warning("FastAPI ne répond pas, forçage de l'arrêt...")
|
| 89 |
-
fastapi_process.kill()
|
| 90 |
-
|
| 91 |
-
logger.info("✅ Arrêt propre effectué")
|
| 92 |
-
sys.exit(0)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
if __name__ == "__main__":
|
| 96 |
try:
|
| 97 |
settings = get_settings()
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
signal.signal(signal.SIGTERM, cleanup)
|
| 102 |
-
|
| 103 |
-
logger.info("=" * 60)
|
| 104 |
-
logger.info("🚀 Démarrage de l'application complète")
|
| 105 |
-
logger.info(" - FastAPI sur http://0.0.0.0:8000")
|
| 106 |
-
logger.info(" - Gradio sur http://0.0.0.0:7860")
|
| 107 |
-
logger.info("=" * 60)
|
| 108 |
-
|
| 109 |
-
# Lancer FastAPI en thread séparé
|
| 110 |
-
fastapi_thread = Thread(target=start_fastapi, daemon=True)
|
| 111 |
-
fastapi_thread.start()
|
| 112 |
-
|
| 113 |
-
# Attendre que FastAPI démarre
|
| 114 |
-
logger.info("⏳ Attente du démarrage de FastAPI...")
|
| 115 |
-
time.sleep(5)
|
| 116 |
-
|
| 117 |
-
# Vérifier que FastAPI est démarré
|
| 118 |
-
import requests
|
| 119 |
-
|
| 120 |
-
for i in range(10):
|
| 121 |
-
try:
|
| 122 |
-
response = requests.get("http://localhost:8000/health", timeout=2)
|
| 123 |
-
if response.status_code == 200:
|
| 124 |
-
logger.info("✅ FastAPI démarré et opérationnel")
|
| 125 |
-
break
|
| 126 |
-
except Exception:
|
| 127 |
-
logger.info(f"⏳ Tentative {i + 1}/10 de connexion à FastAPI...")
|
| 128 |
-
time.sleep(2)
|
| 129 |
-
else:
|
| 130 |
-
logger.warning("⚠️ FastAPI ne répond pas, mais on continue...")
|
| 131 |
-
|
| 132 |
-
# Lancer Gradio (bloquant - dans le thread principal)
|
| 133 |
-
start_gradio()
|
| 134 |
|
|
|
|
| 135 |
except KeyboardInterrupt:
|
| 136 |
logger.info("⏹️ Application arrêtée par l'utilisateur")
|
| 137 |
-
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"❌ Erreur fatale: {e}", exc_info=True)
|
| 140 |
-
cleanup()
|
| 141 |
sys.exit(1)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
App Gradio pour Hugging Face Spaces.
|
| 4 |
|
| 5 |
+
Lance l'interface Gradio pour la prédiction de turnover.
|
| 6 |
"""
|
| 7 |
import sys
|
| 8 |
import os
|
| 9 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Configurer le logging avant toute chose
|
| 12 |
logging.basicConfig(
|
|
|
|
| 22 |
|
| 23 |
from src.config import get_settings # noqa: E402
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if __name__ == "__main__":
|
| 26 |
try:
|
| 27 |
settings = get_settings()
|
| 28 |
+
if not settings.GRADIO_ENABLED:
|
| 29 |
+
logger.info("Gradio désactivée (GRADIO_ENABLED=False) - arrêt.")
|
| 30 |
+
sys.exit(0)
|
| 31 |
|
| 32 |
+
logger.info("🚀 Démarrage de l'application Gradio...")
|
| 33 |
+
from src.gradio_ui import launch_standalone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
launch_standalone()
|
| 36 |
except KeyboardInterrupt:
|
| 37 |
logger.info("⏹️ Application arrêtée par l'utilisateur")
|
| 38 |
+
sys.exit(0)
|
| 39 |
except Exception as e:
|
| 40 |
logger.error(f"❌ Erreur fatale: {e}", exc_info=True)
|
|
|
|
| 41 |
sys.exit(1)
|