Skip to content

Commit

Permalink
fix decord import
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoestq committed Oct 28, 2024
1 parent 65f6eb5 commit bd57f96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/datasets/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
except importlib.metadata.PackageNotFoundError:
pass


DUCKDB_VERSION = "N/A"
DUCKDB_AVAILABLE = importlib.util.find_spec("duckdb") is not None

if DUCKDB_AVAILABLE:
try:
DUCKDB_VERSION = version.parse(importlib.metadata.version("duckdb"))
logger.info(f"Duckdb version {DUCKDB_VERSION} available.")
except importlib.metadata.PackageNotFoundError:
pass

TF_VERSION = "N/A"
TF_AVAILABLE = False

Expand Down
11 changes: 7 additions & 4 deletions src/datasets/features/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class Video:
pa_type: ClassVar[Any] = pa.struct({"bytes": pa.binary(), "path": pa.string()})
_type: str = field(default="Video", init=False, repr=False)

def __post_init__(self):
if config.DECORD_AVAILABLE:
patch_decord()

def __call__(self):
return self.pa_type

Expand Down Expand Up @@ -318,8 +322,11 @@ def patch_decord():
# We need to import torch first, otherwise later it can cause issues
# e.g. "RuntimeError: random_device could not be read"
# when running `torch.tensor(value).share_memory_()`
# Same for duckdb which crashes on import
if config.TORCH_AVAILABLE:
import torch # noqa
if config.DUCKDB_AVAILABLE:
import duckdb # noqa
import decord.video_reader
from decord import VideoReader

Expand All @@ -334,7 +341,3 @@ def patch_decord():
VideoReader._hf_patched = True
else:
raise ImportError("To support decoding videos, please install 'decord'.")


if config.DECORD_AVAILABLE:
patch_decord()

0 comments on commit bd57f96

Please sign in to comment.