Bild Model Auswahl
This commit is contained in:
parent
08bc1e4baa
commit
91b644501a
|
@ -12,7 +12,7 @@ public class ImageGenerator
|
|||
|
||||
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)
|
||||
{
|
||||
|
@ -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="negativePromt">Begriffe, die explizit nicht generiert werden sollen.</param>
|
||||
/// <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
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ public class ImageGenerator
|
|||
|
||||
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);
|
||||
|
||||
|
@ -75,7 +75,7 @@ public class ImageGenerator
|
|||
// Die tatsächliche Url wird in SaveImageStreamAsync gesetzt.
|
||||
Dateiname = "PlaceHolder",
|
||||
Datum = imageDate,
|
||||
ImageModel = ModelName,
|
||||
ImageModel = modelName,
|
||||
WunschId = wunschInfo.Id,
|
||||
Prompt = imagePrompt
|
||||
};
|
||||
|
|
|
@ -44,9 +44,9 @@
|
|||
AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
|
||||
<RadzenText>Resolution:</RadzenText>
|
||||
<RadzenStack Orientation="Orientation.Horizontal">
|
||||
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@width />
|
||||
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@_imageWidth />
|
||||
x
|
||||
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@height />
|
||||
<RadzenNumeric ShowUpDown = "false" TValue = "int?" @bind-Value=@_imageHeight />
|
||||
</RadzenStack>
|
||||
</RadzenStack>
|
||||
<RadzenStack Orientation="Orientation.Horizontal"
|
||||
|
@ -54,6 +54,12 @@
|
|||
<RadzenCheckBox @bind-Value=@_useGpt4 Name="UseGPT4"/>
|
||||
<RadzenLabel Text="Verwende GPT 4" Component="UseGPT4" Style="margin-left: 8px; vertical-align: middle;" />
|
||||
</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>
|
||||
</ChildContent>
|
||||
</RadzenPanel>
|
||||
|
@ -81,25 +87,41 @@
|
|||
<FlippingImage ImageUrl="@_imageUrls[0]" HideImage="false"
|
||||
Show="@(_imageStates[0] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Up"
|
||||
Click="() => ShowImageDialog(_imageUrls[0])" />
|
||||
@_imagePromts[0]
|
||||
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||
@_imagePromts[0]
|
||||
<br/>
|
||||
Model: @_bildInfos[0]?.ImageModel
|
||||
</RadzenPanel>
|
||||
</RadzenColumn>
|
||||
<RadzenColumn Size="6">
|
||||
<FlippingImage ImageUrl="@_imageUrls[1]" HideImage="false"
|
||||
Show="@(_imageStates[1] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Right" FlipDelay="200"
|
||||
Click="() => ShowImageDialog(_imageUrls[1])" />
|
||||
@_imagePromts[1]
|
||||
Click="() => ShowImageDialog(_imageUrls[1])" />
|
||||
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||
@_imagePromts[1]
|
||||
<br />
|
||||
Model: @_bildInfos[1]?.ImageModel
|
||||
</RadzenPanel>
|
||||
</RadzenColumn>
|
||||
<RadzenColumn Size="6">
|
||||
<FlippingImage ImageUrl="@_imageUrls[2]" HideImage="false"
|
||||
Show="@(_imageStates[2] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Left" FlipDelay="600"
|
||||
Click="() => ShowImageDialog(_imageUrls[2])" />
|
||||
@_imagePromts[2]
|
||||
Click="() => ShowImageDialog(_imageUrls[2])" />
|
||||
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||
@_imagePromts[2]
|
||||
<br />
|
||||
Model: @_bildInfos[2]?.ImageModel
|
||||
</RadzenPanel>
|
||||
</RadzenColumn>
|
||||
<RadzenColumn Size="6">
|
||||
<FlippingImage ImageUrl="@_imageUrls[3]" HideImage="false"
|
||||
Show="@(_imageStates[3] == ImageState.FadeIn)" FlipTo="FlippingImage.FlipDirection.Down" FlipDelay="400"
|
||||
Click="() => ShowImageDialog(_imageUrls[3])" />
|
||||
@_imagePromts[3]
|
||||
Click="() => ShowImageDialog(_imageUrls[3])" />
|
||||
<RadzenPanel AllowCollapse="true" Collapsed="true" Text="Info">
|
||||
@_imagePromts[3]
|
||||
<br/>
|
||||
Model: @_bildInfos[3]?.ImageModel
|
||||
</RadzenPanel>
|
||||
</RadzenColumn>
|
||||
</RadzenRow>
|
||||
</RadzenCard>
|
||||
|
@ -125,6 +147,7 @@
|
|||
|
||||
private string?[] _imageUrls = new string?[4];
|
||||
private string?[] _imagePromts = new string?[4];
|
||||
private BildInfoModel?[] _bildInfos = new BildInfoModel?[4];
|
||||
private ImageState[] _imageStates = new ImageState[4];
|
||||
|
||||
enum ImageState
|
||||
|
@ -153,8 +176,8 @@
|
|||
private string _imageIdea = "";
|
||||
|
||||
private float _temperature = 0.9f;
|
||||
private int? width = 1024;
|
||||
private int? height = 1024;
|
||||
private int? _imageWidth = null;
|
||||
private int? _imageHeight = null;
|
||||
private string _userIdea = "";
|
||||
private string _updateRequest = "";
|
||||
private OpenAIAPI _openAiApi;
|
||||
|
@ -167,11 +190,6 @@
|
|||
|
||||
private WunschInfoModel? _wunschInfo;
|
||||
|
||||
//protected override async Task OnInitializedAsync()
|
||||
//{
|
||||
// _basePrompt = await File.ReadAllTextAsync($"{Directory.GetCurrentDirectory()}{@"\wwwroot\prompt.txt"}");
|
||||
//}
|
||||
|
||||
async Task UpdateBusyMessage(string newMessage)
|
||||
{
|
||||
BusyMessage = newMessage;
|
||||
|
@ -180,12 +198,26 @@
|
|||
|
||||
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()
|
||||
{
|
||||
_openAiApiKey = Config.GetValue<string>("API:OpenAI");
|
||||
|
||||
_openAiApi = new OpenAIAPI(_openAiApiKey);
|
||||
|
||||
_imageModels = Config.GetSection("ImageModels").Get<List<ModelInfo>>();
|
||||
//_selectedImageModel = _imageModels[0];
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
|
@ -251,21 +283,24 @@
|
|||
/// <param name="wunschInfo">Der Wunsch für den ein Bild erzeugt wird.</param>
|
||||
private async Task RequestImagePromptAndGenerateImageAsync(int index, WunschInfoModel wunschInfo)
|
||||
{
|
||||
ModelInfo imageModel = _selectedImageModel ?? _imageModels[Random.Shared.Next(_imageModels.Count)];
|
||||
|
||||
// Bild Prompt von ChatGPT anfordern
|
||||
string imagePrompt = await _conversation!.GetResponseFromChatbotAsync();
|
||||
|
||||
// Keywords anhängen um Kirstens Stil zu aktivieren.
|
||||
// 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
|
||||
_imagePromts[index] = imagePrompt;
|
||||
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)
|
||||
_imageUrls[index] = imageUrl;
|
||||
_imageUrls[index] = bildInfo?.Dateiname;
|
||||
_bildInfos[index] = bildInfo;
|
||||
_imageStates[index] = ImageState.FadeIn;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
@ -276,14 +311,14 @@
|
|||
/// <param name="imagePrompt">Der Bild Prompt</param>
|
||||
/// <param name="wunschInfo">Der Wunsch.</param>
|
||||
/// <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
|
||||
{
|
||||
string? imageUrl = (await ImageGenerator.GenerateImageAsync(imagePrompt, wunschInfo, width, height))?.Dateiname;
|
||||
BildInfoModel? bildInfo = await ImageGenerator.GenerateImageAsync(imagePrompt, modelName, wunschInfo, _imageWidth, _imageHeight);
|
||||
|
||||
// Kein Bild -> Fehler
|
||||
if (imageUrl == null)
|
||||
if (bildInfo == null)
|
||||
{
|
||||
bool? retry = await DialogService.Confirm(
|
||||
"Leider konnte das Bild nicht gemalt werden. Möchtest du es noch eimal versuchen?",
|
||||
|
@ -292,11 +327,11 @@
|
|||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -10,7 +10,23 @@
|
|||
"OpenAI": "<put OpenAI Key here>",
|
||||
"HF_Inference": "<put Hugging Face inference API Key here>"
|
||||
},
|
||||
"StableDiffusionModel": "Nacken/kkk-sdxl-18000",
|
||||
"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",
|
||||
"PromptFormat": "kkkk {0}"
|
||||
}
|
||||
],
|
||||
"ConnectionStrings": {
|
||||
"Default": "<put Connection String here>"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue