1
+ using FairPlayCombined . Common ;
2
+ using FairPlayCombined . Common . CustomAttributes ;
3
+ using FairPlayCombined . Common . GeneratorsAttributes ;
4
+ using FairPlayCombined . Interfaces ;
5
+ using FairPlayCombined . Interfaces . Common ;
6
+ using FairPlayCombined . Interfaces . FairPlayTube ;
7
+ using FairPlayCombined . Models . FairPlayTube . NewVideoRecommendation ;
8
+ using FairPlayCombined . Models . FairPlayTube . VideoInfo ;
9
+ using FairPlayCombined . Models . Pagination ;
10
+ using Microsoft . AspNetCore . Components ;
11
+ using Microsoft . Extensions . Localization ;
12
+ using Microsoft . FluentUI . AspNetCore . Components ;
13
+ using Microsoft . JSInterop ;
14
+
15
+ namespace FairPlayTube . SharedUI . Components . Pages . Creator
16
+ {
17
+ public partial class NewVideoRecommendation
18
+ {
19
+ [ Inject ] IJSRuntime ? JsRuntime { get ; set ; }
20
+ [ Inject ] IVideoInfoService ? VideoInfoService { get ; set ; }
21
+ [ Inject ] IUserProviderService ? UserProviderService { get ; set ; }
22
+ [ Inject ] IOpenAIService ? OpenAIService { get ; set ; }
23
+ [ Inject ] IVideoCaptionsService ? VideoCaptionsService { get ; set ; }
24
+ [ Inject ] IPromptGeneratorService ? PromptGeneratorService { get ; set ; }
25
+ [ Inject ] IToastService ? ToastService { get ; set ; }
26
+ [ Inject ] INewVideoRecommendationService ? NewVideoRecommendationService { get ; set ; }
27
+ [ Inject ] IStringLocalizer < NewVideoRecommendation > ? Localizer { get ; set ; }
28
+ private bool IsBusy { get ; set ; }
29
+ private readonly CancellationTokenSource cancellationTokenSource = new ( ) ;
30
+ private string ? NewVideoRecommendationIdea { get ; set ; }
31
+ private GridItemsProvider < NewVideoRecommendationModel > ? ItemsProvider { get ; set ; }
32
+ private readonly PaginationState paginationState = new ( )
33
+ {
34
+ ItemsPerPage = Constants . Pagination . PageSize
35
+ } ;
36
+
37
+ protected override void OnInitialized ( )
38
+ {
39
+ base . OnInitialized ( ) ;
40
+ if ( this . ItemsProvider is null )
41
+ {
42
+ ItemsProvider = async req =>
43
+ {
44
+ this . IsBusy = true ;
45
+ StateHasChanged ( ) ;
46
+ PaginationRequest paginationRequest = new ( )
47
+ {
48
+ PageSize = Constants . Pagination . PageSize ,
49
+ StartIndex = req . StartIndex ,
50
+ SortingItems =
51
+ [
52
+ new SortingItem ( )
53
+ {
54
+ PropertyName = nameof ( NewVideoRecommendationModel . NewVideoRecommendationId ) ,
55
+ SortType = SortType . Descending
56
+ }
57
+ ]
58
+ } ;
59
+ var items = await this . NewVideoRecommendationService !
60
+ . GetPaginatedNewVideoRecommendationForUserIdAsync (
61
+ paginationRequest , this . UserProviderService ! . GetCurrentUserId ( ) ! , this . cancellationTokenSource . Token ) ;
62
+ var result = GridItemsProviderResult . From ( items ! . Items ! , items . TotalItems ) ;
63
+ this . IsBusy = false ;
64
+ StateHasChanged ( ) ;
65
+ return result ;
66
+ } ;
67
+ }
68
+ }
69
+
70
+ private async Task OnCreateNewVideoRecommendationButtonclickedAsync ( )
71
+ {
72
+ try
73
+ {
74
+ this . IsBusy = true ;
75
+ StateHasChanged ( ) ;
76
+ var promptModel = await this . PromptGeneratorService !
77
+ . GetPromptCompleteInfoAsync ( Constants . PromptsNames . CreateNewVideoRecommendationIdea ,
78
+ this . cancellationTokenSource . Token ) ;
79
+ string currentUserId = this . UserProviderService ! . GetCurrentUserId ( ) ! ;
80
+ var videos = await this . VideoInfoService ! . GetPaginatedCompletedVideoInfobyUserIdAsync ( new PaginationRequest ( )
81
+ {
82
+ PageSize = 5 ,
83
+ SortingItems = new [ ]
84
+ {
85
+ new SortingItem ( )
86
+ {
87
+ PropertyName = nameof ( VideoInfoModel . VideoInfoId ) ,
88
+ SortType = SortType . Descending
89
+ }
90
+ } ,
91
+ StartIndex = 0
92
+ } ,
93
+ currentUserId ,
94
+ this . cancellationTokenSource . Token ) ;
95
+
96
+ var titles = videos ! . Items ! . Select ( p => $ "* Title: { p . Name } . Keywords: { String . Join ( "," , p . VideoKeywords ! ) } . Topics: { String . Join ( "," , p . VideoTopics ! ) } . Video Captions: { String . Join ( "," , p . EnglishCaptions ) } \r \n ") ;
97
+ var userMessage = $ "Video Titles: { String . Join ( "." , titles ) } .";
98
+ var result = await this . OpenAIService ! . GenerateChatCompletionAsync ( promptModel ! . BaseText ! ,
99
+ userMessage , this . cancellationTokenSource . Token ) ;
100
+ if ( result != null )
101
+ {
102
+ this . NewVideoRecommendationIdea = result ! . choices ! [ 0 ] ! . message ! . content ! ;
103
+ await NewVideoRecommendationService ! . CreateNewVideoRecommendationAsync (
104
+ createModel : new ( )
105
+ {
106
+ ApplicationUserId = currentUserId ,
107
+ HtmlNewVideoRecommendation = this . NewVideoRecommendationIdea
108
+ } ,
109
+ this . cancellationTokenSource . Token ) ;
110
+ await this . paginationState . SetCurrentPageIndexAsync ( this . paginationState . CurrentPageIndex ) ;
111
+ }
112
+ }
113
+ catch ( Exception ex )
114
+ {
115
+ this . ToastService ! . ShowError ( ex . Message ) ;
116
+ }
117
+ finally
118
+ {
119
+ this . IsBusy = false ;
120
+ StateHasChanged ( ) ;
121
+ }
122
+ }
123
+
124
+ public async ValueTask DisposeAsync ( )
125
+ {
126
+ await this . cancellationTokenSource . CancelAsync ( ) ;
127
+ this . cancellationTokenSource . Dispose ( ) ;
128
+ }
129
+
130
+ #region Resource Keys
131
+ [ ResourceKey ( defaultValue : "New Video Recommendations" ) ]
132
+ public const string NewVideoRecommendationsTextKey = "NewVideoRecommendationsText" ;
133
+ [ ResourceKey ( defaultValue : "Create New Recommendation" ) ]
134
+ public const string CreateNewRecommendationTextKey = "CreateNewRecommendationText" ;
135
+ [ ResourceKey ( defaultValue : "Expand to see the content" ) ]
136
+ public const string ExpandToSeeContentTextKey = "ExpandToSeeContentText" ;
137
+ #endregion Resource Keys
138
+ }
139
+ }
0 commit comments