Spaces:
Running on Zero
Running on Zero
Create fix_lazy_loading.py
#44
by ismail111 - opened
- fix_lazy_loading.py +41 -0
fix_lazy_loading.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Patch for lazy loading issues
|
| 2 |
+
import sys
|
| 3 |
+
import importlib
|
| 4 |
+
|
| 5 |
+
def patch_t5_tokenizer():
|
| 6 |
+
"""Force proper loading of T5 tokenizer"""
|
| 7 |
+
try:
|
| 8 |
+
# Force import of T5 tokenizer module
|
| 9 |
+
import transformers.models.t5.tokenization_t5
|
| 10 |
+
|
| 11 |
+
# Ensure the module is properly loaded
|
| 12 |
+
if hasattr(transformers.models.t5.tokenization_t5, 'T5Tokenizer'):
|
| 13 |
+
print("T5Tokenizer successfully loaded")
|
| 14 |
+
return True
|
| 15 |
+
else:
|
| 16 |
+
print("T5Tokenizer not found in module")
|
| 17 |
+
return False
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Failed to patch T5 tokenizer: {e}")
|
| 20 |
+
return False
|
| 21 |
+
|
| 22 |
+
def patch_transformers():
|
| 23 |
+
"""Apply patches to fix lazy loading"""
|
| 24 |
+
# Import transformers first
|
| 25 |
+
import transformers
|
| 26 |
+
|
| 27 |
+
# Force load tokenizer modules
|
| 28 |
+
from transformers import T5Tokenizer, T5TokenizerFast
|
| 29 |
+
|
| 30 |
+
# Monkey patch the lazy module if needed
|
| 31 |
+
import transformers.models.t5.tokenization_t5 as t5_tokenizer_module
|
| 32 |
+
|
| 33 |
+
# Ensure classes are properly exposed
|
| 34 |
+
if not hasattr(t5_tokenizer_module, 'T5Tokenizer'):
|
| 35 |
+
t5_tokenizer_module.T5Tokenizer = T5Tokenizer
|
| 36 |
+
|
| 37 |
+
print("Transformers patching complete")
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
patch_transformers()
|
| 41 |
+
patch_t5_tokenizer()
|