malek-messaoudii
commited on
Commit
Β·
d43ba60
1
Parent(s):
a47a956
Update main
Browse files- main.py +24 -20
- services/__init__.py +2 -2
main.py
CHANGED
|
@@ -3,20 +3,22 @@ from pathlib import Path
|
|
| 3 |
import logging
|
| 4 |
from contextlib import asynccontextmanager
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
logging.basicConfig(
|
| 7 |
level=logging.INFO,
|
| 8 |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 9 |
)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
app_dir = Path(__file__).parent
|
| 14 |
sys.path.insert(0, str(app_dir))
|
| 15 |
|
| 16 |
-
|
| 17 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 18 |
-
import uvicorn
|
| 19 |
-
|
| 20 |
from config import (
|
| 21 |
API_TITLE, API_DESCRIPTION, API_VERSION,
|
| 22 |
HUGGINGFACE_API_KEY, HUGGINGFACE_STANCE_MODEL_ID, HUGGINGFACE_LABEL_MODEL_ID,
|
|
@@ -25,6 +27,14 @@ from config import (
|
|
| 25 |
PRELOAD_MODELS_ON_STARTUP, LOAD_STANCE_MODEL, LOAD_KPA_MODEL
|
| 26 |
)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@asynccontextmanager
|
| 29 |
async def lifespan(app: FastAPI):
|
| 30 |
logger.info("="*60)
|
|
@@ -33,20 +43,18 @@ async def lifespan(app: FastAPI):
|
|
| 33 |
|
| 34 |
if PRELOAD_MODELS_ON_STARTUP:
|
| 35 |
|
| 36 |
-
#
|
| 37 |
if LOAD_STANCE_MODEL:
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
load_stance(HUGGINGFACE_STANCE_MODEL_ID, HUGGINGFACE_API_KEY)
|
| 41 |
logger.info("β Stance model loaded")
|
| 42 |
except Exception as e:
|
| 43 |
logger.error(f"β Failed loading stance model: {e}")
|
| 44 |
|
| 45 |
-
#
|
| 46 |
if LOAD_KPA_MODEL:
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
load_kpa(HUGGINGFACE_LABEL_MODEL_ID, HUGGINGFACE_API_KEY)
|
| 50 |
logger.info("β KPA model loaded")
|
| 51 |
except Exception as e:
|
| 52 |
logger.error(f"β Failed loading KPA model: {e}")
|
|
@@ -55,7 +63,7 @@ async def lifespan(app: FastAPI):
|
|
| 55 |
yield
|
| 56 |
logger.info("π Shutting down...")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
app = FastAPI(
|
| 60 |
title=API_TITLE,
|
| 61 |
description=API_DESCRIPTION,
|
|
@@ -63,7 +71,7 @@ app = FastAPI(
|
|
| 63 |
lifespan=lifespan
|
| 64 |
)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
app.add_middleware(
|
| 68 |
CORSMiddleware,
|
| 69 |
allow_origins=CORS_ORIGINS,
|
|
@@ -72,9 +80,7 @@ app.add_middleware(
|
|
| 72 |
allow_headers=CORS_HEADERS,
|
| 73 |
)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
# STT route (Groq Whisper)
|
| 78 |
try:
|
| 79 |
from routes.stt_routes import router as stt_router
|
| 80 |
app.include_router(stt_router, prefix="/api/v1/stt", tags=["Speech To Text"])
|
|
@@ -82,7 +88,6 @@ try:
|
|
| 82 |
except Exception as e:
|
| 83 |
logger.warning(f"β Failed loading STT route: {e}")
|
| 84 |
|
| 85 |
-
# TTS route (Groq PlayAI TTS)
|
| 86 |
try:
|
| 87 |
from routes.tts_routes import router as tts_router
|
| 88 |
app.include_router(tts_router, prefix="/api/v1/tts", tags=["Text To Speech"])
|
|
@@ -90,7 +95,6 @@ try:
|
|
| 90 |
except Exception as e:
|
| 91 |
logger.warning(f"β Failed loading TTS route: {e}")
|
| 92 |
|
| 93 |
-
# Main NLP system routes
|
| 94 |
try:
|
| 95 |
from routes import api_router
|
| 96 |
app.include_router(api_router)
|
|
@@ -98,8 +102,7 @@ try:
|
|
| 98 |
except Exception as e:
|
| 99 |
logger.warning(f"β Failed loading main API routes: {e}")
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
@app.get("/health")
|
| 104 |
async def health():
|
| 105 |
return {"status": "healthy", "service": "NLP Debater + Groq Voice"}
|
|
@@ -113,5 +116,6 @@ async def root():
|
|
| 113 |
"voice_tts": "/api/v1/tts"
|
| 114 |
}
|
| 115 |
|
|
|
|
| 116 |
if __name__ == "__main__":
|
| 117 |
uvicorn.run("main:app", host=HOST, port=PORT, reload=RELOAD)
|
|
|
|
| 3 |
import logging
|
| 4 |
from contextlib import asynccontextmanager
|
| 5 |
|
| 6 |
+
from fastapi import FastAPI
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
import uvicorn
|
| 9 |
+
|
| 10 |
+
# --- Logging ---
|
| 11 |
logging.basicConfig(
|
| 12 |
level=logging.INFO,
|
| 13 |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 14 |
)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
+
# --- Ajouter app dir au PATH ---
|
| 18 |
app_dir = Path(__file__).parent
|
| 19 |
sys.path.insert(0, str(app_dir))
|
| 20 |
|
| 21 |
+
# --- Config ---
|
|
|
|
|
|
|
|
|
|
| 22 |
from config import (
|
| 23 |
API_TITLE, API_DESCRIPTION, API_VERSION,
|
| 24 |
HUGGINGFACE_API_KEY, HUGGINGFACE_STANCE_MODEL_ID, HUGGINGFACE_LABEL_MODEL_ID,
|
|
|
|
| 27 |
PRELOAD_MODELS_ON_STARTUP, LOAD_STANCE_MODEL, LOAD_KPA_MODEL
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# --- Import des singletons de services ---
|
| 31 |
+
from services.stance_model_manager import stance_model_manager
|
| 32 |
+
from services.label_model_manager import kpa_model_manager
|
| 33 |
+
|
| 34 |
+
from services.stt_service import speech_to_text
|
| 35 |
+
from services.tts_service import text_to_speech
|
| 36 |
+
|
| 37 |
+
# --- Lifespan / startup API ---
|
| 38 |
@asynccontextmanager
|
| 39 |
async def lifespan(app: FastAPI):
|
| 40 |
logger.info("="*60)
|
|
|
|
| 43 |
|
| 44 |
if PRELOAD_MODELS_ON_STARTUP:
|
| 45 |
|
| 46 |
+
# Charger stance model
|
| 47 |
if LOAD_STANCE_MODEL:
|
| 48 |
try:
|
| 49 |
+
stance_model_manager.load_model(HUGGINGFACE_STANCE_MODEL_ID, HUGGINGFACE_API_KEY)
|
|
|
|
| 50 |
logger.info("β Stance model loaded")
|
| 51 |
except Exception as e:
|
| 52 |
logger.error(f"β Failed loading stance model: {e}")
|
| 53 |
|
| 54 |
+
# Charger KPA model
|
| 55 |
if LOAD_KPA_MODEL:
|
| 56 |
try:
|
| 57 |
+
kpa_model_manager.load_model(HUGGINGFACE_LABEL_MODEL_ID, HUGGINGFACE_API_KEY)
|
|
|
|
| 58 |
logger.info("β KPA model loaded")
|
| 59 |
except Exception as e:
|
| 60 |
logger.error(f"β Failed loading KPA model: {e}")
|
|
|
|
| 63 |
yield
|
| 64 |
logger.info("π Shutting down...")
|
| 65 |
|
| 66 |
+
# --- FastAPI app ---
|
| 67 |
app = FastAPI(
|
| 68 |
title=API_TITLE,
|
| 69 |
description=API_DESCRIPTION,
|
|
|
|
| 71 |
lifespan=lifespan
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# --- CORS ---
|
| 75 |
app.add_middleware(
|
| 76 |
CORSMiddleware,
|
| 77 |
allow_origins=CORS_ORIGINS,
|
|
|
|
| 80 |
allow_headers=CORS_HEADERS,
|
| 81 |
)
|
| 82 |
|
| 83 |
+
# --- Routes ---
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
from routes.stt_routes import router as stt_router
|
| 86 |
app.include_router(stt_router, prefix="/api/v1/stt", tags=["Speech To Text"])
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
logger.warning(f"β Failed loading STT route: {e}")
|
| 90 |
|
|
|
|
| 91 |
try:
|
| 92 |
from routes.tts_routes import router as tts_router
|
| 93 |
app.include_router(tts_router, prefix="/api/v1/tts", tags=["Text To Speech"])
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
logger.warning(f"β Failed loading TTS route: {e}")
|
| 97 |
|
|
|
|
| 98 |
try:
|
| 99 |
from routes import api_router
|
| 100 |
app.include_router(api_router)
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
logger.warning(f"β Failed loading main API routes: {e}")
|
| 104 |
|
| 105 |
+
# --- Basic routes ---
|
|
|
|
| 106 |
@app.get("/health")
|
| 107 |
async def health():
|
| 108 |
return {"status": "healthy", "service": "NLP Debater + Groq Voice"}
|
|
|
|
| 116 |
"voice_tts": "/api/v1/tts"
|
| 117 |
}
|
| 118 |
|
| 119 |
+
# --- Run server ---
|
| 120 |
if __name__ == "__main__":
|
| 121 |
uvicorn.run("main:app", host=HOST, port=PORT, reload=RELOAD)
|
services/__init__.py
CHANGED
|
@@ -5,7 +5,7 @@ from .label_model_manager import KpaModelManager, kpa_model_manager
|
|
| 5 |
|
| 6 |
# NEW imports
|
| 7 |
from .stt_service import speech_to_text
|
| 8 |
-
from .tts_service import
|
| 9 |
|
| 10 |
__all__ = [
|
| 11 |
"StanceModelManager",
|
|
@@ -15,5 +15,5 @@ __all__ = [
|
|
| 15 |
|
| 16 |
# NEW exports
|
| 17 |
"speech_to_text",
|
| 18 |
-
"
|
| 19 |
]
|
|
|
|
| 5 |
|
| 6 |
# NEW imports
|
| 7 |
from .stt_service import speech_to_text
|
| 8 |
+
from .tts_service import text_to_speech
|
| 9 |
|
| 10 |
__all__ = [
|
| 11 |
"StanceModelManager",
|
|
|
|
| 15 |
|
| 16 |
# NEW exports
|
| 17 |
"speech_to_text",
|
| 18 |
+
"text_to_speech",
|
| 19 |
]
|