download.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright 2020 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from argparse import ArgumentParser
  15. from . import BaseTransformersCLICommand
  16. def download_command_factory(args):
  17. return DownloadCommand(args.model, args.cache_dir, args.force, args.trust_remote_code)
  18. class DownloadCommand(BaseTransformersCLICommand):
  19. @staticmethod
  20. def register_subcommand(parser: ArgumentParser):
  21. download_parser = parser.add_parser("download")
  22. download_parser.add_argument(
  23. "--cache-dir", type=str, default=None, help="Path to location to store the models"
  24. )
  25. download_parser.add_argument(
  26. "--force", action="store_true", help="Force the model to be download even if already in cache-dir"
  27. )
  28. download_parser.add_argument(
  29. "--trust-remote-code",
  30. action="store_true",
  31. help="Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you've reviewed the code as it will execute on your local machine",
  32. )
  33. download_parser.add_argument("model", type=str, help="Name of the model to download")
  34. download_parser.set_defaults(func=download_command_factory)
  35. def __init__(self, model: str, cache: str, force: bool, trust_remote_code: bool):
  36. self._model = model
  37. self._cache = cache
  38. self._force = force
  39. self._trust_remote_code = trust_remote_code
  40. def run(self):
  41. from ..models.auto import AutoModel, AutoTokenizer
  42. AutoModel.from_pretrained(
  43. self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code
  44. )
  45. AutoTokenizer.from_pretrained(
  46. self._model, cache_dir=self._cache, force_download=self._force, trust_remote_code=self._trust_remote_code
  47. )