Spaces:
Sleeping
Sleeping
dylanglenister
commited on
Commit
·
9b9a7e2
1
Parent(s):
fdede90
Improving routing code.
Browse filesNo functional change, just using APIRouter more intelligently.
- src/api/routes/audio.py +1 -1
- src/api/routes/chat.py +1 -1
- src/api/routes/doctors.py +15 -15
- src/api/routes/patients.py +37 -26
- src/api/routes/session.py +6 -17
- src/api/routes/static.py +1 -1
- src/api/routes/user.py +3 -3
src/api/routes/audio.py
CHANGED
|
@@ -9,7 +9,7 @@ from src.services.audio_transcription import (get_supported_formats,
|
|
| 9 |
validate_audio_format)
|
| 10 |
from src.utils.logger import logger
|
| 11 |
|
| 12 |
-
router = APIRouter(prefix="/audio", tags=["
|
| 13 |
|
| 14 |
@router.post("/transcribe")
|
| 15 |
async def transcribe_audio(
|
|
|
|
| 9 |
validate_audio_format)
|
| 10 |
from src.utils.logger import logger
|
| 11 |
|
| 12 |
+
router = APIRouter(prefix="/audio", tags=["Audio"])
|
| 13 |
|
| 14 |
@router.post("/transcribe")
|
| 15 |
async def transcribe_audio(
|
src/api/routes/chat.py
CHANGED
|
@@ -12,7 +12,7 @@ from src.services.medical_response import generate_medical_response
|
|
| 12 |
from src.services.summariser import summarise_title_with_nvidia
|
| 13 |
from src.utils.logger import logger
|
| 14 |
|
| 15 |
-
router = APIRouter()
|
| 16 |
|
| 17 |
@router.post("/chat", response_model=ChatResponse)
|
| 18 |
async def chat_endpoint(
|
|
|
|
| 12 |
from src.services.summariser import summarise_title_with_nvidia
|
| 13 |
from src.utils.logger import logger
|
| 14 |
|
| 15 |
+
router = APIRouter(tags=["Chat"])
|
| 16 |
|
| 17 |
@router.post("/chat", response_model=ChatResponse)
|
| 18 |
async def chat_endpoint(
|
src/api/routes/doctors.py
CHANGED
|
@@ -7,9 +7,20 @@ from src.data.repositories.account import (create_doctor, get_all_doctors,
|
|
| 7 |
from src.models.user import DoctorCreateRequest
|
| 8 |
from src.utils.logger import logger
|
| 9 |
|
| 10 |
-
router = APIRouter()
|
| 11 |
|
| 12 |
-
@router.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
async def create_doctor_profile(req: DoctorCreateRequest):
|
| 14 |
try:
|
| 15 |
logger().info(f"POST /doctors name={req.name}")
|
|
@@ -25,7 +36,7 @@ async def create_doctor_profile(req: DoctorCreateRequest):
|
|
| 25 |
logger().error(f"Error creating doctor: {e}")
|
| 26 |
raise HTTPException(status_code=500, detail=str(e))
|
| 27 |
|
| 28 |
-
@router.get("/
|
| 29 |
async def get_doctor(doctor_name: str):
|
| 30 |
try:
|
| 31 |
logger().info(f"GET /doctors/{doctor_name}")
|
|
@@ -39,7 +50,7 @@ async def get_doctor(doctor_name: str):
|
|
| 39 |
logger().error(f"Error getting doctor: {e}")
|
| 40 |
raise HTTPException(status_code=500, detail=str(e))
|
| 41 |
|
| 42 |
-
@router.get("/
|
| 43 |
async def search_doctors_route(q: str, limit: int = 10):
|
| 44 |
try:
|
| 45 |
logger().info(f"GET /doctors/search q='{q}' limit={limit}")
|
|
@@ -49,14 +60,3 @@ async def search_doctors_route(q: str, limit: int = 10):
|
|
| 49 |
except Exception as e:
|
| 50 |
logger().error(f"Error searching doctors: {e}")
|
| 51 |
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
-
|
| 53 |
-
@router.get("/doctors")
|
| 54 |
-
async def get_all_doctors_route(limit: int = 50):
|
| 55 |
-
try:
|
| 56 |
-
logger().info(f"GET /doctors limit={limit}")
|
| 57 |
-
results = get_all_doctors(limit=limit)
|
| 58 |
-
logger().info(f"Retrieved {len(results)} doctors")
|
| 59 |
-
return {"results": results}
|
| 60 |
-
except Exception as e:
|
| 61 |
-
logger().error(f"Error getting all doctors: {e}")
|
| 62 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 7 |
from src.models.user import DoctorCreateRequest
|
| 8 |
from src.utils.logger import logger
|
| 9 |
|
| 10 |
+
router = APIRouter(prefix="/doctors", tags=["Doctors"])
|
| 11 |
|
| 12 |
+
@router.get("/")
|
| 13 |
+
async def get_all_doctors_route(limit: int = 50):
|
| 14 |
+
try:
|
| 15 |
+
logger().info(f"GET /doctors limit={limit}")
|
| 16 |
+
results = get_all_doctors(limit=limit)
|
| 17 |
+
logger().info(f"Retrieved {len(results)} doctors")
|
| 18 |
+
return {"results": results}
|
| 19 |
+
except Exception as e:
|
| 20 |
+
logger().error(f"Error getting all doctors: {e}")
|
| 21 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 22 |
+
|
| 23 |
+
@router.post("/")
|
| 24 |
async def create_doctor_profile(req: DoctorCreateRequest):
|
| 25 |
try:
|
| 26 |
logger().info(f"POST /doctors name={req.name}")
|
|
|
|
| 36 |
logger().error(f"Error creating doctor: {e}")
|
| 37 |
raise HTTPException(status_code=500, detail=str(e))
|
| 38 |
|
| 39 |
+
@router.get("/{doctor_name}")
|
| 40 |
async def get_doctor(doctor_name: str):
|
| 41 |
try:
|
| 42 |
logger().info(f"GET /doctors/{doctor_name}")
|
|
|
|
| 50 |
logger().error(f"Error getting doctor: {e}")
|
| 51 |
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
|
| 53 |
+
@router.get("/search")
|
| 54 |
async def search_doctors_route(q: str, limit: int = 10):
|
| 55 |
try:
|
| 56 |
logger().info(f"GET /doctors/search q='{q}' limit={limit}")
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
logger().error(f"Error searching doctors: {e}")
|
| 62 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/api/routes/patients.py
CHANGED
|
@@ -5,12 +5,35 @@ from fastapi import APIRouter, HTTPException
|
|
| 5 |
from src.data.repositories.patient import (create_patient, get_patient_by_id,
|
| 6 |
search_patients,
|
| 7 |
update_patient_profile)
|
|
|
|
| 8 |
from src.models.user import PatientCreateRequest, PatientUpdateRequest
|
| 9 |
from src.utils.logger import logger
|
| 10 |
|
| 11 |
-
router = APIRouter()
|
| 12 |
|
| 13 |
-
@router.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
async def search_patients_route(q: str, limit: int = 20):
|
| 15 |
try:
|
| 16 |
logger().info(f"GET /patients/search q='{q}' limit={limit}")
|
|
@@ -21,7 +44,7 @@ async def search_patients_route(q: str, limit: int = 20):
|
|
| 21 |
logger().error(f"Error searching patients: {e}")
|
| 22 |
raise HTTPException(status_code=500, detail=str(e))
|
| 23 |
|
| 24 |
-
@router.get("/
|
| 25 |
async def get_patient(patient_id: str):
|
| 26 |
try:
|
| 27 |
logger().info(f"GET /patients/{patient_id}")
|
|
@@ -36,29 +59,7 @@ async def get_patient(patient_id: str):
|
|
| 36 |
logger().error(f"Error getting patient: {e}")
|
| 37 |
raise HTTPException(status_code=500, detail=str(e))
|
| 38 |
|
| 39 |
-
@router.
|
| 40 |
-
async def create_patient_profile(req: PatientCreateRequest):
|
| 41 |
-
try:
|
| 42 |
-
logger().info(f"POST /patients name={req.name}")
|
| 43 |
-
patient = create_patient(
|
| 44 |
-
name=req.name,
|
| 45 |
-
age=req.age,
|
| 46 |
-
sex=req.sex,
|
| 47 |
-
address=req.address,
|
| 48 |
-
phone=req.phone,
|
| 49 |
-
email=req.email,
|
| 50 |
-
medications=req.medications,
|
| 51 |
-
past_assessment_summary=req.past_assessment_summary,
|
| 52 |
-
assigned_doctor_id=req.assigned_doctor_id
|
| 53 |
-
)
|
| 54 |
-
patient["_id"] = str(patient.get("_id")) if patient.get("_id") else None
|
| 55 |
-
logger().info(f"Created patient {patient.get('name')} id={patient.get('patient_id')}")
|
| 56 |
-
return patient
|
| 57 |
-
except Exception as e:
|
| 58 |
-
logger().error(f"Error creating patient: {e}")
|
| 59 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 60 |
-
|
| 61 |
-
@router.patch("/patients/{patient_id}")
|
| 62 |
async def update_patient(patient_id: str, req: PatientUpdateRequest):
|
| 63 |
try:
|
| 64 |
payload = {k: v for k, v in req.model_dump().items() if v is not None}
|
|
@@ -70,3 +71,13 @@ async def update_patient(patient_id: str, req: PatientUpdateRequest):
|
|
| 70 |
except Exception as e:
|
| 71 |
logger().error(f"Error updating patient: {e}")
|
| 72 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from src.data.repositories.patient import (create_patient, get_patient_by_id,
|
| 6 |
search_patients,
|
| 7 |
update_patient_profile)
|
| 8 |
+
from src.data.repositories.session import list_patient_sessions
|
| 9 |
from src.models.user import PatientCreateRequest, PatientUpdateRequest
|
| 10 |
from src.utils.logger import logger
|
| 11 |
|
| 12 |
+
router = APIRouter(prefix="/patients", tags=["Patients"])
|
| 13 |
|
| 14 |
+
@router.post("/")
|
| 15 |
+
async def create_patient_profile(req: PatientCreateRequest):
|
| 16 |
+
try:
|
| 17 |
+
logger().info(f"POST /patients name={req.name}")
|
| 18 |
+
patient = create_patient(
|
| 19 |
+
name=req.name,
|
| 20 |
+
age=req.age,
|
| 21 |
+
sex=req.sex,
|
| 22 |
+
address=req.address,
|
| 23 |
+
phone=req.phone,
|
| 24 |
+
email=req.email,
|
| 25 |
+
medications=req.medications,
|
| 26 |
+
past_assessment_summary=req.past_assessment_summary,
|
| 27 |
+
assigned_doctor_id=req.assigned_doctor_id
|
| 28 |
+
)
|
| 29 |
+
patient["_id"] = str(patient.get("_id")) if patient.get("_id") else None
|
| 30 |
+
logger().info(f"Created patient {patient.get('name')} id={patient.get('patient_id')}")
|
| 31 |
+
return patient
|
| 32 |
+
except Exception as e:
|
| 33 |
+
logger().error(f"Error creating patient: {e}")
|
| 34 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 35 |
+
|
| 36 |
+
@router.get("/search")
|
| 37 |
async def search_patients_route(q: str, limit: int = 20):
|
| 38 |
try:
|
| 39 |
logger().info(f"GET /patients/search q='{q}' limit={limit}")
|
|
|
|
| 44 |
logger().error(f"Error searching patients: {e}")
|
| 45 |
raise HTTPException(status_code=500, detail=str(e))
|
| 46 |
|
| 47 |
+
@router.get("/{patient_id}")
|
| 48 |
async def get_patient(patient_id: str):
|
| 49 |
try:
|
| 50 |
logger().info(f"GET /patients/{patient_id}")
|
|
|
|
| 59 |
logger().error(f"Error getting patient: {e}")
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
|
| 62 |
+
@router.patch("/{patient_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
async def update_patient(patient_id: str, req: PatientUpdateRequest):
|
| 64 |
try:
|
| 65 |
payload = {k: v for k, v in req.model_dump().items() if v is not None}
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
logger().error(f"Error updating patient: {e}")
|
| 73 |
raise HTTPException(status_code=500, detail=str(e))
|
| 74 |
+
|
| 75 |
+
@router.get("/{patient_id}/sessions")
|
| 76 |
+
async def list_sessions_for_patient(patient_id: str):
|
| 77 |
+
"""List sessions for a patient from Mongo"""
|
| 78 |
+
try:
|
| 79 |
+
logger().info(f"GET /patients/{patient_id}/sessions")
|
| 80 |
+
return {"sessions": list_patient_sessions(patient_id)}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logger().error(f"Error listing sessions: {e}")
|
| 83 |
+
raise HTTPException(status_code=500, detail=str(e))
|
src/api/routes/session.py
CHANGED
|
@@ -8,14 +8,13 @@ from src.core.state import MedicalState, get_state
|
|
| 8 |
from src.data.repositories.message import list_session_messages
|
| 9 |
from src.data.repositories.session import (delete_session,
|
| 10 |
delete_session_messages,
|
| 11 |
-
ensure_session
|
| 12 |
-
list_patient_sessions)
|
| 13 |
from src.models.chat import SessionRequest
|
| 14 |
from src.utils.logger import logger
|
| 15 |
|
| 16 |
-
router = APIRouter()
|
| 17 |
|
| 18 |
-
@router.post("/
|
| 19 |
async def create_chat_session(
|
| 20 |
request: SessionRequest,
|
| 21 |
state: MedicalState = Depends(get_state)
|
|
@@ -31,7 +30,7 @@ async def create_chat_session(
|
|
| 31 |
logger().error(f"Error creating session: {e}")
|
| 32 |
raise HTTPException(status_code=500, detail=str(e))
|
| 33 |
|
| 34 |
-
@router.get("/
|
| 35 |
async def get_chat_session(
|
| 36 |
session_id: str,
|
| 37 |
state: MedicalState = Depends(get_state)
|
|
@@ -60,17 +59,7 @@ async def get_chat_session(
|
|
| 60 |
logger().error(f"Error getting session: {e}")
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
|
| 63 |
-
@router.get("/
|
| 64 |
-
async def list_sessions_for_patient(patient_id: str):
|
| 65 |
-
"""List sessions for a patient from Mongo"""
|
| 66 |
-
try:
|
| 67 |
-
logger().info(f"GET /patients/{patient_id}/sessions")
|
| 68 |
-
return {"sessions": list_patient_sessions(patient_id)}
|
| 69 |
-
except Exception as e:
|
| 70 |
-
logger().error(f"Error listing sessions: {e}")
|
| 71 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 72 |
-
|
| 73 |
-
@router.get("/sessions/{session_id}/messages")
|
| 74 |
async def list_messages_for_session(session_id: str, patient_id: str, limit: int | None = None):
|
| 75 |
"""List messages for a session from Mongo, verified to belong to the patient"""
|
| 76 |
try:
|
|
@@ -86,7 +75,7 @@ async def list_messages_for_session(session_id: str, patient_id: str, limit: int
|
|
| 86 |
logger().error(f"Error listing messages: {e}")
|
| 87 |
raise HTTPException(status_code=500, detail=str(e))
|
| 88 |
|
| 89 |
-
@router.delete("/
|
| 90 |
async def delete_chat_session(
|
| 91 |
session_id: str,
|
| 92 |
state: MedicalState = Depends(get_state)
|
|
|
|
| 8 |
from src.data.repositories.message import list_session_messages
|
| 9 |
from src.data.repositories.session import (delete_session,
|
| 10 |
delete_session_messages,
|
| 11 |
+
ensure_session)
|
|
|
|
| 12 |
from src.models.chat import SessionRequest
|
| 13 |
from src.utils.logger import logger
|
| 14 |
|
| 15 |
+
router = APIRouter(prefix="/sessions", tags=["Session"])
|
| 16 |
|
| 17 |
+
@router.post("/")
|
| 18 |
async def create_chat_session(
|
| 19 |
request: SessionRequest,
|
| 20 |
state: MedicalState = Depends(get_state)
|
|
|
|
| 30 |
logger().error(f"Error creating session: {e}")
|
| 31 |
raise HTTPException(status_code=500, detail=str(e))
|
| 32 |
|
| 33 |
+
@router.get("/{session_id}")
|
| 34 |
async def get_chat_session(
|
| 35 |
session_id: str,
|
| 36 |
state: MedicalState = Depends(get_state)
|
|
|
|
| 59 |
logger().error(f"Error getting session: {e}")
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
|
| 62 |
+
@router.get("/{session_id}/messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
async def list_messages_for_session(session_id: str, patient_id: str, limit: int | None = None):
|
| 64 |
"""List messages for a session from Mongo, verified to belong to the patient"""
|
| 65 |
try:
|
|
|
|
| 75 |
logger().error(f"Error listing messages: {e}")
|
| 76 |
raise HTTPException(status_code=500, detail=str(e))
|
| 77 |
|
| 78 |
+
@router.delete("/{session_id}")
|
| 79 |
async def delete_chat_session(
|
| 80 |
session_id: str,
|
| 81 |
state: MedicalState = Depends(get_state)
|
src/api/routes/static.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
from fastapi import APIRouter, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
|
| 6 |
-
router = APIRouter()
|
| 7 |
|
| 8 |
@router.get("/", response_class=HTMLResponse)
|
| 9 |
async def get_medical_chatbot():
|
|
|
|
| 3 |
from fastapi import APIRouter, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
|
| 6 |
+
router = APIRouter(tags=["Static"])
|
| 7 |
|
| 8 |
@router.get("/", response_class=HTMLResponse)
|
| 9 |
async def get_medical_chatbot():
|
src/api/routes/user.py
CHANGED
|
@@ -7,9 +7,9 @@ from src.data.repositories.account import create_account
|
|
| 7 |
from src.models.user import UserProfileRequest
|
| 8 |
from src.utils.logger import logger
|
| 9 |
|
| 10 |
-
router = APIRouter()
|
| 11 |
|
| 12 |
-
@router.post("/
|
| 13 |
async def create_user_profile(
|
| 14 |
request: UserProfileRequest,
|
| 15 |
state: MedicalState = Depends(get_state)
|
|
@@ -38,7 +38,7 @@ async def create_user_profile(
|
|
| 38 |
logger().error(f"Error creating user profile: {e}")
|
| 39 |
raise HTTPException(status_code=500, detail=str(e))
|
| 40 |
|
| 41 |
-
@router.get("/
|
| 42 |
async def get_user_profile(
|
| 43 |
user_id: str,
|
| 44 |
state: MedicalState = Depends(get_state)
|
|
|
|
| 7 |
from src.models.user import UserProfileRequest
|
| 8 |
from src.utils.logger import logger
|
| 9 |
|
| 10 |
+
router = APIRouter(prefix="/users", tags=["Users"])
|
| 11 |
|
| 12 |
+
@router.post("/")
|
| 13 |
async def create_user_profile(
|
| 14 |
request: UserProfileRequest,
|
| 15 |
state: MedicalState = Depends(get_state)
|
|
|
|
| 38 |
logger().error(f"Error creating user profile: {e}")
|
| 39 |
raise HTTPException(status_code=500, detail=str(e))
|
| 40 |
|
| 41 |
+
@router.get("/{user_id}")
|
| 42 |
async def get_user_profile(
|
| 43 |
user_id: str,
|
| 44 |
state: MedicalState = Depends(get_state)
|