@page "/home_old" @using BLAIzor.Models @using BLAIzor.Services @using Google.Cloud.Speech.V1 @using Microsoft.AspNetCore.Identity.UI.Services @inject AIService ChatGptService @rendermode InteractiveServer @inject IJSRuntime jsRuntime; @inject IConfiguration configuration @inject ContentService _contentService @inject ScopedContentService _scopedContentService @inject IEmailSender _emailService @inject NavigationManager _navigationManager Home @* *@ @{ if(isEmailFormVisible) {

Contact us!

} } @* *@

@{ if (!string.IsNullOrEmpty(HtmlContent)) {
@((MarkupString)HtmlContent)
} else {

Working...

@* *@
} } @code { public static Home myHome; private string HtmlContent = ""; private string UserInput = string.Empty; private string ChatGptResponse = string.Empty; private bool isRecording = false; private bool isEmailFormVisible = false; private ContactFormModel ContactFormModel = new(); private string? SuccessMessage; private string? ErrorMessage; private string? DocumentEmailAddress = ""; public void HomeClick() { _navigationManager.Refresh(true); } private async Task HandleValidSubmit() { try { // Simulate sending an email await ((EmailService)_emailService).SendEmailAsync(ContactFormModel); SuccessMessage = "Thank you for contacting us! Your message has been sent."; ErrorMessage = null; // Clear the form ContactFormModel = new(); } catch (Exception ex) { ErrorMessage = "An error occurred while sending your message. Please try again later."; SuccessMessage = null; } } private void CancelEmail() { isEmailFormVisible = false; StateHasChanged(); } public Home() { myHome = this; // Set the static reference to the current instance } [JSInvokable("OpenEmailForm")] public static void OpenEmailForm(string emailAddress) { if (myHome != null) { myHome.DisplayEmailForm(emailAddress); } Console.Write("openEmail with: " + emailAddress); } public void DisplayEmailForm(string emailAddress) { isEmailFormVisible = true; DocumentEmailAddress = emailAddress; StateHasChanged(); } [JSInvokable("CallCSharpMethod")] public static async Task CallCSharpMethod(string input) { if (myHome != null) { await myHome.HandleJsCall(input); } Console.Write("Button clicked:" + input); } [JSInvokable("ProcessAudio")] public static async Task ProcessAudio(string base64Audio) { Console.Write("audio incoming"); if (myHome != null) { var languageCode = "hu-HU"; if (myHome._scopedContentService.SelectedLanguage == "Hungarian") { languageCode = "hu-HU"; } else if (myHome._scopedContentService.SelectedLanguage == "English") { languageCode = "en-US"; } else if (myHome._scopedContentService.SelectedLanguage == "German") { languageCode = "de-DE"; } var credentialsPath = myHome.configuration.GetSection("GoogleAPI").GetValue("CredentialsPath"); Console.Write(credentialsPath); var builder = new SpeechClientBuilder { CredentialsPath = credentialsPath }; var speech = builder.Build(); byte[] audioBytes = Convert.FromBase64String(base64Audio); myHome.HtmlContent = ""; var response = await speech.RecognizeAsync(new RecognitionConfig { Encoding = RecognitionConfig.Types.AudioEncoding.Mp3, SampleRateHertz = 48000, // Match the actual sample rate LanguageCode = languageCode }, RecognitionAudio.FromBytes(audioBytes)); Console.Write("BILLED: " + response.TotalBilledTime); foreach (var result in response.Results) { //Console.Write("RESULT: " + result.Alternatives.Count); foreach (var alternative in result.Alternatives) { //Console.WriteLine($"Transcription: {alternative.Transcript}"); await myHome.HandleVoiceCommand(alternative.Transcript); } } } } private async Task SendMessage() { Console.WriteLine("Button clicked!"); ChatGptResponse = await ChatGptService.Reasoning(UserInput); UserInput = string.Empty; } protected override async Task OnInitializedAsync() { Console.Write("------------------------"); ChatGptService.OnContentReceived += UpdateContent; if (string.IsNullOrEmpty(HtmlContent)) { HtmlContent = await ChatGptService.GetChatGptWelcomeMessage(); } UserInput = string.Empty; } private void UpdateContent(string content) { HtmlContent = content; InvokeAsync(StateHasChanged); // Ensures UI updates dynamically } public async Task Enter(KeyboardEventArgs e) { if (e.Code == "Enter" || e.Code == "NumpadEnter") { HtmlContent = string.Empty; HtmlContent = await ChatGptService.Reasoning(UserInput); UserInput = string.Empty; } } public async Task HandleVoiceCommand(string input) { // HtmlContent = string.Empty; UserInput = input; await InvokeAsync(StateHasChanged); await ProcessWordFile(); //UserInput = string.Empty; } public async Task HandleJsCall(string input) { HtmlContent = string.Empty; await InvokeAsync(StateHasChanged); UserInput = "Summarize " + input; await ProcessWordFile(); UserInput = string.Empty; await InvokeAsync(StateHasChanged); } private async Task ProcessWordFile() { if (!string.IsNullOrEmpty(UserInput)) { HtmlContent = string.Empty; HtmlContent = await ChatGptService.Reasoning(UserInput); UserInput = string.Empty; } } }