Update handler.py
Browse files12334567891011101112131415161718194 unmodified lines2425262227282930313233343536373839404142434445464748495030313233515253363738394054555657584559606162636465526667686910 unmodified lines808182698384858618 unmodified lines105106107941081091104 unmodified lines11511611710511811912012127 unmodified lines1491501511391401411421431441521531541 unmodified line15615715815215916016116215715815916316416511 unmodified lines1771781791771801811822 unmodified lines18518618718618818919010 unmodified lines2012022032032042052062072082092102102112122132152142152168 unmodified lines2252262272302282292302312322332342372352362372412382392402412432422432442452482462472482492502542512522534 unmodified lines2582592602652612622672632692642652712662672682692702712722732742752762792772782792812822802812822832842852862872882892902912922932942952962972982992843003012863023033043053062913073083092953103113124 unmodified lines3173183193063073083093203213113223233243253163173183193203263273283293303313223323333343253353363371 unmodified line3393403413333423433443453463383473483497 unmodified lines3573583593523603613623633643573653663678 unmodified lines37637737837237938038137638238338438533 unmodified lines4194204214164224234244254264224274284294303 unmodified lines4344354364324374384394404414424434444454464404474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864 unmodified lines4914924934574944954964974614984995009 unmodified lines51051151247747851351451551648251751848451952052148852252352452552652749452852953025 unmodified lines556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587528588530589590# -*- coding: utf-8 -*-"""PULSE ECG Handler — Demo Parity + Style HintPULSE 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 kriter- Görsel tensörü: .half() ve model cihazında- Streamer: TextIteratorStreamer (demo gibi), thread ile generate- Seed/deterministic KAPALI (göndermezseniz); demo gibi stokastik- STYLE_HINT: demo üslubuna (narratif + sonda tek satır structured impression) yaklaşmak için- Post-process: YALNIZCA whitespace/biçim normalizasyonu (yönetim/öneri cümleleri korunur)- STYLE_HINT: demo üslubuna (narratif + sonda tek satır structured impression)- 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, /debug"""
import os4 unmodified linesimport datetimefrom io import BytesIOfrom threading import Threadfrom typing import Optional, Unionfrom typing import Optional, Union, Any, Dict
import torchfrom PIL import Imageimport requests
# ====== Debug Helpers ======def _env_bool(name: str, default: bool = False) -> bool: v = os.getenv(name) if v is None: return default return str(v).strip().lower() in {"1", "true", "yes", "y", "on"}
DEBUG = _env_bool("DEBUG", False)
def dbg(*args, **kwargs): if DEBUG: print("[DEBUG]", *args, **kwargs)
def warn(*args, **kwargs): print("[WARN]", *args, **kwargs)
# ====== LLaVA & Transformers ======try: from llava.constants import ( IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, ) from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN from llava.conversation import conv_templates, SeparatorStyle from llava.model.builder import load_pretrained_model from llava.mm_utils import ( tokenizer_image_token, process_images, get_model_name_from_path, ) from llava.mm_utils import tokenizer_image_token, process_images, get_model_name_from_path from llava.utils import disable_torch_init LLAVA_AVAILABLE = Trueexcept Exception as e: LLAVA_AVAILABLE = False print(f"[WARN] LLaVA not available: {e}") warn(f"LLaVA not available: {e}")
try: from transformers import TextIteratorStreamer, StoppingCriteria TRANSFORMERS_AVAILABLE = Trueexcept Exception as e: TRANSFORMERS_AVAILABLE = False print(f"[WARN] transformers not available: {e}") warn(f"transformers not available: {e}")
# ====== HF Hub logging (opsiyonel) ======try:10 unmodified lines api = HfApi() repo_name = os.environ.get("LOG_REPO", "") except Exception as e: print(f"[HF Hub] init failed: {e}") warn(f"[HF Hub] init failed: {e}") api = None repo_name = ""
18 unmodified lines)
# ===================== Utilities =====================
def _safe_upload(path: str): if api and repo_name and path and os.path.isfile(path): try:4 unmodified lines repo_type="dataset", ) except Exception as e: print(f"[upload] failed for {path}: {e}") warn(f"[upload] failed for {path}: {e}")
def _conv_log_path() -> str: t = datetime.datetime.now()27 unmodified lines raise ValueError("Unsupported image input format")
def _normalize_whitespace(text: str) -> str: """ Gereksiz boşluk/boş satırları toparlar: - Satır başı/sonu boşluklarını siler - Birden çok boşluğu tek boşluğa indirger - 3+ boş satırı 1 boş satıra indirger """ text = text.replace("\r\n", "\n").replace("\r", "\n") lines = [re.sub(r"[ \t]+", " ", ln.strip()) for ln in text.split("\n")] text = "\n".join(lines).strip()1 unmodified line return text
def _postprocess_min(text: str) -> str: # Yalnızca whitespace/biçim temizliği return _normalize_whitespace(text)
# ====== Güvenli Stop Kriteri (conv separator) ======class SafeKeywordsStoppingCriteria(StoppingCriteria): """ conv.sep/sep2 bazlı token eşleşmesi; tensör → bool hatası yok. """ def __init__(self, keyword: str, tokenizer): self.tokenizer = tokenizer tok = tokenizer(keyword, add_special_tokens=False, return_tensors="pt").input_ids[0]11 unmodified lines return torch.equal(tail, kw)
# ===================== Core Generation =====================
class InferenceDemo: def __init__(self, args, model_path, tokenizer_, model_, image_processor_, context_len_): if not LLAVA_AVAILABLE:2 unmodified lines self.tokenizer, self.model, self.image_processor, self.context_len = ( tokenizer_, model_, image_processor_, context_len_ ) # Parite için sabit şablon self.conv_mode = "llava_v1" self.conversation = conv_templates[self.conv_mode].copy() self.num_frames = getattr(args, "num_frames", 16)10 unmodified lines self.chatbot = InferenceDemo(args, model_path, tokenizer, model, image_processor, context_len) def get_chatbot(self, args, model_path, tokenizer, model, image_processor, context_len): self.init_if_needed(args, model_path, tokenizer, model, image_processor, context_len) # Her çağrıda taze template (demo gibi yeni tur) self.chatbot.conversation = conv_templates[self.chatbot.conv_mode].copy() return self.chatbot
chat_manager = ChatSessionManager()
def _build_prompt_and_ids(chatbot, user_text: str, device: torch.device): # DEMO PARİTE: sarım yok, tek görüntü için tek image token inp = f"{DEFAULT_IMAGE_TOKEN}\n{user_text}" chatbot.conversation.append_message(chatbot.conversation.roles[0], inp) chatbot.conversation.append_message(chatbot.conversation.roles[1], None) prompt = chatbot.conversation.get_prompt()
input_ids = tokenizer_image_token( prompt, chatbot.tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt" ).unsqueeze(0).to(device)8 unmodified lines max_new_tokens: Optional[int] = None, conv_mode_override: Optional[str] = None, repetition_penalty: Optional[float] = None, det_seed: Optional[int] = None, # None → stokastik (demo gibi) det_seed: Optional[int] = None,): if not (LLAVA_AVAILABLE and TRANSFORMERS_AVAILABLE): return {"error": "Required libraries not available (llava/transformers)"} if not message_text or image_input is None: return {"error": "Both 'message' and 'image' are required"}
# Varsayılanlar → demo if temperature is None: temperature = 0.05 if top_p is None: top_p = 1.0 if max_new_tokens is None: max_new_tokens = 4096 if repetition_penalty is None: repetition_penalty = 1.0 # etkisiz if repetition_penalty is None: repetition_penalty = 1.0
dbg(f"[gen] temperature={temperature} top_p={top_p} max_new_tokens={max_new_tokens} rep={repetition_penalty} seed={det_seed}")
# Chat session 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()
# Görüntü yükle try: pil_img = load_image_any(image_input) except Exception as e: return {"error": f"Failed to load image: {e}"}
# Log için hash+path img_hash, img_path = "NA", None try: buf = BytesIO(); pil_img.save(buf, format="JPEG"); raw = buf.getvalue()4 unmodified lines if not os.path.isfile(img_path): pil_img.save(img_path) except Exception as e: print(f"[log] save image failed: {e}") warn(f"[log] save image failed: {e}")
# Cihaz/dtype device = next(chatbot.model.parameters()).device dtype = torch.float16 # demo: half dtype = torch.float16
# Görüntü ön-işleme → tensör # 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 processed else: return {"error": "Image processing returned empty"} raise ValueError("Image processing returned empty")
if image_tensor.ndim == 3: image_tensor = image_tensor.unsqueeze(0) # (1,C,H,W) image_tensor = image_tensor.to(device=device, dtype=dtype) # demo: half + device 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 transforms from torchvision.transforms import InterpolationMode preprocess = transforms.Compose([ transforms.Resize(224, interpolation=InterpolationMode.BICUBIC), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize( mean=[0.48145466, 0.4578275, 0.40821073], std=[0.26862954, 0.26130258, 0.27577711] ), ]) 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: {e}"} return {"error": f"Image processing failed (and fallback failed): {ee}"}
# STYLE_HINT ekle ve prompt hazırla msg = (message_text or "").strip() msg = f"{msg}\n\n{STYLE_HINT}" dbg(f"[prompt] conv_sep_style={chatbot.conversation.sep_style} sep_len={len(chatbot.conversation.sep)}") _, input_ids = _build_prompt_and_ids(chatbot, msg, device)
# Stop string (conv separator) → güvenli kriter stop_str = chatbot.conversation.sep if chatbot.conversation.sep_style != SeparatorStyle.TWO else chatbot.conversation.sep2 stopping = SafeKeywordsStoppingCriteria(stop_str, chatbot.tokenizer)
# Seed (gönderilmediyse stokastik → demo gibi) if det_seed is not None: try: s = int(det_seed)4 unmodified lines except Exception: pass
# Streamer (demo gibi) streamer = TextIteratorStreamer( chatbot.tokenizer, skip_prompt=True, skip_special_tokens=True ) streamer = TextIteratorStreamer(chatbot.tokenizer, skip_prompt=True, skip_special_tokens=True)
# Generate kwargs — demo ayarları gen_kwargs = dict( inputs=input_ids, images=image_tensor, streamer=streamer, do_sample=True, # DEMO temperature=float(temperature), # DEMO default 0.05 top_p=float(top_p), # DEMO default 1.0 max_new_tokens=int(max_new_tokens), # DEMO slider repetition_penalty=float(repetition_penalty), # default 1.0 → etkisiz do_sample=True, temperature=float(temperature), top_p=float(top_p), max_new_tokens=int(max_new_tokens), repetition_penalty=float(repetition_penalty), use_cache=False, stopping_criteria=[stopping], # DEMO-benzeri durdurma stopping_criteria=[stopping], )
# Üretim (arka thread) + akışı topla try: t = Thread(target=chatbot.model.generate, kwargs=gen_kwargs) t.start()1 unmodified line for piece in streamer: chunks.append(piece) text = "".join(chunks) text = _postprocess_min(text) # yalnızca whitespace/format temizliği text = _postprocess_min(text) chatbot.conversation.messages[-1][-1] = text except Exception as e: return {"error": f"Generation failed: {e}"}
# Log try: row = { "time": datetime.datetime.now().isoformat(),7 unmodified lines f.write(json.dumps(row, ensure_ascii=False) + "\n") _safe_upload(_conv_log_path()); _safe_upload(img_path or "") except Exception as e: print(f"[log] failed: {e}") warn(f"[log] failed: {e}")
return {"status": "success", "response": text, "conversation_id": id(chatbot.conversation)}
# ===================== Public API =====================
def query(payload: dict): """HF Endpoint entry (demo-like).""" global model_initialized, tokenizer, model, image_processor, context_len, args8 unmodified lines if not message.strip(): return {"error": "Missing 'message' text"} if image is None: return {"error": "Missing 'image'. Use 'image', 'image_url', or 'img'."}
# Demo varsayılanları — payload override edebilir temperature = float(payload.get("temperature", 0.05)) top_p = float(payload.get("top_p", 1.0)) max_new_tokens = int(payload.get("max_output_tokens", payload.get("max_new_tokens", payload.get("max_tokens", 4096)))) repetition_penalty = float(payload.get("repetition_penalty", 1.0)) # etkisiz default repetition_penalty = float(payload.get("repetition_penalty", 1.0))
conv_mode_override = payload.get("conv_mode", None) det_seed = payload.get("det_seed", None)33 unmodified lines }
# ===================== Init & Session =====================
class _Args: def __init__(self): self.model_path = os.getenv("HF_MODEL_ID", "PULSE-ECG/PULSE-7B") self.model_base = None self.num_gpus = int(os.getenv("NUM_GPUS", "1")) self.conv_mode = "llava_v1" # Parite için sabit self.conv_mode = "llava_v1" self.max_new_tokens = int(os.getenv("MAX_NEW_TOKENS", "4096")) self.num_frames = 16 self.load_8bit = bool(int(os.getenv("LOAD_8BIT", "0")))3 unmodified linesdef initialize_model(): global tokenizer, model, image_processor, context_len, args if not LLAVA_AVAILABLE: print("[init] LLaVA not available; cannot init.") warn("[init] LLaVA not available; cannot init.") return False try: args = _Args() dbg(f"[init] HF_MODEL_ID={args.model_path} | LOAD_8BIT={args.load_8bit} | LOAD_4BIT={args.load_4bit}") model_name = get_model_name_from_path(args.model_path)
tokenizer_, model_, image_processor_, context_len_ = load_pretrained_model( args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit ) # demo: model'ı genelde cuda’da çalıştırır dbg(f"[init] load_pretrained_model ok | tokenizer={type(tokenizer_)} | model={type(model_)} | image_processor={type(image_processor_)} | context_len={context_len_}")
try: _ = next(model_.parameters()).device except Exception: if torch.cuda.is_available(): model_ = model_.to(torch.device("cuda")) model_.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 AutoProcessor image_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 CLIPImageProcessor image_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_4 unmodified lines print("[init] model/tokenizer/image_processor loaded.") return True except Exception as e: print(f"[init] failed: {e}") warn(f"[init] failed: {e}") return False
# ===================== HF EndpointHandler =====================
class EndpointHandler: """Hugging Face Endpoint uyumlu sınıf""" def __init__(self, model_dir):9 unmodified lines return get_model_info()
if __name__ == "__main__": print("Handler ready (Demo Parity + Style Hint + whitespace post-process). Use `EndpointHandler` or `query`.")
print("Handler ready (Demo Parity + Style Hint + whitespace post-process + fallbacks + debug). Use `EndpointHandler` or `query`.")
# ===================== Minimal FastAPI Wrapper =====================try: from fastapi import FastAPI, Body from fastapi import FastAPI from pydantic import BaseModel from typing import Any, Dict FASTAPI_AVAILABLE = Trueexcept Exception as e: FASTAPI_AVAILABLE = False print(f"[WARN] fastapi/pydantic not available: {e}") warn(f"fastapi/pydantic not available: {e}")
if FASTAPI_AVAILABLE: app = FastAPI(title="PULSE ECG Handler API", version="1.0.0")
class QueryIn(BaseModel): # Hugging Face Endpoint tarzı payload ile uyumlu message: str | None = None query: str | None = None prompt: str | None = None25 unmodified lines async def _info(): return get_model_info()
@app.get("/debug") async def _debug(): try: dev = str(next(model.parameters()).device) if model else "Unknown" except Exception: dev = "Unknown"
try: ip = image_processor ip_cls = ip.__class__.__name__ if ip else 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) except Exception: ip_cls, crop_sz, size_sz = None, None, None
return { "debug": bool(DEBUG), "llava_available": LLAVA_AVAILABLE, "transformers_available": TRANSFORMERS_AVAILABLE, "device": dev, "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, }
@app.post("/query") async def _query(payload: QueryIn): # Boş alanları at, handler.query interface'ine aynen gönder return query({k: v for k, v in payload.dict().items() if v is not None})
else: app = None # uvicorn handler:app çalıştırıldığında import error verirhandler.py+140-82