Update handler.py
Browse files1234564 unmodified lines111213141516141 unmodified lines15815916016116216329 unmodified lines19319419519619719819963 unmodified lines2632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922931 unmodified line295296297298299300301302303150 unmodified lines45445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548623 unmodified lines51051151251351451551650 unmodified lines5675685695705715725735745753 unmodified lines579580581582583584585# -*- coding: utf-8 -*-"""PULSE ECG Handler — Demo Parity + Style Hint + Robust Fallbacks + Debug- Demo app.py ile aynı üretim ayarları:do_sample=True, temperature=0.05, top_p=1.0, max_new_tokens=4096- Stopping: konuşma ayırıcıda (conv.sep/sep2) güvenli token-eşleşmeli kriter4 unmodified lines- Post-process: yalnızca whitespace/biçim temizliği- Ekler:* DEBUG yardımcıları (ENV: DEBUG=1)* image_processor fallback (AutoProcessor → CLIPImageProcessor)* process_images fallback (torchvision + CLIP norm)* FastAPI wrapper: /health, /info, /query, /debug141 unmodified linesdef _postprocess_min(text: str) -> str:return _normalize_whitespace(text)# ====== Güvenli Stop Kriteri (conv separator) ======class SafeKeywordsStoppingCriteria(StoppingCriteria):def __init__(self, keyword: str, tokenizer):29 unmodified linesdef __init__(self):self.chatbot = Noneself.args = Noneself.model_path = Nonedef init_if_needed(self, args, model_path, tokenizer, model, image_processor, context_len):if self.chatbot is None:self.args = args63 unmodified linesdevice = next(chatbot.model.parameters()).devicedtype = torch.float16# Görüntü ön-işleme → tensör (fallback'lı)try:dbg(f"[pre] PIL image size={pil_img.size}, mode={pil_img.mode}, processor={type(chatbot.image_processor)}")processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)dbg("[pre] process_images ok")if isinstance(processed, (list, tuple)) and len(processed) > 0:image_tensor = processed[0]elif isinstance(processed, torch.Tensor):image_tensor = processed[0] if processed.ndim == 4 else processedelse:raise ValueError("Image processing returned empty")if image_tensor.ndim == 3:image_tensor = image_tensor.unsqueeze(0)image_tensor = image_tensor.to(device=device, dtype=dtype)dbg(f"[pre] tensor shape={tuple(image_tensor.shape)} dtype={image_tensor.dtype} device={image_tensor.device}")except Exception as e:warn(f"[pre] process_images failed: {e} → manual CLIP preprocess fallback kullanılacak.")try:from torchvision import transformsfrom torchvision.transforms import InterpolationModepreprocess = transforms.Compose([transforms.Resize(224, interpolation=InterpolationMode.BICUBIC),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073],1 unmodified line),])image_tensor = preprocess(pil_img).unsqueeze(0).to(device=device, dtype=dtype)dbg("[pre] manual CLIP preprocess fallback ok → tensor shape=" + str(tuple(image_tensor.shape)))except Exception as ee:return {"error": f"Image processing failed (and fallback failed): {ee}"}msg = (message_text or "").strip()msg = f"{msg}\n\n{STYLE_HINT}"150 unmodified linesmodel_.eval()dbg(f"[init] device={next(model_.parameters()).device}, cuda_available={torch.cuda.is_available()}")# --- image_processor fallback zinciri ---try:if image_processor_ is None:dbg("[init] image_processor None → AutoProcessor fallback deneniyor…")try:from transformers import AutoProcessorimage_processor_ = AutoProcessor.from_pretrained(args.model_path)dbg("[init] image_processor: AutoProcessor.from_pretrained(model_path) ile yüklendi.")except Exception as _e1:dbg(f"[init] AutoProcessor failed: {_e1} → CLIPImageProcessor fallback deneniyor…")from transformers import CLIPImageProcessorimage_processor_ = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14")warn("[init] image_processor: CLIPImageProcessor(openai/clip-vit-large-patch14) fallback kullanılıyor.")except Exception as _e:warn(f"[init] image_processor fallback failed: {_e}")# --- image_processor introspection ---try:ip = image_processor_if ip is not None:crop_sz = getattr(getattr(ip, "crop_size", None), "height", None) or getattr(ip, "crop_size", None)size_sz = getattr(getattr(ip, "size", None), "height", None) or getattr(ip, "size", None)dbg(f"[init] image_processor crop_size={crop_sz} size={size_sz} class={ip.__class__.__name__}")else:warn("[init] image_processor yine None (fallback da başarısız).")except Exception as e_ip:warn(f"[init] image_processor inspect error: {e_ip}")globals()["tokenizer"] = tokenizer_globals()["model"] = model_23 unmodified linesreturn get_model_info()if __name__ == "__main__":print("Handler ready (Demo Parity + Style Hint + whitespace post-process + fallbacks + debug). Use `EndpointHandler` or `query`.")# ===================== Minimal FastAPI Wrapper =====================try:50 unmodified linesip = image_processorip_cls = ip.__class__.__name__ if ip else Nonecrop_sz = getattr(getattr(ip, "crop_size", None), "height", None) or getattr(ip, "crop_size", None)size_sz = getattr(getattr(ip, "size", None), "height", None) or getattr(ip, "size", None)except Exception:ip_cls, crop_sz, size_sz = None, None, Nonereturn {"debug": bool(DEBUG),3 unmodified lines"context_len": context_len,"image_processor_class": ip_cls,"image_processor_crop_size": crop_sz,"image_processor_size": size_sz,"model_path": args.model_path if args else None,}1234564 unmodified lines11121314151617141 unmodified lines15916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021129 unmodified lines24124224324424524624763 unmodified lines3113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583591 unmodified line361362363364365366367368369370150 unmodified lines52152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057123 unmodified lines59559659759859960060150 unmodified lines6526536546556566576586596606616626636646653 unmodified lines669670671672673674675676# -*- coding: utf-8 -*-"""PULSE ECG Handler — Demo Parity + Style Hint + Robust Fallbacks + Debug + Dynamic Vision Size- Demo app.py ile aynı üretim ayarları:do_sample=True, temperature=0.05, top_p=1.0, max_new_tokens=4096- Stopping: konuşma ayırıcıda (conv.sep/sep2) güvenli token-eşleşmeli kriter4 unmodified lines- Post-process: yalnızca whitespace/biçim temizliği- Ekler:* DEBUG yardımcıları (ENV: DEBUG=1)* Dynamic vision size: vision tower -> processor + preprocess/fallback* image_processor fallback (AutoProcessor → CLIPImageProcessor)* process_images fallback (torchvision + CLIP norm)* FastAPI wrapper: /health, /info, /query, /debug141 unmodified linesdef _postprocess_min(text: str) -> str:return _normalize_whitespace(text)# ====== Vision helpers (dynamic size) ======def get_vision_expected_size(m, default: int = 336) -> int:"""Modelin vision tower'ının beklediği input boyutunu döndürür (örn. 336).LLaVA/CLIP konfiglerinde genelde `image_size` bulunur."""try:vt = m.get_vision_tower()vt_cfg = getattr(getattr(vt, "vision_tower", vt), "config", None)if vt_cfg is None:return defaultif getattr(vt_cfg, "image_size", None):return int(vt_cfg.image_size)vc = getattr(vt_cfg, "vision_config", None)if vc and getattr(vc, "image_size", None):return int(vc.image_size)except Exception as e:dbg(f"[get_vision_expected_size] fallback default={default} because: {e}")return defaultdef force_processor_size(proc, size: int):"""Processor'ın resize/crop alanlarını güvenle hedef boyuta zorlar."""try:# sizeif 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}# crop_sizeif 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}")# ====== Güvenli Stop Kriteri (conv separator) ======class SafeKeywordsStoppingCriteria(StoppingCriteria):def __init__(self, keyword: str, tokenizer):29 unmodified linesdef __init__(self):self.chatbot = Noneself.args = Noneself.model_path = Nonedef init_if_needed(self, args, model_path, tokenizer, model, image_processor, context_len):if self.chatbot is None:self.args = args63 unmodified linesdevice = next(chatbot.model.parameters()).devicedtype = torch.float16# === Görüntü ön-işleme → tensör (dinamik boy) ===expected_size = get_vision_expected_size(chatbot.model, default=336)dbg(f"[pre] dynamic expected_size={expected_size} | processor={type(chatbot.image_processor)}")# 3.1) Processor.preprocess varsa kullan (en stabil yol)image_tensor = Nonetry:if hasattr(chatbot.image_processor, "preprocess"):px = chatbot.image_processor.preprocess(pil_img, return_tensors="pt")image_tensor = px.get("pixel_values", px)if not isinstance(image_tensor, torch.Tensor):# Bazı processor'lar nested dict döndürebilirimage_tensor = image_tensor["pixel_values"]if image_tensor.ndim == 3:image_tensor = image_tensor.unsqueeze(0)image_tensor = image_tensor.to(device=device, dtype=dtype)dbg(f"[pre] processor.preprocess ok → {tuple(image_tensor.shape)}")else:raise AttributeError("processor has no preprocess")except Exception as e_pre:warn(f"[pre] processor.preprocess not used: {e_pre} → process_images denenecek…")# 3.2) LLaVA'nın process_images yolutry:processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)if isinstance(processed, (list, tuple)) and len(processed) > 0:image_tensor = processed[0]elif isinstance(processed, torch.Tensor):image_tensor = processed[0] if processed.ndim == 4 else processedelse:raise ValueError("process_images returned empty")if image_tensor.ndim == 3:image_tensor = image_tensor.unsqueeze(0)image_tensor = image_tensor.to(device=device, dtype=dtype)dbg(f"[pre] process_images ok → {tuple(image_tensor.shape)}")except Exception as e_proc:warn(f"[pre] process_images failed: {e_proc} → manual CLIP fallback (dinamik size) kullanılacak.")# 3.3) Manuel CLIP fallback (dinamik expected_size)from torchvision import transformsfrom torchvision.transforms import InterpolationModepreprocess = transforms.Compose([transforms.Resize(expected_size, interpolation=InterpolationMode.BICUBIC),transforms.CenterCrop(expected_size),transforms.ToTensor(),transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073],1 unmodified line),])image_tensor = preprocess(pil_img).unsqueeze(0).to(device=device, dtype=dtype)dbg(f"[pre] manual fallback ok → {tuple(image_tensor.shape)}")if image_tensor is None:return {"error": "Image processing failed (no tensor produced)"}msg = (message_text or "").strip()msg = f"{msg}\n\n{STYLE_HINT}"150 unmodified linesmodel_.eval()dbg(f"[init] device={next(model_.parameters()).device}, cuda_available={torch.cuda.is_available()}")# --- vision tower beklenen image_size'ı al ---expected_size = get_vision_expected_size(model_, default=336)dbg(f"[init] vision expected image_size={expected_size}")# --- image_processor fallback zinciri (model path > AutoProcessor > CLIP 224/336) ---try:if image_processor_ is None:dbg("[init] image_processor None → AutoProcessor(model_path) deneniyor…")try:from transformers import AutoProcessorimage_processor_ = AutoProcessor.from_pretrained(args.model_path)dbg("[init] image_processor: AutoProcessor.from_pretrained(model_path) yüklendi.")except Exception as _e1:dbg(f"[init] AutoProcessor(model_path) failed: {_e1}")try:from transformers import AutoProcessorclip_id = "openai/clip-vit-large-patch14-336" if expected_size >= 336 else "openai/clip-vit-large-patch14"image_processor_ = AutoProcessor.from_pretrained(clip_id)dbg(f"[init] AutoProcessor({clip_id}) yüklendi.")except Exception as _e2: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)warn(f"[init] CLIPImageProcessor({clip_id}) fallback kullanılıyor.")except Exception as _e:warn(f"[init] image_processor fallback chain failed: {_e}")# --- processor'ın boyutlarını vision tower'a uydur ---try:if image_processor_ is not None:force_processor_size(image_processor_, expected_size)except Exception as e_ip:warn(f"[init] processor size set error: {e_ip}")# --- image_processor introspection ---try:ip = image_processor_if ip is not None:crop_sz = getattr(getattr(ip, "crop_size", None), "height", None) or getattr(ip, "crop_size", None)size_sz = getattr(getattr(ip, "size", None), "shortest_edge", None) or getattr(ip, "size", None)dbg(f"[init] image_processor crop_size={crop_sz} size={size_sz} class={ip.__class__.__name__}")else:warn("[init] image_processor yine None (fallback da başarısız).")except Exception as e_ip2:warn(f"[init] image_processor inspect error: {e_ip2}")globals()["tokenizer"] = tokenizer_globals()["model"] = model_23 unmodified linesreturn get_model_info()if __name__ == "__main__":print("Handler ready (Demo Parity + Style Hint + whitespace post-process + dynamic size + fallbacks + debug). Use `EndpointHandler` or `query`.")# ===================== Minimal FastAPI Wrapper =====================try:50 unmodified linesip = image_processorip_cls = ip.__class__.__name__ if ip else Nonecrop_sz = getattr(getattr(ip, "crop_size", None), "height", None) or getattr(ip, "crop_size", None)size_short = getattr(getattr(ip, "size", None), "shortest_edge", None) or getattr(ip, "size", None)except Exception:ip_cls, crop_sz, size_short = None, None, Nonetry:ve = get_vision_expected_size(model, default=None) if model else Noneexcept Exception:ve = Nonereturn {"debug": bool(DEBUG),3 unmodified lines"context_len": context_len,"image_processor_class": ip_cls,"image_processor_crop_size": crop_sz,"image_processor_size": {"shortest_edge": size_short},"vision_expected_image_size": ve,"model_path": args.model_path if args else None,}
handler.py+130-39