Skip to content

Summarize user comments for image gallery with AI #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/CmsKitDemo/CmsKitDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
<UserSecretsId>5aa5f469-f212-4f04-a3e5-75c7ca424d2b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.0-preview.1.25207.5" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="9.0.0" />
Expand Down
Binary file modified src/CmsKitDemo/CmsKitDemoDb/CmsKitDemo.db
Binary file not shown.
2 changes: 2 additions & 0 deletions src/CmsKitDemo/Entities/GalleryImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class GalleryImage : CreationAuditedAggregateRoot<Guid>
public string Description { get; set; }

public Guid CoverImageMediaId { get; set; }

public string CommentsSummary { get; set; }

protected GalleryImage()
{
Expand Down
70 changes: 70 additions & 0 deletions src/CmsKitDemo/EventHandlers/GalleryImageCommentListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using CmsKitDemo.Entities;
using CmsKitDemo.Utils;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
using Volo.CmsKit.Comments;

namespace CmsKitDemo.EventHandlers;

public class GalleryImageCommentListener : ILocalEventHandler<EntityChangedEventData<Comment>>, ITransientDependency
{
private readonly IRepository<GalleryImage, Guid> _galleryImageRepository;
private readonly IRepository<Comment, Guid> _commentRepository;
private readonly AiCommentSummarizer _aiCommentSummarizer;

public GalleryImageCommentListener(
IRepository<GalleryImage, Guid> galleryImageRepository,
IRepository<Comment, Guid> commentRepository,
AiCommentSummarizer aiCommentSummarizer)
{
_galleryImageRepository = galleryImageRepository;
_commentRepository = commentRepository;
_aiCommentSummarizer = aiCommentSummarizer;
}

public async Task HandleEventAsync(EntityChangedEventData<Comment> eventData)
{
var comment = eventData.Entity;

//Here, we only interest in comments related to image gallery items
if (comment.EntityType != CmsKitDemoConsts.ImageGalleryEntityType)
{
return;
}

if (!Guid.TryParse(comment.EntityId, out var galleryImageId))
{
return;
}

// Get the related image from database
var galleryImage = await _galleryImageRepository.FindAsync(galleryImageId);
if (galleryImage == null)
{
return;
}

// Get all the comments related to the image
var queryable = await _commentRepository.GetQueryableAsync();
var allCommentTexts = await queryable
.Where(c => c.EntityType == CmsKitDemoConsts.ImageGalleryEntityType && c.EntityId == comment.EntityId)
.Select(c => c.Text)
.ToArrayAsync();

// Update the summary of comments related to the image
if (allCommentTexts.Length <= 0)
{
galleryImage.CommentsSummary = "";
}
else
{
galleryImage.CommentsSummary = await _aiCommentSummarizer.SummarizeAsync(allCommentTexts);
}

// Update the image in database
await _galleryImageRepository.UpdateAsync(galleryImage);
}
}
Loading