Skip to content

Commit

Permalink
Merge pull request #38 from AssemblyAI/niels/update-samples
Browse files Browse the repository at this point in the history
Update samples, fix SDK
  • Loading branch information
Swimburger committed Aug 1, 2024
2 parents 9db4524 + 74cd0cc commit e6f3b05
Show file tree
Hide file tree
Showing 22 changed files with 240 additions and 298 deletions.
2 changes: 2 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ src/AssemblyAI/UserAgent.cs
src/AssemblyAI/Event.cs
src/AssemblyAI/AssemblyAIClient.cs
src/AssemblyAI/DependencyInjectionExtensions.cs
src/AssemblyAI/EnumConverter.cs
src/AssemblyAI/Files/ExtendedFilesClient.cs
src/AssemblyAI/Transcripts/ExtendedTranscriptsClient.cs
src/AssemblyAI/Lemur/Types/LemurResponse.cs
Expand All @@ -23,6 +24,7 @@ src/AssemblyAI.Test
src/AssemblyAI.UnitTests
src/AssemblyAI.IntegrationTests


# TODO: remove these ignores when AssemblyAI fixes the API
src/AssemblyAI/Transcripts/Types/ContentSafetyLabelsResult.cs
src/AssemblyAI/Transcripts/Types/TopicDetectionModelResult.cs
Expand Down
2 changes: 1 addition & 1 deletion Samples/Avalonia/Sample.Android/Sample.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<AndroidPackageFormat>apk</AndroidPackageFormat>
<AndroidEnableProfiledAot>False</AndroidEnableProfiledAot>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<RootNamespace>Sample.Android</RootNamespace>
</PropertyGroup>

Expand Down
3 changes: 3 additions & 0 deletions Samples/Avalonia/Sample.iOS/Sample.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<Nullable>enable</Nullable>
<RootNamespace>Sample.iOS</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<CodesignKey>iPhone Developer</CodesignKey>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Avalonia.iOS" Version="$(AvaloniaVersion)"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public LemurMessageType Type
public string Message
{
get => message;
set => this.RaiseAndSetIfChanged(ref message, value, nameof(Message));
set => this.RaiseAndSetIfChanged(ref message, value);
}

public static LemurMessageViewModel FromUser(string message)
Expand Down
108 changes: 33 additions & 75 deletions Samples/Avalonia/Sample/ViewModels/TranscribeFileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading.Tasks;
using AssemblyAI;
using AssemblyAI.Lemur;
using AssemblyAI.Transcripts;
using Avalonia;
using Avalonia.Controls;
Expand Down Expand Up @@ -67,10 +68,18 @@ public bool IsTranscribing
set => this.RaiseAndSetIfChanged(ref _isTranscribing, value);
}

private Transcript _transcript = new Transcript
private Transcript _transcript = new()
{
Text =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
Text = "Your transcript will appear here.",
Id = null,
LanguageModel = null,
AcousticModel = null,
Status = TranscriptStatus.Queued,
AudioUrl = null,
WebhookAuth = false,
AutoHighlights = false,
RedactPii = false,
Summarization = false
};

public Transcript Transcript
Expand All @@ -95,82 +104,26 @@ public async Task OnUploadFileClick(Visual visual)

public async Task Transcribe()
{
IsTranscribing = true;
await using var fileStream = await _selectedFile.OpenReadAsync();
var uploadedFile = await _client.Files.UploadAsync(fileStream);
var transcript = await _client.Transcripts.SubmitAsync(new TranscriptParams
{
AudioUrl = uploadedFile.UploadUrl,
LanguageCode = SelectedLanguage != null
? Enum.Parse<TranscriptLanguageCode>(SelectedLanguage.Value.Value)
: null
});
while (true)
{
if (transcript.Status == TranscriptStatus.Error) throw new Exception();
if (transcript.Status == TranscriptStatus.Completed) break;
await Task.Delay(500);
transcript = await _client.Transcripts.GetAsync(transcript.Id);
}

Transcript = transcript;
IsTranscribing = false;
}

// TODO: Replace when stream is supported by SDK
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = 0;

if (stream.CanSeek)
{
originalPosition = stream.Position;
stream.Position = 0;
}

try
{
byte[] readBuffer = new byte[4096];

int totalBytesRead = 0;
int bytesRead;

while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;

if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}

byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
IsTranscribing = true;
await using var fileStream = await _selectedFile.OpenReadAsync();
var transcript = await _client.Transcripts.TranscribeAsync(fileStream, new TranscriptOptionalParams
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}

return buffer;
LanguageCode = SelectedLanguage != null
? EnumConverter.ToEnum<TranscriptLanguageCode>(SelectedLanguage.Value.Key)
: null
});
Transcript = transcript;
}
finally
catch (Exception e)
{
if (stream.CanSeek)
{
stream.Position = originalPosition;
}
Transcript.Text = $"Error: {e.Message}";
Console.WriteLine(e);
}
IsTranscribing = false;
}

private string _prompt = "";

public string Prompt
Expand All @@ -193,11 +146,16 @@ public bool IsLemurThinking

public async Task AskQuestion()
{
LemurMessages.Add(LemurMessageViewModel.FromUser(Prompt));
var prompt = Prompt;
Prompt = "";
LemurMessages.Add(LemurMessageViewModel.FromUser(prompt));
IsLemurThinking = true;
await Task.Delay(1_000);
LemurMessages.Add(LemurMessageViewModel.FromAssistant("Response"));
var response = await _client.Lemur.TaskAsync(new LemurTaskParams
{
TranscriptIds = new[] {Transcript.Id},
Prompt = prompt
});
LemurMessages.Add(LemurMessageViewModel.FromAssistant(response.Response.Trim()));
IsLemurThinking = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public class TranscribeMicrophoneViewModel : ViewModelBase, IAsyncDisposable
private readonly SortedDictionary<int, string> _transcriptWords = new();
private readonly RealtimeTranscriber _transcriber;

private string _transcript =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n";

private string _transcript = "Your transcript will appear here.";
public string Transcript
{
get => _transcript;
Expand Down
99 changes: 30 additions & 69 deletions Samples/BlazorSample/BlazorSample.Server/ApiEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AssemblyAI;
using AssemblyAI.Files;
using AssemblyAI.Lemur;
using AssemblyAI.Realtime;
using AssemblyAI.Transcripts;
Expand All @@ -9,42 +10,51 @@ namespace BlazorSample.Server;

public static class ApiEndpoints
{
// TODO: Replace when stream is supported by SDK
public static WebApplication MapApiEndpoints(this WebApplication webApplication)
{
var api = webApplication.MapGroup("/api");
api.MapPost("/transcribe-file", TranscribeFile);
api.MapPost("/file", UploadFile);
api.MapPost("/transcript", CreateTranscript);
api.MapGet("/transcript/{transcriptId}", GetTranscript);
api.MapPost("/ask-lemur", AskLemur);
api.MapPost("/realtime/token", GetRealtimeToken);
return webApplication;
}

[RequestSizeLimit(2_306_867_200)]
private static async Task<Transcript> TranscribeFile([FromForm] TranscribeFileModel model,
AssemblyAIClient assemblyAIClient)
private static async Task<UploadedFile> UploadFile(
[FromForm] UploadFileForm form,
AssemblyAIClient assemblyAIClient
)
{
await using var fileStream = form.File.OpenReadStream();
var uploadedFile = await assemblyAIClient.Files.UploadAsync(fileStream);
return uploadedFile;
}

private static async Task<Transcript> CreateTranscript(
[FromBody] TranscriptParams transcriptParams,
AssemblyAIClient assemblyAIClient
)
{
await using var fileStream = model.File.OpenReadStream();
var fileUpload = await assemblyAIClient.Files.UploadAsync(fileStream);
var transcript = await assemblyAIClient.Transcripts.SubmitAsync(new TranscriptParams
{
AudioUrl = fileUpload.UploadUrl,
LanguageCode = Enum.Parse<TranscriptLanguageCode>(model.LanguageCode)
});
var transcript = await assemblyAIClient.Transcripts.SubmitAsync(transcriptParams);
return transcript;
}

private static async Task<Transcript> GetTranscript(
[FromRoute] string transcriptId,
AssemblyAIClient assemblyAIClient)
{
return await assemblyAIClient.Transcripts.GetAsync(transcriptId);
}

private static async Task<object> AskLemur(
[FromForm] string transcriptId,
[FromForm] string question,
private static async Task<LemurTaskResponse> AskLemur(
[FromBody] LemurTaskParams lemurTaskParams,
AssemblyAIClient assemblyAIClient
)
{
var response = await assemblyAIClient.Lemur.TaskAsync(new LemurTaskParams
{
TranscriptIds = [transcriptId],
Prompt = question
});
return new { response = response.Response };
var response = await assemblyAIClient.Lemur.TaskAsync(lemurTaskParams);
return response;
}

private static async Task<RealtimeTemporaryTokenResponse> GetRealtimeToken(AssemblyAIClient assemblyAIClient)
Expand All @@ -53,53 +63,4 @@ private static async Task<RealtimeTemporaryTokenResponse> GetRealtimeToken(Assem
.CreateTemporaryTokenAsync(new CreateRealtimeTemporaryTokenParams { ExpiresIn = 360 });
return tokenResponse;
}

private static async Task<byte[]> ReadToEndAsync(Stream stream)
{
long originalPosition = 0;

if (stream.CanSeek)
{
originalPosition = stream.Position;
stream.Position = 0;
}

var totalBytesRead = 0;
try
{
var readBuffer = new byte[4096];

int bytesRead;

while ((bytesRead = await stream
.ReadAsync(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)
.ConfigureAwait(false)) > 0)
{
totalBytesRead += bytesRead;

if (totalBytesRead != readBuffer.Length) continue;
var nextByte = stream.ReadByte();
if (nextByte == -1) continue;
var temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}

var buffer = readBuffer;
if (readBuffer.Length == totalBytesRead) return buffer;
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);

return buffer;
}
finally
{
if (stream.CanSeek)
{
stream.Position = originalPosition;
}
}
}
}
2 changes: 1 addition & 1 deletion Samples/BlazorSample/BlazorSample.Server/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace BlazorSample.Server;
public class Constants
{
public static readonly IComponentRenderMode RenderMode =
Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveServer;
Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveWebAssembly;
}
Loading

0 comments on commit e6f3b05

Please sign in to comment.