Daten in Wunsch- und Bilddatenbanken speichern
This commit is contained in:
@ -12,6 +12,7 @@
|
||||
@inject DialogService DialogService
|
||||
|
||||
@inject BildInfoData BildInfoData;
|
||||
@inject WunschInfoData WunschInfoData;
|
||||
|
||||
<PageTitle>AiArt</PageTitle>
|
||||
|
||||
@ -263,28 +264,28 @@
|
||||
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_inferenceApiKey}");
|
||||
|
||||
|
||||
BildInfoModel bildInfo = new()
|
||||
{
|
||||
BildBeschreibung = "Test",
|
||||
BildPrompt = "Tost",
|
||||
Dateiname = "Task",
|
||||
Datum = DateTime.Now,
|
||||
GPTModel = "Geht dich nichts an",
|
||||
ImageModel = "Jup",
|
||||
Wunsch = request
|
||||
};
|
||||
// BildInfoModel bildInfo = new()
|
||||
// {
|
||||
// BildBeschreibung = "Test",
|
||||
// BildPrompt = "Tost",
|
||||
// Dateiname = "Task",
|
||||
// Datum = DateTime.Now,
|
||||
// GPTModel = "Geht dich nichts an",
|
||||
// ImageModel = "Jup",
|
||||
// Wunsch = request
|
||||
// };
|
||||
|
||||
try
|
||||
{
|
||||
// try
|
||||
// {
|
||||
|
||||
await BildInfoData.AddBildInfo(bildInfo);
|
||||
// //await BildInfoData.AddBildInfo(bildInfo);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// Console.WriteLine(e);
|
||||
// throw;
|
||||
// }
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
@ -292,68 +293,7 @@
|
||||
/// <summary>
|
||||
/// Geneiert das Bild für den aktuellen <see cref="_imagePrompt"/>
|
||||
/// </summary>
|
||||
public async Task GenerateImageAsync()
|
||||
{
|
||||
var postData = new
|
||||
{
|
||||
inputs = _imagePrompt
|
||||
};
|
||||
|
||||
JsonContent content = JsonContent.Create(postData);
|
||||
|
||||
async Task FailedToDrawImage()
|
||||
{
|
||||
bool? retry = await DialogService.Confirm("Leider konnte das Bild nicht gemalt werden. Möchtest du es noch eimal versuchen?", "Ups, ein Fehler ist aufgetreten...",
|
||||
new ConfirmOptions { OkButtonText = "Ja", CancelButtonText = "Nein" });
|
||||
|
||||
if (retry == true)
|
||||
await GenerateImageAsync();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var inferenceModelUrl = "https://api-inference.huggingface.co/models/Nacken/kkkk-sdxl-5000";
|
||||
|
||||
var response = await _client.PostAsync(inferenceModelUrl, content);
|
||||
|
||||
if (response?.IsSuccessStatusCode == true)
|
||||
{
|
||||
await using Stream imageStream = await response.Content.ReadAsStreamAsync();
|
||||
|
||||
using Image image = await Image.LoadAsync(imageStream);
|
||||
|
||||
string imgUrl = $"generated_images/{DateTime.Now:dd_MM_yyyy_hh_mm_s}.jpg";
|
||||
|
||||
string mapPath = $"./wwwroot/{imgUrl}";
|
||||
await image.SaveAsJpegAsync(mapPath);
|
||||
|
||||
_imageUrl = imgUrl;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Image conversion failed: {response}");
|
||||
|
||||
if (Debugger.IsAttached)
|
||||
Debugger.Break();
|
||||
|
||||
await FailedToDrawImage();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine($"Image request failed: {exception}");
|
||||
|
||||
if (Debugger.IsAttached)
|
||||
Debugger.Break();
|
||||
|
||||
await FailedToDrawImage();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Geneiert das Bild für den aktuellen <see cref="_imagePrompt"/>
|
||||
/// </summary>
|
||||
public async Task<string?> GenerateImageAsync(string prompt, bool isRetry = false)
|
||||
public async Task<string?> GenerateImageAsync(string prompt, WunschInfoModel wunschInfo, bool isRetry = false)
|
||||
{
|
||||
var postData = new
|
||||
{
|
||||
@ -382,7 +322,7 @@
|
||||
|
||||
if (retry == true)
|
||||
{
|
||||
return await GenerateImageAsync(prompt, true);
|
||||
return await GenerateImageAsync(prompt, wunschInfo, true);
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -390,7 +330,9 @@
|
||||
|
||||
try
|
||||
{
|
||||
var inferenceModelUrl = "https://api-inference.huggingface.co/models/Nacken/kkkk-sdxl-5000";
|
||||
const string modelName = "Nacken/kkkk-sdxl-5000";
|
||||
|
||||
var inferenceModelUrl = $"https://api-inference.huggingface.co/models/{modelName}";
|
||||
|
||||
var response = await _client.PostAsync(inferenceModelUrl, content);
|
||||
|
||||
@ -401,30 +343,24 @@
|
||||
using Image image = await Image.LoadAsync(imageStream);
|
||||
|
||||
DateTime imageDate = DateTime.Now;
|
||||
|
||||
BildInfoModel bildInfo = new()
|
||||
{
|
||||
Dateiname = "PlaceHolder",
|
||||
Datum = imageDate,
|
||||
ImageModel = modelName,
|
||||
WunschId = wunschInfo.Id
|
||||
};
|
||||
|
||||
await BildInfoData.AddBildInfoAsync(bildInfo);
|
||||
|
||||
string imgUrl = $"generated_images/{imageDate:dd_MM_yyyy_hh_mm_s_fffffff}.jpg";
|
||||
string imgUrl = $"generated_images/Image_{bildInfo.Id}.jpg";
|
||||
|
||||
string mapPath = $"./wwwroot/{imgUrl}";
|
||||
await image.SaveAsJpegAsync(mapPath);
|
||||
|
||||
// Hier speichern wir die Daten in die 'info_texts.txt'-Datei
|
||||
string infoTextsPath = Path.Combine(_environment.WebRootPath, "generated_images", "info_texts.txt");
|
||||
string desc = _imageDescription.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
|
||||
string newLine = $"{imgUrl}: {request}, {desc}\n";
|
||||
await File.AppendAllTextAsync(infoTextsPath, newLine);
|
||||
|
||||
BildInfoModel bildInfo = new()
|
||||
{
|
||||
BildBeschreibung = desc,
|
||||
BildPrompt = prompt,
|
||||
Dateiname = imgUrl,
|
||||
Datum = imageDate,
|
||||
GPTModel = "Geht dich nichts an",
|
||||
ImageModel = "Jup",
|
||||
Wunsch = request
|
||||
};
|
||||
|
||||
await BildInfoData.AddBildInfo(bildInfo);
|
||||
bildInfo.Dateiname = imgUrl;
|
||||
await BildInfoData.UpdateBildInfoDateinameAsync(bildInfo);
|
||||
|
||||
return imgUrl;
|
||||
}
|
||||
@ -506,11 +442,23 @@
|
||||
_imageDescription = await converse.GetResponseFromChatbotAsync();
|
||||
|
||||
await UpdateBusyMessage("Kirstens Assistent hat eine Idee! Er wird sie nun malen...");
|
||||
|
||||
WunschInfoModel wunschInfo = new()
|
||||
{
|
||||
BildBeschreibung = _imageDescription,
|
||||
BildPrompt = _imagePrompt,
|
||||
Datum = DateTime.Now,
|
||||
GPTModel = converse.Model,
|
||||
Wunsch = request
|
||||
};
|
||||
|
||||
// TODO: Try
|
||||
await WunschInfoData.AddWunschInfoAsync(wunschInfo);
|
||||
|
||||
// Vier Bilder generieren
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
_imageUrls[i] = await GenerateImageAsync(_imagePrompt);
|
||||
_imageUrls[i] = await GenerateImageAsync(_imagePrompt, wunschInfo);
|
||||
_imageStates[i] = ImageState.FadeIn;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@ builder.Services.AddScoped<DialogService>();
|
||||
builder.Services.AddScoped<NotificationService>();
|
||||
builder.Services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
|
||||
builder.Services.AddSingleton<BildInfoData>();
|
||||
builder.Services.AddSingleton<WunschInfoData>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user