Skip to content

Commit

Permalink
Merge pull request #33 from AssemblyAI/niels/add-transcribe
Browse files Browse the repository at this point in the history
Add transcribe and other handwritten methods + tests
  • Loading branch information
Swimburger committed Jul 22, 2024
2 parents 10b4059 + 2c69898 commit 3fb50a6
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 37 deletions.
12 changes: 5 additions & 7 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
README.md
LICENSE.md
src/AssemblyAI/AssemblyAI.csproj
src/AssemblyAI/AssemblyAIClient.cs
src/AssemblyAI/ClientOptions.cs
src/AssemblyAI/Constants.cs
src/AssemblyAI/UserAgent.cs
src/AssemblyAI/AssemblyAIClient.cs
src/AssemblyAI/DependencyInjectionExtensions.cs
src/AssemblyAI/Files/FilesCustomClient.cs
src/AssemblyAI/Transcripts/TranscriptsCustomClient.cs
src/AssemblyAI/Realtime/RealtimeTranscriber.cs
src/AssemblyAI/Realtime/WebSocketClient
src/AssemblyAI/Constants.cs
src/AssemblyAI/UserAgent.cs
src/AssemblyAI.Test

Samples
samples



2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,4 @@ $RECYCLE.BIN/
*.lnk

.idea
.runsettings
.runsettings
6 changes: 6 additions & 0 deletions src/AssemblyAI.Test/AssemblyAI.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
<ProjectReference Include="..\AssemblyAI\AssemblyAI.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
49 changes: 49 additions & 0 deletions src/AssemblyAI.Test/FilesClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NUnit.Framework;

namespace AssemblyAI.Test;

[TestFixture]
public class FilesClientTests
{
private string _apiKey;

[SetUp]
public void Setup()
{
// Retrieve the API key from the .runsettings file
_apiKey = TestContext.Parameters.Get("ASSEMBLYAI_API_KEY");
if(string.IsNullOrEmpty(_apiKey)) throw new Exception("ASSEMBLYAI_API_KEY .runsetting parameter is not set.");
}

[Test]
public async Task Should_Upload_File_Using_FileInfo()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
var fileInfo = new FileInfo(testFilePath);

var uploadedFile = await client.Files.UploadAsync(fileInfo).ConfigureAwait(false);

Assert.That(uploadedFile, Is.Not.Null);
Assert.That(uploadedFile.UploadUrl, Is.Not.Null);
}

[Test]
public async Task Should_Upload_File_Using_Stream()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
await using var fileStream = File.OpenRead(testFilePath);

var uploadedFile = await client.Files.UploadAsync(fileStream).ConfigureAwait(false);

Assert.That(uploadedFile, Is.Not.Null);
Assert.That(uploadedFile.UploadUrl, Is.Not.Null);
}
}
Binary file added src/AssemblyAI.Test/TestData/nbc.mp3
Binary file not shown.
19 changes: 0 additions & 19 deletions src/AssemblyAI.Test/TranscriptTests.cs

This file was deleted.

149 changes: 149 additions & 0 deletions src/AssemblyAI.Test/TranscriptsClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using NUnit.Framework;

namespace AssemblyAI.Test;

[TestFixture]
public class TranscriptsClientTests
{
private string _apiKey;

[SetUp]
public void Setup()
{
// Retrieve the API key from the .runsettings file
_apiKey = TestContext.Parameters.Get("ASSEMBLYAI_API_KEY");
if(string.IsNullOrEmpty(_apiKey)) throw new Exception("ASSEMBLYAI_API_KEY .runsettings parameter is not set.");
}

[Test]
public async Task Should_Submit_Using_Uri()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

var transcript = await client.Transcripts.SubmitAsync(
new Uri("https://storage.googleapis.com/aai-docs-samples/nbc.mp3")
).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Queued));
}

[Test]
public async Task Should_Submit_Using_Stream()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
await using var stream = File.OpenRead(testFilePath);

var transcript = await client.Transcripts.SubmitAsync(stream).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Queued));
}

[Test]
public async Task Should_Submit_Using_FileInfo()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
var fileInfo = new FileInfo(testFilePath);

var transcript = await client.Transcripts.SubmitAsync(fileInfo).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Queued));
}

[Test]
public async Task Should_Transcribe_Using_Uri()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

var transcript = await client.Transcripts.TranscribeAsync(
new Uri("https://storage.googleapis.com/aai-docs-samples/nbc.mp3")
).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Queued));
}

[Test]
public async Task Should_Transcribe_From_FileInfo()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
var fileInfo = new FileInfo(testFilePath);

var transcript = await client.Transcripts.TranscribeAsync(fileInfo).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Completed));
}

[Test]
public async Task Should_Transcribe_Using_Stream()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
await using var stream = File.OpenRead(testFilePath);

var transcript = await client.Transcripts.TranscribeAsync(stream).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Completed));
}

[Test]
public async Task Should_Wait_Until_Ready()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);

// Adjust the path to where your test file is located
var testFilePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "nbc.mp3");
await using var stream = File.OpenRead(testFilePath);

var transcript = await client.Transcripts.SubmitAsync(stream).ConfigureAwait(false);
transcript = await client.Transcripts.WaitUntilReady(transcript.Id).ConfigureAwait(false);

Assert.That(transcript, Is.Not.Null);
Assert.That(transcript.Id, Is.Not.Null);
Assert.That(transcript.Status, Is.EqualTo(TranscriptStatus.Completed));
}

[Test]
public async Task Should_Paginate_Transcripts()
{
// Assuming there's a method to create a configured RawClient instance
var client = new AssemblyAIClient(_apiKey);
var transcriptPage = await client.Transcripts.ListAsync().ConfigureAwait(false);
Assert.That(transcriptPage, Is.Not.Null);
Assert.That(transcriptPage.PageDetails.PrevUrl, Is.Not.Null);
Assert.That(transcriptPage.Transcripts, Is.Not.Empty);

var prevPage = await client.Transcripts.ListAsync(transcriptPage.PageDetails.PrevUrl);
Assert.That(transcriptPage, Is.Not.Null);
Assert.That(transcriptPage.PageDetails.NextUrl, Is.Not.Null);
Assert.That(transcriptPage.Transcripts, Is.Not.Empty);
}
}
7 changes: 7 additions & 0 deletions src/AssemblyAI.Test/sample.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<TestRunParameters>
<Parameter name="ASSEMBLYAI_API_KEY" value="" />
<Parameter name="TEST_TRANSCRIPT_ID" value="" />
</TestRunParameters>
</RunSettings>
7 changes: 4 additions & 3 deletions src/AssemblyAI/AssemblyAIClient.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#nullable enable

using System.Net.Http;
using AssemblyAI;
using AssemblyAI.Core;

#nullable enable

namespace AssemblyAI;

Expand Down Expand Up @@ -38,14 +39,14 @@ public AssemblyAIClient(ClientOptions clientOptions)
}

Files = new FilesClient(client);
Transcripts = new TranscriptsClient(client);
Transcripts = new ExtendedTranscriptsClient(client, this);
Realtime = new RealtimeClient(client);
Lemur = new LemurClient(client);
}

public FilesClient Files { get; init; }

public TranscriptsClient Transcripts { get; init; }
public ExtendedTranscriptsClient Transcripts { get; init; }

public RealtimeClient Realtime { get; init; }

Expand Down
2 changes: 1 addition & 1 deletion src/AssemblyAI/Files/FilesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace AssemblyAI;

public class FilesClient
public partial class FilesClient
{
private RawClient _client;

Expand Down
20 changes: 20 additions & 0 deletions src/AssemblyAI/Files/FilesCustomClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Net.Http;
using System.Text.Json;
using AssemblyAI;
using AssemblyAI.Core;

#nullable enable

namespace AssemblyAI;

public partial class FilesClient
{
/// <summary>
/// Upload a media file to AssemblyAI's servers.
/// </summary>
public async Task<UploadedFile> UploadAsync(FileInfo audioFile)
{
using var audioFileStream = audioFile.OpenRead();
return await UploadAsync(audioFileStream).ConfigureAwait(false);
}
}
Loading

0 comments on commit 3fb50a6

Please sign in to comment.