anomalib icon indicating copy to clipboard operation
anomalib copied to clipboard

๐ŸžModify the import structure of models that don't rely on core requirements.

Open harimkang opened this issue 1 year ago โ€ข 5 comments

๐Ÿ“ Description

This PR modifies anomalib to check if the python-dotenv package is available and import the required VlmAd model.

Issue: python-dotenv is not included in core dependencies, so if only have core installed and using it, we will not be able to import VlmAd. This will be imported and cause an error even if the user using core only does not use VlmAd.

pip install anomalib[core]==1.2.0
from anomalib.models.image import Padim

Traceback (most recent call last):
  File "/home/harimkan/workspace/repo/otx-regression/api_test_2.py", line 1, in <module>
    from anomalib.models.image import Padim
  File "/home/harimkan/workspace/repo/otx-regression/venv/lib/python3.10/site-packages/anomalib/models/__init__.py", line 15, in <module>
    from .image import (
  File "/home/harimkan/workspace/repo/otx-regression/venv/lib/python3.10/site-packages/anomalib/models/image/__init__.py", line 23, in <module>
    from .vlm_ad import VlmAd
  File "/home/harimkan/workspace/repo/otx-regression/venv/lib/python3.10/site-packages/anomalib/models/image/vlm_ad/__init__.py", line 6, in <module>
    from .lightning_model import VlmAd
  File "/home/harimkan/workspace/repo/otx-regression/venv/lib/python3.10/site-packages/anomalib/models/image/vlm_ad/lightning_model.py", line 14, in <module>
    from .backends import Backend, ChatGPT, Huggingface, Ollama
  File "/home/harimkan/workspace/repo/otx-regression/venv/lib/python3.10/site-packages/anomalib/models/image/vlm_ad/backends/__init__.py", line 7, in <module>
    from .chat_gpt import ChatGPT
  File "/home/harimkan/workspace/repo/otx-regression/venv/lib/python3.10/site-packages/anomalib/models/image/vlm_ad/backends/chat_gpt.py", line 12, in <module>
    from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'

โœจ Changes

Select what type of change your PR is:

  • [X] ๐Ÿž Bug fix (non-breaking change which fixes an issue)
  • [ ] ๐Ÿ”จ Refactor (non-breaking change which refactors the code base)
  • [ ] ๐Ÿš€ New feature (non-breaking change which adds functionality)
  • [ ] ๐Ÿ’ฅ Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] ๐Ÿ“š Documentation update
  • [ ] ๐Ÿ”’ Security update

โœ… Checklist

Before you submit your pull request, please make sure you have completed the following steps:

  • [ ] ๐Ÿ“‹ I have summarized my changes in the CHANGELOG and followed the guidelines for my type of change (skip for minor changes, documentation updates, and test enhancements).
  • [ ] ๐Ÿ“š I have made the necessary updates to the documentation (if applicable).
  • [ ] ๐Ÿงช I have written tests that support my changes and prove that my fix is effective or my feature works (if applicable).

For more information about code review checklists, see the Code Review Checklist.

harimkang avatar Nov 05 '24 07:11 harimkang

@samet-akcay In fact, we have a way of raising python-dotenv to core rather than doing something as messy as this PR. What do you think?

harimkang avatar Nov 05 '24 07:11 harimkang

@harimkang, good find! Thanks for reporting and fixing!

@ashwinvaidya17 is python-dotenv only used within VlmAd stuff?

samet-akcay avatar Nov 05 '24 07:11 samet-akcay

@harimkang, good find! Thanks for reporting and fixing!

@ashwinvaidya17 is python-dotenv only used within VlmAd stuff?

Yeah it is only used on VlmAd so that we can fetch the API key from either the dot file or the system environment.

ashwinvaidya17 avatar Nov 05 '24 08:11 ashwinvaidya17

@harimkang, there are some other model-specific packages that are only used by individual models. I would like to keep the core dependencies as minimum as possible. Your approach here would be useful for such models too.

I was wondering if there is any way to make this approach more generic, so we could use it for other models or imports as well. --Not for this PR though, something to think about for the future.

samet-akcay avatar Nov 05 '24 10:11 samet-akcay

@ashwinvaidya17, any thoughts on this. Should we merge this, or trying to find a more generic approach?

samet-akcay avatar Jan 20 '25 18:01 samet-akcay