Shim is_torch_fx_available instead of pinning transformers down
Browse filesPinning transformers to the 4.56 line (for MiniCPM) is impossible in this env:
it requires huggingface-hub<1.0 while Gradio 6.18 requires >=1.0. Keep
transformers current and restore the is_torch_fx_available symbol that
MiniCPM4.1-8B's bundled modeling code imports, before any remote code runs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- app.py +19 -0
- requirements.txt +4 -3
app.py
CHANGED
|
@@ -20,6 +20,25 @@ import spaces
|
|
| 20 |
import torch
|
| 21 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
from shards import SHARD_ORDER, SHARDS
|
| 24 |
|
| 25 |
# ---------------------------------------------------------------------------- #
|
|
|
|
| 20 |
import torch
|
| 21 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 22 |
|
| 23 |
+
# Compatibility shim: MiniCPM4.1-8B's bundled modeling code does
|
| 24 |
+
# `from transformers.utils.import_utils import is_torch_fx_available`, which newer
|
| 25 |
+
# transformers removed from that module. We can't pin transformers down (Gradio
|
| 26 |
+
# 6.18 needs huggingface-hub>=1.0, which the old transformers line conflicts
|
| 27 |
+
# with), so we restore the symbol before any remote code runs.
|
| 28 |
+
import transformers.utils.import_utils as _iu # noqa: E402
|
| 29 |
+
if not hasattr(_iu, "is_torch_fx_available"):
|
| 30 |
+
try:
|
| 31 |
+
from transformers.utils import is_torch_fx_available as _fx # type: ignore
|
| 32 |
+
except Exception:
|
| 33 |
+
try:
|
| 34 |
+
import torch.fx # noqa: F401
|
| 35 |
+
def _fx(): # torch.fx imports fine -> treat as available
|
| 36 |
+
return True
|
| 37 |
+
except Exception:
|
| 38 |
+
def _fx():
|
| 39 |
+
return False
|
| 40 |
+
_iu.is_torch_fx_available = _fx
|
| 41 |
+
|
| 42 |
from shards import SHARD_ORDER, SHARDS
|
| 43 |
|
| 44 |
# ---------------------------------------------------------------------------- #
|
requirements.txt
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
gradio
|
| 2 |
spaces
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
torch
|
| 7 |
accelerate
|
|
|
|
| 1 |
gradio
|
| 2 |
spaces
|
| 3 |
+
# Keep transformers current (Gradio 6.18 needs huggingface-hub>=1.0, which the
|
| 4 |
+
# old 4.56 line conflicts with). MiniCPM's bundled code expects an older symbol
|
| 5 |
+
# (is_torch_fx_available) that we shim in app.py instead of downgrading.
|
| 6 |
+
transformers>=4.51.0
|
| 7 |
torch
|
| 8 |
accelerate
|