Spaces:
Sleeping
Sleeping
File size: 2,455 Bytes
55d0d9e | 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 | from fastapi import APIRouter, HTTPException, Form, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from app.schemas.schemas import EnrichedCode, Code, TableOfContents, AgencyStandards, CommitteeDesignation, IndexTerm, IndexReference
from app.database.db import get_async_session
import logging
import app.services.codes as code_service
from app.services.enrichment import enrichment_service
log = logging.getLogger(__name__)
router = APIRouter(prefix="/api", tags=["codes"])
code_service = code_service.CodeService()
@router.get("/codes")
async def root(
limit: int = 10,
session: AsyncSession = Depends(get_async_session)
):
codes = await code_service.get_codes(session, limit)
return [Code.model_validate(code) for code in codes]
@router.get("/code/{code}")
async def get_code(
code: str,
session: AsyncSession = Depends(get_async_session)
):
code_row, chapter_info = await code_service.get_code(session, code)
if not code_row:
raise HTTPException(status_code=404, detail="Code not found")
return {
"chapter_metadata": {
"title": chapter_info.title if chapter_info else "Appendix/Other",
"description": chapter_info.description if chapter_info else ""
},
"code": Code.model_validate(code_row)
}
@router.get("/get-codes-by-chapter/{chapter}")
async def get_codes_by_chapter(
chapter: str,
session: AsyncSession = Depends(get_async_session)
):
codes = await code_service.get_codes_by_chapter(session, chapter)
return [Code.model_validate(code) for code in codes]
@router.get("/codes-full-context/{code}")
async def get_full_code_context(
code: str,
session: AsyncSession = Depends(get_async_session)
):
code_row, chapter_info, standards, committee_designations, index_terms = await enrichment_service.enrich_code(session, code)
standards_list = []
for standard in standards:
standards_list.append({
"agency": standard.agency,
"standard_id": standard.standard_id,
"definition": standard.definition,
})
return EnrichedCode.model_validate({
"code": code_row,
"chapter_info": chapter_info,
"standards": standards_list,
"committee_designations": [CommitteeDesignation.model_validate(row) for row in committee_designations],
"index_terms": [IndexReference.model_validate(row) for row in index_terms]
})
|