结合SK和ChatGLM3B+whisper+Avalonia实现语音切换城市

先创建一个Avalonia的MVVM项目模板,项目名称GisApp

项目创建完成以后添加以下nuget依赖

<PackageReference Include="Mapsui.Avalonia" Version="4.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta8" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="Whisper.net" Version="1.5.0" />
<PackageReference Include="Whisper.net.Runtime" Version="1.5.0" />
  • Mapsui.Avalonia是Avalonia的一个Gis地图组件
  • Microsoft.Extensions.DependencyInjection用于构建一个DI容器
  • Microsoft.Extensions.Http用于注册一个HttpClient工厂
  • Microsoft.SemanticKernel则是SK用于构建AI插件
  • NAudio是一个用于录制语音的工具包
  • Whisper.net是一个.NET的Whisper封装Whisper用的是OpenAI开源的语音识别模型
  • Whisper.net.Runtime属于Whisper

修改App.cs

打开App.cs,修改成以下代码

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
} public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var services = new ServiceCollection();
services.AddSingleton<MainWindow>((services) => new MainWindow(services.GetRequiredService<IKernel>(), services.GetRequiredService<WhisperProcessor>())
{
DataContext = new MainWindowViewModel(),
});
services.AddHttpClient(); var openAIHttpClientHandler = new OpenAIHttpClientHandler();
var httpClient = new HttpClient(openAIHttpClientHandler);
services.AddTransient<IKernel>((serviceProvider) =>
{
return new KernelBuilder()
.WithOpenAIChatCompletionService("gpt-3.5-turbo-16k", "fastgpt-zE0ub2ZxvPMwtd6XYgDX8jyn5ubiC",
httpClient: httpClient)
.Build();
}); services.AddSingleton(() =>
{
var ggmlType = GgmlType.Base;
// 定义使用模型
var modelFileName = "ggml-base.bin"; return WhisperFactory.FromPath(modelFileName).CreateBuilder()
.WithLanguage("auto") // auto则是自动识别语言
.Build();
}); var serviceProvider = services.BuildServiceProvider(); desktop.MainWindow = serviceProvider.GetRequiredService<MainWindow>();
} base.OnFrameworkInitializationCompleted();
}
}

OpenAIHttpClientHandler.cs,这个文件是用于修改SK的访问地址,默认的SK只支持OpenAI官方的地址并且不能进行修改!

public class OpenAIHttpClientHandler : HttpClientHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.RequestUri.LocalPath == "/v1/chat/completions")
{
var uriBuilder = new UriBuilder("http://您的ChatGLM3B地址/api/v1/chat/completions");
request.RequestUri = uriBuilder.Uri;
} return base.SendAsync(request, cancellationToken);
}
}

修改ViewModels/MainWindowViewModel.cs

public class MainWindowViewModel : ViewModelBase
{
private string subtitle = string.Empty; public string Subtitle
{
get => subtitle;
set => this.RaiseAndSetIfChanged(ref subtitle, value);
} private Bitmap butBackground; public Bitmap ButBackground
{
get => butBackground;
set => this.RaiseAndSetIfChanged(ref butBackground, value);
}
}
  • ButBackground是显示麦克风图标的写到模型是为了切换图标
  • Subtitle用于显示识别的文字

添加SK插件

创建文件/plugins/MapPlugin/AcquireLatitudeLongitude/config.json:这个是插件的相关配置信息

{
"schema": 1,
"type": "completion",
"description": "获取坐标",
"completion": {
"max_tokens": 1000,
"temperature": 0.3,
"top_p": 0.0,
"presence_penalty": 0.0,
"frequency_penalty": 0.0
},
"input": {
"parameters": [
{
"name": "input",
"description": "获取坐标",
"defaultValue": ""
}
]
}
}

创建文件/plugins/MapPlugin/AcquireLatitudeLongitude/skprompt.txt:下面是插件的prompt,通过以下内容可以提取用户城市然后得到城市的经纬度

请返回{{$input}}的经纬度然后返回以下格式,不要回复只需要下面这个格式:
{
"latitude":"",
"longitude":""
}

修改Views/MainWindow.axaml代码,将[素材](# 素材)添加到Assets中,

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:GisApp.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="GisApp.Views.MainWindow"
x:DataType="vm:MainWindowViewModel"
Icon="/Assets/avalonia-logo.ico"
Width="800"
Height="800"
Title="GisApp">
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
<Grid>
<Grid Name="MapStackPanel">
</Grid>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom" Background="Transparent" Margin="25">
<TextBlock Foreground="Black" Text="{Binding Subtitle}" Width="80" TextWrapping="WrapWithOverflow" Padding="8">
</TextBlock>
<Button Width="60" Click="Button_OnClick" Background="Transparent" VerticalAlignment="Center" HorizontalAlignment="Center">
<Image Name="ButBackground" Source="{Binding ButBackground}" Height="40" Width="40"></Image>
</Button>
</StackPanel>
</Grid>
</Window>

修改Views/MainWindow.axaml.cs代码

public partial class MainWindow : Window
{
private bool openVoice = false; private WaveInEvent waveIn; private readonly IKernel _kernel;
private readonly WhisperProcessor _processor; private readonly Channel<string> _channel = Channel.CreateUnbounded<string>();
private MapControl mapControl; public MainWindow(IKernel kernel, WhisperProcessor processor)
{
_kernel = kernel;
_processor = processor;
InitializeComponent();
mapControl = new MapControl();
// 默认定位到深圳
mapControl.Map = new Map()
{
CRS = "EPSG:3857",
Home = n =>
{
var centerOfLondonOntario = new MPoint(114.06667, 22.61667);
var sphericalMercatorCoordinate = SphericalMercator
.FromLonLat(centerOfLondonOntario.X, centerOfLondonOntario.Y).ToMPoint();
n.ZoomToLevel(15);
n.CenterOnAndZoomTo(sphericalMercatorCoordinate, n.Resolutions[15]);
}
};
mapControl.Map?.Layers.Add(Mapsui.Tiling.OpenStreetMap.CreateTileLayer());
MapStackPanel.Children.Add(mapControl); DataContextChanged += (sender, args) =>
{
using var voice = AssetLoader.Open(new Uri("avares://GisApp/Assets/voice.png")); ViewModel.ButBackground = new Avalonia.Media.Imaging.Bitmap(voice);
}; Task.Factory.StartNew(ReadMessage);
} private MainWindowViewModel ViewModel => (MainWindowViewModel)DataContext; private void Button_OnClick(object? sender, RoutedEventArgs e)
{
if (openVoice)
{
using var voice = AssetLoader.Open(new Uri("avares://GisApp/Assets/voice.png")); ViewModel.ButBackground = new Avalonia.Media.Imaging.Bitmap(voice); waveIn.StopRecording();
}
else
{
using var voice = AssetLoader.Open(new Uri("avares://GisApp/Assets/open-voice.png")); ViewModel.ButBackground = new Avalonia.Media.Imaging.Bitmap(voice); // 获取当前麦克风设备
waveIn = new WaveInEvent();
waveIn.DeviceNumber = 0; // 选择麦克风设备,0通常是默认设备 WaveFileWriter writer = new WaveFileWriter("recorded.wav", waveIn.WaveFormat); // 设置数据接收事件
waveIn.DataAvailable += (sender, a) =>
{
Console.WriteLine($"接收到音频数据: {a.BytesRecorded} 字节");
writer.Write(a.Buffer, 0, a.BytesRecorded);
if (writer.Position > waveIn.WaveFormat.AverageBytesPerSecond * 30)
{
waveIn.StopRecording();
}
}; // 录音结束事件
waveIn.RecordingStopped += async (sender, e) =>
{
writer?.Dispose();
writer = null; waveIn.Dispose(); await using var fileStream = File.OpenRead("recorded.wav");
using var wavStream = new MemoryStream(); await using var reader = new WaveFileReader(fileStream);
var resampler = new WdlResamplingSampleProvider(reader.ToSampleProvider(), 16000);
WaveFileWriter.WriteWavFileToStream(wavStream, resampler.ToWaveProvider16()); wavStream.Seek(0, SeekOrigin.Begin); await Dispatcher.UIThread.InvokeAsync(() => { ViewModel.Subtitle = string.Empty; }); string text = string.Empty;
await foreach (var result in _processor.ProcessAsync(wavStream))
{
await Dispatcher.UIThread.InvokeAsync(() => { ViewModel.Subtitle += text += result.Text; });
} _channel.Writer.TryWrite(text);
}; Console.WriteLine("开始录音...");
waveIn.StartRecording();
} openVoice = !openVoice;
} private async Task ReadMessage()
{
try
{
var pluginsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "plugins"); var chatPlugin = _kernel
.ImportSemanticFunctionsFromDirectory(pluginsDirectory, "MapPlugin"); // 循环读取管道中的数据
while (await _channel.Reader.WaitToReadAsync())
{
// 读取管道中的数据
while (_channel.Reader.TryRead(out var message))
{
// 使用AcquireLatitudeLongitude插件,解析用户输入的地点,然后得到地点的经纬度
var value = await _kernel.RunAsync(new ContextVariables
{
["input"] = message
}, chatPlugin["AcquireLatitudeLongitude"]); // 解析字符串成模型
var acquireLatitudeLongitude =
JsonSerializer.Deserialize<AcquireLatitudeLongitude>(value.ToString()); // 使用MapPlugin插件,定位到用户输入的地点
var centerOfLondonOntario = new MPoint(acquireLatitudeLongitude.longitude, acquireLatitudeLongitude.latitude);
var sphericalMercatorCoordinate = SphericalMercator
.FromLonLat(centerOfLondonOntario.X, centerOfLondonOntario.Y).ToMPoint(); // 默认使用15级缩放
mapControl.Map.Navigator.ZoomToLevel(15);
mapControl.Map.Navigator.CenterOnAndZoomTo(sphericalMercatorCoordinate, mapControl.Map.Navigator.Resolutions[15]); }
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
} public class AcquireLatitudeLongitude
{
public double latitude { get; set; }
public double longitude { get; set; }
}
}

流程讲解:

  1. 用户点击了录制按钮触发了Button_OnClick事件,然后在Button_OnClick事件中会打开用户的麦克风,打开麦克风进行录制,在录制结束事件中使用录制完成产生的wav文件,然后拿到Whisper进行识别,识别完成以后会将识别结果写入到_channel
  2. ReadMessage则是一直监听_channel的数据,当有数据写入,这里则会读取到,然后就将数据使用下面的sk执行AcquireLatitudeLongitude函数。
 var value = await _kernel.RunAsync(new ContextVariables
{
["input"] = message
}, chatPlugin["AcquireLatitudeLongitude"]);
  1. 在解析value得到用户的城市经纬度
  2. 通过mapControl.Map.Navigator修改到指定经纬度。

完整的操作流程就完成了,当然实际业务会比这个更复杂。

素材



分享总结

讨论总结:

在本次会议中,讨论了如何结合SK、ChatGLM3B、Whisper和Avalonia来实现语音切换城市的功能。具体讨论了创建Avalonia的MVVM项目模板,添加了相关的NuGet依赖,修改了App.cs、ViewModels/MainWindowViewModel.cs以及添加了SK插件的相关配置和文件。

行动项目:

  1. 创建Avalonia的MVVM项目模板,项目名称为GisApp
  2. 添加所需的NuGet依赖,包括Mapsui.Avalonia, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.Http, Microsoft.SemanticKernel, NAudio, Whisper.netWhisper.net.Runtime
  3. 修改App.csOpenAIHttpClientHandler.csViewModels/MainWindowViewModel.cs以及相关的视图文件。
  4. 添加SK插件,包括创建相关的配置信息和prompt文件。
  5. 实现录制语音、语音识别和切换城市的功能流程。

技术交流群:737776595

结合SK和ChatGLM3B+whisper+Avalonia实现语音切换城市的更多相关文章

  1. Linux ALSA音频库(二) 环境测试+音频合成+语音切换 项目代码分享

    1. 环境测试 alsa_test.c #include <alsa/asoundlib.h> #include <stdio.h> // 官方测试代码, 运行后只要有一堆信息 ...

  2. iOS OC环信实时语音切换听筒免提听不到声音报错:AVAudioSessionErrorCodeBadParam

    出现这个报错:AVAudioSessionErrorCodeBadParam 先看看你的问题是不是在切换听筒免提的时候 听不到声音了, 不是的可以继续搜索去了   问题在这里 把圈住的那个货换成这个就 ...

  3. extjs实现多国语音切换

    http://kuyur.info/blog/archives/2490 http://blog.chinaunix.net/uid-28661623-id-3779637.html http://b ...

  4. 闻其声而知雅意,基于Pytorch(mps/cpu/cuda)的人工智能AI本地语音识别库Whisper(Python3.10)

    前文回溯,之前一篇:含辞未吐,声若幽兰,史上最强免费人工智能AI语音合成TTS服务微软Azure(Python3.10接入),利用AI技术将文本合成语音,现在反过来,利用开源库Whisper再将语音转 ...

  5. 基于Matlab的MMSE的语音增强算法的研究

    本课题隶属于学校的创新性课题研究项目.2012年就已经做完了,今天一并拿来发表.   目录: --基于谱减法的语音信号增强算法..................................... ...

  6. Chapter 3:Speech Production and Perception

    作者:桂. 时间:2017-05-24  09:09:36 主要是<Speech enhancement: theory and practice>的读书笔记,全部内容可以点击这里. 一. ...

  7. 开源不到 48 小时获 35k star 的推荐算法「GitHub 热点速览」

    本周的热点除了 GPT 各类衍生品之外,还多了一个被马斯克预告过.在愚人节开源出来的推特推荐算法,开源不到 2 天就有了 35k+ 的 star,有意思的是,除了推荐算法本身之外,阅读源码的工程师们甚 ...

  8. 天气预报API(三):免费接口测试(“旧编码”)

    说明 我以参考文章为引子,自己测试并扩展,努力寻找更多的气象API... 本文所有测试均以青岛为例. 本文所列接口城市代码(cityid)参数都使用的 "旧编码": 全国城市代码列 ...

  9. WP8.1开发者预览版本号已知 Bug

    偶的 Lumia 920 已经升级到最新的 8.1 开发者预览版本号,使用中没有发现什么问题. 可能是由于偶玩手机的情况比較少吧!忽然看到 MS 停止此版本号的更新,并说明有非常多的 BUG,偶就郁闷 ...

  10. Python 简单的天气预报

    轻巧的树莓派一直是大家的热爱,在上面开发一些小东西让我们很有成就感,而在linux下,python能使麻烦的操作变得简单,而树莓派功耗还很低,相结合,完美! 1,直接进入正题,一般在linux或树莓派 ...

随机推荐

  1. 使用SPEL自定义表达式

    自定义表达式 Spring提供了一个可以自定义表达式的接口 package com.qbb.qmall.item; import org.junit.Test; import org.springfr ...

  2. Gh0st木马

    https://www.secrss.com/articles/50209 Gh0st是一种远程控制软件,它可以在被攻击的计算机上运行并允许攻击者远程控制该计算机.为了查找Gh0st的进程.文件.注册 ...

  3. Go 自动补全gocode

    go语言自动补全代码,需要添加gocode的程序. 执行: go get github.com/nsf/gocode 一般来说,gocode的源码会在$GOPATH/src/github.com/ns ...

  4. BFS(一)单词接龙

    对应 LeetCode 127 单词接龙 问题定义 给定一个字典序列 wordList,一个初始的单词 beginWord 和一个目标单词 endWord,现在要求每次变换满足以下条件将 beginW ...

  5. 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 12.认识GamePlayTag, 实现技能的互斥

    斯坦福课程 UE4 C++ ActionRoguelike游戏实例教程 0.绪论 概述 本篇文章对应Lecture 17 - GameplayTags, 67.67节.本文将会讲述UE4中Gamepl ...

  6. 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 06.敲定AI——游戏框架拓展和细节优化

    斯坦福课程 UE4 C++ ActionRoguelike游戏实例教程 0.绪论 概述 这篇文章对应课程13课, 50~54节.虽然标题是敲定AI,实际内容和AI关联并不大,主要工作是对游戏内各种细节 ...

  7. API安全技术

    自己在日常工作中会涉及到些安全的概念,但是没有成体系,因此最近研读了<API安全技术与实战>一书,在此做些文章记录. API安全是从安全的角度关注API领域的安全问题和这些问题的解决方案, ...

  8. 云小课|MRS基础操作之配置DataNode容量均衡

    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:当HDFS集群出现 ...

  9. 海量监控数据处理如何做,看华为云SRE案例分享

    摘要:openGemini的设计和优化都是根据时序数据特点而来,在面对海量运维监控数据处理需求时,openGemini显然更加有针对性. IT运维诞生于最早的信息化时代.在信息化时代,企业的信息化系统 ...

  10. AI论文解读丨融合视觉、语义、关系多模态信息的文档版面分析架构VSR

    摘要:文档版式分析任务中,文档的视觉信息.文本信息.各版式部件间的关系信息都对分析过程具有很重要的作用.本文提出一种融合视觉.文本.关系多模态信息的版式分析架构VSR. 本文分享自华为云社区<论 ...