Daten in Wunsch- und Bilddatenbanken speichern
This commit is contained in:
parent
d0d9cb8bb3
commit
1b28481b5d
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DataAccess.DbAccess;
|
||||
|
@ -17,8 +18,18 @@ public class BildInfoData
|
|||
_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();
|
||||
}
|
||||
}
|
|
@ -20,7 +20,7 @@ public class SqlDataAccess : ISqlDataAccess
|
|||
|
||||
return await connection.QueryAsync<T>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
|
||||
public async Task SaveData<T>(string storedProcedure, T parameters, string connectionId = "Default")
|
||||
{
|
||||
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));
|
||||
|
|
|
@ -3,12 +3,8 @@
|
|||
public class BildInfoModel
|
||||
{
|
||||
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 Dateiname { get; set; }
|
||||
public string GPTModel { 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 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();
|
||||
|
||||
|
|
|
@ -63,6 +63,9 @@
|
|||
<ItemGroup>
|
||||
<Build Include="dpo\Tables\BildInfo.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>
|
||||
<RefactorLog Include="KiKunstDatenbank.refactorlog" />
|
||||
|
|
|
@ -1,22 +1,16 @@
|
|||
CREATE PROCEDURE [dbo].[spBildInfo_Insert]
|
||||
@Id INT,
|
||||
@BildPrompt NVARCHAR(MAX) ,
|
||||
@Wunsch NVARCHAR(1024) ,
|
||||
@BildBeschreibung NVARCHAR(MAX) ,
|
||||
@Datum DATETIME2 ,
|
||||
@Dateiname NCHAR(256) ,
|
||||
@GPTModel NCHAR(32) ,
|
||||
@ImageModel NCHAR(32) ,
|
||||
@GroupIndex INT
|
||||
@WunschId INT
|
||||
AS
|
||||
BEGIN
|
||||
INSERT INTO [dbo].[BildInfo] (BildPrompt, Wunsch, BildBeschreibung, Datum, Dateiname, GPTModel, ImageModel, GroupIndex)
|
||||
VALUES (@BildPrompt,
|
||||
@Wunsch,
|
||||
@BildBeschreibung,
|
||||
@Datum,
|
||||
@Dateiname,
|
||||
@GPTModel,
|
||||
INSERT INTO [dbo].[BildInfo] (Datum, Dateiname, ImageModel, WunschId)
|
||||
VALUES (@Datum,
|
||||
@Dateiname,
|
||||
@ImageModel,
|
||||
@GroupIndex);
|
||||
END
|
||||
@WunschId);
|
||||
|
||||
SELECT Id FROM [dbo].[BildInfo] WHERE Id = CAST(SCOPE_IDENTITY() AS INT);
|
||||
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]
|
||||
(
|
||||
[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,
|
||||
[Dateiname] NCHAR(256) NOT NULL,
|
||||
[GPTModel] NCHAR(32) NOT NULL,
|
||||
[Dateiname] NCHAR(256) 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