Skip to content
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

Wrap punctuation APIs to C#. #945

Merged
merged 4 commits into from
May 30, 2024
Merged
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
5 changes: 4 additions & 1 deletion .github/scripts/test-dot-net.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

cd dotnet-examples/

cd speaker-identification
cd offline-punctuation
./run.sh

cd ../speaker-identification
./run.sh

cd ../streaming-hlg-decoding/
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-dot-net.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ jobs:
cp -v scripts/dotnet/examples/spoken-language-identification.csproj dotnet-examples/spoken-language-identification/
cp -v scripts/dotnet/examples/streaming-hlg-decoding.csproj dotnet-examples/streaming-hlg-decoding
cp -v scripts/dotnet/examples/speaker-identification.csproj dotnet-examples/speaker-identification
cp -v scripts/dotnet/examples/offline-punctuation.csproj dotnet-examples/offline-punctuation

ls -lh /tmp

Expand Down
45 changes: 45 additions & 0 deletions dotnet-examples/offline-punctuation/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2024 Xiaomi Corporation
//
// This file shows how to add punctuations to text.
//
// 1. Download a model from
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/punctuation-models
//
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
//
// 3. Now run it
//
// dotnet run

using SherpaOnnx;
using System.Collections.Generic;
using System;

class OfflinePunctuationDemo
{

static void Main(string[] args)
{
var config = new OfflinePunctuationConfig();
config.Model.CtTransformer = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx";
config.Model.Debug = 1;
config.Model.NumThreads = 1;
var punct = new OfflinePunctuation(config);

string[] textList = new string[] {
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
"我们都是木头人不会说话不会动",
"The African blogosphere is rapidly expanding bringing more voices online in the form of commentaries opinions analyses rants and poetry",
};

Console.WriteLine("---------");
foreach (string text in textList)
{
string textWithPunct = punct.AddPunct(text);
Console.WriteLine("Input text: {0}", text);
Console.WriteLine("Output text: {0}", textWithPunct);
Console.WriteLine("---------");
}
}
}

15 changes: 15 additions & 0 deletions dotnet-examples/offline-punctuation/offline-punctuation.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>offline_punctuation</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions dotnet-examples/offline-punctuation/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -ex

if [ ! -e ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
fi

dotnet run
6 changes: 6 additions & 0 deletions dotnet-examples/sherpa-onnx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "streaming-hlg-decoding", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "speaker-identification", "speaker-identification\speaker-identification.csproj", "{2B1B140E-A92F-426B-B0DF-5D916B67304F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "offline-punctuation", "offline-punctuation\offline-punctuation.csproj", "{42D85582-BB63-4259-A4EA-837D66AC078B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -60,5 +62,9 @@ Global
{2B1B140E-A92F-426B-B0DF-5D916B67304F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B1B140E-A92F-426B-B0DF-5D916B67304F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B1B140E-A92F-426B-B0DF-5D916B67304F}.Release|Any CPU.Build.0 = Release|Any CPU
{42D85582-BB63-4259-A4EA-837D66AC078B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{42D85582-BB63-4259-A4EA-837D66AC078B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42D85582-BB63-4259-A4EA-837D66AC078B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42D85582-BB63-4259-A4EA-837D66AC078B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion scripts/dotnet/FeatureConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ public FeatureConfig()
public int FeatureDim;
}

}
}
4 changes: 1 addition & 3 deletions scripts/dotnet/OfflineLMConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SherpaOnnx
{

[StructLayout(LayoutKind.Sequential)]
public struct OfflineLMConfig
{
Expand All @@ -22,5 +21,4 @@ public OfflineLMConfig()

public float Scale;
}

}
}
5 changes: 1 addition & 4 deletions scripts/dotnet/OfflineModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SherpaOnnx
{

[StructLayout(LayoutKind.Sequential)]
public struct OfflineModelConfig
{
Expand Down Expand Up @@ -44,6 +43,4 @@ public OfflineModelConfig()
[MarshalAs(UnmanagedType.LPStr)]
public string ModelType;
}


}
}
3 changes: 1 addition & 2 deletions scripts/dotnet/OfflineNemoEncDecCtcModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SherpaOnnx
{

[StructLayout(LayoutKind.Sequential)]
public struct OfflineNemoEncDecCtcModelConfig
{
Expand All @@ -19,4 +18,4 @@ public OfflineNemoEncDecCtcModelConfig()
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
}
}
}
3 changes: 1 addition & 2 deletions scripts/dotnet/OfflineParaformerModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ public OfflineParaformerModelConfig()
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
}

}
}
88 changes: 88 additions & 0 deletions scripts/dotnet/OfflinePunctuation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)

using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;

namespace SherpaOnnx
{
public class OfflinePunctuation : IDisposable
{
public OfflinePunctuation(OfflinePunctuationConfig config)
{
IntPtr h = SherpaOnnxCreateOfflinePunctuation(ref config);
_handle = new HandleRef(this, h);
}

public String AddPunct(String text)
{
IntPtr p = SherpaOfflinePunctuationAddPunct(_handle.Handle, text);

string s = "";
int length = 0;

unsafe
{
byte* b = (byte*)p;
if (b != null)
{
while (*b != 0)
{
++b;
length += 1;
}
}
}

if (length > 0)
{
byte[] stringBuffer = new byte[length];
Marshal.Copy(p, stringBuffer, 0, length);
s = Encoding.UTF8.GetString(stringBuffer);
}

SherpaOfflinePunctuationFreeText(p);

return s;
}

public void Dispose()
{
Cleanup();
// Prevent the object from being placed on the
// finalization queue
System.GC.SuppressFinalize(this);
}

~OfflinePunctuation()
{
Cleanup();
}

private void Cleanup()
{
SherpaOnnxDestroyOfflinePunctuation(_handle.Handle);

// Don't permit the handle to be used again.
_handle = new HandleRef(this, IntPtr.Zero);
}

private HandleRef _handle;


[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOnnxCreateOfflinePunctuation(ref OfflinePunctuationConfig config);

[DllImport(Dll.Filename)]
private static extern void SherpaOnnxDestroyOfflinePunctuation(IntPtr handle);

[DllImport(Dll.Filename)]
private static extern IntPtr SherpaOfflinePunctuationAddPunct(IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] string text);

[DllImport(Dll.Filename)]
private static extern void SherpaOfflinePunctuationFreeText(IntPtr p);
}
}

21 changes: 21 additions & 0 deletions scripts/dotnet/OfflinePunctuationConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)

using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;

namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflinePunctuationConfig
{
public OfflinePunctuationConfig()
{
Model = new OfflinePunctuationModelConfig();
}
public OfflinePunctuationModelConfig Model;
}
}

32 changes: 32 additions & 0 deletions scripts/dotnet/OfflinePunctuationModelConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// Copyright (c) 2024 Xiaomi Corporation (authors: Fangjun Kuang)

using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System;

namespace SherpaOnnx
{
[StructLayout(LayoutKind.Sequential)]
public struct OfflinePunctuationModelConfig
{
public OfflinePunctuationModelConfig()
{
CtTransformer = "";
NumThreads = 1;
Debug = 0;
Provider = "cpu";
}

[MarshalAs(UnmanagedType.LPStr)]
public string CtTransformer;

public int NumThreads;

public int Debug;

[MarshalAs(UnmanagedType.LPStr)]
public string Provider;
}
}
3 changes: 1 addition & 2 deletions scripts/dotnet/OfflineRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ private void Cleanup()
[DllImport(Dll.Filename, EntryPoint = "DecodeMultipleOfflineStreams")]
private static extern void Decode(IntPtr handle, IntPtr[] streams, int n);
}

}
}
5 changes: 1 addition & 4 deletions scripts/dotnet/OfflineRecognizerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SherpaOnnx
{

[StructLayout(LayoutKind.Sequential)]
public struct OfflineRecognizerConfig
{
Expand Down Expand Up @@ -38,6 +37,4 @@ public OfflineRecognizerConfig()

public float HotwordsScore;
}


}
}
5 changes: 1 addition & 4 deletions scripts/dotnet/OfflineRecognizerResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SherpaOnnx
{

public class OfflineRecognizerResult
{
public OfflineRecognizerResult(IntPtr handle)
Expand Down Expand Up @@ -44,6 +43,4 @@ struct Impl
private String _text;
public String Text => _text;
}


}
}
4 changes: 1 addition & 3 deletions scripts/dotnet/OfflineStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SherpaOnnx
{

public class OfflineStream : IDisposable
{
public OfflineStream(IntPtr p)
Expand Down Expand Up @@ -68,5 +67,4 @@ private void Cleanup()
[DllImport(Dll.Filename, EntryPoint = "DestroyOfflineRecognizerResult")]
private static extern void DestroyResult(IntPtr handle);
}

}
}
3 changes: 1 addition & 2 deletions scripts/dotnet/OfflineTdnnModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ public OfflineTdnnModelConfig()
[MarshalAs(UnmanagedType.LPStr)]
public string Model;
}

}
}
3 changes: 1 addition & 2 deletions scripts/dotnet/OfflineTransducerModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ public OfflineTransducerModelConfig()
[MarshalAs(UnmanagedType.LPStr)]
public string Joiner;
}

}
}
Loading
Loading