Upload 2 files
Browse files- adapter_layer.py +237 -219
- handler.py +24 -34
adapter_layer.py
CHANGED
|
@@ -6,29 +6,132 @@ import traceback
|
|
| 6 |
from typing import Dict, Any, Optional, List
|
| 7 |
import importlib.util
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Inline implementation if module isn't available
|
| 14 |
-
def safely_import(module_name):
|
| 15 |
-
try:
|
| 16 |
-
return importlib.import_module(module_name)
|
| 17 |
-
except ImportError:
|
| 18 |
-
return None
|
| 19 |
-
|
| 20 |
-
def is_module_available(module_name):
|
| 21 |
-
try:
|
| 22 |
-
importlib.util.find_spec(module_name)
|
| 23 |
-
return True
|
| 24 |
-
except ImportError:
|
| 25 |
-
return False
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
logger = logging.getLogger(__name__)
|
| 34 |
|
|
@@ -41,7 +144,7 @@ class WildnerveModelAdapter:
|
|
| 41 |
def __init__(self, model_path: str):
|
| 42 |
self.model_path = model_path
|
| 43 |
self.tokenizer = None
|
| 44 |
-
self.
|
| 45 |
self.initialized = False
|
| 46 |
|
| 47 |
# Ensure the model path is in sys.path so we can import from it
|
|
@@ -50,62 +153,27 @@ class WildnerveModelAdapter:
|
|
| 50 |
|
| 51 |
logger.info(f"Model adapter initialized with path: {model_path}")
|
| 52 |
|
| 53 |
-
# Initialize
|
| 54 |
self._initialize_tokenizer()
|
|
|
|
| 55 |
|
| 56 |
def _initialize_tokenizer(self):
|
| 57 |
-
"""Initialize tokenizer
|
| 58 |
-
# First try loading config - use original implementation if available
|
| 59 |
try:
|
| 60 |
-
# Check if we have a config module
|
| 61 |
-
has_config = is_module_available('config')
|
| 62 |
-
|
| 63 |
# Try to import from service_registry if available
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# Use the appropriate approach based on available modules
|
| 67 |
-
if has_registry:
|
| 68 |
-
# Use original registry approach
|
| 69 |
from service_registry import registry, TOKENIZER
|
| 70 |
|
| 71 |
if registry.has(TOKENIZER):
|
| 72 |
self.tokenizer = registry.get(TOKENIZER)
|
| 73 |
logger.info("Retrieved tokenizer from registry")
|
| 74 |
return
|
| 75 |
-
|
| 76 |
# Try loading from the original tokenizer.py
|
| 77 |
-
tokenizer_module = None
|
| 78 |
-
|
| 79 |
-
# First check if it's directly importable
|
| 80 |
if is_module_available('tokenizer'):
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# Next try to load it from model_path
|
| 85 |
-
if tokenizer_module is None:
|
| 86 |
-
tokenizer_path = os.path.join(self.model_path, "tokenizer.py")
|
| 87 |
-
if os.path.exists(tokenizer_path):
|
| 88 |
-
spec = importlib.util.spec_from_file_location("tokenizer_module", tokenizer_path)
|
| 89 |
-
tokenizer_module = importlib.util.module_from_spec(spec)
|
| 90 |
-
spec.loader.exec_module(tokenizer_module)
|
| 91 |
-
logger.info("Loaded tokenizer module from model path")
|
| 92 |
-
|
| 93 |
-
# Create tokenizer if module was loaded
|
| 94 |
-
if tokenizer_module is not None and hasattr(tokenizer_module, 'TokenizerWrapper'):
|
| 95 |
-
# Handle potential missing config_app
|
| 96 |
-
if hasattr(tokenizer_module, 'get_tokenizer'):
|
| 97 |
-
self.tokenizer = tokenizer_module.get_tokenizer()
|
| 98 |
-
else:
|
| 99 |
-
# Try direct instantiation
|
| 100 |
-
self.tokenizer = tokenizer_module.TokenizerWrapper()
|
| 101 |
-
|
| 102 |
logger.info("Created TokenizerWrapper instance")
|
| 103 |
-
|
| 104 |
-
# Register in registry if available
|
| 105 |
-
if has_registry:
|
| 106 |
-
from service_registry import registry, TOKENIZER
|
| 107 |
-
registry.register(TOKENIZER, self.tokenizer)
|
| 108 |
-
|
| 109 |
return
|
| 110 |
|
| 111 |
except Exception as e:
|
|
@@ -116,176 +184,110 @@ class WildnerveModelAdapter:
|
|
| 116 |
from transformers import AutoTokenizer
|
| 117 |
|
| 118 |
models_to_try = [
|
| 119 |
-
"bert-base-uncased",
|
| 120 |
-
"distilbert-base-uncased",
|
| 121 |
-
"gpt2"
|
| 122 |
]
|
| 123 |
|
| 124 |
for model_name in models_to_try:
|
| 125 |
try:
|
| 126 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 127 |
logger.info(f"Using transformers AutoTokenizer with {model_name}")
|
| 128 |
-
|
| 129 |
-
# Register if registry is available
|
| 130 |
-
if 'registry' in locals() and 'TOKENIZER' in locals():
|
| 131 |
-
registry.register(TOKENIZER, self.tokenizer)
|
| 132 |
-
|
| 133 |
return
|
| 134 |
except Exception as e:
|
| 135 |
logger.warning(f"Failed to load {model_name}: {e}")
|
| 136 |
|
| 137 |
except ImportError:
|
| 138 |
logger.warning("transformers package not available")
|
| 139 |
-
|
| 140 |
-
# Last resort: use our SimpleTokenizer implementation
|
| 141 |
-
logger.warning("Using SimpleTokenizer as final fallback")
|
| 142 |
-
self.tokenizer = SimpleTokenizer()
|
| 143 |
|
| 144 |
-
def
|
| 145 |
-
"""
|
| 146 |
-
if self.fallback_model is not None:
|
| 147 |
-
return self.fallback_model
|
| 148 |
-
|
| 149 |
try:
|
| 150 |
-
#
|
| 151 |
-
|
| 152 |
-
models_to_try = ["model_Combn", "model_Custm", "model_PrTr"]
|
| 153 |
|
| 154 |
-
for
|
| 155 |
try:
|
| 156 |
-
if is_module_available(
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
-
logger.warning(f"Failed to import {
|
| 163 |
|
| 164 |
-
# If
|
| 165 |
-
if
|
| 166 |
-
|
| 167 |
-
"Wildnerve_tlm01_Hybrid_Model",
|
| 168 |
-
"Wildnerve_tlm01"
|
| 169 |
-
]
|
| 170 |
|
| 171 |
-
for class_name in model_classes:
|
| 172 |
-
if hasattr(model_module, class_name):
|
| 173 |
-
try:
|
| 174 |
-
# Try to instantiate with minimal parameters
|
| 175 |
-
model_class = getattr(model_module, class_name)
|
| 176 |
-
instance = model_class(
|
| 177 |
-
vocab_size=30522,
|
| 178 |
-
specialization="general",
|
| 179 |
-
dataset_path=None,
|
| 180 |
-
model_name="bert-base-uncased",
|
| 181 |
-
embedding_dim=768,
|
| 182 |
-
num_heads=12,
|
| 183 |
-
hidden_dim=768,
|
| 184 |
-
num_layers=6,
|
| 185 |
-
output_size=768,
|
| 186 |
-
dropout=0.1,
|
| 187 |
-
max_seq_length=512,
|
| 188 |
-
pooling_mode="mean",
|
| 189 |
-
tokenizer=self.tokenizer
|
| 190 |
-
)
|
| 191 |
-
logger.info(f"Created {class_name} instance from {model_module.__name__}")
|
| 192 |
-
self.fallback_model = instance
|
| 193 |
-
return self.fallback_model
|
| 194 |
-
except Exception as e:
|
| 195 |
-
logger.warning(f"Failed to instantiate {class_name}: {e}")
|
| 196 |
-
|
| 197 |
-
# If we couldn't use the original model, use our fallback
|
| 198 |
-
self.fallback_model = SimpleFallbackModel(self.tokenizer)
|
| 199 |
-
logger.info("Created SimpleFallbackModel")
|
| 200 |
-
return self.fallback_model
|
| 201 |
-
|
| 202 |
except Exception as e:
|
| 203 |
-
logger.error(f"Failed to
|
| 204 |
-
|
| 205 |
-
self.fallback_model = SimpleFallbackModel(self.tokenizer)
|
| 206 |
-
return self.fallback_model
|
| 207 |
|
| 208 |
def generate(self, prompt: str, **kwargs) -> str:
|
| 209 |
"""Generate a response to the prompt"""
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
logger.warning("No tokenizer found, re-initializing")
|
| 213 |
-
self._initialize_tokenizer()
|
| 214 |
-
|
| 215 |
-
# If tokenizer is still None after re-initialization, return error message
|
| 216 |
-
if self.tokenizer is None:
|
| 217 |
-
return "Unable to process your request due to missing tokenizer."
|
| 218 |
|
| 219 |
try:
|
| 220 |
-
|
| 221 |
-
model = self.load_fallback_model()
|
| 222 |
-
if model is not None:
|
| 223 |
-
# Try different generation methods the model might have
|
| 224 |
-
if hasattr(model, "generate_streaming"):
|
| 225 |
-
try:
|
| 226 |
-
# For streaming we need to collect all tokens
|
| 227 |
-
tokens = []
|
| 228 |
-
for token in model.generate_streaming(prompt, **kwargs):
|
| 229 |
-
tokens.append(token)
|
| 230 |
-
return "".join(tokens)
|
| 231 |
-
except Exception as e:
|
| 232 |
-
logger.warning(f"Streaming generation failed: {e}")
|
| 233 |
-
|
| 234 |
-
# Try standard generate methods
|
| 235 |
-
gen_methods = ["generate_with_decoding", "generate"]
|
| 236 |
-
for method_name in gen_methods:
|
| 237 |
-
if hasattr(model, method_name):
|
| 238 |
-
try:
|
| 239 |
-
# Tokenize the input if needed
|
| 240 |
-
if hasattr(self.tokenizer, "__call__"):
|
| 241 |
-
input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids
|
| 242 |
-
# Get the result
|
| 243 |
-
method = getattr(model, method_name)
|
| 244 |
-
result = method(input_ids, **kwargs)
|
| 245 |
-
if isinstance(result, str) and result:
|
| 246 |
-
return result
|
| 247 |
-
except Exception as e:
|
| 248 |
-
logger.warning(f"{method_name} failed: {e}")
|
| 249 |
-
|
| 250 |
-
# If we get here, try a final simple generate method
|
| 251 |
try:
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
| 253 |
except Exception as e:
|
| 254 |
-
logger.warning(f"
|
| 255 |
-
|
| 256 |
-
# If fallback model failed, use a simple hardcoded response based on prompt
|
| 257 |
-
logger.warning("Using hardcoded response as fallback")
|
| 258 |
-
|
| 259 |
-
if "code" in prompt.lower() or "programming" in prompt.lower():
|
| 260 |
-
return """I can help with coding tasks! However, I'm currently running in fallback mode due to model loading issues.
|
| 261 |
-
|
| 262 |
-
In normal operation, I can:
|
| 263 |
-
- Write and debug code in multiple languages
|
| 264 |
-
- Explain algorithms and programming concepts
|
| 265 |
-
- Help design software architecture
|
| 266 |
-
- Optimize existing code
|
| 267 |
-
|
| 268 |
-
Please try again later when the full model capabilities are available."""
|
| 269 |
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
Please try again later when these issues have been resolved."""
|
| 288 |
|
|
|
|
|
|
|
|
|
|
| 289 |
except Exception as e:
|
| 290 |
logger.error(f"Error in generate: {e}")
|
| 291 |
logger.error(traceback.format_exc())
|
|
@@ -455,22 +457,38 @@ class SimpleFallbackModel:
|
|
| 455 |
def generate(self, prompt, **kwargs):
|
| 456 |
"""Generate a simple response based on prompt content"""
|
| 457 |
import random
|
| 458 |
-
prompt_lower = prompt.lower()
|
| 459 |
|
| 460 |
-
#
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
-
# Add
|
| 474 |
response += "\n\nThe system is experiencing issues loading the full model capabilities. Please try again later."
|
| 475 |
|
| 476 |
return response
|
|
|
|
| 6 |
from typing import Dict, Any, Optional, List
|
| 7 |
import importlib.util
|
| 8 |
|
| 9 |
+
# IMPROVED MOCKING STRATEGY: Pre-check and create more complete mock modules
|
| 10 |
+
# These need to be defined BEFORE any imports that might require them
|
| 11 |
+
class CompleteModelLoader:
|
| 12 |
+
"""A utility to ensure model loading succeeds by properly handling dependencies"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@staticmethod
|
| 15 |
+
def setup_environment():
|
| 16 |
+
"""Set up the environment to ensure model loading succeeds"""
|
| 17 |
+
# First, create more comprehensive mocks for critical dependencies
|
| 18 |
+
if 'pydantic' not in sys.modules:
|
| 19 |
+
# Create a more complete mock for pydantic
|
| 20 |
+
class BaseModel:
|
| 21 |
+
def __init__(self, **kwargs):
|
| 22 |
+
for key, value in kwargs.items():
|
| 23 |
+
setattr(self, key, value)
|
| 24 |
+
|
| 25 |
+
@classmethod
|
| 26 |
+
def model_validate(cls, obj, **kwargs):
|
| 27 |
+
return cls(**obj)
|
| 28 |
+
|
| 29 |
+
class Field:
|
| 30 |
+
def __call__(self, *args, **kwargs):
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
def __new__(cls, *args, **kwargs):
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
class MockPydantic:
|
| 37 |
+
BaseModel = BaseModel
|
| 38 |
+
Field = Field
|
| 39 |
+
ValidationError = Exception
|
| 40 |
+
ConfigDict = type('ConfigDict', (), {})
|
| 41 |
+
|
| 42 |
+
class ConfigError(Exception):
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
sys.modules['pydantic'] = MockPydantic
|
| 46 |
+
print("Created comprehensive mock for pydantic")
|
| 47 |
+
|
| 48 |
+
if 'codecarbon' not in sys.modules:
|
| 49 |
+
# Create a more complete mock for codecarbon
|
| 50 |
+
class EmissionsTracker:
|
| 51 |
+
def __init__(self, *args, **kwargs):
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
+
def start(self):
|
| 55 |
+
return self
|
| 56 |
+
|
| 57 |
+
def stop(self):
|
| 58 |
+
return 0.0
|
| 59 |
+
|
| 60 |
+
class MockCodecarbon:
|
| 61 |
+
EmissionsTracker = EmissionsTracker
|
| 62 |
+
|
| 63 |
+
sys.modules['codecarbon'] = MockCodecarbon
|
| 64 |
+
print("Created comprehensive mock for codecarbon")
|
| 65 |
+
|
| 66 |
+
# Now ensure that these modules are found when imported
|
| 67 |
+
return True
|
| 68 |
+
|
| 69 |
+
@staticmethod
|
| 70 |
+
def preload_models():
|
| 71 |
+
"""Preload model modules to ensure they're available"""
|
| 72 |
+
import importlib
|
| 73 |
+
|
| 74 |
+
# List of model modules that need to be available
|
| 75 |
+
model_modules = ["model_Combn", "model_Custm", "model_PrTr"]
|
| 76 |
+
loaded_modules = []
|
| 77 |
+
|
| 78 |
+
for module_name in model_modules:
|
| 79 |
+
try:
|
| 80 |
+
# First check if the module already exists
|
| 81 |
+
if module_name in sys.modules:
|
| 82 |
+
print(f"Module {module_name} already loaded")
|
| 83 |
+
loaded_modules.append(module_name)
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
# Try to import the module directly
|
| 87 |
+
module = importlib.import_module(module_name)
|
| 88 |
+
loaded_modules.append(module_name)
|
| 89 |
+
print(f"Successfully loaded {module_name}")
|
| 90 |
+
except ImportError as e:
|
| 91 |
+
# If direct import fails, check if the module file exists
|
| 92 |
+
module_path = None
|
| 93 |
+
|
| 94 |
+
# Check standard paths
|
| 95 |
+
potential_paths = [
|
| 96 |
+
f"{module_name}.py",
|
| 97 |
+
os.path.join(os.getcwd(), f"{module_name}.py"),
|
| 98 |
+
os.path.join("/repository", f"{module_name}.py")
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
for path in potential_paths:
|
| 102 |
+
if os.path.exists(path):
|
| 103 |
+
module_path = path
|
| 104 |
+
break
|
| 105 |
+
|
| 106 |
+
if module_path:
|
| 107 |
+
try:
|
| 108 |
+
# Try to load the module from file
|
| 109 |
+
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
| 110 |
+
module = importlib.util.module_from_spec(spec)
|
| 111 |
+
sys.modules[module_name] = module
|
| 112 |
+
spec.loader.exec_module(module)
|
| 113 |
+
loaded_modules.append(module_name)
|
| 114 |
+
print(f"Loaded {module_name} from file: {module_path}")
|
| 115 |
+
except Exception as e2:
|
| 116 |
+
print(f"Error loading {module_name} from file: {e2}")
|
| 117 |
+
else:
|
| 118 |
+
print(f"Could not find module file for {module_name}")
|
| 119 |
+
|
| 120 |
+
return loaded_modules
|
| 121 |
+
|
| 122 |
+
# Set up the environment before any other imports
|
| 123 |
+
CompleteModelLoader.setup_environment()
|
| 124 |
+
# Now preload the model modules
|
| 125 |
+
loaded_model_modules = CompleteModelLoader.preload_models()
|
| 126 |
+
|
| 127 |
+
# Proceed with regular imports
|
| 128 |
+
# Import dependency helpers - keep this simple
|
| 129 |
+
def is_module_available(module_name):
|
| 130 |
+
try:
|
| 131 |
+
importlib.util.find_spec(module_name)
|
| 132 |
+
return True
|
| 133 |
+
except ImportError:
|
| 134 |
+
return False
|
| 135 |
|
| 136 |
logger = logging.getLogger(__name__)
|
| 137 |
|
|
|
|
| 144 |
def __init__(self, model_path: str):
|
| 145 |
self.model_path = model_path
|
| 146 |
self.tokenizer = None
|
| 147 |
+
self.model = None
|
| 148 |
self.initialized = False
|
| 149 |
|
| 150 |
# Ensure the model path is in sys.path so we can import from it
|
|
|
|
| 153 |
|
| 154 |
logger.info(f"Model adapter initialized with path: {model_path}")
|
| 155 |
|
| 156 |
+
# Initialize components
|
| 157 |
self._initialize_tokenizer()
|
| 158 |
+
self._initialize_model()
|
| 159 |
|
| 160 |
def _initialize_tokenizer(self):
|
| 161 |
+
"""Initialize tokenizer from registry or directly"""
|
|
|
|
| 162 |
try:
|
|
|
|
|
|
|
|
|
|
| 163 |
# Try to import from service_registry if available
|
| 164 |
+
if is_module_available('service_registry'):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
from service_registry import registry, TOKENIZER
|
| 166 |
|
| 167 |
if registry.has(TOKENIZER):
|
| 168 |
self.tokenizer = registry.get(TOKENIZER)
|
| 169 |
logger.info("Retrieved tokenizer from registry")
|
| 170 |
return
|
| 171 |
+
|
| 172 |
# Try loading from the original tokenizer.py
|
|
|
|
|
|
|
|
|
|
| 173 |
if is_module_available('tokenizer'):
|
| 174 |
+
from tokenizer import TokenizerWrapper, get_tokenizer
|
| 175 |
+
self.tokenizer = get_tokenizer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
logger.info("Created TokenizerWrapper instance")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
return
|
| 178 |
|
| 179 |
except Exception as e:
|
|
|
|
| 184 |
from transformers import AutoTokenizer
|
| 185 |
|
| 186 |
models_to_try = [
|
| 187 |
+
"bert-base-uncased",
|
| 188 |
+
"distilbert-base-uncased",
|
| 189 |
+
"gpt2"
|
| 190 |
]
|
| 191 |
|
| 192 |
for model_name in models_to_try:
|
| 193 |
try:
|
| 194 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 195 |
logger.info(f"Using transformers AutoTokenizer with {model_name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
return
|
| 197 |
except Exception as e:
|
| 198 |
logger.warning(f"Failed to load {model_name}: {e}")
|
| 199 |
|
| 200 |
except ImportError:
|
| 201 |
logger.warning("transformers package not available")
|
| 202 |
+
raise ImportError("No tokenizer could be initialized")
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
def _initialize_model(self):
|
| 205 |
+
"""Initialize the actual model"""
|
|
|
|
|
|
|
|
|
|
| 206 |
try:
|
| 207 |
+
# Check for actual model modules
|
| 208 |
+
model_modules = ["model_Combn", "model_Custm", "model_PrTr"]
|
|
|
|
| 209 |
|
| 210 |
+
for module_name in model_modules:
|
| 211 |
try:
|
| 212 |
+
if is_module_available(module_name):
|
| 213 |
+
module = importlib.import_module(module_name)
|
| 214 |
+
|
| 215 |
+
# Look for model classes
|
| 216 |
+
model_classes = [
|
| 217 |
+
"Wildnerve_tlm01_Hybrid_Model",
|
| 218 |
+
"Wildnerve_tlm01"
|
| 219 |
+
]
|
| 220 |
+
|
| 221 |
+
for class_name in model_classes:
|
| 222 |
+
if hasattr(module, class_name):
|
| 223 |
+
model_class = getattr(module, class_name)
|
| 224 |
+
|
| 225 |
+
# Initialize the model
|
| 226 |
+
self.model = model_class(
|
| 227 |
+
vocab_size=30522,
|
| 228 |
+
specialization="general",
|
| 229 |
+
dataset_path=None,
|
| 230 |
+
model_name="bert-base-uncased",
|
| 231 |
+
embedding_dim=768,
|
| 232 |
+
num_heads=12,
|
| 233 |
+
hidden_dim=768,
|
| 234 |
+
num_layers=6,
|
| 235 |
+
output_size=768,
|
| 236 |
+
dropout=0.1,
|
| 237 |
+
max_seq_length=512,
|
| 238 |
+
pooling_mode="mean",
|
| 239 |
+
tokenizer=self.tokenizer
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
logger.info(f"Successfully created {class_name} from {module_name}")
|
| 243 |
+
self.initialized = True
|
| 244 |
+
return
|
| 245 |
except Exception as e:
|
| 246 |
+
logger.warning(f"Failed to import or initialize from {module_name}: {e}")
|
| 247 |
|
| 248 |
+
# If no model was initialized, raise error
|
| 249 |
+
if self.model is None:
|
| 250 |
+
raise ImportError("No suitable model class found")
|
|
|
|
|
|
|
|
|
|
| 251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
except Exception as e:
|
| 253 |
+
logger.error(f"Failed to initialize model: {e}")
|
| 254 |
+
raise
|
|
|
|
|
|
|
| 255 |
|
| 256 |
def generate(self, prompt: str, **kwargs) -> str:
|
| 257 |
"""Generate a response to the prompt"""
|
| 258 |
+
if not self.initialized or self.model is None:
|
| 259 |
+
raise RuntimeError("Model not initialized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
try:
|
| 262 |
+
if hasattr(self.model, "generate_streaming"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
try:
|
| 264 |
+
tokens = []
|
| 265 |
+
for token in self.model.generate_streaming(prompt, **kwargs):
|
| 266 |
+
tokens.append(token)
|
| 267 |
+
return "".join(tokens)
|
| 268 |
except Exception as e:
|
| 269 |
+
logger.warning(f"Streaming generation failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
+
# Try standard generate methods
|
| 272 |
+
gen_methods = ["generate_with_decoding", "generate"]
|
| 273 |
+
for method_name in gen_methods:
|
| 274 |
+
if hasattr(self.model, method_name):
|
| 275 |
+
try:
|
| 276 |
+
# Tokenize the input if needed
|
| 277 |
+
input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids
|
| 278 |
+
|
| 279 |
+
# Get the result
|
| 280 |
+
method = getattr(self.model, method_name)
|
| 281 |
+
result = method(input_ids, **kwargs)
|
| 282 |
+
|
| 283 |
+
if isinstance(result, str) and result:
|
| 284 |
+
return result
|
| 285 |
+
except Exception as e:
|
| 286 |
+
logger.warning(f"{method_name} failed: {e}")
|
|
|
|
|
|
|
| 287 |
|
| 288 |
+
# If we get here, try a simple direct generate method
|
| 289 |
+
return self.model.generate(prompt, **kwargs)
|
| 290 |
+
|
| 291 |
except Exception as e:
|
| 292 |
logger.error(f"Error in generate: {e}")
|
| 293 |
logger.error(traceback.format_exc())
|
|
|
|
| 457 |
def generate(self, prompt, **kwargs):
|
| 458 |
"""Generate a simple response based on prompt content"""
|
| 459 |
import random
|
|
|
|
| 460 |
|
| 461 |
+
# COMPLETELY SIMPLIFIED IMPLEMENTATION:
|
| 462 |
+
# This is a critical function that must not fail
|
| 463 |
+
|
| 464 |
+
# Always set a default response type
|
| 465 |
+
response_type = "default"
|
| 466 |
+
|
| 467 |
+
# Just check for tensor and use a fixed response to prevent ANY processing errors
|
| 468 |
+
if isinstance(prompt, torch.Tensor):
|
| 469 |
+
return """I apologize, but I'm currently operating in fallback mode due to loading issues.
|
| 470 |
|
| 471 |
+
The system administrator should check for missing dependencies like pydantic and codecarbon.
|
| 472 |
+
|
| 473 |
+
Please try again later when full model capabilities are restored."""
|
| 474 |
+
|
| 475 |
+
# For strings, do minimal processing
|
| 476 |
+
if isinstance(prompt, str):
|
| 477 |
+
# Use the most basic string operations that can't fail
|
| 478 |
+
if "?" in prompt:
|
| 479 |
+
response_type = "question"
|
| 480 |
+
elif "code" in prompt.lower():
|
| 481 |
+
response_type = "code"
|
| 482 |
+
elif any(word in prompt.lower() for word in ["hello", "hi"]):
|
| 483 |
+
response_type = "greeting"
|
| 484 |
+
|
| 485 |
+
# Get a response - default if anything went wrong
|
| 486 |
+
try:
|
| 487 |
+
response = random.choice(self.responses[response_type])
|
| 488 |
+
except:
|
| 489 |
+
response = "I'm currently operating in fallback mode due to technical issues."
|
| 490 |
|
| 491 |
+
# Add standard explanation
|
| 492 |
response += "\n\nThe system is experiencing issues loading the full model capabilities. Please try again later."
|
| 493 |
|
| 494 |
return response
|
handler.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# Handler.py -
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import time
|
|
@@ -7,8 +7,18 @@ import traceback
|
|
| 7 |
from typing import Dict, Any, List
|
| 8 |
import importlib.util
|
| 9 |
|
| 10 |
-
# --- DEBUG: confirm correct handler.py is loaded
|
| 11 |
-
print("DEBUG: using Wildnerve-tlm_HF/handler.py — update
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Set up logging
|
| 14 |
logging.basicConfig(
|
|
@@ -19,35 +29,23 @@ logger = logging.getLogger(__name__)
|
|
| 19 |
|
| 20 |
# Make sure adapter_layer.py is properly located
|
| 21 |
try:
|
| 22 |
-
# For more reliable importing
|
| 23 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 24 |
sys.path.insert(0, script_dir)
|
| 25 |
|
| 26 |
-
# Try to import WildnerveModelAdapter
|
| 27 |
from adapter_layer import WildnerveModelAdapter
|
| 28 |
logger.info("Successfully imported adapter_layer module")
|
| 29 |
|
| 30 |
-
# Also try to import TokenizerWrapper
|
| 31 |
-
tokenizer_found = False
|
| 32 |
try:
|
| 33 |
from tokenizer import TokenizerWrapper, get_tokenizer
|
| 34 |
-
logger.info("Successfully imported TokenizerWrapper
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
logger.warning("TokenizerWrapper not found, will use fallbacks in adapter")
|
| 38 |
|
| 39 |
except ImportError as e:
|
| 40 |
-
logger.
|
| 41 |
-
|
| 42 |
-
# Minimal implementations - these are fallbacks if modules can't be imported
|
| 43 |
-
exec("""
|
| 44 |
-
class WildnerveModelAdapter:
|
| 45 |
-
def __init__(self, model_path):
|
| 46 |
-
self.path = model_path
|
| 47 |
-
|
| 48 |
-
def generate(self, prompt, **kwargs):
|
| 49 |
-
return f"Received: '{prompt[:30]}...' - Running in emergency fallback mode. Cannot load required modules."
|
| 50 |
-
""")
|
| 51 |
|
| 52 |
class EndpointHandler:
|
| 53 |
def __init__(self, path=""):
|
|
@@ -65,7 +63,7 @@ class EndpointHandler:
|
|
| 65 |
# Handle result formatting
|
| 66 |
if isinstance(result, list):
|
| 67 |
logger.info(f"Returning list result with {len(result)} items")
|
| 68 |
-
return result
|
| 69 |
elif isinstance(result, dict):
|
| 70 |
return [result]
|
| 71 |
else:
|
|
@@ -82,7 +80,7 @@ class EndpointHandler:
|
|
| 82 |
return True
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
# Create the adapter
|
| 86 |
self.model_adapter = WildnerveModelAdapter(self.path)
|
| 87 |
self.initialized = True
|
| 88 |
return True
|
|
@@ -98,7 +96,7 @@ class EndpointHandler:
|
|
| 98 |
if not self.initialized:
|
| 99 |
success = self.initialize()
|
| 100 |
if not success:
|
| 101 |
-
return [{"generated_text": "Failed to initialize the model.
|
| 102 |
|
| 103 |
# Extract the prompt text
|
| 104 |
text_input = self._extract_input_text(inputs)
|
|
@@ -124,15 +122,7 @@ class EndpointHandler:
|
|
| 124 |
logger.error(f"Error during prediction: {e}")
|
| 125 |
logger.error(traceback.format_exc())
|
| 126 |
|
| 127 |
-
|
| 128 |
-
fallback_message = (
|
| 129 |
-
f"I received your message: '{text_input[:30]}...' (truncated)\n\n"
|
| 130 |
-
"I apologize, but I encountered a critical error while processing your request. "
|
| 131 |
-
"The model is currently unavailable or running in emergency fallback mode.\n\n"
|
| 132 |
-
"Error details: " + str(e)
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
return [{"generated_text": fallback_message}]
|
| 136 |
|
| 137 |
def _extract_input_text(self, inputs) -> str:
|
| 138 |
"""Extract the input text from various possible input formats"""
|
|
|
|
| 1 |
+
# Handler.py - Entry point for Hugging Face inference API
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import time
|
|
|
|
| 7 |
from typing import Dict, Any, List
|
| 8 |
import importlib.util
|
| 9 |
|
| 10 |
+
# --- DEBUG: confirm correct handler.py is loaded ---
|
| 11 |
+
print("DEBUG: using Wildnerve-tlm_HF/handler.py — update with direct dependency installation")
|
| 12 |
+
|
| 13 |
+
# CRITICAL FIX: Install required dependencies first before any imports
|
| 14 |
+
try:
|
| 15 |
+
import subprocess
|
| 16 |
+
print("Installing required dependencies...")
|
| 17 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "pydantic"])
|
| 18 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "codecarbon"])
|
| 19 |
+
print("Dependencies successfully installed")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error installing dependencies: {e}")
|
| 22 |
|
| 23 |
# Set up logging
|
| 24 |
logging.basicConfig(
|
|
|
|
| 29 |
|
| 30 |
# Make sure adapter_layer.py is properly located
|
| 31 |
try:
|
| 32 |
+
# For more reliable importing
|
| 33 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 34 |
sys.path.insert(0, script_dir)
|
| 35 |
|
|
|
|
| 36 |
from adapter_layer import WildnerveModelAdapter
|
| 37 |
logger.info("Successfully imported adapter_layer module")
|
| 38 |
|
| 39 |
+
# Also try to import TokenizerWrapper
|
|
|
|
| 40 |
try:
|
| 41 |
from tokenizer import TokenizerWrapper, get_tokenizer
|
| 42 |
+
logger.info("Successfully imported TokenizerWrapper")
|
| 43 |
+
except ImportError as e:
|
| 44 |
+
logger.warning(f"TokenizerWrapper not found: {e}")
|
|
|
|
| 45 |
|
| 46 |
except ImportError as e:
|
| 47 |
+
logger.error(f"Could not import adapter_layer: {e}")
|
| 48 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
class EndpointHandler:
|
| 51 |
def __init__(self, path=""):
|
|
|
|
| 63 |
# Handle result formatting
|
| 64 |
if isinstance(result, list):
|
| 65 |
logger.info(f"Returning list result with {len(result)} items")
|
| 66 |
+
return result
|
| 67 |
elif isinstance(result, dict):
|
| 68 |
return [result]
|
| 69 |
else:
|
|
|
|
| 80 |
return True
|
| 81 |
|
| 82 |
try:
|
| 83 |
+
# Create the adapter - this will load the actual model
|
| 84 |
self.model_adapter = WildnerveModelAdapter(self.path)
|
| 85 |
self.initialized = True
|
| 86 |
return True
|
|
|
|
| 96 |
if not self.initialized:
|
| 97 |
success = self.initialize()
|
| 98 |
if not success:
|
| 99 |
+
return [{"generated_text": "Failed to initialize the model."}]
|
| 100 |
|
| 101 |
# Extract the prompt text
|
| 102 |
text_input = self._extract_input_text(inputs)
|
|
|
|
| 122 |
logger.error(f"Error during prediction: {e}")
|
| 123 |
logger.error(traceback.format_exc())
|
| 124 |
|
| 125 |
+
return [{"generated_text": f"Error generating response: {str(e)}"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
def _extract_input_text(self, inputs) -> str:
|
| 128 |
"""Extract the input text from various possible input formats"""
|