Bild Model Auswahl
This commit is contained in:
parent
08bc1e4baa
commit
91b644501a
|
@ -12,7 +12,7 @@ public class ImageGenerator
|
||||||
|
|
||||||
private readonly IConfiguration _config;
|
private readonly IConfiguration _config;
|
||||||
|
|
||||||
private string ModelName => _config.GetValue<string>("StableDiffusionModel") ?? "No Model Defined";
|
//private string ModelName => _config.GetValue<string>("StableDiffusionModel") ?? "No Model Defined";
|
||||||
|
|
||||||
public ImageGenerator(IConfiguration config, BildInfoData bildInfoData)
|
public ImageGenerator(IConfiguration config, BildInfoData bildInfoData)
|
||||||
{
|
{
|
||||||
|
@ -34,7 +34,7 @@ public class ImageGenerator
|
||||||
/// <param name="height">Die höhe des zu generierenden Bildes. <see langword="null"/> für Standardhöhe des Modells</param>
|
/// <param name="height">Die höhe des zu generierenden Bildes. <see langword="null"/> für Standardhöhe des Modells</param>
|
||||||
/// <param name="negativePromt">Begriffe, die explizit nicht generiert werden sollen.</param>
|
/// <param name="negativePromt">Begriffe, die explizit nicht generiert werden sollen.</param>
|
||||||
/// <returns>Die BildInfo des generierten Bildes; oder null, wenn ein Fehler auftrat.</returns>
|
/// <returns>Die BildInfo des generierten Bildes; oder null, wenn ein Fehler auftrat.</returns>
|
||||||
public async Task<BildInfoModel?> GenerateImageAsync(string imagePrompt, WunschInfoModel wunschInfo, int? width = null, int? height = null, string negativePromt = "")
|
public async Task<BildInfoModel?> GenerateImageAsync(string imagePrompt, string modelName, WunschInfoModel wunschInfo, int? width = null, int? height = null, string negativePromt = "")
|
||||||
{
|
{
|
||||||
var postData = new
|
var postData = new
|
||||||
{
|
{
|
||||||
|
@ -60,7 +60,7 @@ public class ImageGenerator
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var inferenceModelUrl = $"https://api-inference.huggingface.co/models/{ModelName}";
|
var inferenceModelUrl = $"https://api-inference.huggingface.co/models/{modelName}";
|
||||||
|
|
||||||
var response = await _client.PostAsync(inferenceModelUrl, content);
|
var response = await _client.PostAsync(inferenceModelUrl, content);
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ public class ImageGenerator
|
||||||
// Die tatsächliche Url wird in SaveImageStreamAsync gesetzt.
|
// Die tatsächliche Url wird in SaveImageStreamAsync gesetzt.
|
||||||
Dateiname = "PlaceHolder",
|
Dateiname = "PlaceHolder",
|
||||||
Datum = imageDate,
|
Datum = imageDate,
|
||||||
ImageModel = ModelName,
|
ImageModel = modelName,
|
||||||
WunschId = wunschInfo.Id,
|
WunschId = wunschInfo.Id,
|
||||||
Prompt = imagePrompt
|
Prompt = imagePrompt
|
||||||
};
|
};
|
||||||
|
|
|
@ -44,9 +44,9 @@
|
||||||
AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
|
AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
|
||||||
<RadzenText>Resolution:</RadzenText>
|
<RadzenText>Resolution:</RadzenText>
|
||||||
<RadzenStack Orientation="Orientation.Horizontal">
|
<RadzenStack Orientation="Orientation.Horizontal">
|
||||||
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@width />
|
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@_imageWidth />
|
||||||
x
|
x
|
||||||
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@height />
|
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@_imageHeight />
|
||||||
</RadzenStack>
|
</RadzenStack>
|
||||||
</RadzenStack>
|
</RadzenStack>
|
||||||
<RadzenStack Orientation="Orientation.Horizontal"
|
<RadzenStack Orientation="Orientation.Horizontal"
|
||||||
|
@ -54,6 +54,12 @@
|
||||||
<RadzenCheckBox @bind-Value=@_useGpt4 Name="UseGPT4"/>
|
<RadzenCheckBox @bind-Value=@_useGpt4 Name="UseGPT4"/>
|
||||||
<RadzenLabel Text="Verwende GPT 4" Component="UseGPT4" Style="margin-left: 8px; vertical-align: middle;" />
|
<RadzenLabel Text="Verwende GPT 4" Component="UseGPT4" Style="margin-left: 8px; vertical-align: middle;" />
|
||||||
</RadzenStack>
|
</RadzenStack>
|
||||||
|
@* <RadzenRadioButtonList Data="@_imageModels" @bind-Value=@_selectedImageModel TValue="ModelInfo" TextProperty="Name" class="mb-5"> *@
|
||||||
|
<RadzenRadioButtonList Data="@_imageModels" @bind-Value=@_selectedImageModel TValue="ModelInfo" TextProperty="Name" ValueProperty="" class="mb-5">
|
||||||
|
<Items>
|
||||||
|
<RadzenRadioButtonListItem Text="Zufällig" Value="@null" TValue="ModelInfo"/>
|
||||||
|
</Items>
|
||||||
|
</RadzenRadioButtonList>
|
||||||
</RadzenCard>
|
</RadzenCard>
|
||||||
</ChildContent>
|
</ChildContent>
|
||||||
</RadzenPanel>
|
</RadzenPanel>
|
||||||
|
@ -81,25 +87,41 @@
|
||||||
<FlippingImage ImageUrl="@_imageUrls[0]" HideImage="false"
|
<FlippingImage ImageUrl="@_imageUrls[0]" HideImage="false"
|
||||||
Show="@(_imageStates[0] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Up"
|
Show="@(_imageStates[0] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Up"
|
||||||
Click="() => ShowImageDialog(_imageUrls[0])" />
|
Click="() => ShowImageDialog(_imageUrls[0])" />
|
||||||
|
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||||
@_imagePromts[0]
|
@_imagePromts[0]
|
||||||
|
<br/>
|
||||||
|
Model: @_bildInfos[0]?.ImageModel
|
||||||
|
</RadzenPanel>
|
||||||
</RadzenColumn>
|
</RadzenColumn>
|
||||||
<RadzenColumn Size="6">
|
<RadzenColumn Size="6">
|
||||||
<FlippingImage ImageUrl="@_imageUrls[1]" HideImage="false"
|
<FlippingImage ImageUrl="@_imageUrls[1]" HideImage="false"
|
||||||
Show="@(_imageStates[1] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Right" FlipDelay="200"
|
Show="@(_imageStates[1] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Right" FlipDelay="200"
|
||||||
Click="() => ShowImageDialog(_imageUrls[1])" />
|
Click="() => ShowImageDialog(_imageUrls[1])" />
|
||||||
|
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||||
@_imagePromts[1]
|
@_imagePromts[1]
|
||||||
|
<br />
|
||||||
|
Model: @_bildInfos[1]?.ImageModel
|
||||||
|
</RadzenPanel>
|
||||||
</RadzenColumn>
|
</RadzenColumn>
|
||||||
<RadzenColumn Size="6">
|
<RadzenColumn Size="6">
|
||||||
<FlippingImage ImageUrl="@_imageUrls[2]" HideImage="false"
|
<FlippingImage ImageUrl="@_imageUrls[2]" HideImage="false"
|
||||||
Show="@(_imageStates[2] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Left" FlipDelay="600"
|
Show="@(_imageStates[2] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Left" FlipDelay="600"
|
||||||
Click="() => ShowImageDialog(_imageUrls[2])" />
|
Click="() => ShowImageDialog(_imageUrls[2])" />
|
||||||
|
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||||
@_imagePromts[2]
|
@_imagePromts[2]
|
||||||
|
<br />
|
||||||
|
Model: @_bildInfos[2]?.ImageModel
|
||||||
|
</RadzenPanel>
|
||||||
</RadzenColumn>
|
</RadzenColumn>
|
||||||
<RadzenColumn Size="6">
|
<RadzenColumn Size="6">
|
||||||
<FlippingImage ImageUrl="@_imageUrls[3]" HideImage="false"
|
<FlippingImage ImageUrl="@_imageUrls[3]" HideImage="false"
|
||||||
Show="@(_imageStates[3] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Down" FlipDelay="400"
|
Show="@(_imageStates[3] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Down" FlipDelay="400"
|
||||||
Click="() => ShowImageDialog(_imageUrls[3])" />
|
Click="() => ShowImageDialog(_imageUrls[3])" />
|
||||||
|
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||||
@_imagePromts[3]
|
@_imagePromts[3]
|
||||||
|
<br/>
|
||||||
|
Model: @_bildInfos[3]?.ImageModel
|
||||||
|
</RadzenPanel>
|
||||||
</RadzenColumn>
|
</RadzenColumn>
|
||||||
</RadzenRow>
|
</RadzenRow>
|
||||||
</RadzenCard>
|
</RadzenCard>
|
||||||
|
@ -125,6 +147,7 @@
|
||||||
|
|
||||||
private string?[] _imageUrls = new string?[4];
|
private string?[] _imageUrls = new string?[4];
|
||||||
private string?[] _imagePromts = new string?[4];
|
private string?[] _imagePromts = new string?[4];
|
||||||
|
private BildInfoModel?[] _bildInfos = new BildInfoModel?[4];
|
||||||
private ImageState[] _imageStates = new ImageState[4];
|
private ImageState[] _imageStates = new ImageState[4];
|
||||||
|
|
||||||
enum ImageState
|
enum ImageState
|
||||||
|
@ -153,8 +176,8 @@
|
||||||
private string _imageIdea = "";
|
private string _imageIdea = "";
|
||||||
|
|
||||||
private float _temperature = 0.9f;
|
private float _temperature = 0.9f;
|
||||||
private int? width = 1024;
|
private int? _imageWidth = null;
|
||||||
private int? height = 1024;
|
private int? _imageHeight = null;
|
||||||
private string _userIdea = "";
|
private string _userIdea = "";
|
||||||
private string _updateRequest = "";
|
private string _updateRequest = "";
|
||||||
private OpenAIAPI _openAiApi;
|
private OpenAIAPI _openAiApi;
|
||||||
|
@ -167,11 +190,6 @@
|
||||||
|
|
||||||
private WunschInfoModel? _wunschInfo;
|
private WunschInfoModel? _wunschInfo;
|
||||||
|
|
||||||
//protected override async Task OnInitializedAsync()
|
|
||||||
//{
|
|
||||||
// _basePrompt = await File.ReadAllTextAsync($"{Directory.GetCurrentDirectory()}{@"\wwwroot\prompt.txt"}");
|
|
||||||
//}
|
|
||||||
|
|
||||||
async Task UpdateBusyMessage(string newMessage)
|
async Task UpdateBusyMessage(string newMessage)
|
||||||
{
|
{
|
||||||
BusyMessage = newMessage;
|
BusyMessage = newMessage;
|
||||||
|
@ -180,12 +198,26 @@
|
||||||
|
|
||||||
private string _openAiApiKey = "";
|
private string _openAiApiKey = "";
|
||||||
|
|
||||||
|
class ModelInfo
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string StableDiffusionModel { get; set; }
|
||||||
|
public string PromptFormat { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ModelInfo> _imageModels;
|
||||||
|
|
||||||
|
private ModelInfo? _selectedImageModel = null;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
_openAiApiKey = Config.GetValue<string>("API:OpenAI");
|
_openAiApiKey = Config.GetValue<string>("API:OpenAI");
|
||||||
|
|
||||||
_openAiApi = new OpenAIAPI(_openAiApiKey);
|
_openAiApi = new OpenAIAPI(_openAiApiKey);
|
||||||
|
|
||||||
|
_imageModels = Config.GetSection("ImageModels").Get<List<ModelInfo>>();
|
||||||
|
//_selectedImageModel = _imageModels[0];
|
||||||
|
|
||||||
await base.OnInitializedAsync();
|
await base.OnInitializedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,21 +283,24 @@
|
||||||
/// <param name="wunschInfo">Der Wunsch für den ein Bild erzeugt wird.</param>
|
/// <param name="wunschInfo">Der Wunsch für den ein Bild erzeugt wird.</param>
|
||||||
private async Task RequestImagePromptAndGenerateImageAsync(int index, WunschInfoModel wunschInfo)
|
private async Task RequestImagePromptAndGenerateImageAsync(int index, WunschInfoModel wunschInfo)
|
||||||
{
|
{
|
||||||
|
ModelInfo imageModel = _selectedImageModel ?? _imageModels[Random.Shared.Next(_imageModels.Count)];
|
||||||
|
|
||||||
// Bild Prompt von ChatGPT anfordern
|
// Bild Prompt von ChatGPT anfordern
|
||||||
string imagePrompt = await _conversation!.GetResponseFromChatbotAsync();
|
string imagePrompt = await _conversation!.GetResponseFromChatbotAsync();
|
||||||
|
|
||||||
// Keywords anhängen um Kirstens Stil zu aktivieren.
|
// Keywords anhängen um Kirstens Stil zu aktivieren.
|
||||||
// TODO: Gucken, ob wir dem Watercolor bums brauchen
|
// TODO: Gucken, ob wir dem Watercolor bums brauchen
|
||||||
imagePrompt = "kkkk " + imagePrompt;// + " kkkk Watercolor + ink on paper, Pen drawing, wet-on-wet technique, dry-on-dry technique, dabbing technique. ";
|
imagePrompt = string.Format(imageModel.PromptFormat, imagePrompt);//"kkkk " + imagePrompt;// + " kkkk Watercolor + ink on paper, Pen drawing, wet-on-wet technique, dry-on-dry technique, dabbing technique. ";
|
||||||
|
|
||||||
// Debug only: Promt anzeigen
|
// Debug only: Promt anzeigen
|
||||||
_imagePromts[index] = imagePrompt;
|
_imagePromts[index] = imagePrompt;
|
||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
|
|
||||||
string? imageUrl = await GenerateImageAsync(imagePrompt, wunschInfo);
|
BildInfoModel? bildInfo = await GenerateImageAsync(imagePrompt, imageModel.StableDiffusionModel, wunschInfo);
|
||||||
|
|
||||||
// TODO: Fehler im UI anzeigen (zur Zeit bleibt einfach Ladebalken)
|
// TODO: Fehler im UI anzeigen (zur Zeit bleibt einfach Ladebalken)
|
||||||
_imageUrls[index] = imageUrl;
|
_imageUrls[index] = bildInfo?.Dateiname;
|
||||||
|
_bildInfos[index] = bildInfo;
|
||||||
_imageStates[index] = ImageState.FadeIn;
|
_imageStates[index] = ImageState.FadeIn;
|
||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
}
|
}
|
||||||
|
@ -276,14 +311,14 @@
|
||||||
/// <param name="imagePrompt">Der Bild Prompt</param>
|
/// <param name="imagePrompt">Der Bild Prompt</param>
|
||||||
/// <param name="wunschInfo">Der Wunsch.</param>
|
/// <param name="wunschInfo">Der Wunsch.</param>
|
||||||
/// <returns>Die URL, falls das Bild generiert wurde; oder null, wenn kein Bild generiert werden konnte.</returns>
|
/// <returns>Die URL, falls das Bild generiert wurde; oder null, wenn kein Bild generiert werden konnte.</returns>
|
||||||
private async Task<string?> GenerateImageAsync(string imagePrompt, WunschInfoModel wunschInfo)
|
private async Task<BildInfoModel?> GenerateImageAsync(string imagePrompt, string modelName, WunschInfoModel wunschInfo)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string? imageUrl = (await ImageGenerator.GenerateImageAsync(imagePrompt, wunschInfo, width, height))?.Dateiname;
|
BildInfoModel? bildInfo = await ImageGenerator.GenerateImageAsync(imagePrompt, modelName, wunschInfo, _imageWidth, _imageHeight);
|
||||||
|
|
||||||
// Kein Bild -> Fehler
|
// Kein Bild -> Fehler
|
||||||
if (imageUrl == null)
|
if (bildInfo == null)
|
||||||
{
|
{
|
||||||
bool? retry = await DialogService.Confirm(
|
bool? retry = await DialogService.Confirm(
|
||||||
"Leider konnte das Bild nicht gemalt werden. Möchtest du es noch eimal versuchen?",
|
"Leider konnte das Bild nicht gemalt werden. Möchtest du es noch eimal versuchen?",
|
||||||
|
@ -292,11 +327,11 @@
|
||||||
|
|
||||||
if (retry == true)
|
if (retry == true)
|
||||||
{
|
{
|
||||||
imageUrl = (await ImageGenerator.GenerateImageAsync(imagePrompt, wunschInfo, width, height))?.Dateiname;
|
bildInfo = await ImageGenerator.GenerateImageAsync(imagePrompt, modelName, wunschInfo, _imageWidth, _imageHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return imageUrl;
|
return bildInfo;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -10,7 +10,23 @@
|
||||||
"OpenAI": "<put OpenAI Key here>",
|
"OpenAI": "<put OpenAI Key here>",
|
||||||
"HF_Inference": "<put Hugging Face inference API Key here>"
|
"HF_Inference": "<put Hugging Face inference API Key here>"
|
||||||
},
|
},
|
||||||
|
"ImageModels": [
|
||||||
|
{
|
||||||
|
"Name": "Gen 6",
|
||||||
|
"StableDiffusionModel": "Nacken/Gen6",
|
||||||
|
"PromptFormat": "{0}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Der O.G.",
|
||||||
|
"StableDiffusionModel": "Nacken/ki-kunst-kirsten-kloeckner-colab",
|
||||||
|
"PromptFormat": "{0} kkkk"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "SDXL 18000",
|
||||||
"StableDiffusionModel": "Nacken/kkk-sdxl-18000",
|
"StableDiffusionModel": "Nacken/kkk-sdxl-18000",
|
||||||
|
"PromptFormat": "kkkk {0}"
|
||||||
|
}
|
||||||
|
],
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"Default": "<put Connection String here>"
|
"Default": "<put Connection String here>"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue