Skip to content

Commit

Permalink
Merge pull request #201 from pticostaricags/development
Browse files Browse the repository at this point in the history
merge "development" into "main"
  • Loading branch information
efonsecab authored Nov 11, 2024
2 parents aae556f + 193c25a commit e00ad66
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class OpenAIExtensions
{
public static void AddOpenAI(this WebApplicationBuilder builder)
{
builder.Services.AddTransient<OpenAIServiceConfiguration>(sp =>
builder.Services.AddSingleton<OpenAIServiceConfiguration>(sp =>
{
IDbContextFactory<FairPlayCombinedDbContext> dbContextFactory =
sp.GetRequiredService<IDbContextFactory<FairPlayCombinedDbContext>>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@page "/Creator/VideoChapters/{VideoInfoId:long}"
@implements IDisposable
@implements IAsyncDisposable

@attribute [Authorize(Roles = FairPlayCombined.Common.Constants.RoleName.BasicPlanUser)]

Expand All @@ -11,25 +11,19 @@
@using FairPlayCombined.Models.Pagination
@using FairPlayTube.SharedUI.Components.Spinners

@inject IJSRuntime jsRuntime
@inject IVideoInfoService videoInfoService
@inject IUserProviderService userProviderService
@inject IOpenAIService openAIService
@inject IVideoCaptionsService videoCaptionsService

<PageTitle>
Video Chapters @(videoInfoModel != null ? $"- {videoInfoModel.Name}" : String.Empty)
@Localizer![VideoChaptersTextKey] @(VideoInfoModel != null ? $"- {VideoInfoModel.Name}" : String.Empty)
</PageTitle>

<FluentLabel Typo="Typography.H3">
Video Chapters @(videoInfoModel != null ? $"- {videoInfoModel.Name}" : String.Empty)
@Localizer![VideoChaptersTextKey] @(VideoInfoModel != null ? $"- {VideoInfoModel.Name}" : String.Empty)
</FluentLabel>

<LoadingIndicator ShowSpinners="this.IsBusy"></LoadingIndicator>

<div>
<FluentButton Type="ButtonType.Button" OnClick="OnRecreatePlanButtonclickedAsync">
Re-Create Chapters
@Localizer![ReCreateChaptersTextKey]
</FluentButton>
</div>
<div>
Expand All @@ -39,55 +33,4 @@
@((MarkupString)this.YouTubeChapters)
</p>
}
</div>

@code
{
[Parameter]
public long? VideoInfoId { get; set; }
private VideoInfoModel? videoInfoModel { get; set; }
private bool IsBusy { get; set; }
private readonly CancellationTokenSource cancellationTokenSource = new();
private string? YouTubeChapters { get; set; }
private string? RevisedPrompt { get; set; }

protected override async Task OnInitializedAsync()
{
this.IsBusy = true;
StateHasChanged();
this.videoInfoModel = await this.videoInfoService.GetVideoInfoByIdAsync(id: this.VideoInfoId!.Value,
cancellationToken: this.cancellationTokenSource.Token);
StateHasChanged();
await this.GenerateYouTubeChaptersAsync(videoInfoModel);
this.IsBusy = false;
StateHasChanged();
}

private async Task OnRecreatePlanButtonclickedAsync()
{
this.IsBusy = true;
StateHasChanged();
await this.GenerateYouTubeChaptersAsync(this.videoInfoModel!);
this.IsBusy = false;
StateHasChanged();
}

private async Task GenerateYouTubeChaptersAsync(VideoInfoModel videoInfoModel)
{
var englishCaptions = await this.videoCaptionsService
.GetVideoCaptionsByVideoInfoIdAndLanguageAsync(videoInfoId: videoInfoModel.VideoInfoId,
language: "en-US", cancellationToken: this.cancellationTokenSource.Token);
var systemMessage = "You will take the role of an expert in YouTube SEO. I will give you the information for one of my videos. Your job is to generate the repective YouTube Chapters, timestamp must be in format: hh:mm:ss, do not add milliseconds, always start with 00:00:00. Return them as an HTML 5 bullet list.";
var userMessage = $"Video Title: {videoInfoModel.Description}. Video Captions: {englishCaptions}";
var result = await this.openAIService.GenerateChatCompletionAsync(systemMessage, userMessage, this.cancellationTokenSource.Token);
if (result != null)
{
this.YouTubeChapters = result!.choices![0]!.message!.content!;
}
}

void IDisposable.Dispose()
{
this.cancellationTokenSource.Dispose();
}
}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using FairPlayCombined.Common.CustomAttributes;
using FairPlayCombined.Interfaces.Common;
using FairPlayCombined.Interfaces.FairPlayTube;
using FairPlayCombined.Models.FairPlayTube.VideoInfo;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;

namespace FairPlayTube.SharedUI.Components.Pages.Creator
{
public partial class VideoChapters
{
[Parameter]
public long? VideoInfoId { get; set; }
[Inject] IVideoInfoService? VideoInfoService { get; set; }
[Inject] IOpenAIService? OpenAIService { get; set; }
[Inject] IVideoCaptionsService? VideoCaptionsService { get; set; }
[Inject] IStringLocalizer<VideoChapters>? Localizer { get; set; }
private VideoInfoModel? VideoInfoModel { get; set; }
private bool IsBusy { get; set; }
private readonly CancellationTokenSource cancellationTokenSource = new();
private string? YouTubeChapters { get; set; }

protected override async Task OnInitializedAsync()
{
this.IsBusy = true;
StateHasChanged();
this.VideoInfoModel = await this.VideoInfoService!.GetVideoInfoByIdAsync(id: this.VideoInfoId!.Value,
cancellationToken: this.cancellationTokenSource.Token);
StateHasChanged();
await this.GenerateYouTubeChaptersAsync(VideoInfoModel);
this.IsBusy = false;
StateHasChanged();
}

private async Task OnRecreatePlanButtonclickedAsync()
{
this.IsBusy = true;
StateHasChanged();
await this.GenerateYouTubeChaptersAsync(this.VideoInfoModel!);
this.IsBusy = false;
StateHasChanged();
}

private async Task GenerateYouTubeChaptersAsync(VideoInfoModel videoInfoModel)
{
var englishCaptions = await this.VideoCaptionsService!
.GetVideoCaptionsByVideoInfoIdAndLanguageAsync(videoInfoId: videoInfoModel.VideoInfoId,
language: "en-US", cancellationToken: this.cancellationTokenSource.Token);
var systemMessage = "You will take the role of an expert in YouTube SEO. I will give you the information for one of my videos. Your job is to generate the repective YouTube Chapters, timestamp must be in format: hh:mm:ss, do not add milliseconds, always start with 00:00:00. Return them as an HTML 5 bullet list.";
var userMessage = $"Video Title: {videoInfoModel.Description}. Video Captions: {englishCaptions}";
var result = await this.OpenAIService!.GenerateChatCompletionAsync(systemMessage, userMessage, this.cancellationTokenSource.Token);
if (result != null)
{
this.YouTubeChapters = result!.choices![0]!.message!.content!;
}
}

public async ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
await this.cancellationTokenSource.CancelAsync();
this.cancellationTokenSource.Dispose();
}

#region Resource Keys
[ResourceKey(defaultValue: "Video Chapters")]
public const string VideoChaptersTextKey = "VideoChaptersText";
[ResourceKey(defaultValue: "Re-Create Chapters")]
public const string ReCreateChaptersTextKey = "ReCreateChaptersText";
#endregion Resource Keys
}
}

0 comments on commit e00ad66

Please sign in to comment.