Skip to content

Commit

Permalink
test fix for batch upscale
Browse files Browse the repository at this point in the history
  • Loading branch information
the-database committed Jul 27, 2024
1 parent 664dd84 commit 43ebf35
Show file tree
Hide file tree
Showing 125 changed files with 1,786 additions and 22,718 deletions.
2 changes: 1 addition & 1 deletion MangaJaNaiConverterGui/Services/PythonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public string InstallUpdatePythonDependenciesCommand

var relPythonPath = @".\python\python\python.exe";

return $@"{relPythonPath} -m pip install torch==2.3.1 torchvision==0.18.1 --index-url https://download.pytorch.org/whl/cu121 && {relPythonPath} -m pip install {string.Join(" ", dependencies)}";
return $@"{relPythonPath} -m pip install torch==2.1.0 torchvision==0.18.1 --index-url https://download.pytorch.org/whl/cu121 && {relPythonPath} -m pip install {string.Join(" ", dependencies)}";
}
}

Expand Down
9 changes: 9 additions & 0 deletions MangaJaNaiConverterGui/backend/src/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.1
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
1 change: 1 addition & 0 deletions MangaJaNaiConverterGui/backend/src/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .api import *
from .group import *
from .input import *
from .iter import *
from .lazy import *
from .node_context import *
from .node_data import *
Expand Down
102 changes: 21 additions & 81 deletions MangaJaNaiConverterGui/backend/src/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import importlib
import os
from collections.abc import Awaitable, Callable, Iterable
from dataclasses import asdict, dataclass, field
from typing import (
Any,
Awaitable,
Callable,
Generic,
Iterable,
TypeVar,
)

Expand All @@ -24,7 +21,13 @@
check_naming_conventions,
check_schema_types,
)
from .node_data import IteratorInputInfo, IteratorOutputInfo, KeyInfo, NodeData
from .node_data import (
IteratorInputInfo,
IteratorOutputInfo,
KeyInfo,
NodeData,
SpecialSuggestion,
)
from .output import BaseOutput
from .settings import Setting
from .types import FeatureId, InputId, NodeId, NodeKind, OutputId, RunFn
Expand Down Expand Up @@ -83,7 +86,7 @@ class NodeGroup:
order: list[str | NodeId] = field(default_factory=list)
nodes: list[NodeData] = field(default_factory=list)

def add_node(self, node: NodeData):
def add_node(self, node: NodeData) -> None:
logger.debug(f"Added {node.schema_id}")
self.nodes.append(node)

Expand All @@ -98,6 +101,7 @@ def to_dict(self):
def register(
self,
schema_id: str,
*,
name: str,
description: str | list[str],
inputs: list[BaseInput | NestedGroup],
Expand All @@ -114,6 +118,7 @@ def register(
iterator_outputs: list[IteratorOutputInfo] | IteratorOutputInfo | None = None,
node_context: bool = False,
key_info: KeyInfo | None = None,
suggestions: list[SpecialSuggestion] | None = None,
):
if not isinstance(description, str):
description = "\n\n".join(description)
Expand Down Expand Up @@ -141,14 +146,14 @@ def to_list(x: list[S] | S | None) -> list[S]:
iterator_inputs = to_list(iterator_inputs)
iterator_outputs = to_list(iterator_outputs)

if kind == "collector":
assert len(iterator_inputs) == 1 and len(iterator_outputs) == 0
elif kind == "newIterator":
if kind == "generator": # Generator
assert len(iterator_inputs) == 0 and len(iterator_outputs) == 1
elif kind == "collector":
assert len(iterator_inputs) == 1 and len(iterator_outputs) == 0
else:
assert len(iterator_inputs) == 0 and len(iterator_outputs) == 0

def run_check(level: CheckLevel, run: Callable[[bool], None]):
def run_check(level: CheckLevel, run: Callable[[bool], None]) -> None:
if level == CheckLevel.NONE:
return

Expand Down Expand Up @@ -180,9 +185,10 @@ def inner_wrapper(wrapped_func: T) -> T:
inputs=p_inputs,
group_layout=group_layout,
outputs=p_outputs,
iterator_inputs=iterator_inputs,
iterator_outputs=iterator_outputs,
iterable_inputs=iterator_inputs,
iterable_outputs=iterator_outputs,
key_info=key_info,
suggestions=suggestions or [],
side_effects=side_effects,
deprecated=deprecated,
node_context=node_context,
Expand Down Expand Up @@ -352,10 +358,10 @@ def add_category(
self.categories.append(result)
return result

def add_dependency(self, dependency: Dependency):
def add_dependency(self, dependency: Dependency) -> None:
self.dependencies.append(dependency)

def add_setting(self, setting: Setting):
def add_setting(self, setting: Setting) -> None:
self.settings.append(setting)

def add_feature(
Expand Down Expand Up @@ -457,7 +463,7 @@ def load_nodes(self, current_file: str) -> list[LoadErrorInfo]:

return load_error

def _refresh_nodes(self):
def _refresh_nodes(self) -> None:
self.nodes = {}
self.categories = []

Expand Down Expand Up @@ -495,69 +501,3 @@ def add_package(
dependencies=dependencies or [],
)
)


I = TypeVar("I")
L = TypeVar("L")


@dataclass
class Iterator(Generic[I]):
iter_supplier: Callable[[], Iterable[I | Exception]]
expected_length: int
fail_fast: bool = True

@staticmethod
def from_iter(
iter_supplier: Callable[[], Iterable[I | Exception]],
expected_length: int,
fail_fast: bool = True,
) -> Iterator[I]:
return Iterator(iter_supplier, expected_length, fail_fast=fail_fast)

@staticmethod
def from_list(
l: list[L], map_fn: Callable[[L, int], I], fail_fast: bool = True
) -> Iterator[I]:
"""
Creates a new iterator from a list that is mapped using the given
function. The iterable will be equivalent to `map(map_fn, l)`.
"""

def supplier():
for i, x in enumerate(l):
try:
yield map_fn(x, i)
except Exception as e:
yield e

return Iterator(supplier, len(l), fail_fast=fail_fast)

@staticmethod
def from_range(
count: int, map_fn: Callable[[int], I], fail_fast: bool = True
) -> Iterator[I]:
"""
Creates a new iterator the given number of items where each item is
lazily evaluated. The iterable will be equivalent to `map(map_fn, range(count))`.
"""
assert count >= 0

def supplier():
for i in range(count):
try:
yield map_fn(i)
except Exception as e:
yield e

return Iterator(supplier, count, fail_fast=fail_fast)


N = TypeVar("N")
R = TypeVar("R")


@dataclass
class Collector(Generic[N, R]):
on_iterate: Callable[[N], None]
on_complete: Callable[[], R]
7 changes: 4 additions & 3 deletions MangaJaNaiConverterGui/backend/src/api/input.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from collections.abc import Mapping
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Any, Generic, Literal, Mapping, Optional, TypedDict, TypeVar, Union
from typing import Any, Generic, Literal, Optional, TypedDict, TypeVar, Union

import navi

Expand Down Expand Up @@ -91,7 +92,7 @@ def __init__(
kind: InputKind = "generic",
has_handle: bool = True,
associated_type: Any = None,
):
) -> None:
self.input_type: navi.ExpressionJson = input_type
self.input_conversions: list[InputConversion] = []
self.input_adapt: navi.ExpressionJson | None = None
Expand Down Expand Up @@ -135,7 +136,7 @@ def get_error_value(self, value: object) -> ErrorValue:
# bools need to be 0 or 1
return {"type": "literal", "value": int(value)}

if isinstance(value, (int, float, str)) or value is None:
if isinstance(value, int | float | str) or value is None:
return {"type": "literal", "value": value}

if isinstance(value, Path):
Expand Down
73 changes: 73 additions & 0 deletions MangaJaNaiConverterGui/backend/src/api/iter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from __future__ import annotations

from collections.abc import Callable, Iterable
from dataclasses import dataclass
from typing import Generic, TypeVar

I = TypeVar("I")
L = TypeVar("L")


@dataclass
class Generator(Generic[I]):
supplier: Callable[[], Iterable[I | Exception]]
expected_length: int
fail_fast: bool = True
metadata: object | None = None

def with_fail_fast(self, fail_fast: bool):
self.fail_fast = fail_fast
return self

def with_metadata(self, metadata: object):
self.metadata = metadata
return self

@staticmethod
def from_iter(
supplier: Callable[[], Iterable[I | Exception]], expected_length: int
) -> Generator[I]:
return Generator(supplier, expected_length)

@staticmethod
def from_list(l: list[L], map_fn: Callable[[L, int], I]) -> Generator[I]:
"""
Creates a new generator from a list that is mapped using the given
function. The iterable will be equivalent to `map(map_fn, l)`.
"""

def supplier():
for i, x in enumerate(l):
try:
yield map_fn(x, i)
except Exception as e:
yield e

return Generator(supplier, len(l))

@staticmethod
def from_range(count: int, map_fn: Callable[[int], I]) -> Generator[I]:
"""
Creates a new generator the given number of items where each item is
lazily evaluated. The iterable will be equivalent to `map(map_fn, range(count))`.
"""
assert count >= 0

def supplier():
for i in range(count):
try:
yield map_fn(i)
except Exception as e:
yield e

return Generator(supplier, count)


N = TypeVar("N")
R = TypeVar("R")


@dataclass
class Collector(Generic[N, R]):
on_iterate: Callable[[N], None]
on_complete: Callable[[], R]
7 changes: 4 additions & 3 deletions MangaJaNaiConverterGui/backend/src/api/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import time
from asyncio import AbstractEventLoop
from typing import Any, Callable, Coroutine, Generic, TypeVar
from collections.abc import Callable, Coroutine
from typing import Any, Generic, TypeVar

T = TypeVar("T")


class _Result(Generic[T]):
"""Either an okay value of T or an error value."""

def __init__(self, value: T | None, error: Exception | None):
def __init__(self, value: T | None, error: Exception | None) -> None:
self.value = value
self.error = error

Expand Down Expand Up @@ -45,7 +46,7 @@ def wrapper() -> _Result[T]:


class Lazy(Generic[T]):
def __init__(self, factory: Callable[[], T]):
def __init__(self, factory: Callable[[], T]) -> None:
self._factory = _to_result(factory)
self._value: _Result[T] | None = None
self._evaluating = False
Expand Down
Loading

0 comments on commit 43ebf35

Please sign in to comment.