Browse Source

Add Parse from requirement to PipInstallArgs

pull/333/head
Ionite 11 months ago
parent
commit
2cd9c8a633
No known key found for this signature in database
  1. 7
      StabilityMatrix.Core/Processes/ProcessArgsBuilder.cs
  2. 33
      StabilityMatrix.Core/Python/PipInstallArgs.cs

7
StabilityMatrix.Core/Processes/ProcessArgsBuilder.cs

@ -54,6 +54,13 @@ public static class ProcessArgBuilderExtensions
return builder with { Arguments = builder.Arguments.Add(argument) };
}
[Pure]
public static T AddArgs<T>(this T builder, params Argument[] argument)
where T : ProcessArgsBuilder
{
return builder with { Arguments = builder.Arguments.AddRange(argument) };
}
[Pure]
public static T UpdateArg<T>(this T builder, string key, Argument argument)
where T : ProcessArgsBuilder

33
StabilityMatrix.Core/Python/PipInstallArgs.cs

@ -1,4 +1,7 @@
using StabilityMatrix.Core.Processes;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Processes;
namespace StabilityMatrix.Core.Python;
@ -9,20 +12,36 @@ public record PipInstallArgs : ProcessArgsBuilder
public PipInstallArgs WithTorch(string version = "") => this.AddArg($"torch{version}");
public PipInstallArgs WithTorchDirectML(string version = "") =>
this.AddArg($"torch-directml{version}");
public PipInstallArgs WithTorchDirectML(string version = "") => this.AddArg($"torch-directml{version}");
public PipInstallArgs WithTorchVision(string version = "") =>
this.AddArg($"torchvision{version}");
public PipInstallArgs WithTorchVision(string version = "") => this.AddArg($"torchvision{version}");
public PipInstallArgs WithXFormers(string version = "") => this.AddArg($"xformers{version}");
public PipInstallArgs WithExtraIndex(string indexUrl) =>
this.AddArg(("--extra-index-url", indexUrl));
public PipInstallArgs WithExtraIndex(string indexUrl) => this.AddArg(("--extra-index-url", indexUrl));
public PipInstallArgs WithTorchExtraIndex(string index) =>
this.AddArg(("--extra-index-url", $"https://download.pytorch.org/whl/{index}"));
public PipInstallArgs WithParsedFromRequirementsTxt(
string requirements,
[StringSyntax(StringSyntaxAttribute.Regex)] string? excludePattern = null
)
{
var requirementsEntries = requirements
.SplitLines(StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.AsEnumerable();
if (excludePattern is not null)
{
var excludeRegex = new Regex($"^{excludePattern}$");
requirementsEntries = requirementsEntries.Where(s => !excludeRegex.IsMatch(s));
}
return this.AddArgs(requirementsEntries.Select(s => (Argument)s).ToArray());
}
/// <inheritdoc />
public override string ToString()
{

Loading…
Cancel
Save