Skip to content

Commit

Permalink
Merge pull request #29 from the-database/dev
Browse files Browse the repository at this point in the history
test fix for batch upscale
  • Loading branch information
the-database authored Jul 27, 2024
2 parents aff2cb6 + b3166aa commit df1e601
Show file tree
Hide file tree
Showing 128 changed files with 2,071 additions and 22,904 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
76 changes: 75 additions & 1 deletion MangaJaNaiConverterGui/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ public string[] DeviceList
}
}

private string _pythonPipList = string.Empty;
public string PythonPipList
{
get => _pythonPipList;
set => this.RaiseAndSetIfChanged(ref _pythonPipList, value);
}

private AvaloniaDictionary<string, ReaderDevice> _displayDeviceMap = [];
[DataMember]
public AvaloniaDictionary<string, ReaderDevice> DisplayDeviceMap
Expand Down Expand Up @@ -830,6 +837,56 @@ public async Task<string[]> InitializeDeviceList()
return [];
}

public async Task<string> RunPythonPipList()
{
List<string> result = [];

// Create a new process to run the CMD command
using (var process = new Process())
{
_runningProcess = process;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = @$"/C .\python\python\python.exe -m pip list";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = _pythonService.BackendDirectory;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;

// Create a StreamWriter to write the output to a log file
try
{
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
//outputFile.WriteLine(e.Data); // Write the output to the log file
//ConsoleQueueEnqueue(e.Data);
Debug.WriteLine(e.Data);
}
};

process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
result.Add(e.Data);
}
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine(); // Start asynchronous reading of the output
await process.WaitForExitAsync();
}
catch (IOException) { }
}

return string.Join("\n", result);
}

public async void ShowSettingsDialog()
{
var result = await ShowDialog.Handle(this);
Expand Down Expand Up @@ -902,7 +959,7 @@ public void WriteCurrentWorkflowToFile(string fullPath)
File.WriteAllText(fullPath, lines);
}

public async void CheckAndExtractBackend()
public async Task CheckAndExtractBackend()
{
await Task.Run(async () =>
{
Expand Down Expand Up @@ -956,6 +1013,23 @@ await Downloader.DownloadFileAsync(download.Url, targetPath, (progress) =>
{
UseCpu = true;
}

PythonPipList = await RunPythonPipList();
}

public async Task ReinstallBackend()
{
if (Directory.Exists(_pythonService.ModelsDirectory))
{
Directory.Delete(_pythonService.ModelsDirectory, true);
}

if (Directory.Exists(_pythonService.PythonDirectory))
{
Directory.Delete(_pythonService.PythonDirectory, true);
}

await CheckAndExtractBackend();
}

public async Task DownloadModels()
Expand Down
377 changes: 195 additions & 182 deletions MangaJaNaiConverterGui/Views/MainWindow.axaml

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions MangaJaNaiConverterGui/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private async void MainWindow_Closing(object? sender, WindowClosingEventArgs e)
// Cancel close to show dialog
e.Cancel = true;

_userWantsToQuit = await ShowConfirmationDialog("If you exit now, all unfinished upscales will be canceled. Are you sure you want to exit?");
_userWantsToQuit = await ShowConfirmationDialog("Cancel unfinished upscales?", "If you exit now, all unfinished upscales will be canceled. Are you sure you want to exit?");

// Close if the user confirmed
if (_userWantsToQuit)
Expand Down Expand Up @@ -408,13 +408,26 @@ private async void ExportCurrentWorkflowButtonClick(object? sender, RoutedEventA
}
}

private async Task<bool> ShowConfirmationDialog(string message)
private async void ReinstallBackendClick(object? sender, RoutedEventArgs e)
{
if (DataContext is MainWindowViewModel vm)
{
var confirm = await ShowConfirmationDialog("Reinstall Python Backend", "The existing Python backend will be removed and then reinstalled. Your workflow settings will be preserved. This process will take several minutes. Proceed?");

if (confirm)
{
await vm.ReinstallBackend();
}
}
}

private async Task<bool> ShowConfirmationDialog(string title, string message)
{
var dialog = new Window
{
Title = "Cancel unfinished upscales?",
Title = title,
Width = 480,
Height = 160,
Height = 200,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
//Icon = Icon, // TODO
CanResize = false,
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
Loading

0 comments on commit df1e601

Please sign in to comment.