Skip to content
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

Refactor summariser service #5

Merged
merged 1 commit into from
Oct 1, 2023
Merged
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
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>

<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
9 changes: 8 additions & 1 deletion YouTubeSummariser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F0754B28-F0B
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSummariser.ApiApp", "src\YouTubeSummariser.ApiApp\YouTubeSummariser.ApiApp.csproj", "{D2B6ACCD-125F-4AB8-B072-0F3C104AEBB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeSummariser.Services", "src\YouTubeSummariser.Services\YouTubeSummariser.Services.csproj", "{47F2E24C-23E7-4E07-B52A-7A33C5165645}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSummariser.WebApp.Wasm", "src\YouTubeSummariser.WebApp.Wasm\YouTubeSummariser.WebApp.Wasm.csproj", "{BA0E2490-C014-4325-85C3-5132FEBE1844}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YouTubeSummariser.Components", "src\YouTubeSummariser.Components\YouTubeSummariser.Components.csproj", "{8F0B64D6-E1A2-487F-8978-AA91A2732AF3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YouTubeSummariser.Components", "src\YouTubeSummariser.Components\YouTubeSummariser.Components.csproj", "{8F0B64D6-E1A2-487F-8978-AA91A2732AF3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{D2B6ACCD-125F-4AB8-B072-0F3C104AEBB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2B6ACCD-125F-4AB8-B072-0F3C104AEBB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2B6ACCD-125F-4AB8-B072-0F3C104AEBB2}.Release|Any CPU.Build.0 = Release|Any CPU
{47F2E24C-23E7-4E07-B52A-7A33C5165645}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47F2E24C-23E7-4E07-B52A-7A33C5165645}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47F2E24C-23E7-4E07-B52A-7A33C5165645}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47F2E24C-23E7-4E07-B52A-7A33C5165645}.Release|Any CPU.Build.0 = Release|Any CPU
{BA0E2490-C014-4325-85C3-5132FEBE1844}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA0E2490-C014-4325-85C3-5132FEBE1844}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA0E2490-C014-4325-85C3-5132FEBE1844}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -35,6 +41,7 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D2B6ACCD-125F-4AB8-B072-0F3C104AEBB2} = {F0754B28-F0B2-414C-83DB-06854DBF508E}
{47F2E24C-23E7-4E07-B52A-7A33C5165645} = {F0754B28-F0B2-414C-83DB-06854DBF508E}
{BA0E2490-C014-4325-85C3-5132FEBE1844} = {F0754B28-F0B2-414C-83DB-06854DBF508E}
{8F0B64D6-E1A2-487F-8978-AA91A2732AF3} = {F0754B28-F0B2-414C-83DB-06854DBF508E}
EndGlobalSection
Expand Down
6 changes: 3 additions & 3 deletions src/YouTubeSummariser.ApiApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using YouTubeSummariser.ApiApp.Configurations;
using YouTubeSummariser.ApiApp.Services;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.OpenApi.Models;

using YouTubeSummariser.Services;
using YouTubeSummariser.Services.Configurations;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
//.ConfigureFunctionsWorkerDefaults()
.ConfigureHostConfiguration(config => config.AddEnvironmentVariables())
.ConfigureServices(services =>
{
var openAISettings = services.BuildServiceProvider()

Check warning on line 25 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Dereference of a possibly null reference.

Check warning on line 25 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Dereference of a possibly null reference.
.GetService<IConfiguration>()
.GetSection(OpenAISettings.Name)
.Get<OpenAISettings>();
services.AddSingleton(openAISettings);

Check warning on line 29 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

The type 'YouTubeSummariser.Services.Configurations.OpenAISettings?' cannot be used as type parameter 'TService' in the generic type or method 'ServiceCollectionServiceExtensions.AddSingleton<TService>(IServiceCollection, TService)'. Nullability of type argument 'YouTubeSummariser.Services.Configurations.OpenAISettings?' doesn't match 'class' constraint.

Check warning on line 29 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

The type 'YouTubeSummariser.Services.Configurations.OpenAISettings?' cannot be used as type parameter 'TService' in the generic type or method 'ServiceCollectionServiceExtensions.AddSingleton<TService>(IServiceCollection, TService)'. Nullability of type argument 'YouTubeSummariser.Services.Configurations.OpenAISettings?' doesn't match 'class' constraint.

var promptSettings = services.BuildServiceProvider()

Check warning on line 31 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Dereference of a possibly null reference.

Check warning on line 31 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Dereference of a possibly null reference.
.GetService<IConfiguration>()
.GetSection(PromptSettings.Name)
.Get<PromptSettings>();
services.AddSingleton(promptSettings);

Check warning on line 35 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

The type 'YouTubeSummariser.Services.Configurations.PromptSettings?' cannot be used as type parameter 'TService' in the generic type or method 'ServiceCollectionServiceExtensions.AddSingleton<TService>(IServiceCollection, TService)'. Nullability of type argument 'YouTubeSummariser.Services.Configurations.PromptSettings?' doesn't match 'class' constraint.

Check warning on line 35 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

The type 'YouTubeSummariser.Services.Configurations.PromptSettings?' cannot be used as type parameter 'TService' in the generic type or method 'ServiceCollectionServiceExtensions.AddSingleton<TService>(IServiceCollection, TService)'. Nullability of type argument 'YouTubeSummariser.Services.Configurations.PromptSettings?' doesn't match 'class' constraint.

var endpoint = new Uri(openAISettings.Endpoint);

Check warning on line 37 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Dereference of a possibly null reference.

Check warning on line 37 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString)'.

Check warning on line 37 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Dereference of a possibly null reference.

Check warning on line 37 in src/YouTubeSummariser.ApiApp/Program.cs

View workflow job for this annotation

GitHub Actions / Build Test

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString)'.
var credential = new AzureKeyCredential(openAISettings.ApiKey);
var client = new OpenAIClient(endpoint, credential);
services.AddScoped<OpenAIClient>(_ => client);
Expand Down
20 changes: 15 additions & 5 deletions src/YouTubeSummariser.ApiApp/Triggers/SummariseHttpTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

using YouTubeSummariser.ApiApp.Models;
using YouTubeSummariser.ApiApp.Services;
using YouTubeSummariser.Services;
using YouTubeSummariser.Services.Models;

namespace YouTubeSummariser.ApiApp.Triggers;

Expand Down Expand Up @@ -78,12 +78,22 @@ public async Task<HttpResponseData> SummariseAsync([HttpTrigger(AuthorizationLev

try
{
var transcript = await this._youtube.GetTranscriptAsync(payload.VideoUrl, payload.VideoLanguageCode);
var completion = await this._openai.GetCompletionsAsync(transcript, payload.SummaryLanguageCode);

response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

var transcript = await this._youtube.GetTranscriptAsync(payload.VideoUrl, payload.VideoLanguageCode);
if (string.IsNullOrWhiteSpace(transcript) == true)
{
var message = "The given YouTube video doesn't provide transcripts.";
this._logger.LogInformation(message);

await response.WriteStringAsync(message);

return response;
}

var completion = await this._openai.GetCompletionsAsync(transcript, payload.SummaryLanguageCode);

await response.WriteStringAsync(completion);

return response;
Expand Down
6 changes: 4 additions & 2 deletions src/YouTubeSummariser.ApiApp/YouTubeSummariser.ApiApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aliencube.YouTubeSubtitlesExtractor" Version="1.0.2" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.8" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.OpenApi" Version="1.5.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.14.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\YouTubeSummariser.Services\YouTubeSummariser.Services.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace YouTubeSummariser.ApiApp.Configurations;
namespace YouTubeSummariser.Services.Configurations;

/// <summary>
/// This represents the settings entity for the OpenAI API.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace YouTubeSummariser.ApiApp.Configurations;
namespace YouTubeSummariser.Services.Configurations;

/// <summary>
/// This represents the settings entity for the prompt.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace YouTubeSummariser.ApiApp.Models;
namespace YouTubeSummariser.Services.Models;

/// <summary>
/// This represents the request model entity containing YouTube video details.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Azure.AI.OpenAI;

using YouTubeSummariser.ApiApp.Configurations;
using YouTubeSummariser.Services.Configurations;

namespace YouTubeSummariser.ApiApp.Services;
namespace YouTubeSummariser.Services;

/// <summary>
/// This provides interfaces to the <see cref="OpenAIService"/> class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Aliencube.YouTubeSubtitlesExtractor.Abstractions;

namespace YouTubeSummariser.ApiApp.Services;
namespace YouTubeSummariser.Services;

/// <summary>
/// This provides interfaces to the <see cref="YouTubeService"/> class.
Expand Down Expand Up @@ -38,10 +38,15 @@
var subtitle = await this._youtube
.ExtractSubtitleAsync(videoUrl, languageCode)
.ConfigureAwait(false);
if (subtitle == null)
{
return string.Empty;
}

var transcript = subtitle.Content
.Select(p => p.Text)
.Aggregate((a, b) => $"{a}\n{b}");

return transcript;

Check warning on line 50 in src/YouTubeSummariser.Services/Services/YouTubeService.cs

View workflow job for this annotation

GitHub Actions / Build Test

Possible null reference return.

Check warning on line 50 in src/YouTubeSummariser.Services/Services/YouTubeService.cs

View workflow job for this annotation

GitHub Actions / Build Test

Possible null reference return.
}
}
15 changes: 15 additions & 0 deletions src/YouTubeSummariser.Services/YouTubeSummariser.Services.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>

<AssemblyName>YouTubeSummariser.Services</AssemblyName>
<RootNamespace>YouTubeSummariser.Services</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aliencube.YouTubeSubtitlesExtractor" Version="1.0.2" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.8" />
</ItemGroup>

</Project>
Loading