diff --git a/src/AntSK.Domain/Domain/Service/ChatService.cs b/src/AntSK.Domain/Domain/Service/ChatService.cs index dfa5414..dc2b253 100644 --- a/src/AntSK.Domain/Domain/Service/ChatService.cs +++ b/src/AntSK.Domain/Domain/Service/ChatService.cs @@ -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(); if (!SDHelper.IsInitialized) { diff --git a/src/AntSK/AntSK.csproj b/src/AntSK/AntSK.csproj index dde5c11..8652e62 100644 --- a/src/AntSK/AntSK.csproj +++ b/src/AntSK/AntSK.csproj @@ -35,5 +35,8 @@ PreserveNewest + + PreserveNewest + diff --git a/src/AntSK/Pages/Setting/AIModel/ModelDown.razor.cs b/src/AntSK/Pages/Setting/AIModel/ModelDown.razor.cs index e0b8594..28e1c44 100644 --- a/src/AntSK/Pages/Setting/AIModel/ModelDown.razor.cs +++ b/src/AntSK/Pages/Setting/AIModel/ModelDown.razor.cs @@ -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(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(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(response.Content); - _modelList = model.models; } private async Task Search(string searchKey) diff --git a/src/AntSK/StableDiffusionModelList.txt b/src/AntSK/StableDiffusionModelList.txt new file mode 100644 index 0000000..401e8c9 --- /dev/null +++ b/src/AntSK/StableDiffusionModelList.txt @@ -0,0 +1,6 @@ +AsAHuman/chilloutmix +GraMpa7/dreamsharper +Airic/Anything-V4.5 +liqira/anythingv3 +wind1/MoYou +Reuploadingfromcivitai/DosMix \ No newline at end of file diff --git a/src/AntSk.LLM/AntSK.LLM.csproj b/src/AntSk.LLM/AntSK.LLM.csproj index 312361d..733dcbe 100644 --- a/src/AntSk.LLM/AntSK.LLM.csproj +++ b/src/AntSk.LLM/AntSK.LLM.csproj @@ -20,6 +20,30 @@ Always + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + diff --git a/src/AntSk.LLM/StableDiffusion/Backend/CPU/stable-diffusion.so b/src/AntSk.LLM/StableDiffusion/Backend/CPU/stable-diffusion.so new file mode 100644 index 0000000..8ef3084 Binary files /dev/null and b/src/AntSk.LLM/StableDiffusion/Backend/CPU/stable-diffusion.so differ diff --git a/src/AntSk.LLM/StableDiffusion/Backend/Cuda11/stable-diffusion.so b/src/AntSk.LLM/StableDiffusion/Backend/Cuda11/stable-diffusion.so new file mode 100644 index 0000000..7d4e63f Binary files /dev/null and b/src/AntSk.LLM/StableDiffusion/Backend/Cuda11/stable-diffusion.so differ diff --git a/src/AntSk.LLM/StableDiffusion/Backend/Cuda12/stable-diffusion.so b/src/AntSk.LLM/StableDiffusion/Backend/Cuda12/stable-diffusion.so new file mode 100644 index 0000000..7df863c Binary files /dev/null and b/src/AntSk.LLM/StableDiffusion/Backend/Cuda12/stable-diffusion.so differ diff --git a/src/AntSk.LLM/StableDiffusion/Backend/ROCm/stable-diffusion.so b/src/AntSk.LLM/StableDiffusion/Backend/ROCm/stable-diffusion.so new file mode 100644 index 0000000..c020109 Binary files /dev/null and b/src/AntSk.LLM/StableDiffusion/Backend/ROCm/stable-diffusion.so differ diff --git a/src/AntSk.LLM/StableDiffusion/SDHelper.cs b/src/AntSk.LLM/StableDiffusion/SDHelper.cs index 737f42c..b08fc4e 100644 --- a/src/AntSk.LLM/StableDiffusion/SDHelper.cs +++ b/src/AntSk.LLM/StableDiffusion/SDHelper.cs @@ -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 });