Skip to content

Add AssemblyAIAudioToTextService.cs #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions src/AssemblyAI.SemanticKernel/AssemblyAI.SemanticKernel.csproj
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>AssemblyAI.SemanticKernel</RootNamespace>
<AssemblyName>AssemblyAI.SemanticKernel</AssemblyName>
<PackageId>AssemblyAI.SemanticKernel</PackageId>
<Title>AssemblyAI plugins for Semantic Kernel</Title>
<Authors>AssemblyAI</Authors>
<Description>Transcribe audio using AssemblyAI with Semantic Kernel plugins</Description>
<Copyright>Copyright 2023 (c) AssemblyAI, Inc. All rights reserved.</Copyright>
<PackageTags>SemanticKernel;AI;AssemblyAI;transcript</PackageTags>
<Company>AssemblyAI</Company>
<Product>AssemblyAI</Product>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<PackageVersion>1.1.0</PackageVersion>
<OutputType>Library</OutputType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/AssemblyAI/assemblyai-semantic-kernel</PackageProjectUrl>
<RepositoryUrl>https://github.com/AssemblyAI/assemblyai-semantic-kernel.git</RepositoryUrl>
<PackageIconUrl>https://www.assemblyai.com/favicon.png</PackageIconUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>AssemblyAI.SemanticKernel</RootNamespace>
<AssemblyName>AssemblyAI.SemanticKernel</AssemblyName>
<PackageId>AssemblyAI.SemanticKernel</PackageId>
<Title>AssemblyAI plugins for Semantic Kernel</Title>
<Authors>AssemblyAI</Authors>
<Description>Transcribe audio using AssemblyAI with Semantic Kernel plugins</Description>
<Copyright>Copyright 2023 (c) AssemblyAI, Inc. All rights reserved.</Copyright>
<PackageTags>SemanticKernel;AI;AssemblyAI;transcript</PackageTags>
<Company>AssemblyAI</Company>
<Product>AssemblyAI</Product>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
<PackageVersion>1.2.0</PackageVersion>
<OutputType>Library</OutputType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/AssemblyAI/assemblyai-semantic-kernel</PackageProjectUrl>
<RepositoryUrl>https://github.com/AssemblyAI/assemblyai-semantic-kernel.git</RepositoryUrl>
<PackageIconUrl>https://www.assemblyai.com/favicon.png</PackageIconUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<RepositoryType>git</RepositoryType>
<NoWarn>SKEXP0005</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options">
<Version>8.0.0</Version>
<Version>8.0.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions">
<Version>8.0.0</Version>
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.SemanticKernel">
<Version>1.0.1</Version>
<Version>1.4.0</Version>
</PackageReference>
<PackageReference Include="System.Net.Http.Json">
<Version>8.0.0</Version>
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Include="..\..\icon.png" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
</ItemGroup>
<ItemGroup>
<None Include="..\..\icon.png" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<Reference Include="AssemblyAI">
<HintPath>..\..\..\assemblyai-csharp-sdk\src\AssemblyAI\bin\Debug\netstandard2.0\AssemblyAI.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.SemanticKernel;

namespace AssemblyAI.SemanticKernel
{
/// <summary>
/// Configure AssemblyAI transcript parameters
/// </summary>
public class AssemblyAIAudioToTextExecutionSettings : PromptExecutionSettings
{
/// <summary>
/// Parameters to transcribe audio using AssemblyAI.
/// </summary>
public CreateTranscriptOptionalParameters TranscriptParameters { get; set; }
}
}
78 changes: 78 additions & 0 deletions src/AssemblyAI.SemanticKernel/AssemblyAIAudioToTextService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AssemblyAI.Transcripts;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AudioToText;
using Microsoft.SemanticKernel.Contents;

namespace AssemblyAI.SemanticKernel
{
/// <summary>
/// AssemblyAI speech-to-text service.
/// </summary>
public class AssemblyAIAudioToTextService : IAudioToTextService
{
private readonly AssemblyAIClient _client;

/// <summary>
/// Attributes are not used by AssemblyAIAudioToTextService.
/// </summary>
public IReadOnlyDictionary<string, object> Attributes => new Dictionary<string, object>();

/// <summary>
/// Creates an instance of the <see cref="AssemblyAIAudioToTextService"/> with an AssemblyAI API key.
/// </summary>
/// <param name="apiKey">OpenAI API Key</param>
public AssemblyAIAudioToTextService(string apiKey)
{
_client = new AssemblyAIClient(apiKey);
}

/// <inheritdoc/>
public async Task<TextContent> GetTextContentAsync(
AudioContent content,
PromptExecutionSettings executionSettings = null,
Kernel kernel = null,
CancellationToken cancellationToken = default)
{
UploadedFile uploadedFile;
using (var stream = content.Data.ToStream())
{
uploadedFile = await _client.Files.Upload(stream).ConfigureAwait(false);
}

AssemblyAI.Transcript transcript;
if (executionSettings != null && executionSettings is AssemblyAIAudioToTextExecutionSettings aaiSettings)
{
if (aaiSettings.TranscriptParameters == null)
{
throw new Exception(
"AssemblyAIAudioToTextExecutionSettings.TranscriptParameters is required when passing execution settings.");
}

transcript = await _client.Transcripts.Create(
uploadedFile.UploadUrl,
aaiSettings.TranscriptParameters
).ConfigureAwait(false);
}
else
{
transcript = await _client.Transcripts.Create(new CreateTranscriptParameters
{
AudioUrl = uploadedFile.UploadUrl
}).ConfigureAwait(false);
}

return new TextContent(
text: transcript.Text,
modelId: null,
innerContent: transcript,
encoding: Encoding.UTF8,
metadata: null
);
}
}
}
4 changes: 2 additions & 2 deletions src/Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.SemanticKernel">
<Version>1.0.1</Version>
<Version>1.4.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.SemanticKernel.Planners.Handlebars">
<Version>1.0.1-preview</Version>
<Version>1.4.0-preview</Version>
</PackageReference>
</ItemGroup>

Expand Down