Skip to content

Commit

Permalink
Regen word data (#122)
Browse files Browse the repository at this point in the history
* + use gpt-4o
* + RedoSampleWordCommand
  • Loading branch information
NicklausBrain authored Dec 5, 2024
1 parent 04eca32 commit fcea7aa
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 18 deletions.
2 changes: 1 addition & 1 deletion My1kWordsEe/Components/Layout/GameComposite.razor
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
else
{
<div class="d-flex justify-content-center">
<Spinner />
@* <Spinner /> *@
</div>
}
</div>
Expand Down
73 changes: 57 additions & 16 deletions My1kWordsEe/Components/Pages/WordPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
@inject AddToFavoritesCommand AddToFavoritesCommand
@inject RemoveFromFavoritesCommand RemoveFromFavoritesCommand
@inject DeleteSampleSentenceCommand DeleteSampleSentenceCommand
@inject RedoSampleWordCommand RedoSampleWordCommand


@code {
[Inject] protected PreloadService PreloadService { get; set; } = default!;
[Inject] protected ToastService ToastService { get; set; } = default!;

private ConfirmDialog DeleteSampleDialog = default!;
private ConfirmDialog ConfirmDialog = default!;

[Parameter]
public required string EeWord { get; init; }
Expand All @@ -46,8 +47,8 @@
}

WordMetadata = EeWord.ValidateWord()
? await ResolveSample()
: Result.Failure<SampleWord>("Invalid word");
? await ResolveSample()
: Result.Failure<SampleWord>("Invalid word");

await base.OnParametersSetAsync();
}
Expand Down Expand Up @@ -90,7 +91,7 @@
StateHasChanged();
}

private async Task DeleteSample(SampleSentence sampleSentence)
private async Task DeleteSampleSentence(SampleSentence sampleSentence)
{
var options = new ConfirmDialogOptions
{
Expand All @@ -100,10 +101,10 @@
AutoFocusYesButton = false,
};

var confirmation = await DeleteSampleDialog.ShowAsync(
title: "Are you sure you want to delete this?",
message1: "This will delete the record. Once deleted can not be rolled back.",
message2: "Do you want to proceed?", options);
var confirmation = await ConfirmDialog.ShowAsync(
title: "Are you sure you want to delete this?",
message1: "This will delete the record. Once deleted can not be rolled back.",
message2: "Do you want to proceed?", options);

if (confirmation)
{
Expand All @@ -126,6 +127,42 @@
}
}

private async Task RedoSample()
{
var options = new ConfirmDialogOptions
{
IsScrollable = true,
Dismissable = true,
IsVerticallyCentered = true,
AutoFocusYesButton = false,
};

var confirmation = await ConfirmDialog.ShowAsync(
title: "Is this word described inadequately?",
message1: "This will regenerate the word explanation and all the samples will removed.",
message2: "Do you want to proceed?", options);

if (confirmation)
{
PreloadService.Show(SpinnerColor.Light, "Saving data...");
var redoResult = await this.RedoSampleWordCommand.Invoke(Value.EeWord);
if (redoResult.IsSuccess)
{
WordMetadata = redoResult;
StateHasChanged();
}
else
{
ToastService.Notify(new ToastMessage(ToastType.Warning, redoResult.Error));
}
PreloadService.Hide();
}
else
{
ToastService.Notify(new ToastMessage(ToastType.Secondary, $"Redo action canceled."));
}
}

private bool IsSampleGenerationInProgress = false;

private async Task GenerateSample(MouseEventArgs e)
Expand Down Expand Up @@ -153,9 +190,11 @@

private bool IsLoggedIn => User?.Identity?.IsAuthenticated ?? false;

private bool IsFavorite() => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Words.ContainsKey(EeWord.ToLower());
private bool IsFavorite() => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess &&
Favorites.Value.Value.Words.ContainsKey(EeWord.ToLower());

private bool IsFavorite(SampleSentence sentence) => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Sentences.ContainsKey(sentence.EeSentence.ToLower());
private bool IsFavorite(SampleSentence sentence) => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue &&
Favorites.Value.IsSuccess && Favorites.Value.Value.Sentences.ContainsKey(sentence.EeSentence.ToLower());
}

<script>
Expand Down Expand Up @@ -185,6 +224,11 @@
<audio id="@EeWord" src="@Value.EeAudioUrl"></audio>
</i>
}

@if (IsLoggedIn)
{
<i role="button" class="bi bi-arrow-clockwise float-end" @onclick="RedoSample"></i>
}
</h1>

@if (IsDataLoadedOk)
Expand Down Expand Up @@ -216,11 +260,8 @@
@foreach (var sample in Value.Samples)
{
<div class="col col-12 col-sm-6 p-1">
<SampleV2 Sample="@sample"
AddToFavorites="AddToFavorites"
RemoveFromFavorites="RemoveFromFavorites"
DeleteSample="DeleteSample"
IsFavorite="IsFavorite(sample)" >
<SampleV2 Sample="@sample" AddToFavorites="AddToFavorites" RemoveFromFavorites="RemoveFromFavorites"
DeleteSample="DeleteSampleSentence" IsFavorite="IsFavorite(sample)">
</SampleV2>
</div>
}
Expand All @@ -245,5 +286,5 @@
</div>

<Preload LoadingText="Loading..." />
<ConfirmDialog @ref="DeleteSampleDialog" />
<ConfirmDialog @ref="ConfirmDialog" />
<Toasts class="p-3" AutoHide="true" StackLength="3" Placement="ToastsPlacement.TopRight" />
1 change: 1 addition & 0 deletions My1kWordsEe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static WebApplication BuildWebHost(string[] args)
builder.Services.AddSingleton<GetFavoritesQuery>();
builder.Services.AddSingleton<AddToFavoritesCommand>();
builder.Services.AddSingleton<RemoveFromFavoritesCommand>();
builder.Services.AddSingleton<RedoSampleWordCommand>();

// Blazor-specific services
builder.Services
Expand Down
3 changes: 2 additions & 1 deletion My1kWordsEe/Services/Ai/OpenAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace My1kWordsEe.Services
public class OpenAiClient
{
public const string ApiSecretKey = "Secrets:OpenAiKey";
public const string Model = "gpt-4o-mini";
public const string Model = "gpt-4o";

private readonly IConfiguration config;
private readonly ILogger logger;
Expand Down Expand Up @@ -169,6 +169,7 @@ public static async Task<Result<Sentence>> GetSampleSentence(this OpenAiClient o
"Sinu ülesanne on kirjutada selle kasutamise kohta lihtne lühike näitelause, kasutades seda sõna.\n" +
"Lauses kasuta kõige levinuimaid ja lihtsamaid sõnu eesti keeles et toetada keeleõpet.\n" +
"Eelistan SVO-lausete sõnajärge, kus esikohal on subjekt (S), seejärel tegusõna (V) ja objekt (O)\n" +
"Lausel peaks olema praktiline tegelik elu mõte\n" +
"Teie väljundiks on JSON-objekt koos eestikeelse näidislausega ja sellele vastav tõlge inglise keelde vastavalt lepingule:\n" +
"```\n{\n" +
"\"ee_sentence\": \"<näide eesti keeles>\", \"en_sentence\": \"<näide inglise keeles>\"" +
Expand Down
56 changes: 56 additions & 0 deletions My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using CSharpFunctionalExtensions;

using My1kWordsEe.Models;
using My1kWordsEe.Services.Db;

namespace My1kWordsEe.Services.Cqs
{
public class RedoSampleWordCommand
{
private readonly AzureStorageClient azureBlobService;
private readonly AddSampleWordCommand addSampleWordCommand;
private readonly DeleteSampleSentenceCommand deleteSampleSentenceCommand;

public RedoSampleWordCommand(
AzureStorageClient azureBlobService,
AddSampleWordCommand addSampleWordCommand,
DeleteSampleSentenceCommand deleteSampleSentenceCommand)
{
this.azureBlobService = azureBlobService;
this.addSampleWordCommand = addSampleWordCommand;
this.deleteSampleSentenceCommand = deleteSampleSentenceCommand;
}

public async Task<Result<SampleWord>> Invoke(string eeWord)
{
if (!eeWord.ValidateWord())
{
return Result.Failure<SampleWord>("Not an Estonian word");
}

(await azureBlobService.GetWordData(eeWord)).Deconstruct(
out bool _,
out bool isBlobAccessFailure,
out Maybe<SampleWord> savedWord,
out string blobAccessError);

if (isBlobAccessFailure)
{
return Result.Failure<SampleWord>(blobAccessError);
}

var redoTask = this.addSampleWordCommand.Invoke(eeWord);

if (savedWord.HasValue)
{
await Parallel.ForEachAsync(savedWord.Value.Samples, async (sample, ct) =>
{
if (ct.IsCancellationRequested) { return; }
await deleteSampleSentenceCommand.Invoke(sample);
});
}

return await redoTask;
}
}
}

0 comments on commit fcea7aa

Please sign in to comment.