-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Net; | ||
using System.Net.Mime; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using YouTubeSummariser.Services; | ||
using YouTubeSummariser.Services.Models; | ||
|
||
namespace YouTubeSummariser.ApiApp2.Controllers; | ||
[ApiController] | ||
[Route("api")] | ||
public class SummariseController : ControllerBase | ||
{ | ||
private readonly IOpenAIService _openai; | ||
private readonly IYouTubeService _youtube; | ||
private readonly ILogger<SummariseController> _logger; | ||
|
||
public SummariseController(IOpenAIService openai, IYouTubeService youtube, ILogger<SummariseController> logger) | ||
{ | ||
this._openai = openai ?? throw new ArgumentNullException(nameof(openai)); | ||
this._youtube = youtube ?? throw new ArgumentNullException(nameof(youtube)); | ||
this._logger = logger; | ||
} | ||
|
||
[HttpPost("summarise", Name = "summarise")] | ||
[Consumes(typeof(SummariseRequestModel), MediaTypeNames.Application.Json)] | ||
[Produces(typeof(string))] | ||
[ProducesResponseType((int)HttpStatusCode.BadRequest)] | ||
[ProducesResponseType((int)HttpStatusCode.InternalServerError)] | ||
public async Task<IActionResult> SummariseAsync([FromBody] SummariseRequestModel req) | ||
{ | ||
this._logger.LogInformation("C# HTTP trigger function processed a request."); | ||
Check warning on line 32 in src/YouTubeSummariser.ApiApp2/Controllers/SummariseController.cs GitHub Actions / Build Test
|
||
|
||
var response = default(IActionResult); | ||
var payload = req; | ||
if (payload == null) | ||
{ | ||
this._logger.LogError("Payload is null."); | ||
Check warning on line 38 in src/YouTubeSummariser.ApiApp2/Controllers/SummariseController.cs GitHub Actions / Build Test
|
||
|
||
response = new StatusCodeResult((int)HttpStatusCode.BadRequest); | ||
return response; | ||
} | ||
if (string.IsNullOrWhiteSpace(payload.VideoUrl) == true) | ||
{ | ||
this._logger.LogError("Video URL is null or empty."); | ||
Check warning on line 45 in src/YouTubeSummariser.ApiApp2/Controllers/SummariseController.cs GitHub Actions / Build Test
|
||
|
||
response = new StatusCodeResult((int)HttpStatusCode.BadRequest); | ||
return response; | ||
} | ||
if (string.IsNullOrWhiteSpace(payload.VideoLanguageCode) == true) | ||
{ | ||
payload.VideoLanguageCode = "en"; | ||
} | ||
if (string.IsNullOrWhiteSpace(payload.SummaryLanguageCode) == true) | ||
{ | ||
payload.SummaryLanguageCode = payload.VideoLanguageCode; | ||
} | ||
|
||
try | ||
{ | ||
var result = new ContentResult() { ContentType = MediaTypeNames.Text.Plain, StatusCode = (int)HttpStatusCode.OK }; | ||
|
||
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); | ||
Check warning on line 67 in src/YouTubeSummariser.ApiApp2/Controllers/SummariseController.cs GitHub Actions / Build Test
|
||
|
||
result.Content = message; | ||
response = result; | ||
|
||
return response; | ||
} | ||
|
||
var completion = await this._openai.GetCompletionsAsync(transcript, payload.SummaryLanguageCode); | ||
|
||
result.Content = completion; | ||
response = result; | ||
|
||
return response; | ||
} | ||
catch (Exception ex) | ||
{ | ||
this._logger.LogError(ex, ex.Message); | ||
|
||
response = new StatusCodeResult((int)HttpStatusCode.InternalServerError); | ||
return response; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Aliencube.YouTubeSubtitlesExtractor; | ||
using Aliencube.YouTubeSubtitlesExtractor.Abstractions; | ||
|
||
using Azure; | ||
using Azure.AI.OpenAI; | ||
|
||
using YouTubeSummariser.Services; | ||
using YouTubeSummariser.Services.Configurations; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var openAISettings = builder.Configuration | ||
.GetSection(OpenAISettings.Name) | ||
.Get<OpenAISettings>(); | ||
builder.Services.AddSingleton(openAISettings); | ||
Check warning on line 22 in src/YouTubeSummariser.ApiApp2/Program.cs GitHub Actions / Build Test
|
||
|
||
var promptSettings = builder.Configuration | ||
.GetSection(PromptSettings.Name) | ||
.Get<PromptSettings>(); | ||
builder.Services.AddSingleton(promptSettings); | ||
Check warning on line 27 in src/YouTubeSummariser.ApiApp2/Program.cs GitHub Actions / Build Test
|
||
|
||
var endpoint = new Uri(openAISettings.Endpoint); | ||
Check warning on line 29 in src/YouTubeSummariser.ApiApp2/Program.cs GitHub Actions / Build Test
|
||
var credential = new AzureKeyCredential(openAISettings.ApiKey); | ||
var client = new OpenAIClient(endpoint, credential); | ||
builder.Services.AddScoped<OpenAIClient>(_ => client); | ||
|
||
builder.Services.AddHttpClient(); | ||
builder.Services.AddScoped<IYouTubeVideo, YouTubeVideo>(); | ||
builder.Services.AddScoped<IYouTubeService, YouTubeService>(); | ||
builder.Services.AddScoped<IOpenAIService, OpenAIService>(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
|
||
app.Run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:5000", | ||
"sslPort": 5001 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>YouTubeSummariser.ApiApp2</AssemblyName> | ||
<RootNamespace>YouTubeSummariser.ApiApp2</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\YouTubeSummariser.Services\YouTubeSummariser.Services.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"OpenAI": { | ||
"Endpoint": "https://aoai-summariser4276.openai.azure.com/", | ||
"ApiKey": "48774d2dadd14214ac79c0997ce87f0c", | ||
"DeploymentId": "model-gpt432k" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"OpenAI": { | ||
"Endpoint": "{{AOAI_ENDPOINT_URL}}", | ||
"ApiKey": "{{AOAI_API_KEY}}", | ||
"DeploymentId": "{{AOAI_DEPLOYMENT_ID}}" | ||
}, | ||
"Prompt": { | ||
"System": "You are the expert of summarising long contents. You are going to summarise the following YouTube video transcript in a given language code.", | ||
"MaxTokens": 3000, | ||
"Temperature": 0.7 | ||
}, | ||
"AllowedHosts": "*" | ||
} |