@using BLAIzor.Models @using Microsoft.AspNetCore.Components.Forms @using BLAIzor.Services @using Newtonsoft.Json @using System.Collections.ObjectModel @inject ContentEditorService ContentEditorService @inject HtmlSnippetProcessor HtmlSnippetProcessor @inject QDrantService QDrantService @inject IJSRuntime JSRuntime

Upload Document to Generate Menu Items

@if (IsLoading) {

Processing the document...

} else if (ExtractedMenuItems.Any()) {

Menu Items

@{ @(allowReorder ? "Done" : "Reorder") } @if(allowReorder) { @* *@ @* *@ } else { @foreach (var item in ExtractedMenuItems) { @{ if (MenuItemsSaved) { } } } }
@* @foreach (var item in menuItems) {
Content
@{ if (MenuItemsSaved) { } }
} *@
} else if (!string.IsNullOrEmpty(ErrorMessage)) {

@ErrorMessage

} @code { [Parameter] public int SiteId { get; set; } [Parameter] public string SessionId { get; set; } private IBrowserFile? UploadedFile; private bool IsLoading = false; private string? ErrorMessage; private bool MenuItemsSaved = false; ObservableCollection menuItems; IList selectedMenuItems; private string subject; private string document; private bool hasCollection; private List ExtractedMenuItems = new(); private bool allowReorder = false; private RadzenDataGrid dataGrid; MenuItem draggedItem; MenuItemModel SelectedMenuItemModel = new(""); void Change(string text) { Console.Write($"{text}"); } protected override async Task OnParametersSetAsync() { //get menu items from DB menuItems = new ObservableCollection((await ContentEditorService.GetMenuItemsBySiteIdAsync(SiteId)).OrderBy(x => x.SortOrder)); var collectionResult = await QDrantService.GetCollectionBySiteIdAsync(SiteId); if (!string.IsNullOrEmpty(collectionResult)) { //create colection hasCollection = true; Console.Write("Has collection already"); } if (menuItems.Count() > 0) { foreach (var menuItem in menuItems) { string content; MenuItemModel model = new MenuItemModel(""); //try to get content from qDrant if (menuItem.QdrantPointId != null) { content = await QDrantService.GetContentAsync(SiteId, menuItem.SortOrder); var selectedPoint = JsonConvert.DeserializeObject(content)!; if (selectedPoint != null) { model.Content = selectedPoint.result.payload.content; Console.Write($"Found point: {selectedPoint.result.payload.content}"); } } model.MenuItem = menuItem; ExtractedMenuItems.Add(model); // UpdateMenuItem(model); } MenuItemsSaved = true; } //get menu content from QDrant for each menu item } private async Task HandleFileUpload(InputFileChangeEventArgs e) { UploadedFile = e.File; if (UploadedFile == null) { ErrorMessage = "Please upload a valid .docx file."; return; } IsLoading = true; ErrorMessage = null; try { // Read the uploaded .docx file using var stream = UploadedFile.OpenReadStream(maxAllowedSize: 100 * 1024 * 1024); // 10MB limit using var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); // Load the .docx file with DocX document = WordFileReader.ExtractText(memoryStream); //get website subject var prompt1 = $"Analyze the following text and make an assumption about the characteristics of the company. Do not attach any explanation, make your answer like `A medical clinic of mammography` or `A webdesign and software development company` :\n\n{document}"; var response1 = await ContentEditorService.GetGeneratedContentAsync(SessionId, prompt1); Console.Write(response1); subject = response1; if (!MenuItemsSaved) { var prompt2 = $"Analyze the following text and based on the sections suggest a list of menu items for a website. Do not attach any explanation. Text:\n\n`{document}`."; var response2 = await ContentEditorService.GetMenuSuggestionsAsync(SessionId, prompt2); ExtractedMenuItems = response2.Select(name => new MenuItemModel(name)).ToList(); } // Send the content to ChatGPT } catch (Exception ex) { ErrorMessage = "An error occurred while processing the document."; Console.Write(ex.Message); } finally { IsLoading = false; } } private void RemoveMenuItem(MenuItemModel item) { ExtractedMenuItems!.Remove(item); } private async Task AddMenuItem() { IsLoading = true; ErrorMessage = null; ExtractedMenuItems.Add(new MenuItemModel ("New menu item", "" )); IsLoading = false; } private void UpdateMenuItem(MenuItemModel updatedItem) { var item = ExtractedMenuItems!.FirstOrDefault(i => i.MenuItem.Id == updatedItem.MenuItem.Id); if (item != null) { //item.MenuItem. item.Content = updatedItem.Content; } } private async Task SaveMenuItems(bool updateVectorDatabase) { //bool valami = updateVectorDatabase; var result = await ContentEditorService.ProcessMenuItems(SiteId, hasCollection, ExtractedMenuItems, subject, MenuItemsSaved, updateVectorDatabase); if (result == "OK") { MenuItemsSaved = true; hasCollection = true; } else { //Error } } void RowRender(RowRenderEventArgs args) { if (allowReorder) { args.Attributes.Add("title", "Drag row to reorder"); args.Attributes.Add("style", "cursor:grab"); args.Attributes.Add("draggable", "true"); args.Attributes.Add("ondragover", "event.preventDefault();event.target.closest('.rz-data-row').classList.add('my-class')"); args.Attributes.Add("ondragleave", "event.target.closest('.rz-data-row').classList.remove('my-class')"); args.Attributes.Add("ondragstart", EventCallback.Factory.Create(this, () => draggedItem = args.Data)); args.Attributes.Add("ondrop", EventCallback.Factory.Create(this, () => { var draggedIndex = menuItems.IndexOf(draggedItem); MenuItemModel draggedExtractedMenuItem = ExtractedMenuItems[draggedIndex]; var droppedIndex = menuItems.IndexOf(args.Data); menuItems.Remove(draggedItem); menuItems.Insert(draggedIndex <= droppedIndex ? droppedIndex++ : droppedIndex, draggedItem); ExtractedMenuItems.Remove(draggedExtractedMenuItem); ExtractedMenuItems.Insert(draggedIndex <= droppedIndex ? droppedIndex++ : droppedIndex, draggedExtractedMenuItem); foreach (var menuItemToReorder in menuItems) { menuItemToReorder.SortOrder = menuItems.IndexOf(menuItemToReorder); } foreach (var extractedMenuItemToReorder in ExtractedMenuItems) { extractedMenuItemToReorder.MenuItem.SortOrder = ExtractedMenuItems.IndexOf(extractedMenuItemToReorder); } JSRuntime.InvokeVoidAsync("eval", $"document.querySelector('.my-class').classList.remove('my-class')"); })); } } void Select(MenuItem menuItem) { SelectedMenuItemModel.MenuItem.Name = menuItem.Name; SelectedMenuItemModel.Content = ExtractedMenuItems.Where(x => x.MenuItem.Name == menuItem.Name).FirstOrDefault().Content; SelectedMenuItemModel.MenuItem.SortOrder = menuItem.SortOrder; SelectedMenuItemModel.MenuItem.ShowInMainMenu = menuItem.ShowInMainMenu; } void OnRowSelect(MenuItem menuItem) { Select(menuItem); } async void ToggleReorder() { if(allowReorder) { await SaveMenuItems(false); } allowReorder = !allowReorder; await InvokeAsync(StateHasChanged); } }