Skip to content

Commit

Permalink
remove heavy imports (#110)
Browse files Browse the repository at this point in the history
* add cli

* fix

* fix

* fix

* fix

* test fix
  • Loading branch information
aniketmaurya authored Jul 29, 2021
1 parent cbd518e commit 01c76cd
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 37 deletions.
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,3 @@ repos:
rev: 0.5.0
hooks:
- id: nbstripout

- repo: https://github.com/myint/docformatter
rev: v1.3.1
hooks:
- id: docformatter
args: [ '--in-place' ]
4 changes: 1 addition & 3 deletions chitra/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Deep Learning library for Model Building, Interpretability, Visualization, API Building & Deployment."""

__version__ = "0.1.0"
__version__ = "0.1.1a0"
__license__ = "Apache License 2.0"

from chitra.image import Chitra
3 changes: 3 additions & 0 deletions chitra/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import typer

app = typer.Typer()
9 changes: 9 additions & 0 deletions chitra/cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import typer

from chitra import __version__
from chitra.cli import app


@app.command()
def hello():
typer.echo(f"Hey! You're running chitra version={__version__}")
6 changes: 3 additions & 3 deletions chitra/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

from chitra.constants import _TF, _TORCH, CHITRA_URL_SEP, IMAGE_CACHE_DIR
from chitra.coordinates import BoundingBoxes
from chitra.utility.import_utils import INSTALLED_MODULES
from chitra.utility.import_utils import is_installed

tf = None
torch = None

if INSTALLED_MODULES.get(_TF, None):
if is_installed(_TF):
import tensorflow as tf

if INSTALLED_MODULES.get(_TORCH, None):
if is_installed(_TORCH):
import torch

DATA_FORMATS = Union[str, Image.Image, np.ndarray, "tf.Tensor", "torch.Tensor"]
Expand Down
21 changes: 21 additions & 0 deletions chitra/imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from chitra.constants import _FLAX, _JAX, _TF, _TF_GPU, _TORCH, _TORCHVISION
from chitra.utility.import_utils import is_installed

INSTALLED_MODULES = {
module: is_installed(module)
for module in (_TF, _TF_GPU, _TORCH, _TORCHVISION, _JAX, _FLAX)
}

_FASTAPI_INSTALLED = is_installed("fastapi")
_UVICORN_INSTALLED = is_installed("uvicorn")
_PYDANTIC_INSTALLED = is_installed("pydantic")
_MULTIPART_INSTALLED = is_installed("multipart")
_SERVE_INSTALLED = (
_FASTAPI_INSTALLED
and _UVICORN_INSTALLED
and _PYDANTIC_INSTALLED
and _MULTIPART_INSTALLED
)

_LOGURU_INSTALLED = is_installed("loguru")
_RICH_INSTALLED = is_installed("rich")
2 changes: 1 addition & 1 deletion chitra/logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from chitra.utility.import_utils import _LOGURU_INSTALLED
from chitra.imports import _LOGURU_INSTALLED

if _LOGURU_INSTALLED:
from loguru import logger
Expand Down
22 changes: 0 additions & 22 deletions chitra/utility/import_utils.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import importlib

from chitra.constants import _FLAX, _JAX, _TF, _TF_GPU, _TORCH, _TORCHVISION


def is_installed(module_name: str):
return importlib.util.find_spec(module_name) is not None


INSTALLED_MODULES = {
module: is_installed(module)
for module in (_TF, _TF_GPU, _TORCH, _TORCHVISION, _JAX, _FLAX)
}

_FASTAPI_INSTALLED = is_installed("fastapi")
_UVICORN_INSTALLED = is_installed("uvicorn")
_PYDANTIC_INSTALLED = is_installed("pydantic")
_MULTIPART_INSTALLED = is_installed("multipart")
_SERVE_INSTALLED = (
_FASTAPI_INSTALLED
and _UVICORN_INSTALLED
and _PYDANTIC_INSTALLED
and _MULTIPART_INSTALLED
)

_LOGURU_INSTALLED = is_installed("loguru")
_RICH_INSTALLED = is_installed("rich")
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ test = [

[tool.isort]
profile = "black"

[tool.flit.scripts]
chitra = "chitra.cli:app"
4 changes: 3 additions & 1 deletion tests/serve/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import pytest
from fastapi import FastAPI

from chitra.serve import API, create_api, constants as const
from chitra.serve import API
from chitra.serve import constants as const
from chitra.serve import create_api


def dummy_model(x):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tf_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import tensorflow as tf

from chitra import Chitra
from chitra.image import Chitra
from chitra.tf_image import read_image, resize_image

chitra_banner = (
Expand Down

0 comments on commit 01c76cd

Please sign in to comment.