Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- bhashini.py +35 -25
bhashini.py
CHANGED
|
@@ -6,46 +6,56 @@ import threading
|
|
| 6 |
import sys
|
| 7 |
import types
|
| 8 |
|
| 9 |
-
# --- CRITICAL SPRINT 18 PATCH ---
|
| 10 |
-
# IndicTrans2's configuration_indictrans.py imports
|
| 11 |
-
# `transformers.onnx
|
| 12 |
-
#
|
| 13 |
-
#
|
| 14 |
-
#
|
|
|
|
|
|
|
| 15 |
if 'transformers.onnx' not in sys.modules:
|
| 16 |
-
# Base mock classes that IndicTrans2 config tries to inherit from
|
| 17 |
-
class MockOnnxConfig:
|
| 18 |
-
pass
|
| 19 |
-
class MockOnnxSeq2SeqConfigWithPast(MockOnnxConfig):
|
| 20 |
-
pass
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
mock_onnx.__package__ = 'transformers.onnx'
|
| 26 |
-
mock_onnx.OnnxConfig = MockOnnxConfig
|
| 27 |
-
mock_onnx.OnnxSeq2SeqConfigWithPast = MockOnnxSeq2SeqConfigWithPast
|
| 28 |
|
| 29 |
-
|
| 30 |
-
mock_onnx_utils = types.ModuleType('transformers.onnx.utils')
|
| 31 |
mock_onnx_utils.__package__ = 'transformers.onnx'
|
| 32 |
|
| 33 |
-
|
| 34 |
-
mock_onnx_config = types.ModuleType('transformers.onnx.config')
|
| 35 |
mock_onnx_config.__package__ = 'transformers.onnx'
|
| 36 |
-
mock_onnx_config.OnnxConfig = MockOnnxConfig
|
| 37 |
-
mock_onnx_config.OnnxSeq2SeqConfigWithPast = MockOnnxSeq2SeqConfigWithPast
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
sys.modules['transformers.onnx'] = mock_onnx
|
| 41 |
sys.modules['transformers.onnx.utils'] = mock_onnx_utils
|
| 42 |
sys.modules['transformers.onnx.config'] = mock_onnx_config
|
|
|
|
| 43 |
|
| 44 |
-
# Wire submodules as attributes on parent
|
| 45 |
mock_onnx.utils = mock_onnx_utils
|
| 46 |
mock_onnx.config = mock_onnx_config
|
|
|
|
| 47 |
|
| 48 |
-
# Wire
|
| 49 |
import transformers
|
| 50 |
transformers.onnx = mock_onnx
|
| 51 |
# --------------------------------
|
|
|
|
| 6 |
import sys
|
| 7 |
import types
|
| 8 |
|
| 9 |
+
# --- CRITICAL SPRINT 18 PATCH (DEFINITIVE FIX) ---
|
| 10 |
+
# IndicTrans2's remote `configuration_indictrans.py` imports many classes and functions
|
| 11 |
+
# from `transformers.onnx` (OnnxConfig, OnnxSeq2SeqConfigWithPast, compute_effective_axis_dimension, etc.)
|
| 12 |
+
# This subpackage was REMOVED in transformers>=4.40, but we need transformers>=4.48 for ModernBERT.
|
| 13 |
+
#
|
| 14 |
+
# SOLUTION: Universal mock modules that return a no-op dummy for ANY attribute access.
|
| 15 |
+
# The ONNX classes are ONLY used for ONNX model export — never during inference.
|
| 16 |
+
# So returning harmless dummies is 100% safe for our translation pipeline.
|
| 17 |
if 'transformers.onnx' not in sys.modules:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
class _UniversalDummy:
|
| 20 |
+
"""A universal no-op class that acts as a stand-in for any ONNX class/function.
|
| 21 |
+
Can be instantiated, called, subclassed, and used as a decorator — all no-ops."""
|
| 22 |
+
def __init__(self, *a, **kw): pass
|
| 23 |
+
def __call__(self, *a, **kw): return self
|
| 24 |
+
def __getattr__(self, name): return _UniversalDummy()
|
| 25 |
+
def __init_subclass__(cls, **kw): pass
|
| 26 |
+
|
| 27 |
+
class _MockOnnxModule(types.ModuleType):
|
| 28 |
+
"""A module that returns _UniversalDummy for any attribute access.
|
| 29 |
+
This means `from transformers.onnx.utils import <anything>` always succeeds."""
|
| 30 |
+
def __getattr__(self, name):
|
| 31 |
+
return _UniversalDummy
|
| 32 |
+
|
| 33 |
+
# Build the mock package tree
|
| 34 |
+
mock_onnx = _MockOnnxModule('transformers.onnx')
|
| 35 |
+
mock_onnx.__path__ = [] # Makes Python treat it as a package
|
| 36 |
mock_onnx.__package__ = 'transformers.onnx'
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
mock_onnx_utils = _MockOnnxModule('transformers.onnx.utils')
|
|
|
|
| 39 |
mock_onnx_utils.__package__ = 'transformers.onnx'
|
| 40 |
|
| 41 |
+
mock_onnx_config = _MockOnnxModule('transformers.onnx.config')
|
|
|
|
| 42 |
mock_onnx_config.__package__ = 'transformers.onnx'
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
mock_onnx_features = _MockOnnxModule('transformers.onnx.features')
|
| 45 |
+
mock_onnx_features.__package__ = 'transformers.onnx'
|
| 46 |
+
|
| 47 |
+
# Register in sys.modules (covers all possible import paths)
|
| 48 |
sys.modules['transformers.onnx'] = mock_onnx
|
| 49 |
sys.modules['transformers.onnx.utils'] = mock_onnx_utils
|
| 50 |
sys.modules['transformers.onnx.config'] = mock_onnx_config
|
| 51 |
+
sys.modules['transformers.onnx.features'] = mock_onnx_features
|
| 52 |
|
| 53 |
+
# Wire submodules as attributes on parent
|
| 54 |
mock_onnx.utils = mock_onnx_utils
|
| 55 |
mock_onnx.config = mock_onnx_config
|
| 56 |
+
mock_onnx.features = mock_onnx_features
|
| 57 |
|
| 58 |
+
# Wire onto the real transformers package
|
| 59 |
import transformers
|
| 60 |
transformers.onnx = mock_onnx
|
| 61 |
# --------------------------------
|