Update handler.py
Browse files- handler.py +33 -7
handler.py
CHANGED
|
@@ -119,7 +119,12 @@ def load_image_any(image_input: Union[str, dict]) -> Image.Image:
|
|
| 119 |
if isinstance(image_input, str):
|
| 120 |
s = image_input.strip()
|
| 121 |
if s.startswith(("http://", "https://")):
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
r.raise_for_status()
|
| 124 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 125 |
if os.path.exists(s):
|
|
@@ -270,6 +275,8 @@ def generate_response(
|
|
| 270 |
|
| 271 |
# Görüntü ön-işleme → tensör
|
| 272 |
try:
|
|
|
|
|
|
|
| 273 |
processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)
|
| 274 |
if isinstance(processed, (list, tuple)) and len(processed) > 0:
|
| 275 |
image_tensor = processed[0]
|
|
@@ -361,7 +368,7 @@ def query(payload: dict):
|
|
| 361 |
if not model_initialized:
|
| 362 |
if not initialize_model():
|
| 363 |
return {"error": "Model initialization failed"}
|
| 364 |
-
model_initialized
|
| 365 |
|
| 366 |
try:
|
| 367 |
message = payload.get("message") or payload.get("query") or payload.get("prompt") or payload.get("istem") or ""
|
|
@@ -427,7 +434,7 @@ class _Args:
|
|
| 427 |
self.debug = bool(int(os.getenv("DEBUG", "0")))
|
| 428 |
|
| 429 |
def initialize_model():
|
| 430 |
-
global tokenizer, model, image_processor, context_len, args
|
| 431 |
if not LLAVA_AVAILABLE:
|
| 432 |
print("[init] LLaVA not available; cannot init.")
|
| 433 |
return False
|
|
@@ -437,6 +444,23 @@ def initialize_model():
|
|
| 437 |
tokenizer_, model_, image_processor_, context_len_ = load_pretrained_model(
|
| 438 |
args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit
|
| 439 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
# demo: model'ı genelde cuda’da çalıştırır
|
| 441 |
try:
|
| 442 |
_ = next(model_.parameters()).device
|
|
@@ -445,12 +469,14 @@ def initialize_model():
|
|
| 445 |
model_ = model_.to(torch.device("cuda"))
|
| 446 |
model_.eval()
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
|
|
|
| 452 |
|
| 453 |
chat_manager.init_if_needed(args, args.model_path, tokenizer_, model_, image_processor_, context_len_)
|
|
|
|
| 454 |
print("[init] model/tokenizer/image_processor loaded.")
|
| 455 |
return True
|
| 456 |
except Exception as e:
|
|
|
|
| 119 |
if isinstance(image_input, str):
|
| 120 |
s = image_input.strip()
|
| 121 |
if s.startswith(("http://", "https://")):
|
| 122 |
+
# ✅ UA ekleyerek CDN/Cloudflare engellerini azalt
|
| 123 |
+
r = requests.get(
|
| 124 |
+
s,
|
| 125 |
+
timeout=(5, 20),
|
| 126 |
+
headers={"User-Agent": "Mozilla/5.0 (compatible; RapidECG/1.0)"}
|
| 127 |
+
)
|
| 128 |
r.raise_for_status()
|
| 129 |
return Image.open(BytesIO(r.content)).convert("RGB")
|
| 130 |
if os.path.exists(s):
|
|
|
|
| 275 |
|
| 276 |
# Görüntü ön-işleme → tensör
|
| 277 |
try:
|
| 278 |
+
if chatbot.image_processor is None:
|
| 279 |
+
return {"error": "Image processing failed: image_processor is None"}
|
| 280 |
processed = process_images([pil_img], chatbot.image_processor, chatbot.model.config)
|
| 281 |
if isinstance(processed, (list, tuple)) and len(processed) > 0:
|
| 282 |
image_tensor = processed[0]
|
|
|
|
| 368 |
if not model_initialized:
|
| 369 |
if not initialize_model():
|
| 370 |
return {"error": "Model initialization failed"}
|
| 371 |
+
# model_initialized artık initialize_model içinde True set ediliyor
|
| 372 |
|
| 373 |
try:
|
| 374 |
message = payload.get("message") or payload.get("query") or payload.get("prompt") or payload.get("istem") or ""
|
|
|
|
| 434 |
self.debug = bool(int(os.getenv("DEBUG", "0")))
|
| 435 |
|
| 436 |
def initialize_model():
|
| 437 |
+
global tokenizer, model, image_processor, context_len, args, model_initialized
|
| 438 |
if not LLAVA_AVAILABLE:
|
| 439 |
print("[init] LLaVA not available; cannot init.")
|
| 440 |
return False
|
|
|
|
| 444 |
tokenizer_, model_, image_processor_, context_len_ = load_pretrained_model(
|
| 445 |
args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit
|
| 446 |
)
|
| 447 |
+
|
| 448 |
+
# ✅ Fallback: image_processor None ise otomatik yükle (CLIP 336 varsayılan)
|
| 449 |
+
if image_processor_ is None:
|
| 450 |
+
try:
|
| 451 |
+
from transformers import AutoProcessor, AutoImageProcessor, CLIPImageProcessor
|
| 452 |
+
try:
|
| 453 |
+
image_processor_ = AutoProcessor.from_pretrained(args.model_path, trust_remote_code=True)
|
| 454 |
+
except Exception:
|
| 455 |
+
try:
|
| 456 |
+
image_processor_ = AutoImageProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
|
| 457 |
+
except Exception:
|
| 458 |
+
image_processor_ = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
|
| 459 |
+
print("[init] image_processor fallback activated.")
|
| 460 |
+
except Exception as e:
|
| 461 |
+
print(f"[init] image_processor fallback failed: {e}")
|
| 462 |
+
return False
|
| 463 |
+
|
| 464 |
# demo: model'ı genelde cuda’da çalıştırır
|
| 465 |
try:
|
| 466 |
_ = next(model_.parameters()).device
|
|
|
|
| 469 |
model_ = model_.to(torch.device("cuda"))
|
| 470 |
model_.eval()
|
| 471 |
|
| 472 |
+
# Global state
|
| 473 |
+
tokenizer = tokenizer_
|
| 474 |
+
model = model_
|
| 475 |
+
image_processor = image_processor_
|
| 476 |
+
context_len = context_len_
|
| 477 |
|
| 478 |
chat_manager.init_if_needed(args, args.model_path, tokenizer_, model_, image_processor_, context_len_)
|
| 479 |
+
model_initialized = True # ✅ sağlıkta doğru raporla
|
| 480 |
print("[init] model/tokenizer/image_processor loaded.")
|
| 481 |
return True
|
| 482 |
except Exception as e:
|