Compare commits

..

No commits in common. "29329215e7966164ef6714c248aebe15ce4fa4d1" and "310cf7fbced60c7adc4fc6fa4b628e48a5d22027" have entirely different histories.

6 changed files with 59 additions and 79 deletions

1
.gitignore vendored
View File

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

View File

@ -17,7 +17,6 @@
<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,5 +1,6 @@
@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
@ -7,6 +8,8 @@
@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>
@ -180,82 +183,10 @@
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }
private readonly HttpClient _client = new(); public async Task GenerateImage()
protected override Task OnInitializedAsync()
{ {
var inferenceModelUrl = "https://api-inference.huggingface.co/models/Nacken/ki-kunst-kirsten-kloeckner-colab"; //await JSRuntime.InvokeVoidAsync("DoSexyStuffBridge", _imagePrompt);
_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();
}
} }
@ -303,8 +234,7 @@
_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,5 +34,25 @@
</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

@ -0,0 +1,32 @@
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);
}