File size: 5,691 Bytes
01543cd
333cede
 
 
 
 
 
 
 
8555bb7
333cede
 
8555bb7
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01543cd
333cede
01543cd
 
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01543cd
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01543cd
333cede
 
01543cd
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
8555bb7
 
 
01543cd
8555bb7
 
01543cd
8555bb7
 
 
 
 
333cede
 
 
 
01543cd
333cede
01543cd
 
333cede
 
01543cd
333cede
 
01543cd
333cede
 
01543cd
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01543cd
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01543cd
333cede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""FastAPI application that answers FAQ questions using multilingual embeddings."""
import logging
import sys
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, Response
from fastapi.middleware.cors import CORSMiddleware

from config import FAQ_JSON_PATH, CROSS_ENCODER_MODEL_NAME, MODEL_NAME
from embeddings import EmbeddingManager
from faq_loader import FAQEntry, load_faq_data
from models import AnswerOutput, ConfigResponse, HealthResponse, QuestionInput

# Configuration du logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

# Variables globales
embedding_manager: EmbeddingManager | None = None
faq_entries: list[FAQEntry] = []


@asynccontextmanager
async def lifespan(app: FastAPI):
    """

    Manage the FastAPI application lifecycle.



    On startup, load the FAQ data and embeddings into ChromaDB.

    On shutdown, clean up the resources.

    """
    # --- Startup ---
    logger.info("🚀 Démarrage de l'application...")

    global embedding_manager, faq_entries

    try:
        # Initialiser le gestionnaire d'embeddings
        embedding_manager = EmbeddingManager()

        # Charger les données FAQ
        logger.info("📚 Chargement des données FAQ...")
        faq_entries = load_faq_data(FAQ_JSON_PATH)

        # Peupler ChromaDB
        embedding_manager.populate_collection(faq_entries)

        logger.info(f"✓ Application prête - {embedding_manager.get_collection_size()} FAQs indexées")

    except Exception as e:
        logger.error(f"❌ Erreur au démarrage: {e}")
        # En cas d'erreur fatale au démarrage, quitter le process pour éviter
        # que la plateforme (ex: Hugging Face Spaces) reste en 'starting'.
        sys.exit(1)

    yield

    # --- Shutdown ---
    logger.info("🛑 Arrêt de l'application...")
    embedding_manager = None
    faq_entries = []


# Créer l'application FastAPI
app = FastAPI(
    title="API FAQ Search",
    description="API for answering questions through similarity search over a FAQ dataset",
    version="1.0.0",
    lifespan=lifespan,
)

# Configuration CORS (adapter selon vos besoins)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/health", response_model=HealthResponse)
async def health_check() -> HealthResponse:
    """

    Check the health status of the service.



    Returns:

        A HealthResponse containing the status and indexed FAQ count.

    """
    if embedding_manager is None:
        raise HTTPException(
            status_code=503,
            detail="Service non initialisé - les embeddings ne sont pas chargés",
        )

    faq_count: int = embedding_manager.get_collection_size()

    return HealthResponse(
        status="ok",
        faq_count=faq_count,
    )

@app.get("/config", response_model=ConfigResponse)
async def get_config() -> ConfigResponse:
    """

    Retrieve the application's configuration.



    Returns:

        A ConfigResponse with the model names in use.

    """
    return ConfigResponse(
        cross_encoder_model_name=CROSS_ENCODER_MODEL_NAME,
        embeddings_model_name=MODEL_NAME,
    )

@app.post("/ask", response_model=AnswerOutput)
async def ask_question(input_data: QuestionInput) -> AnswerOutput:
    """

    Handle the main FAQ question endpoint.



    Receives a free-text question, generates a multilingual embedding,

    and returns the most similar answer with a confidence score.



    Args:

        input_data: The request payload containing the question.



    Returns:

        An AnswerOutput with the answer, formulation, theme, and similarity score.



    Raises:

        HTTPException: If the service is not initialized or no match meets the threshold.

    """
    if embedding_manager is None:
        raise HTTPException(
            status_code=503,
            detail="Service non initialisé - les embeddings ne sont pas chargés",
        )

    question: str = input_data.question
    logger.info(f"📝 Question reçue: {question}")

    # Rechercher la FAQ similaire (le seuil peut être fourni dans `input_data.threshold`)
    result = embedding_manager.search_similar_faq(input_data)

    # `search_similar_faq` renvoie déjà un `AnswerOutput` (avec `confidence=False` si pas de match)
    logger.info(f"✓ Réponse préparée pour: {question}")
    return result


@app.get("/list")
async def list_faq_entries() -> Response:
    """Return all FAQ entries as a Markdown string."""
    if not faq_entries:
        raise HTTPException(
            status_code=503,
            detail="Service non initialisé - les FAQ ne sont pas chargées",
        )

    markdown_output: str = "\n\n".join(
        f"**{entry.formulation}**\n{entry.response}" for entry in faq_entries
    )

    return Response(content=markdown_output, media_type="text/markdown")


@app.get("/", response_class=HTMLResponse)
async def root() -> str:
    """Serve the API landing page."""
    return """

    <html>

        <head><title>Page d'accueil</title></head>

        <body>

            <h1>Welcome on the FAQ API !</h1>

        </body>

    </html>

    """

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(
        "app:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info",
    )