Compare commits

..

3 Commits

Author SHA1 Message Date
Simon Lübeß 29329215e7 Merge branch 'main' of https://gitfrieds.nackenbox.xyz/Gottfried/KI-Kunst-Kirsten-Kloeckner 2023-08-04 15:45:39 +02:00
Simon Lübeß 3dd5e7fa98 Merge branch 'main' of https://gitfrieds.nackenbox.xyz/Gottfried/KI-Kunst-Kirsten-Kloeckner
# Conflicts:
#	KIKunstKirstenKlöckner/Pages/AiArt.razor
2023-08-04 15:41:26 +02:00
Simon Lübeß 41ee4efcce ImageMaster3000 ist nach C# umgezogen 2023-08-04 15:37:07 +02:00
6 changed files with 79 additions and 59 deletions

1
.gitignore vendored
View File

@ -396,3 +396,4 @@ FodyWeavers.xsd
# JetBrains Rider # JetBrains Rider
*.sln.iml *.sln.iml
KIKunstKirstenKlöckner/wwwroot/generated_images/*

View File

@ -17,6 +17,7 @@
<ItemGroup> <ItemGroup>
<Folder Include="wwwroot\NewFolder\" /> <Folder Include="wwwroot\NewFolder\" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,5 @@
@page "/aiart" @page "/aiart"
@using OpenAI_API @using OpenAI_API
@using Microsoft.AspNetCore.Components.Web
@using OpenAI_API.Chat @using OpenAI_API.Chat
@using OpenAI_API.Models @using OpenAI_API.Models
@using System.Diagnostics @using System.Diagnostics
@ -8,8 +7,6 @@
@inject TooltipService TooltipService @inject TooltipService TooltipService
@inject DialogService DialogService @inject DialogService DialogService
@inject IJSRuntime JSRuntime;
<PageTitle>AiArt</PageTitle> <PageTitle>AiArt</PageTitle>
<h1>Wunschbilder von KI nur für dich</h1> <h1>Wunschbilder von KI nur für dich</h1>
@ -183,10 +180,82 @@
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }
public async Task GenerateImage() private readonly HttpClient _client = new();
protected override Task OnInitializedAsync()
{ {
//await JSRuntime.InvokeVoidAsync("DoSexyStuffBridge", _imagePrompt); var inferenceModelUrl = "https://api-inference.huggingface.co/models/Nacken/ki-kunst-kirsten-kloeckner-colab";
_imageUrl = await JSRuntime.InvokeAsync<string>("DoSexyStuffBridge", _imagePrompt);
// TODO: nicht im repo speichern
var API_TOKEN = "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";
//_client.BaseAddress = new Uri(inferenceModelUrl);
_client.DefaultRequestHeaders.Clear();
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {API_TOKEN}");
return base.OnInitializedAsync();
}
/// <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/ki-kunst-kirsten-kloeckner-colab";
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();
}
} }
@ -234,7 +303,8 @@
_imageDescription = await converse.GetResponseFromChatbotAsync(); _imageDescription = await converse.GetResponseFromChatbotAsync();
await UpdateBusyMessage("Kirstens Assistent hat eine Idee! Er wird sie nun malen..."); await UpdateBusyMessage("Kirstens Assistent hat eine Idee! Er wird sie nun malen...");
Task makeImagesTask = GenerateImage();
Task makeImagesTask = GenerateImageAsync();
//CancellationTokenSource cancelFunnyMessages = new CancellationTokenSource(); //CancellationTokenSource cancelFunnyMessages = new CancellationTokenSource();

View File

@ -34,25 +34,5 @@
</div> </div>
<script src="_framework/blazor.server.js"></script> <script src="_framework/blazor.server.js"></script>
<script type="module" src="scripts/ImageMaster3000.js"></script>
<script>
window.DoSexyStuffBridge = async function (prompt)
{
// Stellen Sie sicher, dass das Modul geladen und die Funktion verfügbar ist
if (typeof DoSexyStuff === "function")
{
return await DoSexyStuff(prompt);
}
else
{
console.error("Die Funktion DoSexyStuff ist nicht verfügbar.");
return "Error";
}
}
</script>
</body> </body>
</html> </html>

View File

@ -1,32 +0,0 @@
import { HfInference } from 'https://cdn.jsdelivr.net/npm/@huggingface/inference@2.6.1/+esm';
import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@0.8.4/+esm";
async function DoSexyStuff(prompt)
{
const HF_ACCESS_TOKEN = "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";
const inference = new HfInference(HF_ACCESS_TOKEN);
const blob = await inference.textToImage({
//model: 'stabilityai/stable-diffusion-2',
model: 'Nacken/ki-kunst-kirsten-kloeckner-colab',
//inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]',
inputs: prompt,
parameters: {
negative_prompt: 'blurry',
}
})
const imageUrl = window.URL.createObjectURL(blob);
return imageUrl;
//window.open(imageUrl, '_blank');
}
// Exportiere die Funktion in den globalen Kontext
window.DoSexyStuff = DoSexyStuff;
function showAlert(message) {
alert(message);
}