Skip to content

Commit

Permalink
ElevenLabs-DotNet 3.0.2 (#59)
Browse files Browse the repository at this point in the history
- Cleanup and polish for Dubbing API
  • Loading branch information
StephenHodgson authored Sep 3, 2024
1 parent e4955af commit e642904
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
45 changes: 20 additions & 25 deletions ElevenLabs-DotNet/Dubbing/DubbingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ namespace ElevenLabs.Dubbing
/// </summary>
public sealed class DubbingEndpoint(ElevenLabsClient client) : ElevenLabsBaseEndPoint(client)
{
private const string DubbingId = "dubbing_id";
private const string ExpectedDurationSecs = "expected_duration_sec";

protected override string Root => "dubbing";

/// <summary>
/// Dubs provided audio or video file into given language.
/// </summary>
/// <param name="request">The <see cref="DubbingRequest"/> containing dubbing configuration and files.</param>
/// <param name="progress"></param>
/// <param name="progress"><see cref="IProgress{DubbingProjectMetadata}"/> progress callback.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <param name="maxRetries"></param>
/// <param name="pollingInterval"></param>
/// <returns> <see cref="DubbingProjectMetadata"/>.</returns>
/// <param name="maxRetries">Optional, number of retry attempts when polling.</param>
/// <param name="pollingInterval">Optional, <see cref="TimeSpan"/> between making requests.</param>
/// <returns><see cref="DubbingProjectMetadata"/>.</returns>
public async Task<DubbingProjectMetadata> DubAsync(DubbingRequest request, int? maxRetries = null, TimeSpan? pollingInterval = null, IProgress<DubbingProjectMetadata> progress = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(request);
using var payload = new MultipartFormDataContent();

try
{
foreach (var (fileName, mediaType, stream) in request.Files)
if (request.Files != null)
{
await payload.AppendFileToFormAsync("file", stream, fileName, new(mediaType), cancellationToken);
foreach (var (fileName, mediaType, stream) in request.Files)
{
await payload.AppendFileToFormAsync("file", stream, fileName, new(mediaType), cancellationToken);
}
}

if (!string.IsNullOrEmpty(request.ProjectName))
Expand Down Expand Up @@ -97,8 +97,7 @@ public async Task<DubbingProjectMetadata> DubAsync(DubbingRequest request, int?
using var response = await client.Client.PostAsync(GetUrl(), payload, cancellationToken).ConfigureAwait(false);
var responseBody = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
var dubResponse = JsonSerializer.Deserialize<DubbingResponse>(responseBody);
var metadata = await WaitForDubbingCompletionAsync(dubResponse, maxRetries ?? 60, pollingInterval ?? TimeSpan.FromSeconds(dubResponse.ExpectedDurationSeconds), pollingInterval == null, progress, cancellationToken);
return metadata;
return await WaitForDubbingCompletionAsync(dubResponse, maxRetries ?? 60, pollingInterval ?? TimeSpan.FromSeconds(dubResponse.ExpectedDurationSeconds), pollingInterval == null, progress, cancellationToken);
}

private async Task<DubbingProjectMetadata> WaitForDubbingCompletionAsync(DubbingResponse dubbingResponse, int maxRetries, TimeSpan pollingInterval, bool adjustInterval, IProgress<DubbingProjectMetadata> progress = null, CancellationToken cancellationToken = default)
Expand All @@ -122,14 +121,14 @@ private async Task<DubbingProjectMetadata> WaitForDubbingCompletionAsync(Dubbing

if (metadata.Status.Equals("dubbing", StringComparison.Ordinal))
{
if (EnableDebug)
if (adjustInterval && pollingInterval.TotalSeconds > 0.5f)
{
Console.WriteLine($"Dubbing for {dubbingResponse.DubbingId} in progress... Will check status again in {pollingInterval.TotalSeconds} seconds.");
pollingInterval = TimeSpan.FromSeconds(dubbingResponse.ExpectedDurationSeconds / Math.Pow(2, i));
}

if (adjustInterval)
if (EnableDebug)
{
pollingInterval = TimeSpan.FromSeconds(dubbingResponse.ExpectedDurationSeconds / Math.Pow(2, i));
Console.WriteLine($"Dubbing for {dubbingResponse.DubbingId} in progress... Will check status again in {pollingInterval.TotalSeconds} seconds.");
}

await Task.Delay(pollingInterval, cancellationToken).ConfigureAwait(false);
Expand All @@ -146,7 +145,7 @@ private async Task<DubbingProjectMetadata> WaitForDubbingCompletionAsync(Dubbing
/// <summary>
/// Returns metadata about a dubbing project, including whether it’s still in progress or not.
/// </summary>
/// <param name="dubbingId"></param>
/// <param name="dubbingId">Dubbing project id.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="DubbingProjectMetadata"/>.</returns>
public async Task<DubbingProjectMetadata> GetDubbingProjectMetadataAsync(string dubbingId, CancellationToken cancellationToken = default)
Expand All @@ -159,17 +158,13 @@ public async Task<DubbingProjectMetadata> GetDubbingProjectMetadataAsync(string
/// <summary>
/// Returns transcript for the dub in the specified format (SRT or WebVTT).
/// </summary>
/// <param name="dubbingId">The ID of the dubbing project.</param>
/// <param name="dubbingId">Dubbing project id.</param>
/// <param name="languageCode">The language code of the transcript.</param>
/// <param name="formatType">Optional. The format type of the transcript file, either 'srt' or 'webvtt'.</param>
/// <param name="formatType">Optional. The format type of the transcript file, either <see cref="DubbingFormat.Srt"/> or <see cref="DubbingFormat.WebVtt"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>
/// A task representing the asynchronous operation. The task completes with the transcript content
/// as a string in the specified format.
/// A string containing the transcript content in the specified format.
/// </returns>
/// <remarks>
/// If <paramref name="formatType"/> is not specified, the method retrieves the transcript in its default format.
/// </remarks>
public async Task<string> GetTranscriptForDubAsync(string dubbingId, string languageCode, DubbingFormat formatType = DubbingFormat.Srt, CancellationToken cancellationToken = default)
{
var @params = new Dictionary<string, string> { { "format_type", formatType.ToString().ToLower() } };
Expand All @@ -180,7 +175,7 @@ public async Task<string> GetTranscriptForDubAsync(string dubbingId, string lang
/// <summary>
/// Returns dubbed file as a streamed file.
/// </summary>
/// <param name="dubbingId">The ID of the dubbing project.</param>
/// <param name="dubbingId">Dubbing project id.</param>
/// <param name="languageCode">The language code of the dubbed content.</param>
/// <param name="bufferSize">The size of the buffer used to read data from the response stream. Default is 8192 bytes.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
Expand Down Expand Up @@ -211,7 +206,7 @@ public async IAsyncEnumerable<byte[]> GetDubbedFileAsync(string dubbingId, strin
/// <summary>
/// Deletes a dubbing project.
/// </summary>
/// <param name="dubbingId">The ID of the dubbing project.</param>
/// <param name="dubbingId">Dubbing project id.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
public async Task DeleteDubbingProjectAsync(string dubbingId, CancellationToken cancellationToken = default)
{
Expand Down
2 changes: 1 addition & 1 deletion ElevenLabs-DotNet/Dubbing/DubbingProjectMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class DubbingProjectMetadata

[JsonInclude]
[JsonPropertyName("target_languages")]
public List<string> TargetLanguages { get; private set; }
public IReadOnlyList<string> TargetLanguages { get; private set; }

[JsonInclude]
[JsonPropertyName("error")]
Expand Down
4 changes: 3 additions & 1 deletion ElevenLabs-DotNet/ElevenLabs-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ All copyrights, trademarks, logos, and assets are the property of their respecti
<SignAssembly>false</SignAssembly>
<IncludeSymbols>true</IncludeSymbols>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>3.0.1</Version>
<Version>3.0.2</Version>
<PackageReleaseNotes>
Version 3.0.2
- Cleanup and polish for Dubbing API
Version 3.0.1
- Updated Models
Version 3.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ElevenLabs.SoundGeneration
public sealed class SoundGenerationRequest
{
/// <summary>
///
/// Constructor.
/// </summary>
/// <param name="text">
/// The text that will get converted into a sound effect.
Expand Down

0 comments on commit e642904

Please sign in to comment.