Add C# API for spoken language identification (#697)
This commit is contained in:
42
dotnet-examples/spoken-language-identification/Program.cs
Normal file
42
dotnet-examples/spoken-language-identification/Program.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2024 Xiaomi Corporation
|
||||
//
|
||||
// This file shows how to do spoken language identification with whisper.
|
||||
//
|
||||
// 1. Download a whisper multilingual model. We use a tiny model below.
|
||||
// Please refer to https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
|
||||
// to download more models.
|
||||
//
|
||||
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.tar.bz2
|
||||
// tar xvf sherpa-onnx-whisper-tiny.tar.bz2
|
||||
// rm sherpa-onnx-whisper-tiny.tar.bz2
|
||||
//
|
||||
// 2. Now run it
|
||||
//
|
||||
// dotnet run
|
||||
|
||||
using SherpaOnnx;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
class SpokenLanguageIdentificationDemo
|
||||
{
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var config = new SpokenLanguageIdentificationConfig();
|
||||
config.Whisper.Encoder = "./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx";
|
||||
config.Whisper.Decoder = "./sherpa-onnx-whisper-tiny/tiny-decoder.int8.onnx";
|
||||
|
||||
var slid = new SpokenLanguageIdentification(config);
|
||||
var filename = "./sherpa-onnx-whisper-tiny/test_wavs/0.wav";
|
||||
|
||||
WaveReader waveReader = new WaveReader(filename);
|
||||
|
||||
var s = slid.CreateStream();
|
||||
s.AcceptWaveform(waveReader.SampleRate, waveReader.Samples);
|
||||
var result = slid.Compute(s);
|
||||
Console.WriteLine($"Filename: {filename}");
|
||||
Console.WriteLine($"Detected language: {result.Lang}");
|
||||
}
|
||||
}
|
||||
|
||||
1
dotnet-examples/spoken-language-identification/WaveReader.cs
Symbolic link
1
dotnet-examples/spoken-language-identification/WaveReader.cs
Symbolic link
@@ -0,0 +1 @@
|
||||
../offline-decode-files/WaveReader.cs
|
||||
12
dotnet-examples/spoken-language-identification/run.sh
Executable file
12
dotnet-examples/spoken-language-identification/run.sh
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
if [ ! -d ./sherpa-onnx-whisper-tiny ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-whisper-tiny.tar.bz2
|
||||
tar xvf sherpa-onnx-whisper-tiny.tar.bz2
|
||||
rm sherpa-onnx-whisper-tiny.tar.bz2
|
||||
fi
|
||||
|
||||
dotnet run
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>spoken_language_identification</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="*" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user