# -*- coding: utf-8 -*- import os import sys import json import logging import threading import time import csv import io from datetime import datetime from pathlib import Path from typing import Dict, Any, List, Optional from pydantic import BaseModel from fastapi import FastAPI, UploadFile, File, Form, HTTPException, BackgroundTasks, Depends, Header, Query, Request from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, StreamingResponse from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from pathlib import Path from dotenv import load_dotenv # Ensure root and backend .env are loaded first before any service initialization BASE_DIR = Path(__file__).resolve().parent PROJECT_ROOT = BASE_DIR.parent for env_candidate in [PROJECT_ROOT / ".env", BASE_DIR / ".env", BASE_DIR / "config" / ".env.example"]: if env_candidate.exists(): load_dotenv(dotenv_path=str(env_candidate), override=False) from orchestrator.orchestrator import GLOBAL_ORCHESTRATOR, SehatOrchestrator from session_store.session_store import GLOBAL_SESSION_STORE, SehatSessionState from session_store.patient_registry import GLOBAL_PATIENT_REGISTRY from emergency.emergency_detector import GLOBAL_EMERGENCY_DETECTOR, EmergencyDetector from doctor_delivery.doctor_service import GLOBAL_DOCTOR_DELIVERY, DoctorDeliveryService from audit.audit_logger import GLOBAL_AUDIT_LOGGER from audio_pipeline.cura_clinical_flow import GLOBAL_CURA_SESSIONS from database.db_manager import GLOBAL_DB from fastapi import WebSocket, WebSocketDisconnect # Report generation & delivery (graceful — won't crash if reportlab/twilio missing) try: from report.pdf_generator import GLOBAL_PDF_GENERATOR from report.whatsapp_service import GLOBAL_WHATSAPP_SERVICE REPORTS_ENABLED = True except Exception as _report_ex: REPORTS_ENABLED = False GLOBAL_PDF_GENERATOR = None GLOBAL_WHATSAPP_SERVICE = None logger_startup = logging.getLogger("sehat_app") logger_startup.warning(f"Report module load warning: {_report_ex}") EMERGENCY_ALERTS_FEED = GLOBAL_DOCTOR_DELIVERY.emergency_feed def load_config(config_path: str = "config.yaml") -> Dict[str, Any]: for candidate in [config_path, "backend/config.yaml", "../config.yaml", str(BASE_DIR / "config.yaml")]: if os.path.exists(candidate): try: import yaml with open(candidate, "r", encoding="utf-8") as f: return yaml.safe_load(f) or {} except Exception: pass return {} logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s") logger = logging.getLogger("sehat_app") app = FastAPI( title="Diabetes Dost — AI Voice Pre-Screener for Diabetes", description="Context-aware diabetes pre-screening voice assistant in Hindi speech with dynamic question selection, independent emergency detection, and clinician triage summaries.", version="3.0.0" ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Resolve static directory (support both root/static and backend/static) STATIC_DIR = PROJECT_ROOT / "static" if not STATIC_DIR.exists(): STATIC_DIR = BASE_DIR / "static" STATIC_DIR.mkdir(parents=True, exist_ok=True) app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static") # Resolve template paths TEMPLATES_DIR = BASE_DIR / "templates" INDEX_HTML_PATH = TEMPLATES_DIR / "index.html" ADMIN_HTML_PATH = TEMPLATES_DIR / "admin.html" @app.get("/favicon.ico", include_in_schema=False) async def favicon_endpoint(): fav_candidate = STATIC_DIR / "dmh_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "favicon.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "chellaram_hospital_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "chellaram_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "kem_hospital_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "kem_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "ruby_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") fav_candidate = STATIC_DIR / "diabetes_dost_logo.png" if fav_candidate.exists(): return FileResponse(fav_candidate, media_type="image/png") return FileResponse(STATIC_DIR / "favicon.ico") training_state = { "is_training": False, "status": "idle", "current_epoch": 0, "total_epochs": 0, "best_wer": None, "latest_metrics": {}, "error": None, "logs": [] } class TranscriptUpdateRequest(BaseModel): id: str corrected_transcript: str review_status: str verified_by_human: bool = True class PipelineConfigUpdate(BaseModel): sarvam_api_key: Optional[str] = None language_code: Optional[str] = None batch_size: Optional[int] = None num_epochs: Optional[int] = None learning_rate: Optional[float] = None for folder in [ "datasets/raw_audio", "datasets/processed_audio", "datasets/transcripts", "datasets/manifests", "datasets/reviewed", "outputs/checkpoints", "outputs/logs", "outputs/reports", "outputs/reports/sessions" ]: Path(folder).mkdir(parents=True, exist_ok=True) # ========================================== # GENERAL & AUDIO API ENDPOINTS # ========================================== @app.get("/api/health") async def health_check(): try: import torch cuda_ok = torch.cuda.is_available() if torch is not None else False gpu_name = torch.cuda.get_device_name(0) if cuda_ok else "CPU Mode" except Exception: cuda_ok = False gpu_name = "CPU Mode" return { "status": "online", "app_name": "Dinanath Mangeshkar Hospital — Diabetes Dost", "hospital_name": "Dinanath Mangeshkar Hospital", "motto": "Diabetes Care & Multispeciality", "bot_name": "Diabetes Dost", "language": "hi-IN", "voice_gender": "Male", "cuda_available": cuda_ok, "gpu_name": gpu_name, "active_sessions": len(GLOBAL_SESSION_STORE.sessions), "sarvam_configured": bool(os.getenv("SARVAM_API_KEY") and os.getenv("SARVAM_API_KEY") != "your_sarvam_api_key_here") } @app.get("/api/config") async def get_config(): cfg = load_config() key = os.getenv("SARVAM_API_KEY", "") masked_key = f"{key[:4]}...{key[-4:]}" if len(key) > 8 else ("Configured" if key else "Not Set") return { "config": cfg, "sarvam_api_key_status": masked_key } @app.get("/api/audio/stream/{filename}") async def stream_audio(filename: str): search_dirs = [ BASE_DIR / "outputs" / "reports", PROJECT_ROOT / "outputs" / "reports", BASE_DIR / "datasets" / "processed_audio", BASE_DIR / "datasets" / "raw_audio", Path("outputs/reports"), Path("datasets/processed_audio"), Path("datasets/raw_audio") ] for folder in search_dirs: target = folder / filename if target.exists() and target.is_file(): mime = "audio/mpeg" try: with open(target, "rb") as f: header = f.read(12) if header.startswith(b"RIFF") and b"WAVE" in header: mime = "audio/wav" elif header.startswith(b"\xff\xfb") or header.startswith(b"\xff\xf3") or header.startswith(b"\xff\xf2") or header.startswith(b"ID3"): mime = "audio/mpeg" elif filename.lower().endswith(".wav"): mime = "audio/wav" except Exception: mime = "audio/mpeg" if filename.lower().endswith(".mp3") else "audio/wav" return FileResponse( str(target), media_type=mime, headers={ "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache", "Expires": "0", "Accept-Ranges": "bytes" } ) raise HTTPException(status_code=404, detail=f"Audio file '{filename}' not found") # ========================================== # SEHAT AI VOICE PRE-SCREENER API ENDPOINTS # ========================================== @app.post("/api/sehat/session/start") @app.post("/api/cura/session/start") async def start_sehat_voice_session(session_id: Optional[str] = Form(None)): res = GLOBAL_ORCHESTRATOR.start_session(session_id=session_id) return JSONResponse(content=res) @app.post("/api/sehat/session/start_followup") @app.post("/api/cura/session/start_followup") async def start_sehat_followup_session( patient_name: str = Form(...), session_id: Optional[str] = Form(None) ): res = GLOBAL_ORCHESTRATOR.start_followup_session(patient_name_or_id=patient_name, session_id=session_id) return JSONResponse(content=res) def require_admin_auth( authorization: Optional[str] = Header(None), token: Optional[str] = Query(None) ) -> Dict[str, Any]: extracted_token = None if authorization: if authorization.lower().startswith("bearer "): extracted_token = authorization[7:].strip() else: extracted_token = authorization.strip() elif token: extracted_token = token.strip() if not extracted_token: raise HTTPException( status_code=401, detail="Authentication required. Please login with admin credentials." ) user = GLOBAL_DB.verify_token(extracted_token) if not user: raise HTTPException( status_code=401, detail="Session expired or invalid token. Please log in again." ) return user @app.get("/api/patients/list") async def list_registered_patients(admin: Dict[str, Any] = Depends(require_admin_auth)): patients = GLOBAL_PATIENT_REGISTRY.list_all_patients() return { "status": "success", "count": len(patients), "patients": patients } @app.get("/api/patients/{patient_name_or_id}/history") async def get_patient_history( patient_name_or_id: str, admin: Dict[str, Any] = Depends(require_admin_auth) ): record = GLOBAL_PATIENT_REGISTRY.find_patient(patient_name_or_id) if not record: raise HTTPException(status_code=404, detail=f"Patient '{patient_name_or_id}' not found.") return { "status": "success", "patient": record } @app.post("/api/sehat/session/turn") @app.post("/api/cura/session/turn") async def process_sehat_voice_turn( file: Optional[UploadFile] = File(None), session_id: str = Form(...), transcript: Optional[str] = Form(None), language: Optional[str] = Form(None) # 'hi' or 'mr' — Marathi support ): audio_bytes = None if file is not None: audio_bytes = await file.read() # Map short language code to BCP-47 code for ASR/TTS language_code = "mr-IN" if (language or "").startswith("mr") else "hi-IN" res = GLOBAL_ORCHESTRATOR.process_patient_turn( session_id=session_id, transcript_text=transcript, audio_bytes=audio_bytes, language_code=language_code ) return JSONResponse(content=res) @app.get("/api/sehat/session/{session_id}/summary") @app.get("/api/cura/session/{session_id}/summary") async def get_sehat_session_summary(session_id: str): summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: db_record = GLOBAL_DB.get_consultation_details(session_id) if db_record and db_record.get("full_data"): return { "status": "success", "summary": db_record["full_data"] } raise HTTPException(status_code=404, detail=f"Session '{session_id}' not found.") GLOBAL_DB.save_consultation(summary) return { "status": "success", "summary": summary } @app.get("/api/sehat/session/{session_id}/download") @app.get("/api/cura/session/{session_id}/download") async def download_sehat_session_report(session_id: str): summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: raise HTTPException(status_code=404, detail=f"Session '{session_id}' not found.") txt_file = summary.get("summary_file_txt") if txt_file and os.path.exists(txt_file): p_name = summary.get("patient", {}).get("name") or "Patient" filename = f"Summary_{p_name}_{session_id}.txt" return FileResponse(txt_file, media_type="text/plain; charset=utf-8", filename=filename) # Fallback to narrative text narrative = summary.get("clinical_summary_en", "") + "\n\n" + summary.get("clinical_summary_hi", "") return HTMLResponse(content=narrative, media_type="text/plain; charset=utf-8") @app.get("/api/sehat/session/{session_id}/download/json") async def download_sehat_session_json(session_id: str): summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: raise HTTPException(status_code=404, detail=f"Session '{session_id}' not found.") json_file = summary.get("summary_file_json") if json_file and os.path.exists(json_file): p_name = summary.get("patient", {}).get("name") or "Patient" filename = f"Summary_{p_name}_{session_id}.json" return FileResponse(json_file, media_type="application/json", filename=filename) return JSONResponse(content=summary) @app.get("/api/sehat/summaries") async def list_all_patient_summaries(admin: Dict[str, Any] = Depends(require_admin_auth)): """Lists all saved patient summary reports.""" summaries_dir = "outputs/reports/summaries" results = [] if os.path.exists(summaries_dir): for f in sorted(os.listdir(summaries_dir), reverse=True): if f.endswith(".json"): fpath = os.path.join(summaries_dir, f) try: with open(fpath, "r", encoding="utf-8") as jf: data = json.load(jf) results.append({ "session_id": data.get("session_id"), "patient": data.get("patient"), "branch": data.get("branch"), "recommended_urgency": data.get("recommended_urgency"), "generated_at": data.get("generated_at"), "summary_file_txt": data.get("summary_file_txt"), "summary_file_json": data.get("summary_file_json") }) except Exception: pass return { "status": "success", "total": len(results), "summaries": results } @app.get("/api/sehat/emergencies") @app.get("/api/cura/emergencies") async def get_sehat_emergency_alerts(admin: Dict[str, Any] = Depends(require_admin_auth)): emergencies = GLOBAL_DOCTOR_DELIVERY.get_emergency_feed() return { "status": "success", "count": len(emergencies), "emergencies": emergencies } @app.get("/api/sehat/sessions") @app.get("/api/cura/sessions") async def list_all_sehat_sessions(admin: Dict[str, Any] = Depends(require_admin_auth)): sessions_list = GLOBAL_DOCTOR_DELIVERY.get_doctor_queue() if not sessions_list: sessions_list = GLOBAL_SESSION_STORE.list_all_sessions() return { "status": "success", "total": len(sessions_list), "sessions": sessions_list } @app.get("/api/sehat/audit/{session_id}") async def get_session_audit_log( session_id: str, admin: Dict[str, Any] = Depends(require_admin_auth) ): events = GLOBAL_AUDIT_LOGGER.get_session_events(session_id) return { "status": "success", "session_id": session_id, "count": len(events), "events": events } # ========================================== # WEBSOCKET REAL-TIME STREAMING ENDPOINT # ========================================== @app.websocket("/ws/session") async def websocket_session_endpoint(websocket: WebSocket): await websocket.accept() session_id = None try: while True: data = await websocket.receive_text() msg = json.loads(data) event_type = msg.get("event") if event_type == "SESSION_START": session_id = msg.get("session_id") start_res = GLOBAL_ORCHESTRATOR.start_session(session_id) session_id = start_res["session_id"] await websocket.send_json({ "event": "SESSION_STARTED", "data": start_res }) elif event_type in ["AUDIO_CHUNK", "ASR_FINAL", "PATIENT_TURN"]: transcript = msg.get("transcript", "") sid = msg.get("session_id") or session_id await websocket.send_json({"event": "AI_THINKING", "session_id": sid}) turn_res = GLOBAL_ORCHESTRATOR.process_patient_turn( session_id=sid, transcript_text=transcript ) await websocket.send_json({ "event": "AI_RESPONSE", "data": turn_res }) if turn_res.get("is_emergency"): await websocket.send_json({ "event": "EMERGENCY_DETECTED", "data": turn_res }) elif turn_res.get("is_completed"): await websocket.send_json({ "event": "SESSION_COMPLETED", "data": turn_res }) except WebSocketDisconnect: logger.info(f"WebSocket client disconnected for session: {session_id}") except Exception as e: logger.error(f"WebSocket error: {e}") try: await websocket.send_json({"event": "SESSION_ERROR", "error": str(e)}) except Exception: pass # ========================================== # MODERN WEB APP & ADMIN PORTAL # ========================================== TEMPLATES_DIR = Path(__file__).parent / "templates" INDEX_HTML_PATH = TEMPLATES_DIR / "index.html" ADMIN_HTML_PATH = TEMPLATES_DIR / "admin.html" class AdminLoginRequest(BaseModel): username: str password: str @app.post("/api/admin/login") async def admin_login(req: AdminLoginRequest): auth_res = GLOBAL_DB.authenticate_admin(req.username, req.password) if not auth_res: raise HTTPException(status_code=401, detail="Invalid username or password.") return { "status": "success", "message": "Authentication successful", "token": auth_res["token"], "user": { "username": auth_res["username"], "full_name": auth_res["full_name"], "role": auth_res["role"] } } @app.get("/api/admin/check-auth") async def check_admin_auth(admin: Dict[str, Any] = Depends(require_admin_auth)): return {"status": "success", "user": admin} @app.post("/api/admin/logout") async def admin_logout( token: Optional[str] = Form(None), authorization: Optional[str] = Header(None) ): extracted_token = token if not extracted_token and authorization: if authorization.lower().startswith("bearer "): extracted_token = authorization[7:].strip() else: extracted_token = authorization.strip() if extracted_token: GLOBAL_DB.invalidate_token(extracted_token) return {"status": "success", "message": "Logged out successfully."} @app.get("/api/admin/metrics") async def get_admin_metrics(admin: Dict[str, Any] = Depends(require_admin_auth)): metrics = GLOBAL_DB.get_dashboard_metrics() return {"status": "success", "metrics": metrics} @app.get("/api/admin/consultations") async def list_admin_consultations( search: Optional[str] = None, urgency: Optional[str] = None, limit: int = 50, offset: int = 0, admin: Dict[str, Any] = Depends(require_admin_auth) ): res = GLOBAL_DB.get_consultations(search=search, urgency=urgency, limit=limit, offset=offset) return {"status": "success", "data": res} @app.get("/api/admin/export/csv") async def export_consultations_csv(admin: Dict[str, Any] = Depends(require_admin_auth)): res = GLOBAL_DB.get_consultations(limit=1000, offset=0) consultations = res.get("consultations", []) output = io.StringIO() writer = csv.writer(output) writer.writerow([ "Session ID", "Date", "Patient Name", "Patient ID", "Age", "Gender", "Diabetes Type", "Branch", "Triage Urgency", "Is Emergency", "IDRS Score", "IDRS Category", "Summary (English)", "Summary (Hindi)" ]) for c in consultations: writer.writerow([ c.get("session_id", ""), c.get("created_at", ""), c.get("patient_name", ""), c.get("patient_id", ""), c.get("age", ""), c.get("gender", ""), c.get("diabetes_type", ""), c.get("branch", ""), c.get("recommended_urgency", ""), "YES" if c.get("is_emergency") else "NO", c.get("idrs_score", 0), c.get("idrs_category", "Low Risk"), (c.get("summary_en") or "").replace("\n", " "), (c.get("summary_hi") or "").replace("\n", " ") ]) output.seek(0) filename = f"diabetes_dost_consultations_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv" return StreamingResponse( iter([output.getvalue()]), media_type="text/csv", headers={"Content-Disposition": f"attachment; filename={filename}"} ) @app.get("/api/admin/consultations/{session_id}") async def get_admin_consultation_detail( session_id: str, admin: Dict[str, Any] = Depends(require_admin_auth) ): record = GLOBAL_DB.get_consultation_details(session_id) if not record: raise HTTPException(status_code=404, detail=f"Consultation '{session_id}' not found.") return {"status": "success", "consultation": record} @app.delete("/api/admin/consultations/{session_id}") async def delete_admin_consultation( session_id: str, admin: Dict[str, Any] = Depends(require_admin_auth) ): deleted = GLOBAL_DB.delete_consultation(session_id) if not deleted: raise HTTPException(status_code=404, detail=f"Consultation '{session_id}' not found.") return {"status": "success", "message": "Consultation deleted successfully."} # ========================================== # PDF REPORT & WHATSAPP DELIVERY ENDPOINTS # ========================================== @app.get("/api/admin/consultations/{session_id}/pdf") async def download_consultation_pdf( session_id: str, admin: Dict[str, Any] = Depends(require_admin_auth) ): """Generate and stream a bilingual clinical summary PDF.""" if not REPORTS_ENABLED or not GLOBAL_PDF_GENERATOR: raise HTTPException(status_code=503, detail="PDF generation module not available. Install reportlab.") summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: record = GLOBAL_DB.get_consultation_details(session_id) if record and record.get("full_data"): summary = record["full_data"] else: raise HTTPException(status_code=404, detail=f"Session '{session_id}' not found.") try: pdf_bytes = GLOBAL_PDF_GENERATOR.generate(summary) p_name = (summary.get("patient") or {}).get("name") or "Patient" import re safe_name = re.sub(r"[^\w\-_]", "_", p_name.strip()) or "Patient" filename = f"Report_{safe_name}_{session_id}.pdf" return StreamingResponse( io.BytesIO(pdf_bytes), media_type="application/pdf", headers={"Content-Disposition": f"attachment; filename={filename}"} ) except Exception as e: raise HTTPException(status_code=500, detail=f"PDF generation failed: {e}") class WhatsAppSendRequest(BaseModel): phone_number: str language: Optional[str] = "mr" include_pdf: Optional[bool] = True @app.post("/api/admin/consultations/{session_id}/send-whatsapp") async def send_whatsapp_report( session_id: str, req: WhatsAppSendRequest, admin: Dict[str, Any] = Depends(require_admin_auth) ): """Send clinical summary PDF via WhatsApp to a given phone number.""" if not REPORTS_ENABLED: raise HTTPException(status_code=503, detail="Report module not available.") summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: record = GLOBAL_DB.get_consultation_details(session_id) summary = record.get("full_data") if record else None if not summary: raise HTTPException(status_code=404, detail=f"Session '{session_id}' not found.") pdf_bytes = GLOBAL_PDF_GENERATOR.generate(summary) if GLOBAL_PDF_GENERATOR else b"" p_name = (summary.get("patient") or {}).get("name") or "Patient" result = GLOBAL_WHATSAPP_SERVICE.send_whatsapp_report( phone_number=req.phone_number, patient_name=p_name, pdf_bytes=pdf_bytes, session_id=session_id, language=req.language or "mr" ) return {"status": "success", "delivery": result} @app.post("/api/sehat/session/{session_id}/send-report") async def patient_send_report( session_id: str, req: WhatsAppSendRequest ): """Patient-facing: send their own report via WhatsApp after consultation.""" if not REPORTS_ENABLED: return {"status": "unavailable", "message": "WhatsApp delivery not configured."} summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: record = GLOBAL_DB.get_consultation_details(session_id) summary = record.get("full_data") if record else None if not summary: raise HTTPException(status_code=404, detail="Session not found.") pdf_bytes = GLOBAL_PDF_GENERATOR.generate(summary) if GLOBAL_PDF_GENERATOR else b"" p_name = (summary.get("patient") or {}).get("name") or "Patient" result = GLOBAL_WHATSAPP_SERVICE.send_whatsapp_report( phone_number=req.phone_number, patient_name=p_name, pdf_bytes=pdf_bytes, session_id=session_id, language=req.language or "mr" ) return {"status": "success", "delivery": result} class DoctorSummarySendRequest(BaseModel): session_id: Optional[str] = None doctor_phone: Optional[str] = None include_pdf: Optional[bool] = True @app.get("/api/doctor/summary-preview") async def get_doctor_summary_preview_endpoint( session_id: str, doctor_phone: Optional[str] = None, request: Request = None ): """ Returns the complete structured summary text, patient metadata, and pre-calculated 1-Click WhatsApp direct link without requiring Twilio dispatch. """ if not session_id: raise HTTPException(status_code=400, detail="session_id is required.") summary = GLOBAL_ORCHESTRATOR.get_session_summary(session_id) if not summary: record = GLOBAL_DB.get_consultation_details(session_id) summary = record.get("full_data") if record else None if not summary: raise HTTPException(status_code=404, detail=f"Session '{session_id}' summary not found.") doc_phone = doctor_phone or os.getenv("DOCTOR_WHATSAPP_NUMBER", "").strip() or "+918830578295" host_url = str(request.base_url) if request else "" preview = GLOBAL_WHATSAPP_SERVICE.get_doctor_summary_preview(doc_phone, summary, host_url=host_url) return { "status": "success", "preview": preview } @app.post("/api/doctor/send-summary") @app.post("/api/sehat/session/{session_id}/send-doctor-summary") async def send_summary_to_doctor_endpoint( req: DoctorSummarySendRequest, session_id: Optional[str] = None, request: Request = None ): """ Sends the complete clinical pre-screening summary directly to the doctor. Supports Dual-Delivery: 1. Generates 1-Click WhatsApp Direct URL (wa.me) that works 100% reliably on all devices. 2. Attempts Twilio Cloud background dispatch if configured, with graceful fallback. """ sid = session_id or req.session_id if not sid: raise HTTPException(status_code=400, detail="session_id is required.") if not REPORTS_ENABLED or not GLOBAL_WHATSAPP_SERVICE: raise HTTPException(status_code=503, detail="WhatsApp service is not available.") summary = GLOBAL_ORCHESTRATOR.get_session_summary(sid) if not summary: record = GLOBAL_DB.get_consultation_details(sid) summary = record.get("full_data") if record else None if not summary: raise HTTPException(status_code=404, detail=f"Session '{sid}' summary not found.") doc_phone = req.doctor_phone or os.getenv("DOCTOR_WHATSAPP_NUMBER", "").strip() or "+918830578295" pdf_bytes = None if req.include_pdf and GLOBAL_PDF_GENERATOR: try: pdf_bytes = GLOBAL_PDF_GENERATOR.generate(summary) except Exception as pe: logger.warning(f"PDF generation for doctor summary notice: {pe}") host_url = str(request.base_url) if request else "" delivery_res = GLOBAL_WHATSAPP_SERVICE.send_doctor_summary_whatsapp( doctor_phone=doc_phone, summary=summary, pdf_bytes=pdf_bytes, host_url=host_url ) return { "status": "success", "session_id": sid, "doctor_phone": doc_phone, "delivery": delivery_res, "whatsapp_url": delivery_res.get("whatsapp_url"), "message_text": delivery_res.get("message_text"), "pdf_url": delivery_res.get("pdf_url"), "delivery_type": delivery_res.get("delivery_type"), "user_message": delivery_res.get("message") } @app.get("/api/reports/pdf/{filename}") async def serve_pdf_report(filename: str): """Serve locally saved PDF reports.""" import re # Security: only alphanumeric filenames if not re.match(r'^[\w\-_.]+\.pdf$', filename): raise HTTPException(status_code=400, detail="Invalid filename.") pdf_dir = BASE_DIR / "outputs" / "reports" / "pdf_reports" filepath = pdf_dir / filename if not filepath.exists(): raise HTTPException(status_code=404, detail="PDF not found.") return FileResponse(str(filepath), media_type="application/pdf", filename=filename) @app.get("/", response_class=HTMLResponse) async def serve_cura_app(): if INDEX_HTML_PATH.exists(): with open(INDEX_HTML_PATH, "r", encoding="utf-8") as f: return HTMLResponse(content=f.read()) return HTMLResponse(content="

Index template not found

", status_code=404) @app.get("/admin", response_class=HTMLResponse) async def serve_admin_portal(): if ADMIN_HTML_PATH.exists(): with open(ADMIN_HTML_PATH, "r", encoding="utf-8") as f: return HTMLResponse(content=f.read()) return HTMLResponse(content="

Admin template not found

", status_code=404) if __name__ == "__main__": import uvicorn port = int(os.getenv("PORT", 8000)) host = os.getenv("HOST", "0.0.0.0") uvicorn.run(app, host=host, port=port)