Skip to content

Commit

Permalink
Add AI pretranslation method and data handler for prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
ce-nistal committed Oct 3, 2024
1 parent 5e8b17a commit d00bb35
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Apps.Crowdin/Actions/TranslationActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public async Task<PreTranslationEntity> PreTranslate(

var client = new CrowdinClient(Creds);

PreTranslationMethod? method = input.Method is null ? null : input.Method == "Mt" ? PreTranslationMethod.Mt : PreTranslationMethod.Tm;
PreTranslationMethod? method = input.Method is null ? null :
input.Method == "Mt" ? PreTranslationMethod.Mt :
input.Method == "Tm" ? PreTranslationMethod.Tm :
PreTranslationMethod.Ai;
AutoApproveOption? option = input.AutoApproveOption is null ? null : input.AutoApproveOption == "None" ? AutoApproveOption.None : input.AutoApproveOption == "All" ? AutoApproveOption.All : input.AutoApproveOption == "ExceptAutoSubstituted" ? AutoApproveOption.ExceptAutoSubstituted : AutoApproveOption.PerfectMatchOnly;

var request = new ApplyPreTranslationRequest
Expand All @@ -51,6 +54,7 @@ public async Task<PreTranslationEntity> PreTranslate(
FileIds = input.FileIds.Select(fileId => IntParser.Parse(fileId, nameof(fileId))!.Value).ToList(),
EngineId = intEngineId,
Method = method,
AiPromptId = input.aiPromptId is null ? null : IntParser.Parse(input.aiPromptId, nameof(input.aiPromptId)),
AutoApproveOption = option,
DuplicateTranslations = input.DuplicateTranslations,
TranslateUntranslatedOnly = input.TranslateUntranslatedOnly,
Expand Down
2 changes: 1 addition & 1 deletion Apps.Crowdin/Apps.Crowdin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.8.0" />
<PackageReference Include="Blackbird.Applications.SDK.Extensions.FileManagement" Version="1.0.1" />
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.25" />
<PackageReference Include="Crowdin.Api" Version="2.23.1" />
<PackageReference Include="Crowdin.Api" Version="2.25.0" />
<PackageReference Include="MimeTypes" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
55 changes: 55 additions & 0 deletions Apps.Crowdin/DataSourceHandlers/AiPromptIdDataHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Apps.Crowdin.Api.RestSharp;
using Apps.Crowdin.Models.Entities;
using Apps.Crowdin.Models.Request;
using Apps.Crowdin.Models.Request.Project;
using Apps.Crowdin.Models.Response;
using Apps.Crowdin.Utils;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;
using Crowdin.Api;
using Newtonsoft.Json;
using RestSharp;

namespace Apps.Crowdin.DataSourceHandlers;

public class AiPromptIdDataHandler(
InvocationContext invocationContext,
[ActionParameter] ProjectRequest project,
[ActionParameter][Display("User ID")] string? UserId)

: BaseInvocable(invocationContext), IAsyncDataSourceHandler
{
private AuthenticationCredentialsProvider[] Creds =>
InvocationContext.AuthenticationCredentialsProviders.ToArray();

public async Task<Dictionary<string, string>> GetDataAsync(DataSourceContext context,
CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(project.ProjectId) || string.IsNullOrEmpty(UserId))
throw new("You should input Project ID and User ID first");

var client = new CrowdinRestClient();

var items = await Paginator.Paginate(async (lim, offset)
=>
{
var request =
new CrowdinRestRequest(
$"/users/{UserId}/ai/prompts?limit={lim}&offset={offset}",
Method.Get, Creds);
request.AddQueryParameter("projectId", project.ProjectId);
request.AddQueryParameter("action", "pre_translate");
var response = await client.ExecuteAsync(request, cancellationToken: cancellationToken);
return JsonConvert.DeserializeObject<ResponseList<DataResponse<AiPromptEntity>>>(response.Content);
});

return items
.Select(x => x.Data)
.Where(x => context.SearchString == null ||
x.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase))
.Take(20)
.ToDictionary(x => x.Id.ToString(), x => x.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class PreTranslationMethodHandler : IStaticDataSourceHandler
{
{ "Mt", "Machine translation" },
{ "Tm", "Translation memory" },
{ "Ai", "Artificial intelligence" }
};
}
}
14 changes: 14 additions & 0 deletions Apps.Crowdin/Models/Entities/AiPromptEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.Crowdin.Models.Entities
{
public class AiPromptEntity
{
public string Id { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public class PreTranslateRequest
[Display("Pre translation method")]
public string? Method { get; set; }

[DataSource(typeof(AiPromptIdDataHandler))]
[Display("AI prompt ID")]
public string? aiPromptId { get; set; }

}

0 comments on commit d00bb35

Please sign in to comment.