-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Improve handling of optional dependencies
- Loading branch information
Showing
43 changed files
with
295 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +0,0 @@ | ||
__all__ = ["audio", "core", "io", "text", "tools"] | ||
|
||
from medkit.core.utils import modules_are_available | ||
|
||
if modules_are_available(["torch"]): | ||
__all__ += ["training"] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
import importlib | ||
import inspect | ||
import sys | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
import types | ||
|
||
|
||
__all__ = ["import_optional"] | ||
|
||
|
||
def import_optional(name: str, extra: str | None = None) -> types.ModuleType: | ||
"""Import an optional dependency or raise an appropriate error message. | ||
Parameters | ||
---------- | ||
name : str | ||
Module name to import. | ||
extra : str, optional | ||
Group of optional dependencies to suggest installing if the import fails. | ||
If unspecified, assume the extra is named after the caller's module. | ||
Returns | ||
------- | ||
ModuleType | ||
The successfully imported module. | ||
Raises | ||
------ | ||
ModuleNotFoundError | ||
In case the requested import failed. | ||
""" | ||
try: | ||
module = importlib.import_module(name) | ||
except ModuleNotFoundError as err: | ||
if not extra: | ||
calling_module = inspect.getmodulename(inspect.stack()[1][1]) | ||
extra = calling_module.replace("_", "-") if calling_module else None | ||
|
||
note = f"Consider installing the appropriate extra with:\npip install 'medkit-lib[{extra}]'" if extra else None | ||
|
||
if sys.version_info >= (3, 11): | ||
if note: | ||
err.add_note(note) | ||
raise | ||
|
||
message = "\n".join([str(err), note or ""]) | ||
raise ModuleNotFoundError(message) from err | ||
return module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +0,0 @@ | ||
__all__ = [] | ||
|
||
from medkit.core.utils import modules_are_available | ||
|
||
if modules_are_available(["pyannote"]) and modules_are_available(["pyannote.core", "pyannote.metrics"]): | ||
__all__ += ["diarization"] | ||
|
||
if modules_are_available(["speechbrain"]): | ||
__all__ += ["transcription"] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
__all__ = [] | ||
|
||
from medkit.core.utils import modules_are_available | ||
try: | ||
from medkit.audio.segmentation.pa_speaker_detector import PASpeakerDetector | ||
|
||
if modules_are_available(["webrtcvad"]): | ||
__all__ += ["webrtc_voice_detector"] | ||
__all__ += ["PASpeakerDetector"] | ||
except ModuleNotFoundError: | ||
pass | ||
|
||
if modules_are_available(["pyannote"]) and modules_are_available(["torch", "pyannote.audio"]): | ||
__all__ += ["pa_speaker_detector"] | ||
try: | ||
from medkit.audio.segmentation.webrtc_voice_detector import WebRTCVoiceDetector | ||
|
||
__all__ += ["WebRTCVoiceDetector"] | ||
except ModuleNotFoundError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
from medkit.audio.transcription.doc_transcriber import DocTranscriber, TranscriptionOperation | ||
from medkit.audio.transcription.transcribed_text_document import TranscribedTextDocument | ||
from medkit.core.utils import modules_are_available | ||
|
||
__all__ = [ | ||
"DocTranscriber", | ||
"TranscriptionOperation", | ||
"TranscribedTextDocument", | ||
] | ||
|
||
if modules_are_available(["torchaudio", "transformers"]): | ||
try: | ||
from medkit.audio.transcription.hf_transcriber import HFTranscriber | ||
|
||
__all__ += ["HFTranscriber"] | ||
except ModuleNotFoundError: | ||
pass | ||
|
||
if modules_are_available(["torch", "speechbrain"]): | ||
try: | ||
from medkit.audio.transcription.sb_transcriber import SBTranscriber | ||
|
||
__all__ += ["SBTranscriber"] | ||
except ModuleNotFoundError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +0,0 @@ | ||
__all__ = [] | ||
|
||
from medkit.core.utils import modules_are_available | ||
|
||
if modules_are_available(["seqeval", "transformers", "torch"]): | ||
__all__ += ["ner"] | ||
|
||
if modules_are_available(["sklearn"]): | ||
__all__ += ["classification"] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.