Merge pull request #58 from AIDotNet/feature_StableDiffusion

fix 修改为静态类
This commit is contained in:
zyxucp
2024-04-01 23:57:12 +08:00
committed by GitHub
3 changed files with 22 additions and 20 deletions

View File

@@ -29,7 +29,6 @@ namespace AntSK.Domain.Domain.Service
IAIModels_Repositories _aIModels_Repositories
) : IChatService
{
private readonly SDHelper helper = new SDHelper();
/// <summary>
/// 发送消息
/// </summary>
@@ -141,7 +140,7 @@ namespace AntSK.Domain.Domain.Service
if (chatResult.IsNotNull())
{
string prompt = chatResult.GetValue<string>();
if (!helper.IsInitialized)
if (!SDHelper.IsInitialized)
{
Structs.ModelParams modelParams = new Structs.ModelParams
{
@@ -152,7 +151,7 @@ namespace AntSK.Domain.Domain.Service
//VaeTiling = vaeTiling,
//LoraModelDir = loraModelDir,
};
bool result = helper.Initialize(modelParams);
bool result = SDHelper.Initialize(modelParams);
}
Structs.TextToImageParams textToImageParams = new Structs.TextToImageParams
@@ -168,11 +167,12 @@ namespace AntSK.Domain.Domain.Service
SampleSteps = 20,
Seed = -1,
};
Bitmap[] outputImages = helper.TextToImage(textToImageParams);
var base64=ImageUtils.BitmapToBase64(outputImages[0]);
Bitmap[] outputImages = SDHelper.TextToImage(textToImageParams);
var base64 = ImageUtils.BitmapToBase64(outputImages[0]);
return base64;
}
else {
else
{
return "";
}
}

View File

@@ -80,7 +80,9 @@ namespace AntSK.Pages.ChatPage.Components
{
MessageList.Clear();
await _localStorage.SetItemAsync<List<MessageInfo>>("msgs", MessageList);
await InvokeAsync(StateHasChanged);
_ = Message.Info("清理成功");
}
}
else

View File

@@ -10,13 +10,13 @@ namespace AntSK.LLM.StableDiffusion
using SDImagePtr = IntPtr;
using UpscalerContext = IntPtr;
public class SDHelper
public static class SDHelper
{
public bool IsInitialized => SdContext.Zero != sd_ctx;
public bool IsUpscalerInitialized => UpscalerContext.Zero != upscaler_ctx;
public static bool IsInitialized => SdContext.Zero != sd_ctx;
public static bool IsUpscalerInitialized => UpscalerContext.Zero != upscaler_ctx;
private SdContext sd_ctx = new SdContext();
private UpscalerContext upscaler_ctx = new UpscalerContext();
private static SdContext sd_ctx = new SdContext();
private static UpscalerContext upscaler_ctx = new UpscalerContext();
public static event EventHandler<StableDiffusionEventArgs.StableDiffusionLogEventArgs> Log;
public static event EventHandler<StableDiffusionEventArgs.StableDiffusionProgressEventArgs> Progress;
@@ -33,7 +33,7 @@ namespace AntSK.LLM.StableDiffusion
}
public bool Initialize(ModelParams modelParams)
public static bool Initialize(ModelParams modelParams)
{
sd_ctx = Native.new_sd_ctx(modelParams.ModelPath,
modelParams.VaePath,
@@ -55,13 +55,13 @@ namespace AntSK.LLM.StableDiffusion
return SdContext.Zero != sd_ctx;
}
public bool InitializeUpscaler(UpscalerParams @params)
public static bool InitializeUpscaler(UpscalerParams @params)
{
upscaler_ctx = Native.new_upscaler_ctx(@params.ESRGANPath, @params.Threads, @params.SdType);
return UpscalerContext.Zero != upscaler_ctx;
}
public void FreeSD()
public static void FreeSD()
{
if (SdContext.Zero != sd_ctx)
{
@@ -70,7 +70,7 @@ namespace AntSK.LLM.StableDiffusion
}
}
public void FreeUpscaler()
public static void FreeUpscaler()
{
if (UpscalerContext.Zero != upscaler_ctx)
{
@@ -79,7 +79,7 @@ namespace AntSK.LLM.StableDiffusion
}
}
public Bitmap[] TextToImage(TextToImageParams textToImageParams)
public static Bitmap[] TextToImage(TextToImageParams textToImageParams)
{
if (!IsInitialized) throw new ArgumentNullException("Model not loaded!");
@@ -110,7 +110,7 @@ namespace AntSK.LLM.StableDiffusion
}
public Bitmap ImageToImage(ImageToImageParams imageToImageParams)
public static Bitmap ImageToImage(ImageToImageParams imageToImageParams)
{
if (!IsInitialized) throw new ArgumentNullException("Model not loaded!");
SDImage input_sd_image = GetSDImageFromBitmap(imageToImageParams.InputImage);
@@ -133,7 +133,7 @@ namespace AntSK.LLM.StableDiffusion
return GetBitmapFromSdImage(sdImg);
}
public Bitmap UpscaleImage(Bitmap image, int upscaleFactor)
public static Bitmap UpscaleImage(Bitmap image, int upscaleFactor)
{
if (!IsUpscalerInitialized) throw new ArgumentNullException("Upscaler not loaded!");
SDImage inputSDImg = GetSDImageFromBitmap(image);
@@ -141,7 +141,7 @@ namespace AntSK.LLM.StableDiffusion
return GetBitmapFromSdImage(result);
}
private Bitmap GetBitmapFromSdImage(SDImage sd_Image)
private static Bitmap GetBitmapFromSdImage(SDImage sd_Image)
{
int width = (int)sd_Image.Width;
int height = (int)sd_Image.Height;
@@ -167,7 +167,7 @@ namespace AntSK.LLM.StableDiffusion
return bmp;
}
private SDImage GetSDImageFromBitmap(Bitmap bmp)
private static SDImage GetSDImageFromBitmap(Bitmap bmp)
{
int width = bmp.Width;
int height = bmp.Height;