Skip to content

Commit

Permalink
ElevenLabs 1.3.0 (#14)
Browse files Browse the repository at this point in the history
- Added ModelsEndpoint
- Updated TextToSpeech.TextToSpeechAsync with optional Model parameter.
Defaults to eleven_monolingual_v1
- Added default voices
  • Loading branch information
StephenHodgson authored May 1, 2023
1 parent 57cd367 commit 5f7f729
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 8 deletions.
30 changes: 30 additions & 0 deletions ElevenLabs-DotNet-Tests/Test_Fixture_06_Models.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using NUnit.Framework;
using System;
using System.Threading.Tasks;

namespace ElevenLabs.Tests
{
internal class Test_Fixture_06_Models : AbstractTestFixture
{
[Test]
public async Task Test_01_GetModels()
{
Assert.NotNull(ElevenLabsClient.ModelsEndpoint);
var models = await ElevenLabsClient.ModelsEndpoint.GetModelsAsync();
Assert.NotNull(models);
Assert.IsNotEmpty(models);

foreach (var model in models)
{
Console.WriteLine($"{model.Id} | {model.Name} | {model.Description}");

foreach (var language in model.Languages)
{
Console.WriteLine($" {language.Id} | {language.Name}");
}
}
}
}
}
7 changes: 5 additions & 2 deletions ElevenLabs-DotNet/ElevenLabs-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
<Company>RageAgainstThePixel</Company>
<Copyright>2023</Copyright>
<PackageTags>ElevenLabs, AI, ML, Voice, TTS</PackageTags>
<Version>1.2.1</Version>
<PackageReleaseNotes>Version 1.2.1
<Version>1.3.0</Version>
<PackageReleaseNotes>Version 1.3.0
- Added ModelsEndpoint
- Updated TextToSpeech.TextToSpeechAsync with optional Model parameter. Defaults to eleven_monolingual_v1
Version 1.2.1
- Updated docs
Version 1.2.0
- Added ability to create voice from Id
Expand Down
4 changes: 4 additions & 0 deletions ElevenLabs-DotNet/ElevenLabsClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using ElevenLabs.History;
using ElevenLabs.Models;
using ElevenLabs.TextToSpeech;
using ElevenLabs.User;
using ElevenLabs.VoiceGeneration;
Expand Down Expand Up @@ -44,6 +45,7 @@ public ElevenLabsClient(ElevenLabsAuthentication elevenLabsAuthentication = null

UserEndpoint = new UserEndpoint(this);
VoicesEndpoint = new VoicesEndpoint(this);
ModelsEndpoint = new ModelsEndpoint(this);
HistoryEndpoint = new HistoryEndpoint(this);
TextToSpeechEndpoint = new TextToSpeechEndpoint(this);
VoiceGenerationEndpoint = new VoiceGenerationEndpoint(this);
Expand All @@ -70,6 +72,8 @@ public ElevenLabsClient(ElevenLabsAuthentication elevenLabsAuthentication = null

public VoicesEndpoint VoicesEndpoint { get; }

public ModelsEndpoint ModelsEndpoint { get; }

public HistoryEndpoint HistoryEndpoint { get; }

public TextToSpeechEndpoint TextToSpeechEndpoint { get; }
Expand Down
17 changes: 17 additions & 0 deletions ElevenLabs-DotNet/Models/Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Text.Json.Serialization;

namespace ElevenLabs.Models
{
public sealed class Language
{
[JsonInclude]
[JsonPropertyName("language_id")]
public string Id { get; private set; }

[JsonInclude]
[JsonPropertyName("name")]
public string Name { get; private set; }
}
}
61 changes: 61 additions & 0 deletions ElevenLabs-DotNet/Models/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace ElevenLabs.Models
{
public sealed class Model
{
public Model(string id)
{
Id = id;
}

[JsonInclude]
[JsonPropertyName("model_id")]
public string Id { get; private set; }

[JsonInclude]
[JsonPropertyName("name")]
public string Name { get; private set; }

[JsonInclude]
[JsonPropertyName("can_be_finetuned")]
public bool CanBeFineTuned { get; private set; }

[JsonInclude]
[JsonPropertyName("can_do_text_to_speech")]
public bool CanDoTextToSpeech { get; private set; }

[JsonInclude]
[JsonPropertyName("can_do_voice_conversion")]
public bool CanDoVoiceConversion { get; private set; }

[JsonInclude]
[JsonPropertyName("token_cost_factor")]
public double TokenCostFactor { get; private set; }

[JsonInclude]
[JsonPropertyName("description")]
public string Description { get; private set; }

[JsonInclude]
[JsonPropertyName("languages")]
public IReadOnlyList<Language> Languages { get; private set; }

public static implicit operator string(Model model) => model.ToString();

public override string ToString() => Id;

#region Predefined Models

[JsonIgnore]
public static Model MonoLingualV1 { get; } = new Model("eleven_monolingual_v1");

[JsonIgnore]
public static Model MultiLingualV1 { get; } = new Model("eleven_multilingual_v1");

#endregion Predefined Models
}
}
28 changes: 28 additions & 0 deletions ElevenLabs-DotNet/Models/ModelsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using ElevenLabs.Extensions;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

namespace ElevenLabs.Models
{
public sealed class ModelsEndpoint : BaseEndPoint
{
public ModelsEndpoint(ElevenLabsClient api) : base(api) { }

protected override string Root => "models";

/// <summary>
/// Access the different models available to the platform.
/// </summary>
/// <returns>A list of <see cref="Model"/>s you can use.</returns>
public async Task<IReadOnlyList<Model>> GetModelsAsync()
{
var response = await Api.Client.GetAsync(GetUrl());
var responseAsString = await response.ReadAsStringAsync();
return JsonSerializer.Deserialize<IReadOnlyList<Model>>(responseAsString, Api.JsonSerializationOptions);
}
}
}
7 changes: 4 additions & 3 deletions ElevenLabs-DotNet/TextToSpeech/TextToSpeechEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using ElevenLabs.Extensions;
using ElevenLabs.Models;
using ElevenLabs.Voices;
using System;
using System.IO;
Expand All @@ -25,17 +26,17 @@ public TextToSpeechEndpoint(ElevenLabsClient api) : base(api) { }
/// <param name="text">Text input to synthesize speech for. Maximum 5000 characters.</param>
/// <param name="voice"><see cref="Voice"/> to use.</param>
/// <param name="voiceSettings">Optional, <see cref="VoiceSettings"/> that will override the default settings in <see cref="Voice.Settings"/>.</param>
/// <param name="model">Optional, <see cref="Model"/> to use. Defaults to <see cref="Model.MonoLingualV1"/>.</param>
/// <param name="saveDirectory">Optional, The save directory to save the audio clip.</param>
/// <param name="deleteCachedFile">Optional, deletes the cached file for this text string. Default is false.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>Downloaded clip path.</returns>
public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSettings voiceSettings = null, string saveDirectory = null, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSettings voiceSettings = null, Model model = null, string saveDirectory = null, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
{
if (text.Length > 5000)
{
throw new ArgumentOutOfRangeException(nameof(text), $"{nameof(text)} cannot exceed 5000 characters");
}

var rootDirectory = (saveDirectory ?? Directory.GetCurrentDirectory()).CreateNewDirectory(nameof(ElevenLabs));
var downloadDirectory = rootDirectory.CreateNewDirectory("TextToSpeech");
var fileName = $"{text.GenerateGuid()}.mp3";
Expand All @@ -49,7 +50,7 @@ public async Task<string> TextToSpeechAsync(string text, Voice voice, VoiceSetti
if (!File.Exists(filePath))
{
var defaultVoiceSettings = voiceSettings ?? voice.Settings ?? await Api.VoicesEndpoint.GetDefaultVoiceSettingsAsync(cancellationToken);
var payload = JsonSerializer.Serialize(new TextToSpeechRequest(text, defaultVoiceSettings)).ToJsonStringContent();
var payload = JsonSerializer.Serialize(new TextToSpeechRequest(text, model ?? Model.MonoLingualV1, defaultVoiceSettings)).ToJsonStringContent();
var response = await Api.Client.PostAsync(GetUrl($"/{voice.Id}"), payload, cancellationToken);
await response.CheckResponseAsync(cancellationToken);
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
Expand Down
7 changes: 6 additions & 1 deletion ElevenLabs-DotNet/TextToSpeech/TextToSpeechRequest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using ElevenLabs.Models;
using ElevenLabs.Voices;
using System.Text.Json.Serialization;

namespace ElevenLabs.TextToSpeech
{
public sealed class TextToSpeechRequest
{
public TextToSpeechRequest(string text, VoiceSettings voiceSettings)
public TextToSpeechRequest(string text, Model model, VoiceSettings voiceSettings)
{
Text = text;
Model = model;
VoiceSettings = voiceSettings;
}

[JsonPropertyName("text")]
public string Text { get; }

[JsonPropertyName("model_id")]
public string Model { get; }

[JsonPropertyName("voice_settings")]
public VoiceSettings VoiceSettings { get; }
}
Expand Down
37 changes: 35 additions & 2 deletions ElevenLabs-DotNet/Voices/Voice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public Voice(string id)
Id = id;
}

public static implicit operator string(Voice voice) => voice.Id;

[JsonInclude]
[JsonPropertyName("voice_id")]
public string Id { get; private set; }
Expand Down Expand Up @@ -45,5 +43,40 @@ public Voice(string id)
[JsonInclude]
[JsonPropertyName("settings")]
public VoiceSettings Settings { get; internal set; }

public static implicit operator string(Voice voice) => voice.ToString();

public override string ToString() => Id;

#region Premade Voices

[JsonIgnore]
public static Voice Adam { get; } = new Voice("pNInz6obpgDQGcFmaJgB");

[JsonIgnore]
public static Voice Antoni { get; } = new Voice("ErXwobaYiN019PkySvjV");

[JsonIgnore]
public static Voice Arnold { get; } = new Voice("VR6AewLTigWG4xSOukaG");

[JsonIgnore]
public static Voice Bella { get; } = new Voice("EXAVITQu4vr4xnSDxMaL");

[JsonIgnore]
public static Voice Domi { get; } = new Voice("AZnzlk1XvdvUeBnXmlld");

[JsonIgnore]
public static Voice Elli { get; } = new Voice("MF3mGyEYCl7XYWbV9V6O");

[JsonIgnore]
public static Voice Josh { get; } = new Voice("TxGEqnHWrfWFTfGW9XjX");

[JsonIgnore]
public static Voice Rachel { get; } = new Voice("21m00Tcm4TlvDq8ikWAM");

[JsonIgnore]
public static Voice Sam { get; } = new Voice("yoZ06aMxZJJ28mfd3POQ");

#endregion Premade Voices
}
}

0 comments on commit 5f7f729

Please sign in to comment.