ImageMaster3000 ist nach C# umgezogen
This commit is contained in:
parent
70da97e23b
commit
41ee4efcce
|
@ -396,3 +396,4 @@ FodyWeavers.xsd
|
|||
|
||||
# JetBrains Rider
|
||||
*.sln.iml
|
||||
KIKunstKirstenKlöckner/wwwroot/generated_images/*
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\NewFolder\" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
@page "/aiart"
|
||||
@using OpenAI_API
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using OpenAI_API.Chat
|
||||
@using OpenAI_API.Models
|
||||
@using System.Diagnostics
|
||||
|
@ -8,8 +7,6 @@
|
|||
@inject TooltipService TooltipService
|
||||
@inject DialogService DialogService
|
||||
|
||||
@inject IJSRuntime JSRuntime;
|
||||
|
||||
<PageTitle>AiArt</PageTitle>
|
||||
|
||||
<h1>Wunschbilder von KI nur für dich</h1>
|
||||
|
@ -170,11 +167,83 @@
|
|||
BusyMessage = newMessage;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
public async Task GenerateImage()
|
||||
|
||||
private readonly HttpClient _client = new();
|
||||
|
||||
protected override Task OnInitializedAsync()
|
||||
{
|
||||
//await JSRuntime.InvokeVoidAsync("DoSexyStuffBridge", _imagePrompt);
|
||||
_imageUrl = await JSRuntime.InvokeAsync<string>("DoSexyStuffBridge", _imagePrompt);
|
||||
var inferenceModelUrl = "https://api-inference.huggingface.co/models/Nacken/ki-kunst-kirsten-kloeckner-colab";
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DoStuff()
|
||||
|
@ -215,7 +284,7 @@
|
|||
|
||||
await UpdateBusyMessage("Kirstens Assistent hat eine Idee! Er wird sie nun malen...");
|
||||
|
||||
Task makeImagesTask = GenerateImage();
|
||||
Task makeImagesTask = GenerateImageAsync();
|
||||
|
||||
//CancellationTokenSource cancelFunnyMessages = new CancellationTokenSource();
|
||||
|
||||
|
|
|
@ -34,25 +34,5 @@
|
|||
</div>
|
||||
|
||||
<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>
|
||||
</html>
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue