CoolFace
Apppublic

Clicko777/RVC_HFv2

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
utils.py102 linesDownload Raw Back to julius
1# File under the MIT license, see https://github.com/adefossez/julius/LICENSE for details.2# Author: adefossez, 20203"""4Non signal processing related utilities.5"""6 7import inspect8import typing as tp9import sys10import time11 12 13def simple_repr(obj, attrs: tp.Optional[tp.Sequence[str]] = None,14                overrides: dict = {}):15    """16    Return a simple representation string for `obj`.17    If `attrs` is not None, it should be a list of attributes to include.18    """19    params = inspect.signature(obj.__class__).parameters20    attrs_repr = []21    if attrs is None:22        attrs = list(params.keys())23    for attr in attrs:24        display = False25        if attr in overrides:26            value = overrides[attr]27        elif hasattr(obj, attr):28            value = getattr(obj, attr)29        else:30            continue31        if attr in params:32            param = params[attr]33            if param.default is inspect._empty or value != param.default:  # type: ignore34                display = True35        else:36            display = True37 38        if display:39            attrs_repr.append(f"{attr}={value}")40    return f"{obj.__class__.__name__}({','.join(attrs_repr)})"41 42 43class MarkdownTable:44    """45    Simple MarkdownTable generator. The column titles should be large enough46    for the lines content. This will right align everything.47 48    >>> import io  # we use io purely for test purposes, default is sys.stdout.49    >>> file = io.StringIO()50    >>> table = MarkdownTable(["Item Name", "Price"], file=file)51    >>> table.header(); table.line(["Honey", "5"]); table.line(["Car", "5,000"])52    >>> print(file.getvalue().strip())  # Strip for test purposes53    | Item Name | Price |54    |-----------|-------|55    |     Honey |     5 |56    |       Car | 5,000 |57    """58    def __init__(self, columns, file=sys.stdout):59        self.columns = columns60        self.file = file61 62    def _writeln(self, line):63        self.file.write("|" + "|".join(line) + "|\n")64 65    def header(self):66        self._writeln(f" {col} " for col in self.columns)67        self._writeln("-" * (len(col) + 2) for col in self.columns)68 69    def line(self, line):70        out = []71        for val, col in zip(line, self.columns):72            val = format(val, '>' + str(len(col)))73            out.append(" " + val + " ")74        self._writeln(out)75 76 77class Chrono:78    """79    Measures ellapsed time, calling `torch.cuda.synchronize` if necessary.80    `Chrono` instances can be used as context managers (e.g. with `with`).81    Upon exit of the block, you can access the duration of the block in seconds82    with the `duration` attribute.83 84    >>> with Chrono() as chrono:85    ...     _ = sum(range(10_000))86    ...87    >>> print(chrono.duration < 10)  # Should be true unless on a really slow computer.88    True89    """90    def __init__(self):91        self.duration = None92 93    def __enter__(self):94        self._begin = time.time()95        return self96 97    def __exit__(self, exc_type, exc_value, exc_tracebck):98        import torch99        if torch.cuda.is_available():100            torch.cuda.synchronize()101        self.duration = time.time() - self._begin102