File size: 9,284 Bytes
d03f587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# api.py
import re
import json
import gc
import time
from typing import Optional
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from .model_loader import load_model_and_tokenizer
from .prompt_template import PROMPT_TEMPLATE
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="Medical Coding API",
    description="Extract ICD-10 and CPT codes from clinical notes using AI",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

class NoteRequest(BaseModel):
    note: str = Field(
        ..., 
        min_length=10,
        max_length=50000,
        description="Clinical provider note (10-50,000 characters)"
    )
    
    class Config:
        json_schema_extra = {
            "example": {
                "note": "Patient presents with essential hypertension. BP 160/95. Prescribed lisinopril 10mg daily. Office visit for established patient."
            }
        }

class CodingResponse(BaseModel):
    result: dict = Field(..., description="Extracted ICD-10 and CPT codes")
    raw_output: str = Field(..., description="Raw model output")
    note_length: int = Field(..., description="Length of input note in characters")
    truncated: bool = Field(..., description="Whether note was truncated")
    processing_time: float = Field(..., description="Time taken to process in seconds")

# Global variables for lazy loading
_gen_pipeline = None
_tokenizer = None
_model_load_time = None

def get_model():
    """Lazy load model on first request with error handling."""
    global _gen_pipeline, _tokenizer, _model_load_time
    
    if _gen_pipeline is None:
        logger.info("๐Ÿ”„ Loading model for the first time...")
        start_time = time.time()
        
        try:
            _gen_pipeline, _tokenizer = load_model_and_tokenizer()
            _model_load_time = time.time() - start_time
            logger.info(f"โœ… Model loaded in {_model_load_time:.2f} seconds")
        except Exception as e:
            logger.error(f"โŒ Failed to load model: {str(e)}")
            raise HTTPException(
                status_code=503,
                detail=f"Model loading failed: {str(e)}. Please try again in a few moments."
            )
    
    return _gen_pipeline, _tokenizer

def extract_json_from_text(text: str) -> Optional[str]:
    """Extract JSON object from text using brace counting."""
    start_idx = text.find('{')
    if start_idx == -1:
        return None
    
    brace_count = 0
    for i in range(start_idx, len(text)):
        if text[i] == '{':
            brace_count += 1
        elif text[i] == '}':
            brace_count -= 1
            if brace_count == 0:
                return text[start_idx:i+1]
    return None

def truncate_note(note: str, max_chars: int = 10000) -> str:
    """Truncate note to prevent token limit issues."""
    if len(note) <= max_chars:
        return note
    
    logger.warning(f"Note truncated from {len(note)} to {max_chars} characters")
    return note[:max_chars]

# ===== ENDPOINTS =====

@app.get("/")
async def root():
    """Root endpoint with API information."""
    return {
        "name": "Medical Coding API",
        "version": "1.0.0",
        "description": "Extract ICD-10 and CPT codes from clinical notes",
        "model": "RayyanAhmed9477/med-coding (Phi-3 based)",
        "endpoints": {
            "/predict": "POST - Extract medical codes from clinical note",
            "/health": "GET - Check API health status",
            "/docs": "GET - Interactive API documentation",
            "/metrics": "GET - API usage metrics"
        },
        "usage": {
            "endpoint": "/predict",
            "method": "POST",
            "body": {"note": "Your clinical note here (10-50,000 chars)"},
            "max_note_length": "50,000 characters (~10,000 words)"
        }
    }

@app.get("/health")
async def health_check():
    """Health check endpoint."""
    return {
        "status": "healthy",
        "model": "RayyanAhmed9477/med-coding",
        "model_loaded": _gen_pipeline is not None,
        "model_load_time": f"{_model_load_time:.2f}s" if _model_load_time else "not loaded yet"
    }

@app.get("/metrics")
async def metrics():
    """Get API usage metrics."""
    return {
        "model_loaded": _gen_pipeline is not None,
        "model_load_time_seconds": _model_load_time,
        "status": "operational"
    }

@app.post("/predict", response_model=CodingResponse)
async def predict(request: NoteRequest):
    """
    Extract ICD-10 and CPT codes from clinical notes.
    
    **Input:** Clinical note (10-50,000 characters)
    
    **Output:** JSON with extracted codes:
    - icd10_codes: List of ICD-10 diagnosis codes
    - cpt_codes: List of CPT procedure codes
    
    **Note:** First request may take 30-60 seconds as model loads into memory.
    Subsequent requests will be faster (2-10 seconds).
    """
    start_time = time.time()
    
    try:
        # Validate input
        note = request.note.strip()
        if not note:
            raise HTTPException(status_code=400, detail="Empty note provided")
        
        # Load model (lazy loading)
        logger.info(f"๐Ÿ“ Processing note ({len(note)} characters)")
        gen_pipeline, tokenizer = get_model()
        
        # Truncate if needed
        original_length = len(note)
        note_truncated = truncate_note(note, max_chars=10000)
        
        # Build prompt
        prompt = PROMPT_TEMPLATE.format(note=note_truncated)
        logger.info(f"๐Ÿ”ฎ Generating prediction (prompt length: {len(prompt)} chars)")
        
        # Generate prediction
        outputs = gen_pipeline(
            prompt,
            max_new_tokens=600,
            do_sample=False,
            num_return_sequences=1,
            pad_token_id=tokenizer.eos_token_id,
            eos_token_id=tokenizer.eos_token_id,
            temperature=0.1,
            top_p=0.95,
            return_full_text=False
        )
        
        # Extract generated text
        if isinstance(outputs, list) and len(outputs) > 0:
            text = outputs[0].get("generated_text", "")
        elif isinstance(outputs, dict):
            text = outputs.get("generated_text", "")
        else:
            text = str(outputs)
        
        logger.info(f"๐Ÿ“ค Model output length: {len(text)} characters")
        
        # Remove prompt if present
        if prompt in text:
            text = text.replace(prompt, "").strip()
        
        # Extract JSON
        json_str = extract_json_from_text(text)
        
        if json_str is None:
            logger.error(f"No JSON found in output: {text[:500]}")
            raise HTTPException(
                status_code=500,
                detail={
                    "error": "No valid JSON found in model output",
                    "raw_output_preview": text[:300],
                    "suggestion": "Model may need fine-tuning or prompt adjustment"
                }
            )
        
        # Parse JSON
        try:
            parsed = json.loads(json_str)
        except json.JSONDecodeError as e:
            logger.error(f"JSON parse error: {str(e)}")
            raise HTTPException(
                status_code=500,
                detail={
                    "error": f"Invalid JSON format: {str(e)}",
                    "json_preview": json_str[:300]
                }
            )
        
        # Validate response structure
        if not isinstance(parsed, dict):
            raise HTTPException(
                status_code=500,
                detail="Model output is not a valid JSON object"
            )
        
        # Clean up memory
        gc.collect()
        
        processing_time = time.time() - start_time
        logger.info(f"โœ… Prediction completed in {processing_time:.2f} seconds")
        
        return CodingResponse(
            result=parsed,
            raw_output=text,
            note_length=original_length,
            truncated=original_length > 10000,
            processing_time=round(processing_time, 2)
        )
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"โŒ Prediction failed: {str(e)}", exc_info=True)
        gc.collect()
        raise HTTPException(
            status_code=500,
            detail=f"Prediction failed: {str(e)}"
        )

@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    """Global exception handler for unhandled errors."""
    logger.error(f"Unhandled exception: {str(exc)}", exc_info=True)
    return JSONResponse(
        status_code=500,
        content={
            "detail": "Internal server error",
            "error": str(exc),
            "path": str(request.url)
        }
    )

# Startup event
@app.on_event("startup")
async def startup_event():
    """Log startup information."""
    logger.info("=" * 60)
    logger.info("๐Ÿš€ Medical Coding API Starting...")
    logger.info("=" * 60)
    logger.info("โณ Model will be loaded on first /predict request")
    logger.info("๐Ÿ“š API Documentation: /docs")
    logger.info("=" * 60)