Upload handler.py
Browse files- handler.py +118 -42
handler.py
CHANGED
|
@@ -33,10 +33,10 @@ class EndpointHandler:
|
|
| 33 |
def __init__(self, path=""):
|
| 34 |
"""
|
| 35 |
Hey there! Let's get this PULSE-7B model up and running.
|
| 36 |
-
We'll load
|
| 37 |
|
| 38 |
Args:
|
| 39 |
-
path: Model directory path (
|
| 40 |
"""
|
| 41 |
print("🚀 Starting up PULSE-7B handler...")
|
| 42 |
print("📝 Enhanced by Ubden® Team - github.com/ck-cankurt")
|
|
@@ -67,59 +67,135 @@ class EndpointHandler:
|
|
| 67 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 68 |
print(f"🖥️ Running on: {self.device}")
|
| 69 |
|
| 70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
trust_remote_code=True,
|
| 81 |
-
model_kwargs={
|
| 82 |
-
"low_cpu_mem_usage": True,
|
| 83 |
-
"use_safetensors": True
|
| 84 |
-
}
|
| 85 |
-
)
|
| 86 |
-
self.use_pipeline = True
|
| 87 |
-
self.model = None
|
| 88 |
-
self.processor = None
|
| 89 |
-
self.tokenizer = None
|
| 90 |
-
print("✅ Model loaded successfully via pipeline!")
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
#
|
| 96 |
try:
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
print("📦
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
low_cpu_mem_usage=True,
|
| 106 |
-
trust_remote_code=True
|
| 107 |
-
)
|
| 108 |
|
| 109 |
if self.tokenizer.pad_token is None:
|
| 110 |
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 111 |
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 112 |
|
| 113 |
-
|
| 114 |
-
self.
|
| 115 |
-
self.pipe =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
self.processor = None
|
| 117 |
-
print("✅
|
| 118 |
|
| 119 |
-
except Exception as
|
| 120 |
-
print(f"
|
| 121 |
-
print(
|
| 122 |
-
print(f"Manual error: {e2}")
|
| 123 |
|
| 124 |
self.model = None
|
| 125 |
self.processor = None
|
|
|
|
| 33 |
def __init__(self, path=""):
|
| 34 |
"""
|
| 35 |
Hey there! Let's get this PULSE-7B model up and running.
|
| 36 |
+
We'll try to load from local files first, then fallback to HuggingFace hub.
|
| 37 |
|
| 38 |
Args:
|
| 39 |
+
path: Model directory path (defaults to current directory)
|
| 40 |
"""
|
| 41 |
print("🚀 Starting up PULSE-7B handler...")
|
| 42 |
print("📝 Enhanced by Ubden® Team - github.com/ck-cankurt")
|
|
|
|
| 67 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 68 |
print(f"🖥️ Running on: {self.device}")
|
| 69 |
|
| 70 |
+
# Set model path - use local files if available
|
| 71 |
+
self.model_path = path if path else "."
|
| 72 |
+
print(f"📁 Model path: {self.model_path}")
|
| 73 |
+
|
| 74 |
+
# Check if we have local model files
|
| 75 |
+
import os
|
| 76 |
+
local_files = {
|
| 77 |
+
'config': os.path.exists(os.path.join(self.model_path, 'config.json')),
|
| 78 |
+
'tokenizer_config': os.path.exists(os.path.join(self.model_path, 'tokenizer_config.json')),
|
| 79 |
+
'tokenizer_model': os.path.exists(os.path.join(self.model_path, 'tokenizer.model')),
|
| 80 |
+
'model_index': os.path.exists(os.path.join(self.model_path, 'model.safetensors.index.json')),
|
| 81 |
+
'generation_config': os.path.exists(os.path.join(self.model_path, 'generation_config.json'))
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
local_available = all(local_files.values())
|
| 85 |
+
print(f"📦 Local model files: {'✅ Available' if local_available else '❌ Missing'}")
|
| 86 |
+
for file_type, exists in local_files.items():
|
| 87 |
+
print(f" - {file_type}: {'✅' if exists else '❌'}")
|
| 88 |
+
|
| 89 |
+
# KESIN ÇÖZÜM: Local files varsa onları kullan, yoksa HuggingFace Hub
|
| 90 |
try:
|
| 91 |
+
print("📦 KESIN ÇÖZÜM: Model'in kendi architecture dosyalarını yüklüyorum...")
|
| 92 |
|
| 93 |
+
# Önce model'in custom dosyalarını indir ve import et
|
| 94 |
+
from transformers import AutoConfig, AutoTokenizer
|
| 95 |
+
from transformers.utils import cached_file
|
| 96 |
+
import importlib.util
|
| 97 |
+
import sys
|
| 98 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Model config'i yükle (local varsa local, yoksa hub)
|
| 101 |
+
model_source = self.model_path if local_available else "PULSE-ECG/PULSE-7B"
|
| 102 |
+
config = AutoConfig.from_pretrained(model_source, trust_remote_code=True)
|
| 103 |
+
print(f"🔧 Model config yüklendi: {config.model_type} (source: {'local' if local_available else 'hub'})")
|
| 104 |
|
| 105 |
+
# Custom modeling dosyasını indir veya bul
|
| 106 |
try:
|
| 107 |
+
if local_available:
|
| 108 |
+
# Local modeling file'ı ara
|
| 109 |
+
modeling_file = os.path.join(self.model_path, "modeling_llava.py")
|
| 110 |
+
if not os.path.exists(modeling_file):
|
| 111 |
+
# Local'de yoksa hub'dan indir
|
| 112 |
+
modeling_file = cached_file("PULSE-ECG/PULSE-7B", "modeling_llava.py", _raise_exceptions_for_missing_entries=False)
|
| 113 |
+
else:
|
| 114 |
+
# Hub'dan indir
|
| 115 |
+
modeling_file = cached_file("PULSE-ECG/PULSE-7B", "modeling_llava.py", _raise_exceptions_for_missing_entries=False)
|
| 116 |
+
if modeling_file and os.path.exists(modeling_file):
|
| 117 |
+
print(f"🔧 Custom modeling dosyası bulundu: {modeling_file}")
|
| 118 |
+
|
| 119 |
+
# Dosyayı modül olarak yükle
|
| 120 |
+
spec = importlib.util.spec_from_file_location("modeling_llava", modeling_file)
|
| 121 |
+
modeling_module = importlib.util.module_from_spec(spec)
|
| 122 |
+
sys.modules["modeling_llava"] = modeling_module
|
| 123 |
+
spec.loader.exec_module(modeling_module)
|
| 124 |
+
|
| 125 |
+
print("🔧 Custom modeling modülü yüklendi")
|
| 126 |
+
|
| 127 |
+
# Model class'ını bul ve kullan
|
| 128 |
+
if hasattr(modeling_module, 'LlavaLlamaForCausalLM'):
|
| 129 |
+
print("🎯 LlavaLlamaForCausalLM bulundu, yükleniyor...")
|
| 130 |
+
|
| 131 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_source, trust_remote_code=True)
|
| 132 |
+
self.model = modeling_module.LlavaLlamaForCausalLM.from_pretrained(
|
| 133 |
+
model_source,
|
| 134 |
+
config=config,
|
| 135 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 136 |
+
device_map="auto",
|
| 137 |
+
low_cpu_mem_usage=True,
|
| 138 |
+
trust_remote_code=True
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
if self.tokenizer.pad_token is None:
|
| 142 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 143 |
+
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 144 |
+
|
| 145 |
+
self.model.eval()
|
| 146 |
+
self.use_pipeline = False
|
| 147 |
+
self.pipe = None
|
| 148 |
+
self.processor = None
|
| 149 |
+
print("✅ PULSE-7B başarıyla custom implementation ile yüklendi!")
|
| 150 |
+
|
| 151 |
+
else:
|
| 152 |
+
raise Exception("LlavaLlamaForCausalLM class'ı bulunamadı")
|
| 153 |
+
else:
|
| 154 |
+
raise Exception("modeling_llava.py dosyası bulunamadı")
|
| 155 |
+
|
| 156 |
+
except Exception as modeling_error:
|
| 157 |
+
print(f"⚠️ Custom modeling yüklenemedi: {modeling_error}")
|
| 158 |
+
raise modeling_error
|
| 159 |
+
|
| 160 |
+
except Exception as e_final:
|
| 161 |
+
print(f"😓 Custom approach da başarısız: {e_final}")
|
| 162 |
+
print("🔄 En basit çözüme geçiyorum...")
|
| 163 |
+
|
| 164 |
+
# En basit çözüm: Sadece text generation pipeline
|
| 165 |
+
try:
|
| 166 |
+
from transformers import pipeline, AutoTokenizer
|
| 167 |
|
| 168 |
+
print("📦 EN BASIT ÇÖZÜM: Sadece tokenizer + basit generation...")
|
| 169 |
+
|
| 170 |
+
# Sadece tokenizer yükle (local varsa local)
|
| 171 |
+
tokenizer_source = self.model_path if local_available else "PULSE-ECG/PULSE-7B"
|
| 172 |
+
self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_source, trust_remote_code=True)
|
| 173 |
+
print(f"🔧 Tokenizer yüklendi (source: {'local' if local_available else 'hub'})")
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
if self.tokenizer.pad_token is None:
|
| 176 |
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 177 |
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 178 |
|
| 179 |
+
# Pipeline'ı text-generation için kur
|
| 180 |
+
pipeline_source = self.model_path if local_available else "PULSE-ECG/PULSE-7B"
|
| 181 |
+
self.pipe = pipeline(
|
| 182 |
+
"text-generation",
|
| 183 |
+
tokenizer=self.tokenizer,
|
| 184 |
+
model=pipeline_source,
|
| 185 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 186 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 187 |
+
trust_remote_code=True
|
| 188 |
+
)
|
| 189 |
+
print(f"🔧 Pipeline kuruldu (source: {'local' if local_available else 'hub'})")
|
| 190 |
+
|
| 191 |
+
self.use_pipeline = True
|
| 192 |
+
self.model = None
|
| 193 |
self.processor = None
|
| 194 |
+
print("✅ BASIT ÇÖZÜM BAŞARILI: Tokenizer + Pipeline yüklendi!")
|
| 195 |
|
| 196 |
+
except Exception as e_simple:
|
| 197 |
+
print(f"💥 En basit çözüm de başarısız: {e_simple}")
|
| 198 |
+
print("❌ Model hiçbir şekilde yüklenemedi")
|
|
|
|
| 199 |
|
| 200 |
self.model = None
|
| 201 |
self.processor = None
|