Update handler.py
Browse files1234567891011121314151617181920214 unmodified lines262728293031329 unmodified lines4243444546474812 unmodified lines616263646566679 unmodified lines777879808182838485868788891 unmodified line919293949596972 unmodified lines10010110210310410510610710810911011111211311411511611711811912012112212312412512612711 unmodified lines13914014114214314420 unmodified lines1651661671681691701711729 unmodified lines1821831841851861871881891901911921931941951961971981992002012022032042052062072082092102118 unmodified lines22022122222322422522633 unmodified lines2602612622632642652662672682691 unmodified line27127227327427527627727827928028128228328428528628728828929029129229329432 unmodified lines3273283293303313323333343353 unmodified lines3393403413423433443453463473483493502 unmodified lines35335435535635735835936036136236336436536636736836923 unmodified lines3933943953963973984 unmodified lines40340440540640740840918 unmodified lines4284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624637 unmodified lines4714724734744754764778 unmodified lines4864874884894904918 unmodified lines5005015025035045055065075085095105115129 unmodified lines5225235245255265275285295305315325332 unmodified lines5365375385395405415425 unmodified lines54854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258314 unmodified lines59859960060160260360416 unmodified lines62162262362462562662735 unmodified lines66366466566666766866919 unmodified lines6896906916926936941 unmodified line6966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287 unmodified lines7367377387397407417427437443 unmodified lines74874975075175275375446 unmodified lines801802803804805# -*- coding: utf-8 -*-"""PULSE ECG Handler — Deterministic JSON→Narrative (age+sex aware)- Model still processes image (LLaVA/transformers)- output_mode="json" → returns structured JSON (single model call)- output_mode="report_en" → JSON + table + narrative (derived deterministically from JSON; still single model call)- output_mode="narrative" → classic narrative paragraph (model free-form)Notes:- For "json" and "report_en" modes we prompt the model with a strict JSON schema hint.- Age group ("0-15" | "15-65" | "65+") and sex ("male" | "female") are accepted from payloadand used only in deterministic narrative rendering (not sent to the model)."""import osimport reimport jsonimport base64import hashlibimport datetimefrom io import BytesIO4 unmodified linesfrom PIL import Imageimport requests# ==== Debug helpers ====def _env_bool(name: str, default: bool = False) -> bool:v = os.getenv(name)if v is None:9 unmodified linesdef warn(*args, **kwargs):print("[WARN]", *args, **kwargs)# ==== LLaVA & Transformers ====try:from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKENfrom llava.conversation import conv_templates, SeparatorStyle12 unmodified linesTRANSFORMERS_AVAILABLE = Falsewarn(f"transformers not available: {e}")# ==== HF Hub logging (optional) ====try:from huggingface_hub import HfApi, loginHF_HUB_AVAILABLE = True9 unmodified linesrepo_name = os.environ.get("LOG_REPO", "")except Exception as e:warn(f"[HF Hub] init failed: {e}")api = Nonerepo_name = ""LOGDIR = "./logs"os.makedirs(LOGDIR, exist_ok=True)# ==== Global state ====tokenizer = Nonemodel = Noneimage_processor = None1 unmodified lineargs = Nonemodel_initialized = False# ==== Prompts ====STYLE_HINT = ("Write one concise narrative paragraph that covers rhythm, heart rate, cardiac axis, ""P waves and PR interval, QRS morphology and duration, ST segments, T waves, and QT/QTc. "2 unmodified lines"followed by a succinct, comma-separated summary of the key diagnoses.")JSON_SCHEMA_HINT_EN = """Return ONLY a valid JSON object that matches EXACTLY this schema:{"heart_rate_bpm": int | null,"rhythm": "string","qrs_axis": "string","p_waves": "string","pr_interval_ms": int | null,"qrs_duration_ms": int | null,"t_waves": "string","qtc_ms": int | null,"qtc_comment": "string","additional_comments": "string"}Rules:- Output MUST be valid JSON with no extra text before or after.- Units: use integers for bpm and ms where applicable.- If unknown, use null for numeric fields and empty string for text fields.- Use standard cardiology terminology in English."""# ===================== Utilities =====================def _safe_upload(path: str):if api and repo_name and path and os.path.isfile(path):try:11 unmodified linesreturn os.path.join(LOGDIR, f"{t.year:04d}-{t.month:02d}-{t.day:02d}-user_conv.json")def load_image_any(image_input: Union[str, dict]) -> Image.Image:if isinstance(image_input, str):s = image_input.strip()if s.startswith(("http://", "https://")):20 unmodified linesdef _postprocess_min(text: str) -> str:return _normalize_whitespace(text)# ====== Vision helpers ======def get_vision_expected_size(m, default: int = 336) -> int:try:vt = m.get_vision_tower()vt_cfg = getattr(getattr(vt, "vision_tower", vt), "config", None)9 unmodified linesreturn defaultdef force_processor_size(proc, size: int):try:if hasattr(proc, "size"):if isinstance(proc.size, dict):proc.size["shortest_edge"] = sizeelse:try:proc.size.shortest_edge = sizeexcept Exception:proc.size = {"shortest_edge": size}if hasattr(proc, "crop_size"):if isinstance(proc.crop_size, dict):proc.crop_size["height"] = sizeproc.crop_size["width"] = sizeelse:try:proc.crop_size.height = sizeproc.crop_size.width = sizeexcept Exception:proc.crop_size = {"height": size, "width": size}dbg(f"[processor] forced size={size}")except Exception as e:warn(f"[processor] force size failed: {e}")# ====== Stop Criteria ======class SafeKeywordsStoppingCriteria(StoppingCriteria):def __init__(self, keyword: str, tokenizer):tok = tokenizer(keyword, add_special_tokens=False, return_tensors="pt").input_ids[0]8 unmodified linestail = out[-n:]return torch.equal(tail, self.kw_ids.to(tail.device))# ===================== Core =====================class InferenceDemo:def __init__(self, args, model_path, tokenizer_, model_, image_processor_, context_len_):if not LLAVA_AVAILABLE:33 unmodified lines).unsqueeze(0).to(device)return prompt, input_ids# ===================== Deterministic renderers =====================def render_ecg_table_en(d: Dict[str, Any]) -> str:lines = ["ECG ANALYSIS", "────────────"]if "heart_rate_bpm" in d and d["heart_rate_bpm"] is not None:lines.append(f"Heart rate : {d['heart_rate_bpm']} beats/min")if "rhythm" in d:lines.append(f"Rhythm : {d['rhythm']}")1 unmodified linelines.append(f"QRS axis : {d['qrs_axis']}")if "p_waves" in d:lines.append(f"P waves : {d['p_waves']}")if "pr_interval_ms" in d and d["pr_interval_ms"] is not None:lines.append(f"PR interval : {d['pr_interval_ms']} ms")if "qrs_duration_ms" in d and d["qrs_duration_ms"] is not None:lines.append(f"QRS duration : {d['qrs_duration_ms']} ms")if "t_waves" in d:lines.append(f"T waves : {d['t_waves']}")if "qtc_ms" in d and d["qtc_ms"] is not None:qtc_c = d.get("qtc_comment", "").strip()qtc_c = qtc_c if qtc_c else "—"lines.append(f"QTc : {qtc_c} ({d['qtc_ms']} ms)")lines.append("")lines.append("Additional comments")lines.append("──────────────────")lines.append(d.get("additional_comments", "").strip())return "\n".join(lines)def render_ecg_narrative_en(d: Dict[str, Any]) -> str:"""Deterministic narrative based on JSON + age_group + sex"""hr = d.get("heart_rate_bpm")rhythm = d.get("rhythm")axis = d.get("qrs_axis")32 unmodified lineselif sex:para.append(f"The patient is {sex}.")if rhythm:para.append(f"The electrocardiogram shows {rhythm.lower()}.")if isinstance(hr, int):if hr < hr_low:hr_comment = "bradycardia"3 unmodified lineshr_comment = "within normal range"para.append(f"The heart rate is {hr} bpm ({hr_comment}).")if axis:para.append(f"The QRS axis is {axis.lower()}.")if p:para.append(f"P waves are {p.lower()}.")if isinstance(pr, int):if pr < pr_low:pr_comment = "short PR interval"2 unmodified lineselse:pr_comment = "within normal range"para.append(f"PR interval is {pr} ms ({pr_comment}).")if isinstance(qrs_dur, int):if qrs_dur >= qrs_limit:qrs_comment = "prolonged QRS (possible conduction delay)"else:qrs_comment = "normal QRS duration"para.append(f"QRS duration is {qrs_dur} ms ({qrs_comment}).")if t:para.append(f"T waves: {t}.")if isinstance(qtc, int):if sex == "male":if qtc > qtc_male:23 unmodified linesparagraph = " ".join(para).strip()sci_bits = []if rhythm: sci_bits.append(rhythm)if axis: sci_bits.append(f"QRS axis: {axis}")4 unmodified linesreturn paragraph + "\n\n" + "Structured clinical impression: " + ", ".join(sci_bits)# ===================== Generation =====================def generate_response(message_text: str,image_input,18 unmodified linesif max_new_tokens is None: max_new_tokens = 4096if repetition_penalty is None: repetition_penalty = 1.0dbg(f"[gen] temp={temperature} top_p={top_p} max_new={max_new_tokens} rep={repetition_penalty} mode={output_mode}")chatbot = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)if conv_mode_override and conv_mode_override in conv_templates:chatbot.conversation = conv_templates[conv_mode_override].copy()# Load imagetry:pil_img = load_image_any(image_input)except Exception as e:return {"error": f"Failed to load image: {e}"}# Save image (log)img_hash, img_path = "NA", Nonetry:buf = BytesIO(); pil_img.save(buf, format="JPEG"); raw = buf.getvalue()img_hash = hashlib.md5(raw).hexdigest()t = datetime.datetime.now()img_path = os.path.join(LOGDIR, "serve_images", f"{t.year:04d}-{t.month:02d}-{t.day:02d}", f"{img_hash}.jpg")os.makedirs(os.path.dirname(img_path), exist_ok=True)if not os.path.isfile(img_path):pil_img.save(img_path)except Exception as e:warn(f"[log] save image failed: {e}")device = next(chatbot.model.parameters()).devicedtype = torch.float16# Preprocess image → tensorexpected_size = get_vision_expected_size(chatbot.model, default=336)image_tensor = Nonetry:if hasattr(chatbot.image_processor, "preprocess"):7 unmodified lineselse:raise AttributeError("processor has no preprocess")except Exception:# Fallback chain: process_images → manual CLIP normtry:processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)if isinstance(processed, (list, tuple)) and len(processed) > 0:8 unmodified linesexcept Exception:from torchvision import transformsfrom torchvision.transforms import InterpolationModepreprocess = transforms.Compose([transforms.Resize(expected_size, interpolation=InterpolationMode.BICUBIC),transforms.CenterCrop(expected_size),8 unmodified linesif image_tensor is None:return {"error": "Image processing failed (no tensor produced)"}# Prompt selectionbase_msg = (message_text or "").strip()if output_mode in ("json", "report_en"):msg = f"{base_msg}\n\n{JSON_SCHEMA_HINT_EN}"else: # narrativemsg = f"{base_msg}\n\n{STYLE_HINT}"_, input_ids = _build_prompt_and_ids(chatbot, msg, device)stop_str = chatbot.conversation.sep if chatbot.conversation.sep_style != SeparatorStyle.TWO else chatbot.conversation.sep29 unmodified linesexcept Exception:passstreamer = TextIteratorStreamer(chatbot.tokenizer, skip_prompt=True, skip_special_tokens=True)gen_kwargs = dict(inputs=input_ids,images=image_tensor,streamer=streamer,do_sample=True,temperature=float(temperature),top_p=float(top_p),max_new_tokens=int(max_new_tokens),2 unmodified linesstopping_criteria=[stopping],)# Generatetry:t = Thread(target=chatbot.model.generate, kwargs=gen_kwargs)t.start()5 unmodified linesexcept Exception as e:return {"error": f"Generation failed: {e}"}# Logtry:row = {"time": datetime.datetime.now().isoformat(),"type": "chat","model": "PULSE-7B","state": [(message_text, text)],"image_hash": img_hash,"image_path": img_path or "",}with open(_conv_log_path(), "a", encoding="utf-8") as f:f.write(json.dumps(row, ensure_ascii=False) + "\n")_safe_upload(_conv_log_path()); _safe_upload(img_path or "")except Exception as e:warn(f"[log] failed: {e}")# Output modesif output_mode == "narrative":return {"status": "success", "response": text, "conversation_id": id(chatbot.conversation)}# For json & report_en we need to parse JSON oncetry:start = text.find("{"); end = text.rfind("}")if start == -1 or end == -1 or end <= start:return {"error": "JSON block not found", "raw": text}data = json.loads(text[start:end+1])except Exception as e:return {"error": f"JSON parse failed: {e}", "raw": text}# Inject patient metadata (not sent to model; used for deterministic narrative)if patient_age_group:data["patient_age_group"] = patient_age_groupif patient_sex:14 unmodified lines# Fallbackreturn {"status": "success", "response": text, "conversation_id": id(chatbot.conversation)}# ===================== Public API =====================def query(payload: dict):global model_initialized, tokenizer, model, image_processor, context_len, argsif not model_initialized:16 unmodified linesdet_seed = payload.get("det_seed", None)output_mode = payload.get("output_mode", "narrative")# Optional patient metapatient_age_group = payload.get("patient_age_group")patient_sex = payload.get("patient_sex")35 unmodified lines"device": str(next(model.parameters()).device) if model else "Unknown",}# ===================== Init & Session =====================class _Args:def __init__(self):self.model_path = os.getenv("HF_MODEL_ID", "PULSE-ECG/PULSE-7B")19 unmodified linestokenizer_, model_, image_processor_, context_len_ = load_pretrained_model(args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit)try:_ = next(model_.parameters()).device1 unmodified lineif torch.cuda.is_available():model_ = model_.to(torch.device("cuda"))model_.eval()expected_size = get_vision_expected_size(model_, default=336)if image_processor_ is None:try:from transformers import AutoProcessorimage_processor_ = AutoProcessor.from_pretrained(args.model_path)except Exception:from transformers import CLIPImageProcessorclip_id = "openai/clip-vit-large-patch14-336" if expected_size >= 336 else "openai/clip-vit-large-patch14"image_processor_ = CLIPImageProcessor.from_pretrained(clip_id)force_processor_size(image_processor_, expected_size)globals()["tokenizer"] = tokenizer_globals()["model"] = model_globals()["image_processor"] = image_processor_globals()["context_len"] = context_len_chat_manager.init_if_needed(args, args.model_path, tokenizer_, model_, image_processor_, context_len_)print("[init] model/tokenizer/image_processor loaded.]")return Trueexcept Exception as e:warn(f"[init] failed: {e}")return False# ===================== EndpointHandler =====================class EndpointHandler:"""Hugging Face Endpoint compatible"""def __init__(self, model_dir):self.model_dir = model_dirprint(f"EndpointHandler initialized with model_dir: {model_dir}")7 unmodified linesreturn get_model_info()if __name__ == "__main__":print("Handler ready (Deterministic JSON→Narrative, age+sex aware). Use `EndpointHandler` or `query`.")# ===================== FastAPI Wrapper =====================try:from fastapi import FastAPIfrom pydantic import BaseModel3 unmodified lineswarn(f"fastapi/pydantic not available: {e}")if FASTAPI_AVAILABLE:app = FastAPI(title="PULSE ECG Handler API", version="1.2.0")class QueryIn(BaseModel):message: str | None = None46 unmodified linesdata["output_mode"] = "report_en"return query(data)else:app = None1234567891011121314151617181920212223242526274 unmodified lines323334353637389 unmodified lines4849505152535412 unmodified lines676869707172739 unmodified lines8384858687888990919293941 unmodified line969798991001011022 unmodified lines10510610710810911011111211311411511611711811912012112212312412512612712812913011 unmodified lines14214314414514614714814915020 unmodified lines1711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802819 unmodified lines2912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203218 unmodified lines33033133233333433533633 unmodified lines3703713723733743753763773783791 unmodified line38138238338438538638738838939039139239339439539639739839940032 unmodified lines4334344354364374384394404414424434444454464474484493 unmodified lines4534544554564574584594604614624632 unmodified lines46646746846947047147247347447547623 unmodified lines5005015025035045055064 unmodified lines51151251351451551651718 unmodified lines5365375385395405415425435445455465475485495505515525535545555565575585595605615625637 unmodified lines5715725735745755768 unmodified lines5855865875885895905918 unmodified lines6006016026036046056066076086096106116126139 unmodified lines6236246256266276286296306316326336346352 unmodified lines6386396406416426435 unmodified lines64965065165265365465565665765865966066166266366466566666766866967067167267367467567614 unmodified lines69169269369469569669716 unmodified lines71471571671771871972035 unmodified lines75675775875976076176219 unmodified lines7827837847857867877881 unmodified line7907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258267 unmodified lines8348358368378388398408418423 unmodified lines84684784884985085185246 unmodified lines899900901902# -*- coding: utf-8 -*-"""PULSE ECG Handler — Deterministic JSON → Table + Narrative (age+sex aware) with Robust FallbacksModes- output_mode="json" → returns structured JSON (single model call)- output_mode="report_en" → returns JSON + table + deterministic narrative (single model call)- output_mode="narrative" → classic free-form model narrative (STYLE_HINT used)Highlights- Age group ("0-15" | "15-65" | "65+") and sex ("male" | "female") are accepted in payload and areused only in deterministic narrative rendering (not sent to the model).- Robust JSON parsing:1) direct JSON slice2) cleanup pseudo-JSON (_coerce_pseudo_json)3) regex-based field extraction from free text (_extract_fields_from_text)- Safe stop criteria, dynamic vision-size processor, logging hooks (optional HF Hub upload)."""import osimport reimport jsonimport base64import mathimport hashlibimport datetimefrom io import BytesIO4 unmodified linesfrom PIL import Imageimport requests# ========= Debug Helpers =========def _env_bool(name: str, default: bool = False) -> bool:v = os.getenv(name)if v is None:9 unmodified linesdef warn(*args, **kwargs):print("[WARN]", *args, **kwargs)# ========= LLaVA & Transformers =========try:from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKENfrom llava.conversation import conv_templates, SeparatorStyle12 unmodified linesTRANSFORMERS_AVAILABLE = Falsewarn(f"transformers not available: {e}")# ========= (Optional) HF Hub logging =========try:from huggingface_hub import HfApi, loginHF_HUB_AVAILABLE = True9 unmodified linesrepo_name = os.environ.get("LOG_REPO", "")except Exception as e:warn(f"[HF Hub] init failed: {e}")api, repo_name = None, ""LOGDIR = "./logs"os.makedirs(LOGDIR, exist_ok=True)# ========= Global State =========tokenizer = Nonemodel = Noneimage_processor = None1 unmodified lineargs = Nonemodel_initialized = False# ========= Prompts =========STYLE_HINT = ("Write one concise narrative paragraph that covers rhythm, heart rate, cardiac axis, ""P waves and PR interval, QRS morphology and duration, ST segments, T waves, and QT/QTc. "2 unmodified lines"followed by a succinct, comma-separated summary of the key diagnoses.")# Example-only schema (no type hints). The model copies this structure.JSON_SCHEMA_HINT_EN = """Return ONLY a valid JSON object. Do not include comments, types, or extra text.If a value is unknown, use null (for numbers) or "" (for strings).{"heart_rate_bpm": 100,"rhythm": "Sinus rhythm","qrs_axis": "Normal","p_waves": "Normal","pr_interval_ms": 160,"qrs_duration_ms": 90,"t_waves": "Normal","qtc_ms": 420,"qtc_comment": "Normal","additional_comments": ""}"""# ========= Utilities =========def _safe_upload(path: str):if api and repo_name and path and os.path.isfile(path):try:11 unmodified linesreturn os.path.join(LOGDIR, f"{t.year:04d}-{t.month:02d}-{t.day:02d}-user_conv.json")def load_image_any(image_input: Union[str, dict]) -> Image.Image:"""Supports: http(s) URL, local path, base64 (with or without data URL prefix), or {"image": <...>}"""if isinstance(image_input, str):s = image_input.strip()if s.startswith(("http://", "https://")):20 unmodified linesdef _postprocess_min(text: str) -> str:return _normalize_whitespace(text)def _coerce_pseudo_json(text: str) -> str:"""Coerce pseudo-JSON (e.g., 'int | none', 'none', Python booleans) into valid JSON string."""if not isinstance(text, str):return ""s = text# Keep only the outermost JSON object if stray tokens are aroundi, j = s.find("{"), s.rfind("}")if i != -1 and j != -1 and j > i:s = s[i:j+1]# Remove type-like hints → replace with valid JSON placeholderss = re.sub(r':\s*int\s*\|\s*none', ': null', s, flags=re.I)s = re.sub(r':\s*string\s*\|\s*none', ': ""', s, flags=re.I)# Python/other tokens → JSONs = re.sub(r'\bNone\b|\bnone\b', 'null', s, flags=re.I)s = re.sub(r'\bTrue\b', 'true', s)s = re.sub(r'\bFalse\b', 'false', s)# Strip inline commentss = re.sub(r'//.*', '', s) # JS styles = re.sub(r'#.*', '', s) # Python style# Collapse repeated commass = re.sub(r',\s*,+', ',', s)return s.strip()def _to_int_or_none(x: Optional[str]) -> Optional[int]:if x is None:return Nonex = x.strip()if not x:return Nonetry:v = int(float(x))if math.isnan(v):return Nonereturn vexcept Exception:return Nonedef _extract_fields_from_text(text: str) -> Dict[str, Any]:"""Extract fields from free text when model failed to return valid JSON.Missing numeric fields -> None; missing text -> ""."""if not isinstance(text, str):text = str(text or "")def rex(pattern, flags=re.I):m = re.search(pattern, text, flags)return m.group(1).strip() if m else None# bpmhr = rex(r"(?:heart\s*rate|hr)\s*[:=]?\s*(\d{1,3})\s*(?:bpm|beats?/min)?")if hr is None:hr = rex(r"\b(\d{2,3})\s*(?:bpm|beats?/min)\b")# PR/QRS/QTc mspr = rex(r"\bPR\s*(?:interval)?\s*[:=]?\s*(\d{2,4})\s*ms\b")qrs = rex(r"\bQRS\s*(?:duration)?\s*[:=]?\s*(\d{2,4})\s*ms\b")qtc = rex(r"\bQTc?\s*[:=]?\s*(\d{2,4})\s*ms\b")# Axisaxis = rex(r"\bQRS\s*axis\s*[:=]?\s*([+\-]?\d+°|normal|left|right|indeterminate)\b")# Rhythmrhythm = rex(r"\brhythm\s*[:=]?\s*([A-Za-z \-]+)")if rhythm is None:rhythm = rex(r"\b(sinus\s+(?:tachycardia|bradycardia|rhythm)|atrial fibrillation|afib|atrial flutter|junctional rhythm)\b")# P / T wavesp_waves = rex(r"\bP\s*waves?\s*[:=]?\s*([A-Za-z0-9, \-]+)")t_waves = rex(r"\bT\s*waves?\s*[:=]?\s*([A-Za-z0-9, \-]+)")# QTc commentqtc_comment = rex(r"\bQTc\s*(?:comment|status)?\s*[:=]?\s*([A-Za-z \-]+)")# Additionaladditional = rex(r"(?:Additional\s*comments|Notes?)\s*[:\-]?\s*([\s\S]{0,300})")if not additional:additional = rex(r"\b(ST[- ](?:elevation|depression)|S1Q3T3|early repolarization|strain pattern)\b(?:[^\n\r]{0,120})")return {"heart_rate_bpm": _to_int_or_none(hr),"rhythm": (rhythm or "").strip(),"qrs_axis": (axis or "").strip(),"p_waves": (p_waves or "").strip(),"pr_interval_ms": _to_int_or_none(pr),"qrs_duration_ms": _to_int_or_none(qrs),"t_waves": (t_waves or "").strip(),"qtc_ms": _to_int_or_none(qtc),"qtc_comment": (qtc_comment or "").strip(),"additional_comments": (additional or "").strip(),}# ========= Vision helpers =========def get_vision_expected_size(m, default: int = 336) -> int:"""Return expected image size for the model vision tower if available."""try:vt = m.get_vision_tower()vt_cfg = getattr(getattr(vt, "vision_tower", vt), "config", None)9 unmodified linesreturn defaultdef force_processor_size(proc, size: int):"""Force processor resize/crop to target size safely."""try:if hasattr(proc, "size"):if isinstance(proc.size, dict):proc.size["shortest_edge"] = sizeelse:try:proc.size.shortest_edge = size # type: ignore[attr-defined]except Exception:proc.size = {"shortest_edge": size}if hasattr(proc, "crop_size"):if isinstance(proc.crop_size, dict):proc.crop_size["height"] = sizeproc.crop_size["width"] = sizeelse:try:proc.crop_size.height = size # type: ignore[attr-defined]proc.crop_size.width = size # type: ignore[attr-defined]except Exception:proc.crop_size = {"height": size, "width": size}dbg(f"[processor] forced size={size}")except Exception as e:warn(f"[processor] force size failed: {e}")# ========= Safe Stopper =========class SafeKeywordsStoppingCriteria(StoppingCriteria):def __init__(self, keyword: str, tokenizer):tok = tokenizer(keyword, add_special_tokens=False, return_tensors="pt").input_ids[0]8 unmodified linestail = out[-n:]return torch.equal(tail, self.kw_ids.to(tail.device))# ========= Core Session =========class InferenceDemo:def __init__(self, args, model_path, tokenizer_, model_, image_processor_, context_len_):if not LLAVA_AVAILABLE:33 unmodified lines).unsqueeze(0).to(device)return prompt, input_ids# ========= Deterministic Renderers =========def render_ecg_table_en(d: Dict[str, Any]) -> str:lines = ["ECG ANALYSIS", "────────────"]if d.get("heart_rate_bpm") is not None:lines.append(f"Heart rate : {d['heart_rate_bpm']} beats/min")if "rhythm" in d:lines.append(f"Rhythm : {d['rhythm']}")1 unmodified linelines.append(f"QRS axis : {d['qrs_axis']}")if "p_waves" in d:lines.append(f"P waves : {d['p_waves']}")if d.get("pr_interval_ms") is not None:lines.append(f"PR interval : {d['pr_interval_ms']} ms")if d.get("qrs_duration_ms") is not None:lines.append(f"QRS duration : {d['qrs_duration_ms']} ms")if "t_waves" in d:lines.append(f"T waves : {d['t_waves']}")if d.get("qtc_ms") is not None:qtc_c = (d.get("qtc_comment") or "").strip() or "—"lines.append(f"QTc : {qtc_c} ({d['qtc_ms']} ms)")lines += ["", "Additional comments", "──────────────────", (d.get("additional_comments") or "").strip()]return "\n".join(lines)def render_ecg_narrative_en(d: Dict[str, Any]) -> str:"""Deterministic narrative based on JSON + age_group + sex with 'Structured clinical impression' at the end."""hr = d.get("heart_rate_bpm")rhythm = d.get("rhythm")axis = d.get("qrs_axis")32 unmodified lineselif sex:para.append(f"The patient is {sex}.")# Rhythm with age-adjusted normalization for sinus tachycardiaif rhythm:if rhythm.lower() == "sinus tachycardia" and isinstance(hr, int) and hr_low <= hr <= hr_high:para.append(f"The electrocardiogram shows sinus rhythm, normal for age. "f"Although labelled as sinus tachycardia, the heart rate of {hr} bpm is within the normal range for this age group.")else:para.append(f"The electrocardiogram shows {rhythm.lower()}.")# Heart rate commentif isinstance(hr, int):if hr < hr_low:hr_comment = "bradycardia"3 unmodified lineshr_comment = "within normal range"para.append(f"The heart rate is {hr} bpm ({hr_comment}).")# Axis / P / PR / QRS / T / QTcif axis:para.append(f"The QRS axis is {axis.lower()}.")if p:para.append(f"P waves are {p.lower()}.")if isinstance(pr, int):if pr < pr_low:pr_comment = "short PR interval"2 unmodified lineselse:pr_comment = "within normal range"para.append(f"PR interval is {pr} ms ({pr_comment}).")if isinstance(qrs_dur, int):qrs_comment = "normal QRS duration" if qrs_dur < qrs_limit else "prolonged QRS (possible conduction delay)"para.append(f"QRS duration is {qrs_dur} ms ({qrs_comment}).")if t:para.append(f"T waves: {t}.")if isinstance(qtc, int):if sex == "male":if qtc > qtc_male:23 unmodified linesparagraph = " ".join(para).strip()# Structured clinical impression (deterministic summary)sci_bits = []if rhythm: sci_bits.append(rhythm)if axis: sci_bits.append(f"QRS axis: {axis}")4 unmodified linesreturn paragraph + "\n\n" + "Structured clinical impression: " + ", ".join(sci_bits)# ========= Generation =========def generate_response(message_text: str,image_input,18 unmodified linesif max_new_tokens is None: max_new_tokens = 4096if repetition_penalty is None: repetition_penalty = 1.0# Deterministic settings for schema modesif output_mode in ("json", "report_en"):temperature = 0.0top_p = 1.0repetition_penalty = 1.0max_new_tokens = min(int(max_new_tokens), 1024)dbg(f"[gen] temp={temperature} top_p={top_p} max_new={max_new_tokens} rep={repetition_penalty} mode={output_mode}")chatbot = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)if conv_mode_override and conv_mode_override in conv_templates:chatbot.conversation = conv_templates[conv_mode_override].copy()# Load image → tensortry:pil_img = load_image_any(image_input)except Exception as e:return {"error": f"Failed to load image: {e}"}device = next(chatbot.model.parameters()).devicedtype = torch.float16image_tensor = Nonetry:if hasattr(chatbot.image_processor, "preprocess"):7 unmodified lineselse:raise AttributeError("processor has no preprocess")except Exception:try:processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)if isinstance(processed, (list, tuple)) and len(processed) > 0:8 unmodified linesexcept Exception:from torchvision import transformsfrom torchvision.transforms import InterpolationModeexpected_size = get_vision_expected_size(chatbot.model, default=336)preprocess = transforms.Compose([transforms.Resize(expected_size, interpolation=InterpolationMode.BICUBIC),transforms.CenterCrop(expected_size),8 unmodified linesif image_tensor is None:return {"error": "Image processing failed (no tensor produced)"}# Build promptbase_msg = (message_text or "").strip()if output_mode in ("json", "report_en"):msg = f"{base_msg}\n\n{JSON_SCHEMA_HINT_EN}"else: # "narrative"msg = f"{base_msg}\n\n{STYLE_HINT}"dbg(f"[prompt] mode={output_mode}")_, input_ids = _build_prompt_and_ids(chatbot, msg, device)stop_str = chatbot.conversation.sep if chatbot.conversation.sep_style != SeparatorStyle.TWO else chatbot.conversation.sep29 unmodified linesexcept Exception:pass# Generate with streamerstreamer = TextIteratorStreamer(chatbot.tokenizer, skip_prompt=True, skip_special_tokens=True)gen_kwargs = dict(inputs=input_ids,images=image_tensor,streamer=streamer,do_sample=(temperature > 0.0),temperature=float(temperature),top_p=float(top_p),max_new_tokens=int(max_new_tokens),2 unmodified linesstopping_criteria=[stopping],)try:t = Thread(target=chatbot.model.generate, kwargs=gen_kwargs)t.start()5 unmodified linesexcept Exception as e:return {"error": f"Generation failed: {e}"}# output_mode handlersif output_mode == "narrative":return {"status": "success", "response": text, "conversation_id": id(chatbot.conversation)}# For json & report_en → parse once, with robust fallbackstry:start = text.find("{"); end = text.rfind("}")if start == -1 or end == -1 or end <= start:raise ValueError("JSON braces not found")data = json.loads(text[start:end+1])data["_parse_mode"] = "direct"except Exception:cleaned = _coerce_pseudo_json(text)try:data = json.loads(cleaned)data["_parse_mode"] = "cleaned"except Exception:# Last resort: extract with regex from free textdata = _extract_fields_from_text(text)data["_parse_mode"] = "extracted"# Inject patient meta (local only)if patient_age_group:data["patient_age_group"] = patient_age_groupif patient_sex:14 unmodified lines# Fallbackreturn {"status": "success", "response": text, "conversation_id": id(chatbot.conversation)}# ========= Public API =========def query(payload: dict):global model_initialized, tokenizer, model, image_processor, context_len, argsif not model_initialized:16 unmodified linesdet_seed = payload.get("det_seed", None)output_mode = payload.get("output_mode", "narrative")# Optional patient meta (local use only)patient_age_group = payload.get("patient_age_group")patient_sex = payload.get("patient_sex")35 unmodified lines"device": str(next(model.parameters()).device) if model else "Unknown",}# ========= Init & Session =========class _Args:def __init__(self):self.model_path = os.getenv("HF_MODEL_ID", "PULSE-ECG/PULSE-7B")19 unmodified linestokenizer_, model_, image_processor_, context_len_ = load_pretrained_model(args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit)dbg(f"[init] loaded model/tokenizer/processor | context_len={context_len_}")try:_ = next(model_.parameters()).device1 unmodified lineif torch.cuda.is_available():model_ = model_.to(torch.device("cuda"))model_.eval()dbg(f"[init] device={next(model_.parameters()).device}, cuda={torch.cuda.is_available()}")expected_size = get_vision_expected_size(model_, default=336)try:if image_processor_ is None:from transformers import AutoProcessor, CLIPImageProcessortry:image_processor_ = AutoProcessor.from_pretrained(args.model_path)except Exception:clip_id = "openai/clip-vit-large-patch14-336" if expected_size >= 336 else "openai/clip-vit-large-patch14"image_processor_ = CLIPImageProcessor.from_pretrained(clip_id)force_processor_size(image_processor_, expected_size)except Exception as e_ip:warn(f"[init] image_processor fallback/size set failed: {e_ip}")# publishglobals()["tokenizer"] = tokenizer_globals()["model"] = model_globals()["image_processor"] = image_processor_globals()["context_len"] = context_len_chat_manager.init_if_needed(args, args.model_path, tokenizer_, model_, image_processor_, context_len_)print("[init] model/tokenizer/image_processor loaded.")return Trueexcept Exception as e:warn(f"[init] failed: {e}")return False# ========= HF EndpointHandler =========class EndpointHandler:"""Hugging Face Endpoint compatible."""def __init__(self, model_dir):self.model_dir = model_dirprint(f"EndpointHandler initialized with model_dir: {model_dir}")7 unmodified linesreturn get_model_info()if __name__ == "__main__":print("Handler ready (Deterministic JSON→Narrative with robust fallbacks, age+sex aware). Use `EndpointHandler` or `query`.")# ========= Optional FastAPI Wrapper =========try:from fastapi import FastAPIfrom pydantic import BaseModel3 unmodified lineswarn(f"fastapi/pydantic not available: {e}")if FASTAPI_AVAILABLE:app = FastAPI(title="PULSE ECG Handler API", version="1.4.0")class QueryIn(BaseModel):message: str | None = None46 unmodified linesdata["output_mode"] = "report_en"return query(data)else:app = None # uvicorn handler:app would fail if FastAPI is not installed
handler.py+224-127