Skip to content

Commit d5fbc07

Browse files
authored
Merge pull request LykosAI#1205 from ionite34/backport/main/pr-1204
[dev to main] backport: update wan2gp logging by adding a wrapper script to capture output in console… (1204)
2 parents e5787d8 + 2d4a4a4 commit d5fbc07

5 files changed

Lines changed: 97 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2
2222
- Updated Forge Classic installs/updates to use the upstream install script for better version compatibility with torch/sage/triton/nunchaku
2323
- Backslashes can now be escaped in Inference prompts via `\\`
2424
### Fixed
25-
- Fixed [#1541](https://github.com/LykosAI/StabilityMatrix/issues/1541), [#1518](https://github.com/LykosAI/StabilityMatrix/issues/1518), [#1513](https://github.com/LykosAI/StabilityMatrix/issues/1513), [#1488](https://github.com/LykosAI/StabilityMatrix/issues/1488) - Forge Neo update breaking things
25+
- Fixed parsing of escape sequences in Inference such as `\\`
26+
- Fixed batch notification firing when only one image is generated
27+
- Fixed [#1546](https://github.com/LykosAI/StabilityMatrix/issues/1546), [#1541](https://github.com/LykosAI/StabilityMatrix/issues/1541) - "No module named 'pkg_resources'" error when installing Automatic1111/Forge/reForge packages
28+
- Fixed [#1545](https://github.com/LykosAI/StabilityMatrix/issues/1545), [#1518](https://github.com/LykosAI/StabilityMatrix/issues/1518), [#1513](https://github.com/LykosAI/StabilityMatrix/issues/1513), [#1488](https://github.com/LykosAI/StabilityMatrix/issues/1488) - Forge Neo update breaking things
2629
- Fixed [#1529](https://github.com/LykosAI/StabilityMatrix/issues/1529) - "Selected commit is null" error when installing packages and rate limited by GitHub
2730
- Fixed [#1525](https://github.com/LykosAI/StabilityMatrix/issues/1525) - Crash after downloading a model
2831
- Fixed [#1523](https://github.com/LykosAI/StabilityMatrix/issues/1523), [#1499](https://github.com/LykosAI/StabilityMatrix/issues/1499), [#1494](https://github.com/LykosAI/StabilityMatrix/issues/1494) - Automatic1111 using old stable diffusion repo

StabilityMatrix.Core/Models/Packages/A3WebUI.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ torchIndex is TorchIndex.Cuda
226226
// 1. Configure the entire install process declaratively.
227227
var config = new PipInstallConfig
228228
{
229-
RequirementsFilePaths = ["requirements_versions.txt"],
229+
RequirementsFilePaths = ["requirements_versions.txt", "setuptools<82"],
230230
TorchVersion = torchIndex == TorchIndex.Mps ? "==2.3.1" : (isBlackwell ? "" : "==2.1.2"),
231231
TorchvisionVersion = torchIndex == TorchIndex.Mps ? "==0.18.1" : (isBlackwell ? "" : "==0.16.2"),
232232
XformersVersion = isBlackwell ? " " : "==0.0.23.post1",
@@ -235,6 +235,7 @@ torchIndex is TorchIndex.Cuda
235235
ExtraPipArgs =
236236
[
237237
"https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip",
238+
"setuptools<82",
238239
],
239240
};
240241

@@ -306,7 +307,7 @@ void HandleConsoleOutput(ProcessOutput s)
306307
public override IReadOnlyList<string> ExtraLaunchArguments =>
307308
settingsManager.IsLibraryDirSet ? ["--gradio-allowed-path", settingsManager.ImagesDirectory] : [];
308309

309-
private ImmutableDictionary<string, string> GetEnvVars(ImmutableDictionary<string, string> env)
310+
protected virtual ImmutableDictionary<string, string> GetEnvVars(ImmutableDictionary<string, string> env)
310311
{
311312
// Set the Stable Diffusion repository URL to a working fork
312313
// This is required because the original Stability-AI/stablediffusion repo was removed

StabilityMatrix.Core/Models/Packages/Reforge.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Injectio.Attributes;
1+
using System.Collections.Immutable;
2+
using Injectio.Attributes;
23
using StabilityMatrix.Core.Helper;
34
using StabilityMatrix.Core.Helper.Cache;
45
using StabilityMatrix.Core.Python;
@@ -36,4 +37,8 @@ IPipWheelService pipWheelService
3637
public override PackageDifficulty InstallerSortOrder => PackageDifficulty.Recommended;
3738
public override bool OfferInOneClickInstaller => true;
3839
public override PackageType PackageType => PackageType.SdInference;
40+
41+
protected override ImmutableDictionary<string, string> GetEnvVars(
42+
ImmutableDictionary<string, string> env
43+
) => env;
3944
}

StabilityMatrix.Core/Models/Packages/SDWebForge.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ torchIndex is TorchIndex.Cuda
189189

190190
var config = new PipInstallConfig
191191
{
192-
PrePipInstallArgs = ["joblib"],
192+
PrePipInstallArgs = ["joblib", "setuptools<82"],
193193
RequirementsFilePaths = requirementsPaths,
194194
TorchVersion = "",
195195
TorchvisionVersion = "",
@@ -199,7 +199,7 @@ torchIndex is TorchIndex.Cuda
199199
[
200200
"https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip",
201201
],
202-
PostInstallPipArgs = ["numpy==1.26.4"],
202+
PostInstallPipArgs = ["numpy==1.26.4", "setuptools<82"],
203203
};
204204

205205
await StandardPipInstallProcessAsync(

StabilityMatrix.Core/Models/Packages/Wan2GP.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,78 @@ IPipWheelService pipWheelService
8686
/// </summary>
8787
private bool IsAmdRocm => GetRecommendedTorchVersion() == TorchIndex.Rocm;
8888

89+
/// <summary>
90+
/// Python wrapper script that patches logging to also print to stdout/stderr, so
91+
/// StabilityMatrix can capture the output. Wan2GP logs through Gradio UI notifications
92+
/// (gr.Info/Warning/Error) and callback-driven UI updates that never reach the console.
93+
/// This script:
94+
/// 1. Configures Python's logging module to output to stderr (captures library logging)
95+
/// 2. Prevents transformers from suppressing its own logging (wgp.py calls set_verbosity_error)
96+
/// 3. Monkey-patches gr.Info/Warning/Error to also print to stdout/stderr
97+
/// 4. Runs the target script (wgp.py) via runpy
98+
/// </summary>
99+
private const string GradioLogPatchScript = """
100+
# StabilityMatrix: Patch logging to print to console for capture.
101+
import sys
102+
import logging
103+
104+
def _apply_logging_patch():
105+
# Configure Python's root logger to output to stderr at INFO level.
106+
# Many libraries (torch, diffusers, transformers, etc.) use the logging
107+
# module but output may be suppressed without a handler configured.
108+
root = logging.getLogger()
109+
if not any(isinstance(h, logging.StreamHandler) for h in root.handlers):
110+
handler = logging.StreamHandler(sys.stderr)
111+
handler.setFormatter(logging.Formatter("[%(name)s] %(levelname)s: %(message)s"))
112+
root.addHandler(handler)
113+
if root.level > logging.INFO:
114+
root.setLevel(logging.INFO)
115+
116+
# Prevent transformers from suppressing its own logging.
117+
# wgp.py calls transformers.utils.logging.set_verbosity_error() which
118+
# silences all non-error messages. We neutralize those calls so model
119+
# loading and download messages remain visible.
120+
try:
121+
import transformers.utils.logging as tf_logging
122+
tf_logging.set_verbosity_error = lambda: None
123+
tf_logging.set_verbosity_warning = lambda: None
124+
tf_logging.set_verbosity(logging.INFO)
125+
except Exception as e:
126+
print(f"[StabilityMatrix] Failed to patch transformers logging: {e}", file=sys.stderr, flush=True)
127+
128+
# Monkey-patch Gradio's UI notification functions to also print to console.
129+
# These only fire for validation/error messages, not generation progress.
130+
try:
131+
import gradio as gr
132+
_orig_info = getattr(gr, 'Info', None)
133+
_orig_warning = getattr(gr, 'Warning', None)
134+
_orig_error = getattr(gr, 'Error', None)
135+
if _orig_info is not None:
136+
def patched_info(message, *args, **kwargs):
137+
print(f"[Gradio] {message}", flush=True)
138+
return _orig_info(message, *args, **kwargs)
139+
gr.Info = patched_info
140+
if _orig_warning is not None:
141+
def patched_warning(message, *args, **kwargs):
142+
print(f"[Gradio] WARNING: {message}", flush=True)
143+
return _orig_warning(message, *args, **kwargs)
144+
gr.Warning = patched_warning
145+
if _orig_error is not None:
146+
def patched_error(message, *args, **kwargs):
147+
print(f"[Gradio] ERROR: {message}", file=sys.stderr, flush=True)
148+
return _orig_error(message, *args, **kwargs)
149+
gr.Error = patched_error
150+
except Exception as e:
151+
print(f"[StabilityMatrix] Failed to patch Gradio logging: {e}", file=sys.stderr, flush=True)
152+
153+
if __name__ == "__main__":
154+
_apply_logging_patch()
155+
target_script = sys.argv[1]
156+
sys.argv = sys.argv[1:]
157+
import runpy
158+
runpy.run_path(target_script, run_name="__main__")
159+
""";
160+
89161
public override List<LaunchOptionDefinition> LaunchOptions =>
90162
[
91163
new()
@@ -368,13 +440,22 @@ await SetupVenv(installLocation, pythonVersion: PyVersion.Parse(installedPackage
368440
// Fix for distutils compatibility issue with Python 3.10 and setuptools
369441
VenvRunner.UpdateEnvironmentVariables(env => env.SetItem("SETUPTOOLS_USE_DISTUTILS", "stdlib"));
370442

443+
// Write the Gradio logging patch wrapper script so gr.Info/Warning/Error
444+
// messages are also printed to stdout/stderr for console capture
445+
var patchScriptPath = Path.Combine(installLocation, "_sm_gradio_log_patch.py");
446+
await File.WriteAllTextAsync(patchScriptPath, GradioLogPatchScript, cancellationToken)
447+
.ConfigureAwait(false);
448+
449+
var targetScript = Path.Combine(installLocation, options.Command ?? LaunchCommand);
450+
371451
// Notify user that the package is starting (loading can take a while)
372452
onConsoleOutput?.Invoke(
373453
new ProcessOutput { Text = "Launching Wan2GP, please wait while the UI initializes...\n" }
374454
);
375455

456+
// Launch via the patch wrapper, which monkey-patches Gradio then runs wgp.py
376457
VenvRunner.RunDetached(
377-
[Path.Combine(installLocation, options.Command ?? LaunchCommand), .. options.Arguments],
458+
[patchScriptPath, targetScript, .. options.Arguments],
378459
HandleConsoleOutput,
379460
OnExit
380461
);

0 commit comments

Comments
 (0)