Skip to content

Commit dda4162

Browse files
AbdullahKIRMANsukrukirmanfcakyon
authored
fix: improve handling of missing torch dependency in pipeline initialization (#6)
Co-authored-by: sukrukirman <sukru.kirman@ug.bilkent.edu.tr> Co-authored-by: Fatih Akyon <fcakyon@gmail.com>
1 parent 4e52916 commit dda4162

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/moderators/integrations/transformers_moderator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3+
import importlib
34
import json
5+
import sys
46
from pathlib import Path
57
from typing import Any
68

@@ -41,11 +43,19 @@ def load_model(self) -> None:
4143
"Install with: uv pip install -e '.[transformers]' or: uv pip install transformers"
4244
) from e
4345

44-
pipeline = getattr(_transformers, "pipeline")
46+
had_torch = "torch" in sys.modules
4547

4648
# Ensure a DL framework (pt/tf/flax)
4749
framework = ensure_dl_framework(auto_install)
4850

51+
if framework == "pt" and not had_torch and "torch" in sys.modules:
52+
try:
53+
_transformers = importlib.reload(_transformers)
54+
except Exception:
55+
pass
56+
57+
pipeline = getattr(_transformers, "pipeline")
58+
4959
# Ensure Pillow for image tasks
5060
ensure_pillow_for_task(task, auto_install)
5161

0 commit comments

Comments
 (0)