no stop update
Browse files1234567834567891011127 unmodified lines20212222232425261 unmodified line282930303132333422 unmodified lines575859596061626 unmodified lines697071727273747576787778791 unmodified line81828386878485869 unmodified lines969798103104991001011021081091101111121131031041052 unmodified lines1081091101221111121251261271131141281291151161171 unmodified line1191201211371381391401411421431441221231241251261271282 unmodified lines13113213315315413413513613713813916114014114216514314414515 unmodified lines1611621631871641651661671911681691701951711721731741751762021771781791801812082092101821831842122132141851861871881892192201901911921932241941951961971981992302312322332342002012022032042053 unmodified lines2092102112442452122132142152502512162172182192542202212222234 unmodified lines2282292302652662672682692702712312322332342352362372382392732402412421 unmodified line2442452462812472482832842492502512522532542552562572582592602612622632642892652662672681 unmodified line2702712722972732742752762773023033042782792802812823092832842852865 unmodified lines2922932943213223232952962972982993003293013023033323043053343063073082 unmodified lines31131231331431531634434534634734834931731831932032135535635732232335936036136236336436536636736832432532632732837037137232933037333133233328 unmodified lines36236336440836536636736816 unmodified lines38538638743143243343443538838939039139214 unmodified lines4074084094564104114124 unmodified lines4174184194674204214224234244254744264274281 unmodified line430431432482483433485434435436# -*- coding: utf-8 -*-"""PULSE ECG Handler - Demo-like (sampling) + no_stop bayrağı- Demo davranışı: do_sample=True, temperature/top_p payload'dan- max_new_tokens: payload/slider değeri (KIRPMA YOK, direkt kullanılır)- İsteğe bağlı: no_stop=True ile stopping_criteria devre dışı- Tek görsel işleme; IM_START/END otomatik; 3D/4D/5D tensör uyumlu- Çıktıya post-format/deduplicate UYGULANMAZ (demo ile bire bir)PULSE ECG Handler - Demo-like sampling + no_stop (hard) + min_new_tokens auto- do_sample=True, temperature/top_p payload'dan- max_new_tokens: payload değeri (kırpma yok)- no_stop=True: stopping_criteria KAPALI + eos_token_id=None- no_stop=True ve min_new_tokens boşsa: otomatik min_new_tokens (uzun yanıt garantisi)- Tek görsel; IM_START/END otomatik; 3D/4D/5D tensör uyumlu- Post-format yok (demo davranışı)"""
import os7 unmodified linesfrom PIL import Imageimport requests
# --- Opsiyonel bağımlılıklar ---# --- Opsiyonel ---try: import cv2 CV2_AVAILABLE = True1 unmodified line CV2_AVAILABLE = False print("Warning: OpenCV (cv2) not available; video is disabled.")
# --- LLaVA / Transformers ---# --- LLaVA ---try: from llava.constants import ( IMAGE_TOKEN_INDEX,22 unmodified linesexcept Exception: HF_HUB_AVAILABLE = False
# ------------- HF Hub init (opsiyonel) -------------api = Nonerepo_name = ""if HF_HUB_AVAILABLE and "HF_TOKEN" in os.environ:6 unmodified lines api = None repo_name = ""
# ------------- Klasörler -------------LOGDIR = "./logs"VOTEDIR = "./votes"os.makedirs(LOGDIR, exist_ok=True)os.makedirs(VOTEDIR, exist_ok=True)
# ------------- Global durum -------------tokenizer = Nonemodel = Noneimage_processor = None1 unmodified lineargs = Nonemodel_initialized = False
# ------------- Yardımcılar -------------
def _safe_upload(path: str): if api and repo_name and os.path.isfile(path): try:9 unmodified linesdef _conv_log_path(): t = datetime.datetime.now() p = os.path.join(LOGDIR, f"{t.year:04d}-{t.month:02d}-{t.day:02d}-user_conv.json") os.makedirs(os.path.dirname(p), exist_ok=True ) os.makedirs(os.path.dirname(p), exist_ok=True) return p
def load_image_any(image_input): """ Desteklenen formatlar: - URL (http/https) - Yerel dosya yolu - base64 (opsiyonel data URL prefix ile) """ if isinstance(image_input, str): s = image_input.strip() if s.startswith(("http://", "https://")):2 unmodified lines return Image.open(BytesIO(r.content)).convert("RGB") if os.path.exists(s): return Image.open(s).convert("RGB") # base64 if s.startswith("data:image"): s = s.split(",", 1)[1] try: raw = base64.b64decode(s) return Image.open(BytesIO(raw)).convert("RGB") raw = base64.b64decode(s) return Image.open(BytesIO(raw)).convert("RGB") except Exception as e: raise ValueError(f"Invalid image string (not URL/path/base64): {e}") elif isinstance(image_input, dict) and "image" in image_input: return load_image_any(image_input["image"]) else:1 unmodified line
def _guess_conv_mode(model_path: str) -> str: name = get_model_name_from_path(model_path).lower() if "llama-2" in name: return "llava_llama_2" if "v1" in name or "pulse" in name: return "llava_v1" if "mpt" in name: return "mpt" if "qwen" in name: return "qwen_1_5" if "llama-2" in name: return "llava_llama_2" if "v1" in name or "pulse" in name: return "llava_v1" if "mpt" in name: return "mpt" if "qwen" in name: return "qwen_1_5" return "llava_v0"
def _wrap_image_token_if_needed(model_cfg) -> bool:2 unmodified lines except Exception: return False
# ------------- Çekirdek üretim -------------
def _build_prompt_and_ids(chatbot, user_text: str, device: torch.device): use_wrap = _wrap_image_token_if_needed(chatbot.model.config) if use_wrap: inp = f"{DEFAULT_IM_START_TOKEN}{DEFAULT_IMAGE_TOKEN}{DEFAULT_IM_END_TOKEN}\n{user_text}" else: 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)15 unmodified lines conv_mode_override: str | None = None, det_seed: int | None = None, no_stop: bool = False, min_new_tokens: int | None = None, # opsiyonel, uzunluğu zorlamak istersen min_new_tokens: int | None = None, # otomatik atanabilir): if not LLAVA_AVAILABLE: return {"error": "LLaVA modules not available"}
if not message_text or image_input is None: return {"error": "Both 'message' and 'image' are required"}
# Chatbot/konuşma hazırla (her çağrıda sıfırdan, demo gibi) 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() else: chatbot.conversation = conv_templates[chatbot.conv_mode].copy()
# Görüntüyü 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 kaydet img_hash = "NA" img_path = None # log img_hash, img_path = "NA", None try: buf = BytesIO() pil_img.save(buf, format="JPEG") img_bytes = buf.getvalue() buf = BytesIO(); pil_img.save(buf, format="JPEG"); img_bytes = buf.getvalue() img_hash = hashlib.md5(img_bytes).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) if not os.path.isfile(img_path): pil_img.save(img_path) except Exception as e: print(f"[log] saving image failed: {e}")
# Görüntüyü tensöre çevir # görüntü tensörü device = next(chatbot.model.parameters()).device dtype = next(chatbot.model.parameters()).dtype try: processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config) if isinstance(processed, torch.Tensor): if processed.ndim == 3: # (C,H,W) image_tensor = processed.unsqueeze(0) elif processed.ndim == 4: # (B,C,H,W) image_tensor = processed elif processed.ndim == 5: # (B,T,C,H,W) -> (B*T,C,H,W) if processed.ndim == 3: image_tensor = processed.unsqueeze(0) elif processed.ndim == 4: image_tensor = processed elif processed.ndim == 5: b,t,c,h,w = processed.shape image_tensor = processed.reshape(b*t, c, h, w) else:3 unmodified lines image_tensor = first.unsqueeze(0) if isinstance(first, torch.Tensor) and first.ndim == 3 else first else: return {"error": "Image processing returned empty"}
# Demo tarafında half + to(device) kalıbı yaygın image_tensor = image_tensor.to(device=device, dtype=dtype) except Exception as e: return {"error": f"Image processing failed: {e}"}
# Prompt & tokenizasyon prompt, input_ids = _build_prompt_and_ids(chatbot, message_text, device) # prompt & ids _, input_ids = _build_prompt_and_ids(chatbot, message_text, device) stopping = None if no_stop else _stopping(chatbot, input_ids)
# (opsiyonel) deterministik sampling # deterministik sample (opsiyonel) if det_seed is not None: try: det_seed = int(det_seed)4 unmodified lines except Exception: pass
# EOS/PAD güvenli al eos_id = chatbot.tokenizer.eos_token_id if eos_id is None: try: eos_id = chatbot.tokenizer.convert_tokens_to_ids("</s>") except Exception: eos_id = 0 # EOS/PAD eos_id = tokenizer.eos_token_id pad_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else (eos_id if eos_id is not None else 0) # no_stop=True ise eos'a göre durmayı tamamen kapat if no_stop: eos_for_gen = None else: eos_for_gen = eos_id
# generate kwargs (demo-like) gen_kwargs = dict( inputs=input_ids, images=image_tensor,1 unmodified line temperature=float(temperature), top_p=float(top_p), repetition_penalty=float(repetition_penalty), max_new_tokens=int(max_new_tokens), # KIRPMA YOK max_new_tokens=int(max_new_tokens), use_cache=False, pad_token_id=eos_id, eos_token_id=eos_id, pad_token_id=pad_id, eos_token_id=eos_for_gen, length_penalty=1.0, early_stopping=False, stopping_criteria=None if no_stop else [stopping], )
# min_new_tokens otomatik (no_stop=True ve kullanıcı vermediyse) if no_stop and (min_new_tokens is None): try: req = int(max_new_tokens) auto_min = max(300, min(req - 64, 1024)) # 300–1024 bandında güvenli if auto_min > 0: gen_kwargs["min_new_tokens"] = auto_min except Exception: pass if min_new_tokens is not None: elif min_new_tokens is not None: try: mn = int(min_new_tokens) if mn > 0 and mn <= int(max_new_tokens):1 unmodified line except Exception: pass
# Üretim # generate try: with torch.no_grad(): outputs = chatbot.model.generate(**gen_kwargs) gen = outputs[0][input_ids.shape[1]:] text = chatbot.tokenizer.decode(gen, skip_special_tokens=True)
# Konuşmaya yerleştir (demo gibi) text = tokenizer.decode(gen, skip_special_tokens=True) chatbot.conversation.messages[-1][-1] = text except Exception as e: return {"error": f"Generation failed: {e}"}
# Log yaz # log try: row = { "time": datetime.datetime.now().isoformat(),5 unmodified lines } 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()) if img_path: _safe_upload(img_path) _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)}
# ------------- API Yüzeyi -------------# -------- API --------
def query(payload: dict): """HF Endpoint ana giriş noktası (demo uyumlu)""" """HF Endpoint entry (demo-like)""" global model_initialized, tokenizer, model, image_processor, context_len, args
if not model_initialized: if not initialize_model(): return {"error": "Model initialization failed"}2 unmodified lines 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'."}
if not message.strip(): return {"error": "Missing 'message' text"} if image is None: return {"error": "Missing 'image'. Use 'image', 'image_url', or 'img'."}
# Demo: slider benzeri parametreler max_new_tokens = int(payload.get("max_output_tokens", payload.get("max_new_tokens", payload.get("max_tokens", 4096)))) temperature = float(payload.get("temperature", 0.05)) top_p = float(payload.get("top_p", 1.0)) repetition_penalty = float(payload.get("repetition_penalty", 1.0)) conv_mode_override = payload.get("conv_mode", None)
# (Opsiyonel) deterministik sample için seed det_seed = payload.get("det_seed", 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
# (Yeni) stopping_criteria kapatma bayrağı no_stop = bool(payload.get("no_stop", False))
# (Opsiyonel) min_new_tokens mnt = payload.get("min_new_tokens", None) try: det_seed = int(det_seed) except Exception: det_seed = None no_stop = bool(payload.get("no_stop", False)) mnt = payload.get("min_new_tokens", None) if mnt is not None: try: mnt = int(mnt) except Exception: try: mnt = int(mnt) except Exception: mnt = None mnt = None
return generate_response( message_text=message,28 unmodified lines "device": str(next(model.parameters()).device) if model else "Unknown", }
# ------------- Model init -------------# -------- init --------
class _Args: def __init__(self):16 unmodified lines tokenizer, model, image_processor, context_len ) conv_mode_auto = _guess_conv_mode(model_path) if args.conv_mode and args.conv_mode != conv_mode_auto: self.conv_mode = args.conv_mode else: self.conv_mode = conv_mode_auto args.conv_mode = conv_mode_auto self.conv_mode = args.conv_mode if args.conv_mode else conv_mode_auto args.conv_mode = self.conv_mode self.conversation = conv_templates[self.conv_mode].copy() self.num_frames = args.num_frames
14 unmodified lineschat_manager = ChatSessionManager()
def initialize_model(): """Modeli yükle (lazy)""" global tokenizer, model, image_processor, context_len, args if not LLAVA_AVAILABLE: print("LLaVA not available; cannot init.")4 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 ) # Cihaz try: _ = next(model.parameters()).device except Exception: if torch.cuda.is_available(): model = model.to(torch.device("cuda")) model.eval() # Chatbot init chat_manager.init_if_needed(args, args.model_path, tokenizer, model, image_processor, context_len) print("[init] model/tokenizer/image_processor loaded.") return True1 unmodified line print(f"[init] failed: {e}") return False
# ------------- HF EndpointHandler -------------
class EndpointHandler: """Hugging Face Endpoint uyumlu sınıf""" def __init__(self, model_dir): self.model_dir = model_dir print(f"EndpointHandler initialized with model_dir: {model_dir}")handler.py+68-120