Skip to content

Commit 59c1af7

Browse files
[Commands] Add env command (huggingface#352)
* [Commands] Add env command * Apply suggestions from code review
1 parent fd76845 commit 59c1af7

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

.github/ISSUE_TEMPLATE/bug-report.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ body:
66
attributes:
77
value: |
88
Thanks for taking the time to fill out this bug report!
9+
- type: textarea
10+
id: system-info
11+
attributes:
12+
label: System Info
13+
description: Please share your system info with us. You can run the command `diffusers-cli env` and copy-paste its output below.
14+
placeholder: diffusers version, platform, python version, ...
15+
validations:
16+
required: true
917
- type: textarea
1018
id: bug-description
1119
attributes:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def run(self):
201201
python_requires=">=3.6.0",
202202
install_requires=install_requires,
203203
extras_require=extras,
204+
entry_points={"console_scripts": ["diffusers-cli=diffusers.commands.diffusers_cli:main"]},
204205
classifiers=[
205206
"Development Status :: 5 - Production/Stable",
206207
"Intended Audience :: Developers",
@@ -209,7 +210,6 @@ def run(self):
209210
"License :: OSI Approved :: Apache Software License",
210211
"Operating System :: OS Independent",
211212
"Programming Language :: Python :: 3",
212-
"Programming Language :: Python :: 3.6",
213213
"Programming Language :: Python :: 3.7",
214214
"Programming Language :: Python :: 3.8",
215215
"Programming Language :: Python :: 3.9",

src/diffusers/commands/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2022 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+
15+
from abc import ABC, abstractmethod
16+
from argparse import ArgumentParser
17+
18+
19+
class BaseDiffusersCLICommand(ABC):
20+
@staticmethod
21+
@abstractmethod
22+
def register_subcommand(parser: ArgumentParser):
23+
raise NotImplementedError()
24+
25+
@abstractmethod
26+
def run(self):
27+
raise NotImplementedError()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# Copyright 2022 The HuggingFace Team. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from argparse import ArgumentParser
17+
18+
from .env import EnvironmentCommand
19+
20+
21+
def main():
22+
parser = ArgumentParser("Diffusers CLI tool", usage="diffusers-cli <command> [<args>]")
23+
commands_parser = parser.add_subparsers(help="diffusers-cli command helpers")
24+
25+
# Register commands
26+
EnvironmentCommand.register_subcommand(commands_parser)
27+
28+
# Let's go
29+
args = parser.parse_args()
30+
31+
if not hasattr(args, "func"):
32+
parser.print_help()
33+
exit(1)
34+
35+
# Run
36+
service = args.func(args)
37+
service.run()
38+
39+
40+
if __name__ == "__main__":
41+
main()

src/diffusers/commands/env.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2022 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+
15+
import platform
16+
from argparse import ArgumentParser
17+
18+
import huggingface_hub
19+
20+
from .. import __version__ as version
21+
from ..utils import is_torch_available, is_transformers_available
22+
from . import BaseDiffusersCLICommand
23+
24+
25+
def info_command_factory(_):
26+
return EnvironmentCommand()
27+
28+
29+
class EnvironmentCommand(BaseDiffusersCLICommand):
30+
@staticmethod
31+
def register_subcommand(parser: ArgumentParser):
32+
download_parser = parser.add_parser("env")
33+
download_parser.set_defaults(func=info_command_factory)
34+
35+
def run(self):
36+
hub_version = huggingface_hub.__version__
37+
38+
pt_version = "not installed"
39+
pt_cuda_available = "NA"
40+
if is_torch_available():
41+
import torch
42+
43+
pt_version = torch.__version__
44+
pt_cuda_available = torch.cuda.is_available()
45+
46+
transformers_version = "not installed"
47+
if is_transformers_available:
48+
import transformers
49+
50+
transformers_version = transformers.__version__
51+
52+
info = {
53+
"`diffusers` version": version,
54+
"Platform": platform.platform(),
55+
"Python version": platform.python_version(),
56+
"PyTorch version (GPU?)": f"{pt_version} ({pt_cuda_available})",
57+
"Huggingface_hub version": hub_version,
58+
"Transformers version": transformers_version,
59+
"Using GPU in script?": "<fill in>",
60+
"Using distributed or parallel set-up in script?": "<fill in>",
61+
}
62+
63+
print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n")
64+
print(self.format_dict(info))
65+
66+
return info
67+
68+
@staticmethod
69+
def format_dict(d):
70+
return "\n".join([f"- {prop}: {val}" for prop, val in d.items()]) + "\n"

0 commit comments

Comments
 (0)