SeemGen/Components/Partials/GenerateFromDocument.razor

275 lines
10 KiB
Plaintext

@using BLAIzor.Models
@using Microsoft.AspNetCore.Components.Forms
@using BLAIzor.Services
@using Newtonsoft.Json
@inject ContentEditorService ContentEditorService
@inject HtmlSnippetProcessor HtmlSnippetProcessor
@inject QDrantService QDrantService
<h3>Upload Document to Generate Menu Items</h3>
<div>
<label for="fileInput">Upload a document:</label>
<InputFile id="fileInput" OnChange="HandleFileUpload" Accept=".docx" />
</div>
@if (IsLoading)
{
<p>Processing the document...</p>
}
else if (ExtractedMenuItems.Any())
{
<h4>Suggested Menu Items</h4>
<div class="row">
<div class="rz-p-sm-12">
<RadzenPanel AllowCollapse="true" class="rz-my-10 rz-mx-auto" Style="width: 100%"
Expand=@(() => Change("Panel expanded")) Collapse=@(() => Change("Panel collapsed"))>
<HeaderTemplate>
<RadzenText TextStyle="TextStyle.H6" class="rz-display-flex rz-align-items-center rz-m-0">
<RadzenIcon Icon="account_box" class="rz-me-1" /><b>Orders</b>
</RadzenText>
</HeaderTemplate>
<ChildContent>
<RadzenCard class="rz-mt-4">
<RadzenDataList PageSize="1" WrapItems="true" AllowPaging="true"
Data="@ExtractedMenuItems" TItem="MenuItemModel">
<Template Context="item">
<RadzenCard Style="width: 100%">
<h3>@item.Name</h3>
<hr style="border: none; background-color: var(--rz-text-disabled-color); height: 1px; margin: 1rem 0;" />
@{
if (MenuItemsSaved)
{
<RadzenStack Orientation="Orientation.Vertical" AlignItems="AlignItems.Center" Gap="1rem">
<input @bind="item.Name" class="form-control border-0" placeholder="Menu item name" />
<MenuItemContentEditor Subject=@subject MenuItem="item" WordFile=@document SessionId="SessionId" OnContentUpdated="UpdateMenuItem" />
</RadzenStack>
}
<RadzenStack Orientation="@Orientation.Horizontal" Gap="10px" Reverse="false" JustifyContent="@JustifyContent.Center" AlignItems="@AlignItems.Center" Wrap="@FlexWrap.Wrap" Style="height: fit-content">
<button class="btn btn-default btn-sm mt-2" @onclick="() => AddMenuItem()">Add a new menu item after this one</button>
<button class="btn btn-danger btn-sm mt-2" @onclick="() => RemoveMenuItem(item)">Remove</button>
</RadzenStack>
}
</RadzenCard>
</Template>
</RadzenDataList>
</RadzenCard>
</ChildContent>
<SummaryTemplate>
<RadzenCard class="rz-mt-4">
<b>@ExtractedMenuItems.Count() menu items</b>
</RadzenCard>
</SummaryTemplate>
</RadzenPanel>
@* <RadzenAccordion Multiple="true">
<Items>
@foreach (var item in ExtractedMenuItems)
{
<RadzenAccordionItem Text="@item.Name" Icon="account_balance_wallet">
@{
if (MenuItemsSaved)
{
<input @bind="item.Name" class="form-control border-0" placeholder="Menu item name" />
<MenuItemContentEditor Subject=@subject MenuItem="item" WordFile=@document SessionId="SessionId" OnContentUpdated="UpdateMenuItem" />
}
<div class="card-footer">
<button class="btn btn-default btn-sm mt-2" @onclick="() => AddMenuItem()">Add a new menu item after this one</button>
<button class="btn btn-danger btn-sm mt-2" @onclick="() => RemoveMenuItem(item)">Remove</button>
</div>
}
</RadzenAccordionItem>
}
</Items>
</RadzenAccordion> *@
</div>
@* @foreach (var item in menuItems)
{
<div class="col-12">
<div class="card m-2">
<div class="card-header">
<input @bind="item.Name" class="form-control border-0" placeholder="Menu Item Name" />
</div>
<div class="card-body">
<h5>Content</h5>
@{
if (MenuItemsSaved)
{
<MenuItemContentEditor Subject=@subject MenuItem="item" OnContentUpdated="UpdateMenuItem" />
}
}
</div>
<div class="card-footer">
<button class="btn btn-danger btn-sm mt-2" @onclick="() => RemoveMenuItem(item)">Remove</button>
</div>
</div>
</div>
} *@
</div>
<button class="btn btn-success mt-3" @onclick="SaveMenuItems">Save All</button>
}
else if (!string.IsNullOrEmpty(ErrorMessage))
{
<p class="text-danger">@ErrorMessage</p>
}
@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;
private string subject;
private string document;
private bool hasCollection;
private List<MenuItemModel> ExtractedMenuItems = new();
void Change(string text)
{
Console.Write($"{text}");
}
protected override async Task OnParametersSetAsync()
{
//get menu items from DB
List<MenuItem> Site = await ContentEditorService.GetMenuItemsBySiteIdAsync(SiteId);
var collectionResult = await QDrantService.GetCollectionBySiteIdAsync(SiteId);
if (!string.IsNullOrEmpty(collectionResult))
{
//create colection
hasCollection = true;
Console.Write("Has collection already");
}
if (Site.Count() > 0)
{
foreach (var menuItem in Site)
{
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<QDrantGetContentPointResult>(content)!;
if (selectedPoint != null)
{
model.Content = selectedPoint.result.payload.content;
Console.Write($"Found point: {selectedPoint.result.payload.content}");
}
}
model.Name = menuItem.Name;
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 = 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 { Name = "New menu item", Content = "" });
IsLoading = false;
}
private void UpdateMenuItem(MenuItemModel updatedItem)
{
var item = ExtractedMenuItems!.FirstOrDefault(i => i.Name == updatedItem.Name);
if (item != null)
{
item.Content = updatedItem.Content;
}
}
private async Task SaveMenuItems()
{
var result = await ContentEditorService.ProcessMenuItems(SiteId, hasCollection, ExtractedMenuItems, subject, MenuItemsSaved);
if (result == "OK")
{
MenuItemsSaved = true;
hasCollection = true;
}
else
{
//Error
}
}
}