Update handler.py
Browse files1234563456791011891011131213143 unmodified lines18192023212223102 unmodified lines12612712813212913013113513213313413522 unmodified lines15815916016416116216316425 unmodified lines1901911921931941951961971981992002012028 unmodified lines2112122132152142152163 unmodified lines2202212222252262232242252262272302282292302312322332362372382392342352362372422382392402412422472432442452462472482532492502512526 unmodified lines2592602612662622632642652702662672682693 unmodified lines2732742752762772782792802812852822832842852862902872882892902942952912922932942953 unmodified lines2993003013053023033043053063103113073083093103113123163173183193203133143153163173183193203213223233223243253243263273283296 unmodified lines3363373383373393403413429 unmodified lines3523533543533553563573585 unmodified lines36436536636536736836937037137237137237337337437537637737637737837938038138238338338438538638750 unmodified lines4384394404404414424434444454464474484474494504514524524534544558 unmodified lines464465466467468469470471472473474475476477478479477480# -*- coding: utf-8 -*-"""PULSE ECG Handler — Demo Parity + Style Hint- 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 kriterPULSE ECG Handler — Deterministik Versiyon- Üretim ayarları: do_sample=False (Tutarlı çıktı), temperature/top_p etkisiz- 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)- Seed/deterministic KAPALI (do_sample=False ile determinizm sağlanır)- STYLE_HINT: demo üslubuna yaklaşmak için- Post-process: YALNIZCA whitespace/biçim normalizasyonu"""
import osimport reimport json3 unmodified linesfrom io import BytesIOfrom threading import Threadfrom typing import Optional, Union
import torchfrom PIL import Imageimport requests102 unmodified lines s = s.split(",", 1)[1] raw = base64.b64decode(s) return Image.open(BytesIO(raw)).convert("RGB")
if isinstance(image_input, dict) and "image" in image_input: return load_image_any(image_input["image"])
raise ValueError("Unsupported image input format")
def _normalize_whitespace(text: str) -> str:22 unmodified lines self.tokenizer = tokenizer tok = tokenizer(keyword, add_special_tokens=False, return_tensors="pt").input_ids[0] self.kw_ids = tok # shape: (n,)
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool: if input_ids is None or input_ids.shape[0] == 0: return False25 unmodified lines self.chatbot = None self.args = None self.model_path = None def init_if_needed(self, args, model_path, tokenizer, model, image_processor, context_len): if self.chatbot is None: self.args = args self.model_path = model_path 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)8 unmodified lines 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)3 unmodified lines message_text: str, image_input, *, temperature: Optional[float] = None, top_p: Optional[float] = None, temperature: Optional[float] = None, # Deterministik modda yoksayılır top_p: Optional[float] = None, # Deterministik modda yoksayılır 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, # Deterministik modda yoksayılır): 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 # Varsayılanlar if max_new_tokens is None: max_new_tokens = 4096 if repetition_penalty is None: repetition_penalty = 1.0 # etkisiz
# 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:6 unmodified lines pil_img.save(img_path) except Exception as e: print(f"[log] save image failed: {e}")
# Cihaz/dtype device = next(chatbot.model.parameters()).device dtype = torch.float16 # demo: half
# Görüntü ön-işleme → tensör try: processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)3 unmodified lines image_tensor = processed[0] if processed.ndim == 4 else processed else: return {"error": "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 except Exception as e: return {"error": f"Image processing failed: {e}"}
# STYLE_HINT ekle ve prompt hazırla msg = (message_text or "").strip() msg = f"{msg}\n\n{STYLE_HINT}" _, 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) # Seed (do_sample=False olduğu için önemsiz, ancak kodda bırakılabilir) if det_seed is not None: try: s = int(det_seed)3 unmodified lines torch.cuda.manual_seed_all(s) except Exception: pass
# Streamer (demo gibi) streamer = TextIteratorStreamer( chatbot.tokenizer, skip_prompt=True, skip_special_tokens=True )
# Generate kwargs — demo ayarları # Generate kwargs — Deterministik 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 # 🟢 ÖNEMLİ DEĞİŞİKLİK: Deterministiği (Tutarlılığı) Aç do_sample=False, # temperature ve top_p ayarları artık yoksayılır # 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)6 unmodified lines chatbot.conversation.messages[-1][-1] = text except Exception as e: return {"error": f"Generation failed: {e}"}
# Log try: row = {9 unmodified lines _safe_upload(_conv_log_path()); _safe_upload(img_path or "") except Exception as e: print(f"[log] failed: {e}")
return {"status": "success", "response": text, "conversation_id": id(chatbot.conversation)}
# ===================== Public API =====================5 unmodified lines if not initialize_model(): return {"error": "Model initialization failed"} model_initialized = True
try: message = payload.get("message") or payload.get("query") or payload.get("prompt") or payload.get("istem") or "" image = payload.get("image") or payload.get("image_url") or payload.get("img") or None 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)) # Deterministik modda temperature/top_p yoksayılır, ancak API uyumluluğu için tutulur temperature = float(payload.get("temperature", 0.0)) # Default 0.0 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) if det_seed is not None: try: det_seed = int(det_seed) except Exception: det_seed = None
return generate_response( message_text=message, image_input=image,50 unmodified lines 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 # model'ı cuda’ya taşı try: _ = next(model_.parameters()).device except Exception: if torch.cuda.is_available(): model_ = model_.to(torch.device("cuda")) model_.eval()
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 True8 unmodified lines def __init__(self, model_dir): self.model_dir = model_dir print(f"EndpointHandler initialized with model_dir: {model_dir}") def __call__(self, payload): if "inputs" in payload: return query(payload["inputs"]) return query(payload) def health_check(self): return health_check() def get_model_info(self): return get_model_info()
if __name__ == "__main__": print("Handler ready (Demo Parity + Style Hint + whitespace post-process). Use `EndpointHandler` or `query`.") print("Handler ready (Deterministik Mode: do_sample=False). Use `EndpointHandler` or `query`.")handler.py+55-52