312 lines
11 KiB
C#
312 lines
11 KiB
C#
using BLAIzor.Models;
|
|
using BLAIzor.Services;
|
|
using Google.Api;
|
|
using Google.Cloud.Speech.V1;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.JSInterop;
|
|
using System.Collections;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using UglyToad.PdfPig.DocumentLayoutAnalysis;
|
|
|
|
|
|
namespace BLAIzor.Components.Pages
|
|
{
|
|
public class SharedDisplayLogic : ComponentBase
|
|
{
|
|
public string SelectedBrandName = "default";
|
|
[Inject] protected ScopedContentService _scopedContentService { get; set; }
|
|
[Inject] protected ContentEditorService _contentEditorService { get; set; }
|
|
[Inject] protected IConfiguration configuration { get; set; }
|
|
[Inject] protected HttpClient Http { get; set; }
|
|
[Inject] protected IJSRuntime jsRuntime { get; set; }
|
|
[Inject] protected AIService ChatGptService { get; set; }
|
|
|
|
public static readonly Dictionary<string, SharedDisplayLogic> _instances = new();
|
|
|
|
public string SessionId;
|
|
public static SharedDisplayLogic myHome;
|
|
|
|
public int SiteId;
|
|
public SiteInfo SiteInfo;
|
|
public StringBuilder HtmlContent = new StringBuilder("");
|
|
public string TextContent = "";
|
|
public string StatusContent = "";
|
|
public string UserInput = string.Empty;
|
|
public string CollectionName = "html_snippets";
|
|
|
|
public bool VoiceEnabled;
|
|
public bool TTSEnabled;
|
|
public bool STTEnabled;
|
|
public bool _initVoicePending = false;
|
|
public bool welcomeStage = true;
|
|
public bool AiVoicePermitted = false;
|
|
|
|
public string FirstColumnClass = "";
|
|
public bool isEmailFormVisible = false;
|
|
public ContactFormModel ContactFormModel = new();
|
|
// private string? SuccessMessage;
|
|
// private string? ErrorMessage;
|
|
public string? DocumentEmailAddress = "";
|
|
|
|
public void DoSharedWork()
|
|
{
|
|
// Logic here
|
|
}
|
|
public async void HandleBrandNameChanged()
|
|
{
|
|
SelectedBrandName = _scopedContentService.SelectedBrandName;
|
|
//await InvokeAsync(() =>
|
|
// {
|
|
// StateHasChanged();
|
|
// });
|
|
|
|
|
|
try
|
|
{
|
|
StateHasChanged();
|
|
|
|
//await Task.Run(() =>
|
|
//{
|
|
// StateHasChanged();
|
|
//}).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<string> GetMenuList(int siteId)
|
|
{
|
|
List<MenuItem> menuItems = (await _contentEditorService.GetMenuItemsBySiteIdAsync(siteId)).Where(m => m.ShowInMainMenu == true).OrderBy(m => m.SortOrder).ToList();
|
|
string menuList = "";
|
|
foreach (MenuItem item in menuItems)
|
|
{
|
|
menuList += item.Name + ",";
|
|
}
|
|
return menuList;
|
|
}
|
|
|
|
public async Task<List<MenuItem>> GetMenuItems(int siteId)
|
|
{
|
|
List<MenuItem> menuItems = (await _contentEditorService.GetMenuItemsBySiteIdAsync(siteId)).Where(m => m.ShowInMainMenu == true).OrderBy(m => m.SortOrder).ToList();
|
|
return menuItems;
|
|
}
|
|
|
|
|
|
private string GetApiKey() =>
|
|
configuration?.GetSection("ElevenLabsAPI")?.GetValue<string>("ApiKey") ?? string.Empty;
|
|
|
|
public async Task ConvertTextToSpeech(string textContent)
|
|
{
|
|
// string plainText = WebUtility.HtmlDecode(HtmlContent.ToString());
|
|
|
|
if (string.IsNullOrWhiteSpace(textContent) || VoiceEnabled == false || TTSEnabled == false || welcomeStage || !AiVoicePermitted)
|
|
return;
|
|
|
|
Console.WriteLine("------------------------------OMGOMGOMG TTS call!!!!-------------");
|
|
|
|
var requestContent = new
|
|
{
|
|
text = textContent,
|
|
// model_id = "eleven_multilingual_v2",
|
|
model_id = "eleven_flash_v2_5",
|
|
voice_settings = new
|
|
{
|
|
stability = 0.5,
|
|
similarity_boost = 0.75,
|
|
speed = 1.1
|
|
}
|
|
};
|
|
|
|
var requestJson = JsonSerializer.Serialize(requestContent);
|
|
string voiceId;
|
|
if (SiteInfo.voiceId != null)
|
|
{
|
|
voiceId = SiteInfo.voiceId;
|
|
}
|
|
else
|
|
{
|
|
voiceId = "rE22Kc7UGoQj4zdHNYvd";
|
|
}
|
|
// string voiceId = "yyPLNYHg3CvjlSdSOdLh";
|
|
|
|
var httpRequest = new HttpRequestMessage(HttpMethod.Post, $"https://api.elevenlabs.io/v1/text-to-speech/{voiceId}/stream")
|
|
{
|
|
Content = new StringContent(requestJson, Encoding.UTF8, "application/json")
|
|
};
|
|
|
|
httpRequest.Headers.Add("xi-api-key", GetApiKey());
|
|
|
|
var response = await Http.SendAsync(httpRequest);
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var audioBytes = await response.Content.ReadAsByteArrayAsync();
|
|
var base64Audio = Convert.ToBase64String(audioBytes);
|
|
var audioDataUrl = $"data:audio/mpeg;base64,{base64Audio}";
|
|
|
|
await jsRuntime.InvokeVoidAsync("playAudio", audioDataUrl);
|
|
}
|
|
else
|
|
{
|
|
// Handle error response
|
|
var errorContent = await response.Content.ReadAsStringAsync();
|
|
Console.Error.WriteLine($"Error: {errorContent}");
|
|
}
|
|
}
|
|
|
|
[JSInvokable("CallCSharpMethod2")]
|
|
public static async Task CallCSharpMethod2(string input, string sessionId, bool forceUnModified = false)
|
|
{
|
|
// if (myHome != null)
|
|
// {
|
|
// await myHome.HandleJsCall(input, );
|
|
// }
|
|
|
|
if (_instances.TryGetValue(sessionId, out var instance))
|
|
{
|
|
await instance.HandleJsCall(input, sessionId, forceUnModified);
|
|
}
|
|
Console.Write("Button clicked:" + input);
|
|
}
|
|
|
|
public async Task HandleJsCall(string input, string sessionId, bool forceUnmodified)
|
|
{
|
|
HtmlContent.Clear();
|
|
await InvokeAsync(StateHasChanged);
|
|
UserInput = input;
|
|
await DisplayMenuContent(input, forceUnmodified);
|
|
UserInput = string.Empty;
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
public async Task HandleVoiceCommand(string input, string sessionId)
|
|
{
|
|
// HtmlContent = string.Empty;
|
|
UserInput = input;
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
await SendUserQuery();
|
|
//UserInput = string.Empty;
|
|
}
|
|
|
|
public async Task SendUserQuery()
|
|
{
|
|
welcomeStage = false;
|
|
if (!string.IsNullOrEmpty(UserInput))
|
|
{
|
|
HtmlContent.Clear();
|
|
var menu = await GetMenuList(SiteId);
|
|
await ChatGptService.ProcessUserIntent(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu);
|
|
UserInput = string.Empty;
|
|
}
|
|
}
|
|
|
|
public async Task DisplayMenuContent(string input, bool forceUnmodified)
|
|
{
|
|
welcomeStage = false;
|
|
if (!string.IsNullOrEmpty(UserInput))
|
|
{
|
|
HtmlContent.Clear();
|
|
var menu = await GetMenuList(SiteId);
|
|
var menuItem = (await GetMenuItems(SiteId)).Where(m => m.Name == input).FirstOrDefault();
|
|
if (menuItem == null)
|
|
{
|
|
await ChatGptService.ProcessContentRequest(SessionId, input, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu, forceUnmodified);
|
|
}
|
|
else
|
|
{
|
|
await ChatGptService.ProcessContentRequest(SessionId, menuItem, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu, forceUnmodified);
|
|
}
|
|
UserInput = string.Empty;
|
|
}
|
|
}
|
|
|
|
public async Task DisplayEmailForm(string emailAddress)
|
|
{
|
|
FirstColumnClass = "col-12 col-md-6";
|
|
DocumentEmailAddress = emailAddress;
|
|
isEmailFormVisible = true;
|
|
StateHasChanged();
|
|
var result = await jsRuntime.InvokeAsync<object>("getDivContent", "currentContent");
|
|
_scopedContentService.CurrentDOM = JsonSerializer.Serialize(result);
|
|
// Console.Write($"{_scopedContentService.CurrentDOM}");
|
|
}
|
|
|
|
[JSInvokable("ProcessAudio2")]
|
|
public static async Task ProcessAudio2(string base64Audio, string SessionId)
|
|
{
|
|
|
|
Console.Write("audio incoming");
|
|
if (myHome != null)
|
|
{
|
|
if (myHome.STTEnabled == false) return;
|
|
Console.WriteLine("STT ENABLED -------------------------------------------------------------------------------");
|
|
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<string>("CredentialsPath");
|
|
Console.Write(credentialsPath);
|
|
var builder = new SpeechClientBuilder
|
|
{
|
|
CredentialsPath = credentialsPath
|
|
};
|
|
var speech = builder.Build();
|
|
|
|
byte[] audioBytes = Convert.FromBase64String(base64Audio);
|
|
myHome.HtmlContent.Clear();
|
|
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, SessionId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[JSInvokable("OpenEmailForm2")]
|
|
public static async void OpenEmailForm2(string emailAddress)
|
|
{
|
|
if (myHome != null)
|
|
{
|
|
await myHome.DisplayEmailForm(emailAddress);
|
|
}
|
|
Console.Write("openEmail with: " + emailAddress);
|
|
}
|
|
|
|
|
|
public async Task SendMessage()
|
|
{
|
|
Console.WriteLine("Button clicked!");
|
|
var menu = await GetMenuList(SiteId);
|
|
HtmlContent.Clear();
|
|
await ChatGptService.ProcessUserIntent(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu);
|
|
UserInput = string.Empty;
|
|
}
|
|
|
|
|
|
}
|
|
}
|