Daten in Wunsch- und Bilddatenbanken speichern
This commit is contained in:
parent
d0d9cb8bb3
commit
1b28481b5d
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DataAccess.DbAccess;
|
using DataAccess.DbAccess;
|
||||||
|
@ -17,8 +18,18 @@ public class BildInfoData
|
||||||
_db = db;
|
_db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddBildInfo(BildInfoModel bildInfo)
|
public async Task AddBildInfoAsync(BildInfoModel bildInfo)
|
||||||
{
|
{
|
||||||
await _db.SaveData("dbo.spBildInfo_Insert", bildInfo);
|
var id = await _db.LoadData<int, BildInfoModel>("dbo.spBildInfo_Insert", bildInfo);
|
||||||
|
bildInfo.Id = id.Single();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateBildInfoDateinameAsync(BildInfoModel bildInfo)
|
||||||
|
{
|
||||||
|
await _db.SaveData("dbo.spBildInfo_UpdateFileName",
|
||||||
|
new {
|
||||||
|
Id = bildInfo.Id,
|
||||||
|
Dateiname = bildInfo.Dateiname,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DataAccess.DbAccess;
|
||||||
|
using DataAccess.Models;
|
||||||
|
|
||||||
|
namespace DataAccess.Data;
|
||||||
|
|
||||||
|
public class WunschInfoData
|
||||||
|
{
|
||||||
|
private readonly ISqlDataAccess _db;
|
||||||
|
|
||||||
|
public WunschInfoData(ISqlDataAccess db)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddWunschInfoAsync(WunschInfoModel wunschInfo)
|
||||||
|
{
|
||||||
|
var id = await _db.LoadData<int, WunschInfoModel>("dbo.spWunschInfo_Insert", wunschInfo);
|
||||||
|
wunschInfo.Id = id.Single();
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,12 +3,8 @@
|
||||||
public class BildInfoModel
|
public class BildInfoModel
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string BildPrompt { get; set; }
|
|
||||||
public string Wunsch { get; set; }
|
|
||||||
public string BildBeschreibung { get; set; }
|
|
||||||
public DateTime Datum { get; set; }
|
public DateTime Datum { get; set; }
|
||||||
public string Dateiname { get; set; }
|
public string Dateiname { get; set; }
|
||||||
public string GPTModel { get; set; }
|
|
||||||
public string ImageModel { get; set; }
|
public string ImageModel { get; set; }
|
||||||
public int GroupIndex { get; set; }
|
public int WunschId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
namespace DataAccess.Models;
|
||||||
|
|
||||||
|
public class WunschInfoModel
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string BildPrompt { get; set; }
|
||||||
|
public string Wunsch { get; set; }
|
||||||
|
public string BildBeschreibung { get; set; }
|
||||||
|
public DateTime Datum { get; set; }
|
||||||
|
public string GPTModel { get; set; }
|
||||||
|
}
|
|
@ -12,6 +12,7 @@
|
||||||
@inject DialogService DialogService
|
@inject DialogService DialogService
|
||||||
|
|
||||||
@inject BildInfoData BildInfoData;
|
@inject BildInfoData BildInfoData;
|
||||||
|
@inject WunschInfoData WunschInfoData;
|
||||||
|
|
||||||
<PageTitle>AiArt</PageTitle>
|
<PageTitle>AiArt</PageTitle>
|
||||||
|
|
||||||
|
@ -263,28 +264,28 @@
|
||||||
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_inferenceApiKey}");
|
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_inferenceApiKey}");
|
||||||
|
|
||||||
|
|
||||||
BildInfoModel bildInfo = new()
|
// BildInfoModel bildInfo = new()
|
||||||
{
|
// {
|
||||||
BildBeschreibung = "Test",
|
// BildBeschreibung = "Test",
|
||||||
BildPrompt = "Tost",
|
// BildPrompt = "Tost",
|
||||||
Dateiname = "Task",
|
// Dateiname = "Task",
|
||||||
Datum = DateTime.Now,
|
// Datum = DateTime.Now,
|
||||||
GPTModel = "Geht dich nichts an",
|
// GPTModel = "Geht dich nichts an",
|
||||||
ImageModel = "Jup",
|
// ImageModel = "Jup",
|
||||||
Wunsch = request
|
// Wunsch = request
|
||||||
};
|
// };
|
||||||
|
|
||||||
try
|
// try
|
||||||
{
|
// {
|
||||||
|
|
||||||
await BildInfoData.AddBildInfo(bildInfo);
|
// //await BildInfoData.AddBildInfo(bildInfo);
|
||||||
|
|
||||||
}
|
// }
|
||||||
catch (Exception e)
|
// catch (Exception e)
|
||||||
{
|
// {
|
||||||
Console.WriteLine(e);
|
// Console.WriteLine(e);
|
||||||
throw;
|
// throw;
|
||||||
}
|
// }
|
||||||
|
|
||||||
await base.OnInitializedAsync();
|
await base.OnInitializedAsync();
|
||||||
}
|
}
|
||||||
|
@ -292,68 +293,7 @@
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Geneiert das Bild für den aktuellen <see cref="_imagePrompt"/>
|
/// Geneiert das Bild für den aktuellen <see cref="_imagePrompt"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task GenerateImageAsync()
|
public async Task<string?> GenerateImageAsync(string prompt, WunschInfoModel wunschInfo, bool isRetry = false)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
var postData = new
|
var postData = new
|
||||||
{
|
{
|
||||||
|
@ -382,7 +322,7 @@
|
||||||
|
|
||||||
if (retry == true)
|
if (retry == true)
|
||||||
{
|
{
|
||||||
return await GenerateImageAsync(prompt, true);
|
return await GenerateImageAsync(prompt, wunschInfo, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@ -390,7 +330,9 @@
|
||||||
|
|
||||||
try
|
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);
|
var response = await _client.PostAsync(inferenceModelUrl, content);
|
||||||
|
|
||||||
|
@ -402,29 +344,23 @@
|
||||||
|
|
||||||
DateTime imageDate = DateTime.Now;
|
DateTime imageDate = DateTime.Now;
|
||||||
|
|
||||||
string imgUrl = $"generated_images/{imageDate:dd_MM_yyyy_hh_mm_s_fffffff}.jpg";
|
BildInfoModel bildInfo = new()
|
||||||
|
{
|
||||||
|
Dateiname = "PlaceHolder",
|
||||||
|
Datum = imageDate,
|
||||||
|
ImageModel = modelName,
|
||||||
|
WunschId = wunschInfo.Id
|
||||||
|
};
|
||||||
|
|
||||||
|
await BildInfoData.AddBildInfoAsync(bildInfo);
|
||||||
|
|
||||||
|
string imgUrl = $"generated_images/Image_{bildInfo.Id}.jpg";
|
||||||
|
|
||||||
string mapPath = $"./wwwroot/{imgUrl}";
|
string mapPath = $"./wwwroot/{imgUrl}";
|
||||||
await image.SaveAsJpegAsync(mapPath);
|
await image.SaveAsJpegAsync(mapPath);
|
||||||
|
|
||||||
// Hier speichern wir die Daten in die 'info_texts.txt'-Datei
|
bildInfo.Dateiname = imgUrl;
|
||||||
string infoTextsPath = Path.Combine(_environment.WebRootPath, "generated_images", "info_texts.txt");
|
await BildInfoData.UpdateBildInfoDateinameAsync(bildInfo);
|
||||||
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);
|
|
||||||
|
|
||||||
return imgUrl;
|
return imgUrl;
|
||||||
}
|
}
|
||||||
|
@ -507,10 +443,22 @@
|
||||||
|
|
||||||
await UpdateBusyMessage("Kirstens Assistent hat eine Idee! Er wird sie nun malen...");
|
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
|
// Vier Bilder generieren
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
_imageUrls[i] = await GenerateImageAsync(_imagePrompt);
|
_imageUrls[i] = await GenerateImageAsync(_imagePrompt, wunschInfo);
|
||||||
_imageStates[i] = ImageState.FadeIn;
|
_imageStates[i] = ImageState.FadeIn;
|
||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ builder.Services.AddScoped<DialogService>();
|
||||||
builder.Services.AddScoped<NotificationService>();
|
builder.Services.AddScoped<NotificationService>();
|
||||||
builder.Services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
|
builder.Services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
|
||||||
builder.Services.AddSingleton<BildInfoData>();
|
builder.Services.AddSingleton<BildInfoData>();
|
||||||
|
builder.Services.AddSingleton<WunschInfoData>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,9 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Build Include="dpo\Tables\BildInfo.sql" />
|
<Build Include="dpo\Tables\BildInfo.sql" />
|
||||||
<Build Include="dpo\StoredProcedures\spBildInfo_Insert.sql" />
|
<Build Include="dpo\StoredProcedures\spBildInfo_Insert.sql" />
|
||||||
|
<Build Include="dpo\Tables\WunschInfo.sql" />
|
||||||
|
<Build Include="dpo\StoredProcedures\spWunschInfo_Insert.sql" />
|
||||||
|
<Build Include="dpo\StoredProcedures\spBildInfo_UpdateFileName.sql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<RefactorLog Include="KiKunstDatenbank.refactorlog" />
|
<RefactorLog Include="KiKunstDatenbank.refactorlog" />
|
||||||
|
|
|
@ -1,22 +1,16 @@
|
||||||
CREATE PROCEDURE [dbo].[spBildInfo_Insert]
|
CREATE PROCEDURE [dbo].[spBildInfo_Insert]
|
||||||
@Id INT,
|
@Id INT,
|
||||||
@BildPrompt NVARCHAR(MAX) ,
|
|
||||||
@Wunsch NVARCHAR(1024) ,
|
|
||||||
@BildBeschreibung NVARCHAR(MAX) ,
|
|
||||||
@Datum DATETIME2 ,
|
@Datum DATETIME2 ,
|
||||||
@Dateiname NCHAR(256) ,
|
@Dateiname NCHAR(256) ,
|
||||||
@GPTModel NCHAR(32) ,
|
|
||||||
@ImageModel NCHAR(32) ,
|
@ImageModel NCHAR(32) ,
|
||||||
@GroupIndex INT
|
@WunschId INT
|
||||||
AS
|
AS
|
||||||
BEGIN
|
BEGIN
|
||||||
INSERT INTO [dbo].[BildInfo] (BildPrompt, Wunsch, BildBeschreibung, Datum, Dateiname, GPTModel, ImageModel, GroupIndex)
|
INSERT INTO [dbo].[BildInfo] (Datum, Dateiname, ImageModel, WunschId)
|
||||||
VALUES (@BildPrompt,
|
VALUES (@Datum,
|
||||||
@Wunsch,
|
|
||||||
@BildBeschreibung,
|
|
||||||
@Datum,
|
|
||||||
@Dateiname,
|
@Dateiname,
|
||||||
@GPTModel,
|
|
||||||
@ImageModel,
|
@ImageModel,
|
||||||
@GroupIndex);
|
@WunschId);
|
||||||
|
|
||||||
|
SELECT Id FROM [dbo].[BildInfo] WHERE Id = CAST(SCOPE_IDENTITY() AS INT);
|
||||||
END
|
END
|
|
@ -0,0 +1,9 @@
|
||||||
|
CREATE PROCEDURE [dbo].[spBildInfo_UpdateFileName]
|
||||||
|
@Id INT,
|
||||||
|
@Dateiname NCHAR(256)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
UPDATE [dbo].[BildInfo]
|
||||||
|
SET Dateiname = @Dateiname
|
||||||
|
WHERE Id = @Id;
|
||||||
|
END
|
|
@ -0,0 +1,18 @@
|
||||||
|
CREATE PROCEDURE [dbo].[spWunschInfo_Insert]
|
||||||
|
@Id INT,
|
||||||
|
@Wunsch NVARCHAR(1024),
|
||||||
|
@BildPrompt NVARCHAR(MAX),
|
||||||
|
@BildBeschreibung NVARCHAR(MAX),
|
||||||
|
@Datum DATETIME2,
|
||||||
|
@GPTModel NCHAR(32)
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
INSERT INTO [dbo].[WunschInfo] (Wunsch, BildPrompt, BildBeschreibung, Datum, GPTModel)
|
||||||
|
VALUES (@Wunsch,
|
||||||
|
@BildPrompt,
|
||||||
|
@BildBeschreibung,
|
||||||
|
@Datum,
|
||||||
|
@GPTModel);
|
||||||
|
|
||||||
|
SELECT Id FROM [dbo].[WunschInfo] WHERE Id = CAST(SCOPE_IDENTITY() AS INT);
|
||||||
|
END
|
|
@ -1,22 +1,8 @@
|
||||||
CREATE TABLE [dbo].[BildInfo]
|
CREATE TABLE [dbo].[BildInfo]
|
||||||
(
|
(
|
||||||
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
|
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
|
||||||
[BildPrompt] NVARCHAR(MAX) NOT NULL,
|
|
||||||
[Wunsch] NVARCHAR(1024) NOT NULL,
|
|
||||||
[BildBeschreibung] NVARCHAR(MAX) NOT NULL,
|
|
||||||
[Datum] DATETIME2 NOT NULL,
|
[Datum] DATETIME2 NOT NULL,
|
||||||
[Dateiname] NCHAR(256) NOT NULL,
|
[Dateiname] NCHAR(256) NOT NULL,
|
||||||
[GPTModel] NCHAR(32) NOT NULL,
|
|
||||||
[ImageModel] NCHAR(32) NOT NULL,
|
[ImageModel] NCHAR(32) NOT NULL,
|
||||||
[GroupIndex] INT NOT NULL
|
[WunschId] INT NOT NULL
|
||||||
)
|
)
|
||||||
|
|
||||||
GO
|
|
||||||
EXEC sp_addextendedproperty @name = N'MS_Description',
|
|
||||||
@value = N'Index der Gruppe, in der dieses Bild generiert wurde und so.',
|
|
||||||
@level0type = N'SCHEMA',
|
|
||||||
@level0name = N'dbo',
|
|
||||||
@level1type = N'TABLE',
|
|
||||||
@level1name = N'BildInfo',
|
|
||||||
@level2type = N'COLUMN',
|
|
||||||
@level2name = 'GroupIndex'
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
CREATE TABLE [dbo].[WunschInfo]
|
||||||
|
(
|
||||||
|
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
|
||||||
|
[Wunsch] NVARCHAR (1024) NOT NULL,
|
||||||
|
[BildPrompt] NVARCHAR (MAX) NOT NULL,
|
||||||
|
[BildBeschreibung] NVARCHAR (MAX) NOT NULL,
|
||||||
|
[Datum] DATETIME2 (7) NOT NULL,
|
||||||
|
[GPTModel] NCHAR (32) NOT NULL
|
||||||
|
)
|
Loading…
Reference in New Issue