Skip to content

Commit

Permalink
Remove distutils (huggingface#7455)
Browse files Browse the repository at this point in the history
* strtobool

* replace Command from setuptools.
  • Loading branch information
sayakpaul authored and AbhinavGopal committed Mar 27, 2024
1 parent 18d74db commit 0ab01ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@
import os
import re
import sys
from distutils.core import Command

from setuptools import find_packages, setup
from setuptools import Command, find_packages, setup


# IMPORTANT:
Expand Down Expand Up @@ -163,7 +162,7 @@ def deps_list(*pkgs):

class DepsTableUpdateCommand(Command):
"""
A custom distutils command that updates the dependency table.
A custom command that updates the dependency table.
usage: python setup.py deps_table_update
"""

Expand Down
19 changes: 17 additions & 2 deletions src/diffusers/utils/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import unittest
import urllib.parse
from contextlib import contextmanager
from distutils.util import strtobool
from io import BytesIO, StringIO
from pathlib import Path
from typing import Callable, Dict, List, Optional, Union
Expand Down Expand Up @@ -151,7 +150,7 @@ def parse_flag_from_env(key, default=False):
else:
# KEY is set, convert it to True or False.
try:
_value = strtobool(value)
_value = str_to_bool(value)
except ValueError:
# More values are supported, but let's keep the message simple.
raise ValueError(f"If set, {key} must be yes or no.")
Expand Down Expand Up @@ -921,6 +920,22 @@ def backend_supports_training(device: str):
return BACKEND_SUPPORTS_TRAINING[device]


# Taken from the following PR:
# https://github.com/huggingface/accelerate/pull/1964
def str_to_bool(value) -> int:
"""
Converts a string representation of truth to `True` (1) or `False` (0).
True values are `y`, `yes`, `t`, `true`, `on`, and `1`; False value are `n`, `no`, `f`, `false`, `off`, and `0`;
"""
value = value.lower()
if value in ("y", "yes", "t", "true", "on", "1"):
return 1
elif value in ("n", "no", "f", "false", "off", "0"):
return 0
else:
raise ValueError(f"invalid truth value {value}")


# Guard for when Torch is not available
if is_torch_available():
# Update device function dict mapping
Expand Down
4 changes: 2 additions & 2 deletions tests/others/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

import os
import unittest
from distutils.util import strtobool

import pytest

from diffusers import __version__
from diffusers.utils import deprecate
from diffusers.utils.testing_utils import str_to_bool


# Used to test the hub
Expand Down Expand Up @@ -191,7 +191,7 @@ def parse_flag_from_env(key, default=False):
else:
# KEY is set, convert it to True or False.
try:
_value = strtobool(value)
_value = str_to_bool(value)
except ValueError:
# More values are supported, but let's keep the message simple.
raise ValueError(f"If set, {key} must be yes or no.")
Expand Down

0 comments on commit 0ab01ae

Please sign in to comment.