Skip to content

Commit

Permalink
trt working with r65
Browse files Browse the repository at this point in the history
  • Loading branch information
the-database committed Jul 24, 2024
1 parent 7469d36 commit 15d99a6
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 24 deletions.
13 changes: 7 additions & 6 deletions VideoJaNai/Services/PythonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class PythonService : IPythonService
"win32",
new PythonDownload
{
//Url = "https://github.com/indygreg/python-build-standalone/releases/download/20240713/cpython-3.12.4+20240713-x86_64-pc-windows-msvc-shared-install_only.tar.gz",
Url = "https://github.com/indygreg/python-build-standalone/releases/download/20240415/cpython-3.11.9+20240415-x86_64-pc-windows-msvc-shared-install_only.tar.gz",
Url = "https://github.com/indygreg/python-build-standalone/releases/download/20240713/cpython-3.12.4+20240713-x86_64-pc-windows-msvc-shared-install_only.tar.gz",
//Url = "https://github.com/indygreg/python-build-standalone/releases/download/20240415/cpython-3.11.9+20240415-x86_64-pc-windows-msvc-shared-install_only.tar.gz",
Path = "python.exe",
Version = "3.12.4",
//Version = "3.11.9",
//Version = "3.12.4",
Version = "3.11.9",
Filename = "Python.tar.gz"
}
},
Expand Down Expand Up @@ -143,8 +143,9 @@ public string InstallUpdatePythonDependenciesCommand
"packaging"
};

return $@"{PythonPath} -m pip install {PythonDirectory}\wheel\VapourSynth-69-cp312-cp312-win_amd64.whl && {PythonPath} -m pip install {string.Join(" ", dependencies)}";
//return $@"{PythonPath} -m pip install {PythonDirectory}\wheel\VapourSynth-65-cp311-cp311-win_amd64.whl && {PythonPath} -m pip install {string.Join(" ", dependencies)}";
//return $@"{PythonPath} -m pip install {PythonDirectory}\wheel\VapourSynth-69-cp312-cp312-win_amd64.whl && {PythonPath} -m pip install {string.Join(" ", dependencies)}";
return $@"{PythonPath} -m pip install {PythonDirectory}\wheel\VapourSynth-65-cp311-cp311-win_amd64.whl && {PythonPath} -m pip install {string.Join(" ", dependencies)}";
//return $@"{PythonPath} -m pip install {string.Join(" ", dependencies)}";
}
}

Expand Down
96 changes: 85 additions & 11 deletions VideoJaNai/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,16 +663,21 @@ public void CheckAndExtractBackend()
// TODO test full reinstall, to test pip install packaging
if (!_pythonService.IsPythonInstalled())
{
// 1. Install embedded Python + portable VS
//await InstallPortableVapourSynth();
await InstallPython();
await InstallVapourSynth();

// 3. Install VapourSynth Python wheel
// 2. Python dependencies
await RunInstallCommand(_pythonService.InstallUpdatePythonDependenciesCommand);
// 4. VapourSynth ffms2

// 3. VapourSynth plugins
await RunInstallCommand(_pythonService.InstallVapourSynthPluginsCommand);

// 4. vs-mlrt
await InstallVsmlrt();

await InstallVsmlrt(); // TODO
// 5. ffmpeg
await InstallFfmpeg();
}
else
Expand Down Expand Up @@ -748,22 +753,91 @@ await Downloader.DownloadFileAsync(downloadUrl, targetPath, (progress) =>
File.Delete(targetPath);
}

private async Task InstallPortableVapourSynth()
{
// Download Python Installer
var vsVersion = 65;
BackendSetupMainStatus = "Downloading Portable VapourSynth Installer...";
var downloadUrl = $"https://github.com/vapoursynth/vapoursynth/releases/download/R69/Install-Portable-VapourSynth-R69.ps1";
var targetPath = Path.Join(_pythonService.BackendDirectory, "installvs.ps1");
await Downloader.DownloadFileAsync(downloadUrl, targetPath, (progress) =>
{
BackendSetupMainStatus = $"Downloading Portable VapourSynth Installer ({progress}%)...";
});

// Install Python
BackendSetupMainStatus = "Installing Embedded Python with Portable VapourSynth...";

using (PowerShell powerShell = PowerShell.Create())
{
powerShell.AddScript("Set-ExecutionPolicy RemoteSigned -Scope Process -Force");
powerShell.AddScript("Import-Module Microsoft.PowerShell.Archive");

var scriptContents = File.ReadAllText(targetPath);
// Force Python 3.11
scriptContents = scriptContents.Replace("$PythonVersionMid = 12", "$PythonVersionMid = 11");

powerShell.AddScript(scriptContents);
powerShell.AddParameter("Unattended");
powerShell.AddParameter("TargetFolder", _pythonService.PythonDirectory);
powerShell.AddParameter("VSVersion", vsVersion);

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

PSDataCollection<PSObject> outputCollection = [];
outputCollection.DataAdded += (sender, e) =>
{
Debug.WriteLine("DataAdded");
Debug.WriteLine(e.Index);
Debug.WriteLine(outputCollection[e.Index]);
BackendSetupSubStatusQueueEnqueue(outputCollection[e.Index].ToString());
};

try
{
IAsyncResult asyncResult = powerShell.BeginInvoke<PSObject, PSObject>(null, outputCollection);
powerShell.EndInvoke(asyncResult);
}
catch (Exception ex)
{
Debug.WriteLine($"An error occurred: {ex.Message}");
}

if (powerShell.Streams.Error.Count > 0)
{
foreach (var error in powerShell.Streams.Error)
{
Debug.WriteLine($"Error: {error}");
}
}
}

File.Delete(targetPath);
}

private async Task InstallVapourSynth()
{
BackendSetupMainStatus = "Downloading VapourSynth...";
var downloadUrl = "https://github.com/vapoursynth/vapoursynth/releases/download/R69/VapourSynth64-Portable-R69.zip";
//var downloadUrl = "https://github.com/vapoursynth/vapoursynth/releases/download/R65/VapourSynth64-Portable-R65.zip";
var targetPath = Path.Join(_pythonService.BackendDirectory, "vapoursynth.zip");
//var downloadUrl = "https://github.com/vapoursynth/vapoursynth/releases/download/R69/VapourSynth64-Portable-R69.zip";
var downloadUrl = "https://github.com/vapoursynth/vapoursynth/releases/download/R65/VapourSynth64-Portable-R65.7z";
var targetPath = Path.Join(_pythonService.BackendDirectory, "vapoursynth.7z");
await Downloader.DownloadFileAsync(downloadUrl, targetPath, (progress) =>
{
BackendSetupMainStatus = $"Downloading VapourSynth ({progress}%)...";
});

BackendSetupMainStatus = "Extracting VapourSynth...";
_pythonService.ExtractZip(targetPath, _pythonService.PythonDirectory, (double progress) =>
//_pythonService.ExtractZip(targetPath, _pythonService.PythonDirectory, (double progress) =>
//{
// BackendSetupMainStatus = $"Extracting VapourSynth ({progress}%)...";
//});
using (ArchiveFile archiveFile = new(targetPath))
{
BackendSetupMainStatus = $"Extracting VapourSynth ({progress}%)...";
});
archiveFile.Extract(_pythonService.PythonDirectory);
}
File.Delete(targetPath);
}

Expand All @@ -780,8 +854,8 @@ await Downloader.DownloadFileAsync(downloadUrl, targetPath, (progress) =>
BackendSetupMainStatus = "Extracting vs-mlrt (this may take several minutes)...";
using (ArchiveFile archiveFile = new(targetPath))
{
var targetDirectory = Path.Join(_pythonService.PythonDirectory, "vs-plugins");
//var targetDirectory = Path.Join(_pythonService.PythonDirectory, "vapoursynth64", "plugins");
//var targetDirectory = Path.Join(_pythonService.PythonDirectory, "vs-plugins");
var targetDirectory = Path.Join(_pythonService.PythonDirectory, "vapoursynth64", "plugins");
Directory.CreateDirectory(targetDirectory);
archiveFile.Extract(targetDirectory);
}
Expand Down
11 changes: 6 additions & 5 deletions VideoJaNai/backend/animejanai/core/animejanai_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
core = vs.core
core.num_threads = 4 # can influence ram usage

plugin_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
r"..\..\python\vs-plugins\vsmlrt-cuda")
# plugin_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
# r"..\..\python\vapoursynth64\plugins\vsmlrt-cuda")
# r"..\..\python\vs-plugins\vsmlrt-cuda")
plugin_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
r"..\..\python\vapoursynth64\plugins\vsmlrt-cuda")


model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
Expand All @@ -39,7 +39,7 @@ def init_logger():
rfh.setLevel(logging.DEBUG)
logger.handlers.clear()
logger.addHandler(rfh)
# logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.StreamHandler())


def write_current_log_empty():
Expand Down Expand Up @@ -110,7 +110,7 @@ def create_dynamic_engine(onnx_name, width, height):

# SwinIR test
commands = [os.path.join(plugin_path, "trtexec"), "--fp16", f"--onnx={onnx_path}",
#"--minShapes=input:1x3x8x8", "--optShapes=input:1x3x1080x1920", "--maxShapes=input:1x3x1080x1920",
"--minShapes=input:1x3x8x8", "--optShapes=input:1x3x1080x1920", "--maxShapes=input:1x3x1080x1920",
"--skipInference", "--infStreams=4", "--builderOptimizationLevel=4",
f"--saveEngine={engine_path}", "--tacticSources=-CUDNN,-CUBLAS,-CUBLAS_LT"]

Expand Down Expand Up @@ -244,6 +244,7 @@ def run_animejanai(clip, container_fps, chain_conf, backend):
clip = run_animejanai_upscale(clip, backend, model_conf, num_streams)

except Exception as e:
logger.debug("hello?",e)
clip = vs.core.resize.Spline36(clip, format=vs.RGBS, matrix_in_s=colorspace,
width=clip.width * resize_factor_before_upscale / 100,
height=clip.height * resize_factor_before_upscale / 100)
Expand Down
4 changes: 4 additions & 0 deletions VideoJaNai/backend/animejanai/core/animejanai_encode.vpy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ for arg in required_args:
raise ValueError(f"{arg} must be defined in globals")
sys.exit(1)

# vs.core.std.LoadPlugin(os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir, os.path.pardir, r"python\vapoursynth64\plugins\vstrt.dll")))
# vs.core.std.LoadPlugin(os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir, os.path.pardir, r"python\vapoursynth64\plugins\vsort.dll")))
# vs.core.std.LoadPlugin(os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir, os.path.pardir, r"python\vapoursynth64\plugins\vsncnn.dll")))
# raise ValueError(vs.core.trt)
clip = vs.core.ffms2.Source(source=args["video_path"])
slot = int(args["slot"])
animejanai_core.run_animejanai_with_keybinding(clip, clip.fps, slot)
4 changes: 2 additions & 2 deletions VideoJaNai/backend/animejanai/core/rife_cuda.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://github.com/hooke007/MPV_lazy/blob/main/k7sfunc.py

from packaging.version import parse
from distutils.version import LooseVersion
import fractions
import math
import os
Expand Down Expand Up @@ -103,7 +103,7 @@ def rife(
import vsmlrt
except ImportError:
raise ImportError(f"模块 {func_name} 依赖错误:缺失脚本 vsmlrt")
if parse(vsmlrt.__version__) < parse("3.18.22"):
if LooseVersion(vsmlrt.__version__) < LooseVersion("3.18.22"):
raise ImportError(f"模块 {func_name} 依赖错误:缺失脚本 vsmlrt 的版本号过低,至少 3.18.22")

core.num_threads = vs_t
Expand Down

0 comments on commit 15d99a6

Please sign in to comment.