Upload dependency_helpers.py
Browse files- utils/dependency_helpers.py +124 -0
utils/dependency_helpers.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Dependency helpers to make the model work even if some libraries are missing.
|
| 3 |
+
This file provides fallback implementations for missing dependencies.
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import logging
|
| 7 |
+
import importlib.util
|
| 8 |
+
from typing import Any, Dict, Optional, Type, Callable
|
| 9 |
+
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
# Dictionary to track mock implementations
|
| 13 |
+
MOCK_MODULES = {}
|
| 14 |
+
|
| 15 |
+
def is_module_available(module_name: str) -> bool:
|
| 16 |
+
"""Check if a module is available without importing it"""
|
| 17 |
+
return importlib.util.find_spec(module_name) is not None
|
| 18 |
+
|
| 19 |
+
def create_mock_emissions_tracker() -> Type:
|
| 20 |
+
"""Create a mock implementation of codecarbon's EmissionsTracker"""
|
| 21 |
+
class MockEmissionsTracker:
|
| 22 |
+
def __init__(self, *args, **kwargs):
|
| 23 |
+
logger.info("Using mock EmissionsTracker")
|
| 24 |
+
|
| 25 |
+
def __enter__(self):
|
| 26 |
+
return self
|
| 27 |
+
|
| 28 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def start(self):
|
| 32 |
+
return self
|
| 33 |
+
|
| 34 |
+
def stop(self):
|
| 35 |
+
return 0.0 # Return zero emissions
|
| 36 |
+
|
| 37 |
+
return MockEmissionsTracker
|
| 38 |
+
|
| 39 |
+
def create_mock_pydantic_classes() -> Dict[str, Type]:
|
| 40 |
+
"""Create mock implementations of pydantic classes"""
|
| 41 |
+
class MockBaseModel:
|
| 42 |
+
"""Mock implementation of pydantic's BaseModel"""
|
| 43 |
+
def __init__(self, **kwargs):
|
| 44 |
+
for key, value in kwargs.items():
|
| 45 |
+
setattr(self, key, value)
|
| 46 |
+
|
| 47 |
+
def dict(self) -> Dict[str, Any]:
|
| 48 |
+
return {k: v for k, v in self.__dict__.items()
|
| 49 |
+
if not k.startswith('_')}
|
| 50 |
+
|
| 51 |
+
def json(self) -> str:
|
| 52 |
+
import json
|
| 53 |
+
return json.dumps(self.dict())
|
| 54 |
+
|
| 55 |
+
def mock_field(*args, **kwargs) -> Any:
|
| 56 |
+
"""Mock implementation of pydantic's Field"""
|
| 57 |
+
return kwargs.get('default', None)
|
| 58 |
+
|
| 59 |
+
class MockValidationError(Exception):
|
| 60 |
+
"""Mock implementation of pydantic's ValidationError"""
|
| 61 |
+
pass
|
| 62 |
+
|
| 63 |
+
mock_config_dict = dict
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
"BaseModel": MockBaseModel,
|
| 67 |
+
"Field": mock_field,
|
| 68 |
+
"ValidationError": MockValidationError,
|
| 69 |
+
"ConfigDict": mock_config_dict
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
def setup_dependency_fallbacks():
|
| 73 |
+
"""Setup fallbacks for all required dependencies"""
|
| 74 |
+
# Handle codecarbon
|
| 75 |
+
if not is_module_available("codecarbon"):
|
| 76 |
+
logger.warning("codecarbon not found, using mock implementation")
|
| 77 |
+
MOCK_MODULES["codecarbon"] = type("MockCodecarbon", (), {
|
| 78 |
+
"EmissionsTracker": create_mock_emissions_tracker()
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
# Handle pydantic
|
| 82 |
+
if not is_module_available("pydantic"):
|
| 83 |
+
logger.warning("pydantic not found, using mock implementation")
|
| 84 |
+
mock_classes = create_mock_pydantic_classes()
|
| 85 |
+
MOCK_MODULES["pydantic"] = type("MockPydantic", (), mock_classes)
|
| 86 |
+
|
| 87 |
+
# Setup service_registry fallback if needed
|
| 88 |
+
if not is_module_available("service_registry"):
|
| 89 |
+
from types import SimpleNamespace
|
| 90 |
+
registry_obj = SimpleNamespace()
|
| 91 |
+
registry_obj.register = lambda *args, **kwargs: None
|
| 92 |
+
registry_obj.get = lambda *args: None
|
| 93 |
+
registry_obj.has = lambda *args: False
|
| 94 |
+
|
| 95 |
+
MOCK_MODULES["service_registry"] = type("MockServiceRegistry", (), {
|
| 96 |
+
"registry": registry_obj,
|
| 97 |
+
"MODEL": "MODEL",
|
| 98 |
+
"TOKENIZER": "TOKENIZER"
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
+
# Custom import hook to provide mock implementations
|
| 102 |
+
class DependencyImportFinder:
|
| 103 |
+
def __init__(self):
|
| 104 |
+
self._mock_modules = MOCK_MODULES
|
| 105 |
+
|
| 106 |
+
def find_module(self, fullname, path=None):
|
| 107 |
+
if fullname in self._mock_modules:
|
| 108 |
+
return self
|
| 109 |
+
|
| 110 |
+
def load_module(self, fullname):
|
| 111 |
+
import sys
|
| 112 |
+
if fullname in sys.modules:
|
| 113 |
+
return sys.modules[fullname]
|
| 114 |
+
|
| 115 |
+
module = self._mock_modules[fullname]
|
| 116 |
+
sys.modules[fullname] = module
|
| 117 |
+
return module
|
| 118 |
+
|
| 119 |
+
# Initialize the fallbacks
|
| 120 |
+
setup_dependency_fallbacks()
|
| 121 |
+
|
| 122 |
+
# Install the custom import hook
|
| 123 |
+
import sys
|
| 124 |
+
sys.meta_path.insert(0, DependencyImportFinder())
|