-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
* Ollama provider * - namespace fix - stable difussion with automatic1111
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using LangChain.Abstractions.Schema; | ||
using LangChain.Chains.HelperChains; | ||
|
||
namespace LangChain.Chains.StackableChains.Files; | ||
|
||
public class SaveIntoFileChain:BaseStackableChain | ||
{ | ||
private readonly string _filename; | ||
|
||
public SaveIntoFileChain(string filename, string inputKey="data") | ||
Check warning on line 10 in src/libs/LangChain.Core/Chains/StackableChains/Files/SaveIntoFileChain.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
{ | ||
_filename = filename; | ||
InputKeys = new[] { inputKey }; | ||
OutputKeys = Array.Empty<string>(); | ||
} | ||
|
||
protected override Task<IChainValues> InternalCall(IChainValues values) | ||
Check warning on line 17 in src/libs/LangChain.Core/Chains/StackableChains/Files/SaveIntoFileChain.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
{ | ||
if (values.Value[InputKeys[0]] is byte[] data) | ||
{ | ||
File.WriteAllBytes(_filename, data); | ||
} | ||
else if (values.Value[InputKeys[0]] is string text) | ||
{ | ||
File.WriteAllText(_filename, text); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException($"Input key {InputKeys[0]} must be byte[] or string"); | ||
} | ||
|
||
return Task.FromResult(values); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using LangChain.Abstractions.Schema; | ||
using LangChain.Chains.HelperChains; | ||
using LangChain.Providers; | ||
|
||
namespace LangChain.Chains.StackableChains.ImageGeneration; | ||
|
||
public class ImageGenerationChain : BaseStackableChain | ||
{ | ||
private readonly IGenerateImageModel _model; | ||
|
||
public ImageGenerationChain(IGenerateImageModel model, string inputKey = "prompt", string outputKey = "image") | ||
Check warning on line 11 in src/libs/LangChain.Core/Chains/StackableChains/ImageGeneration/ImageGenerationChain.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
{ | ||
_model = model; | ||
InputKeys = new[] { inputKey }; | ||
OutputKeys = new[] { outputKey }; | ||
|
||
} | ||
|
||
protected override async Task<IChainValues> InternalCall(IChainValues values) | ||
Check warning on line 19 in src/libs/LangChain.Core/Chains/StackableChains/ImageGeneration/ImageGenerationChain.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
{ | ||
var prompt = values.Value[InputKeys[0]].ToString(); | ||
Check warning on line 21 in src/libs/LangChain.Core/Chains/StackableChains/ImageGeneration/ImageGenerationChain.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
var image = await _model.GenerateImageAsBytesAsync(prompt); | ||
Check warning on line 22 in src/libs/LangChain.Core/Chains/StackableChains/ImageGeneration/ImageGenerationChain.cs GitHub Actions / Build, test and publish / Build, test and publish
|
||
values.Value[OutputKeys[0]] = image; | ||
return values; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using LangChain.Providers; | ||
|
||
namespace StableDiffusion; | ||
|
||
public class Automatic1111Model: IGenerateImageModel | ||
{ | ||
public Automatic1111ModelOptions Options { get; } | ||
private readonly StableDiffusionClient _client; | ||
|
||
public Automatic1111Model(string url= "http://localhost:7860/", Automatic1111ModelOptions options=null ) | ||
{ | ||
Options = options?? new Automatic1111ModelOptions(); | ||
HttpClient httpClient = new HttpClient(); | ||
_client = new StableDiffusionClient(url, httpClient); | ||
} | ||
|
||
|
||
public async Task<Uri> GenerateImageAsUrlAsync(string prompt, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public async Task<Stream> GenerateImageAsStreamAsync(string prompt, CancellationToken cancellationToken = default) | ||
{ | ||
var bytes = await GenerateImageAsBytesAsync(prompt, cancellationToken); | ||
return new MemoryStream(bytes); | ||
} | ||
|
||
public async Task<byte[]> GenerateImageAsBytesAsync(string prompt, CancellationToken cancellationToken = default) | ||
{ | ||
var response = await _client.Text2imgapi_sdapi_v1_txt2img_postAsync(new StableDiffusionProcessingTxt2Img() | ||
{ | ||
Prompt = prompt, | ||
Negative_prompt = Options.NegativePrompt, | ||
Height = Options.Height, | ||
Width = Options.Width, | ||
Steps = Options.Steps, | ||
Seed = Options.Seed, | ||
Cfg_scale = Options.CFGScale | ||
|
||
}); | ||
|
||
var encoded = response.Images.First(); | ||
// base64 to png | ||
|
||
var bytes = Convert.FromBase64String(encoded); | ||
return bytes; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace StableDiffusion; | ||
|
||
public class Automatic1111ModelOptions | ||
{ | ||
public string NegativePrompt { get; set; }=""; | ||
public int Seed { get; set; } = -1; | ||
public int Steps { get; set; } = 20; | ||
public double CFGScale { get; set; } = 7; | ||
public int Width { get; set; }= 512; | ||
public int Height { get; set; }= 512; | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks> | ||
<SignAssembly>false</SignAssembly> | ||
<NoWarn>$(NoWarn);CA1003;CA1307</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="LLamaSharp" /> | ||
<PackageReference Include="Newtonsoft.Json" /> | ||
<PackageReference Include="System.Net.Http" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Label="Usings"> | ||
<Using Include="System.Net.Http" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Label="NuGet"> | ||
<Description>Automatic1111 Stable DIffusion model provider.</Description> | ||
<PackageTags>$(PackageTags);Auto1111;Automatic1111;Stable Diffusion;Stable Diffusion;api</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\LangChain.Core\LangChain.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |