Update Stable Diffusion

This commit is contained in:
IntptrMax
2024-04-11 15:11:17 +08:00
parent 9b07d88392
commit bfbed44270
10 changed files with 185 additions and 43 deletions

View File

@@ -1,23 +1,21 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel;
using System.Text;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Model.Dto;
using AntSK.Domain.Domain.Model.Constant;
using DocumentFormat.OpenXml.Drawing;
using System.Reflection.Metadata;
using Microsoft.KernelMemory;
using System.Collections.Generic;
using Markdig;
using ChatHistory = Microsoft.SemanticKernel.ChatCompletion.ChatHistory;
using Microsoft.SemanticKernel.Plugins.Core;
using Azure.Core;
using AntSK.Domain.Domain.Model;
using AntSK.Domain.Domain.Model.Constant;
using AntSK.Domain.Domain.Model.Dto;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using AntSK.LLM.StableDiffusion;
using Markdig;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using ChatHistory = Microsoft.SemanticKernel.ChatCompletion.ChatHistory;
namespace AntSK.Domain.Domain.Service
{
@@ -44,7 +42,7 @@ namespace AntSK.Domain.Domain.Service
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString() + "{{$input}}";
}
KernelArguments args =new KernelArguments();
KernelArguments args = new KernelArguments();
if (history.Count > 10)
{
app.Prompt = @"${{ConversationSummaryPlugin.SummarizeConversation $history}}" + app.Prompt;
@@ -53,14 +51,14 @@ namespace AntSK.Domain.Domain.Service
{ "input", questions }
};
}
else
else
{
args=new()
args = new()
{
{ "input", $"{string.Join("\n", history.Select(x => x.Role + ": " + x.Content))}{Environment.NewLine} user:{questions}" }
};
}
var _kernel = _kernelService.GetKernelByApp(app);
var temperature = app.Temperature / 100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() { Temperature = temperature };
@@ -70,7 +68,7 @@ namespace AntSK.Domain.Domain.Service
settings.ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions;
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync(function: func,
var chatResult = _kernel.InvokeStreamingAsync(function: func,
arguments: args);
await foreach (var content in chatResult)
{
@@ -105,14 +103,14 @@ namespace AntSK.Domain.Domain.Service
var dataMsg = new StringBuilder();
if (relevantSourceList.Any())
{
bool isSearch=false;
bool isSearch = false;
foreach (var item in relevantSourceList)
{
//匹配相似度
if (item.Relevance >= app.Relevance/100)
if (item.Relevance >= app.Relevance / 100)
{
dataMsg.AppendLine(item.ToString());
isSearch=true;
isSearch = true;
}
}
@@ -138,10 +136,10 @@ namespace AntSK.Domain.Domain.Service
yield return content;
}
}
else
else
{
yield return new StreamingTextContent(KmsConstantcs.KmsSearchNull);
}
}
}
else
{
@@ -163,6 +161,77 @@ namespace AntSK.Domain.Domain.Service
var chatResult = await _kernel.InvokeAsync(function: func, arguments: args);
if (chatResult.IsNotNull())
{
//Can Load stable-diffusion library in diffenert environment
//SDHelper.LoadLibrary()
string versionString = string.Empty;
string extensionString = string.Empty;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
extensionString = ".dll";
string cuda_path = Environment.GetEnvironmentVariable("CUDA_PATH");
Regex regex = new Regex(@"v1(\d).[\d]");
Match match = regex.Match(cuda_path);
if (match.Success)
{
switch (match.Groups[1].Value.ToString())
{
case "1":
versionString = "Cuda11";
break;
case "2":
versionString = "Cuda12";
break;
default:
versionString = "CPU";
break;
}
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
//Have to math the cuda version in linux
extensionString = ".so";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = "nvcc--version | grep 'release'",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Regex regex = new Regex(@"release (\d).[\d]");
Match match = regex.Match(result);
if (match.Success)
{
switch (match.Groups[1].Value.ToString())
{
case "1":
versionString = "Cuda11";
break;
case "2":
versionString = "Cuda12";
break;
default:
versionString = "CPU";
break;
}
}
}
}
}
else
{
throw new InvalidOperationException("OS Platform no support");
}
string libraryPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StableDiffusion", "Backend", versionString, "stable-diffusion" + extensionString);
NativeLibrary.TryLoad(libraryPath, out IntPtr s);
string prompt = chatResult.GetValue<string>();
if (!SDHelper.IsInitialized)
{

View File

@@ -35,5 +35,8 @@
<None Update="plugins\KMSPlugin\Ask\skprompt.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusionModelList.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,12 +1,9 @@
using AntDesign;
using AntSK.Domain.Domain.Model.hfmirror;
using AntSK.Domain.Utils;
using AntSK.Models;
using AntSK.Services;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
using RestSharp;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Model.hfmirror;
namespace AntSK.Pages.Setting.AIModel
{
@@ -25,18 +22,45 @@ namespace AntSK.Pages.Setting.AIModel
private void InitData(string searchKey)
{
var param = searchKey.ConvertToString().Split(" ");
string urlBase = $"https://hf-mirror.com/models-json?sort=trending&search={_modelType}";
if (param.Count() > 0)
if (string.IsNullOrEmpty(_modelType))
{
urlBase += "+" + string.Join("+", param);
return;
}
if (_modelType.Contains("safetensors"))
{
_modelList.Clear();
var param = searchKey.ConvertToString().Split(" ");
string[] lines = File.ReadAllLines("StableDiffusionModelList.txt");
foreach (string line in lines)
{
string urlBase = $"https://hf-mirror.com/models-json?sort=trending&search={line}";
if (param.Count() > 0)
{
urlBase += "+" + string.Join("+", param);
}
RestClient client = new RestClient();
RestRequest request = new RestRequest(urlBase, Method.Get);
var response = client.Execute(request);
var model = JsonConvert.DeserializeObject<HfModel>(response.Content);
_modelList.AddRange(model.models);
}
}
else
{
var param = searchKey.ConvertToString().Split(" ");
string urlBase = $"https://hf-mirror.com/models-json?sort=trending&search={_modelType}";
if (param.Count() > 0)
{
urlBase += "+" + string.Join("+", param);
}
RestClient client = new RestClient();
RestRequest request = new RestRequest(urlBase, Method.Get);
var response = client.Execute(request);
var model = JsonConvert.DeserializeObject<HfModel>(response.Content);
_modelList = model.models;
}
RestClient client = new RestClient();
RestRequest request = new RestRequest(urlBase, Method.Get);
var response = client.Execute(request);
var model = JsonConvert.DeserializeObject<HfModel>(response.Content);
_modelList = model.models;
}
private async Task Search(string searchKey)

View File

@@ -0,0 +1,6 @@
AsAHuman/chilloutmix
GraMpa7/dreamsharper
Airic/Anything-V4.5
liqira/anythingv3
wind1/MoYou
Reuploadingfromcivitai/DosMix

View File

@@ -20,6 +20,30 @@
<None Update="stable-diffusion.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\CPU\stable-diffusion.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\CPU\stable-diffusion.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\Cuda11\stable-diffusion.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\Cuda11\stable-diffusion.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\Cuda12\stable-diffusion.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\Cuda12\stable-diffusion.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\ROCm\stable-diffusion.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="StableDiffusion\Backend\ROCm\stable-diffusion.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
@@ -83,6 +82,16 @@ namespace AntSK.LLM.StableDiffusion
{
if (!IsInitialized) throw new ArgumentNullException("Model not loaded!");
IntPtr cnPtr = IntPtr.Zero;
if (textToImageParams.ControlCond != null)
{
if (textToImageParams.ControlCond.Width > 1)
{
SDImage cnImg = GetSDImageFromBitmap(textToImageParams.ControlCond);
cnPtr = GetPtrFromImage(cnImg);
}
}
SDImagePtr sd_Image_ptr = Native.txt2img(sd_ctx,
textToImageParams.Prompt,
textToImageParams.NegativePrompt,
@@ -94,7 +103,7 @@ namespace AntSK.LLM.StableDiffusion
textToImageParams.SampleSteps,
textToImageParams.Seed,
textToImageParams.BatchCount,
SDImagePtr.Zero,
cnPtr,
textToImageParams.ControlStrength,
textToImageParams.StyleStrength,
textToImageParams.NormalizeInput,
@@ -200,6 +209,13 @@ namespace AntSK.LLM.StableDiffusion
return sd_Image;
}
private static IntPtr GetPtrFromImage(SDImage sdImg)
{
IntPtr imgPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SDImage)));
Marshal.StructureToPtr(sdImg, imgPtr, false);
return imgPtr;
}
private static void OnNativeLog(SdLogLevel level, string text, IntPtr data)
{
Log?.Invoke(null, new StableDiffusionEventArgs.StableDiffusionLogEventArgs { Level = level, Text = text });