使用 Web Speech API 和 ChatGPT API 开发一个智能语音机器人

sxkk20082年前知识分享188

前言

随着 AI 的不断发展,我们前端工程师也可以开发出一个智能语音机器人,下面是我开发的一个简单示例,大家可以访问这个视频地址查看效果。

原理

首先说一下这个 demo 的实现原理和步骤

  1. 我们使用 Web Speech API 获得输入的文本
  2. 将获得的文本作文 ChatGPT API 的 prompt 的输入
  3. 使用语音合成或者 微软的文字转语音服务,将文字作为语音输入

语音识别的功能在百度搜索页面就有,使用的是 Web Speech API

我们可以在 MDN 中查看这个 API 的使用

下面代码是一个简单示例

DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Web Speech API Demotitle>
  head>
  <body>
    <h1>Web Speech API Demoh1>
    <p>请说出一些文字:p>
    <textarea id="input" cols="50" rows="5">textarea>
    <br />
    <button id="speakBtn">语言合成button>
    <button id="transcribeBtn">语音识别button>
    <br />
    <p id="transcription">p>

    <script>
      const recognition = new webkitSpeechRecognition() // 实例化语音识别对象
      recognition.continuous = true // 连续识别,直到 stop() 被调用

      const transcribeBtn = document.getElementById('transcribeBtn')
      transcribeBtn.addEventListener('click', function () {
        recognition.start() // 开始语音识别
      })

      recognition.onresult = function (event) {
        let result = ''
        for (let i = event.resultIndex; i < event.results.length; i++) {
          result += event.results[i][0].transcript
        }
        const transcript = document.getElementById('transcription')
        transcript.innerHTML = result // 显示语音识别结果
      }

      const speakBtn = document.getElementById('speakBtn')
      speakBtn.addEventListener('click', function () {
        const text = document.getElementById('input').value // 获取文本框中的文本
        const msg = new SpeechSynthesisUtterance(text) // 实例化语音合成对象
        window.speechSynthesis.speak(msg) // 开始语音合成
      })
    script>
  body>
html>

这个例子很简单,点击语音识别可以将文字识别再文本框中。输入文字,电脑可以合成语音, 但是电脑合成的声音比较机械,不够逼真,因此我们可以使用微软的语音合成,大家可以访问这个地址体验。

https://speech.microsoft.com/audiocontentcreation

如果没有登录的话,只能试听,注册登录后就可以免费使用官方的 api 了

注册的话,大家只需要按照步骤注册就可以了,并且需要准备一张境外使用信用卡,注册后每月可以免费 50w 字的使用权限。

创建资源的时候选择 F0,创建完成后,就会有一个秘钥。

有了秘钥我们就可以将 chatGPT 返回的文字转成真人语音了,在 Github 上有代码示例

完整代码

DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Web Speech API Demotitle>
  head>
  <body>
    <h1>Web Speech API + ChatGPT APIh1>
    <button id="transcribeBtn">按住说话button>
    <br />
    <p id="transcription">p>
    <script src="https://aka.ms/csspeech/jsbrowserpackageraw">script>

    <script>
      async function requestOpenAI(content) {
        const BASE_URL = ``
        const OPENAI_API_KEY = 'sk-xxxxx'
        const messages = [
          {
            role: 'system',
            content: 'You are a helpful assistant',
          },
          { role: 'user', content },
        ]
        const res = await fetch(`${BASE_URL || 'https://api.openai.com'}/v1/chat/completions`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            authorization: `Bearer ${OPENAI_API_KEY}`,
          },
          body: JSON.stringify({
            model: 'gpt-3.5-turbo',
            messages,
            temperature: 0.7,
            top_p: 1,
            frequency_penalty: 0,
            presence_penalty: 0,
          }),
        })
        const response = await res.json()

        const result = response.choices[0].message.content
        console.log(result)
        return result
      }
      // 下载 mp3 文件
      function download(result) {
        const blob = new Blob([result.audioData])
        const url = URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = url
        link.download = 'filename.mp3' // set the filename here
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        URL.revokeObjectURL(url)
      }

      function synthesizeSpeech(text) {
        const sdk = SpeechSDK
        const speechConfig = sdk.SpeechConfig.fromSubscription('TTS_KEY', 'TTS_REGION')
        const audioConfig = sdk.AudioConfig.fromDefaultSpeakerOutput()

        const speechSynthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, audioConfig)
        // 可以更改 Ssml 来改变声音
        speechSynthesizer.speakSsmlAsync(
          `${text}`,
          (result) => {
            if (result) {
              speechSynthesizer.close()

              return result.audioData
            }
          },
          (error) => {
            console.log(error)
            speechSynthesizer.close()
          }
        )
      }

      const SpeechRecognition = window.SpeechRecognition || webkitSpeechRecognition

      const recognition = new SpeechRecognition() // 实例化语音识别对象
      recognition.continuous = true // 连续识别,直到 stop() 被调用
      recognition.lang = 'cmn-Hans-CN' // 普通话 (中国大陆)

      const transcribeBtn = document.getElementById('transcribeBtn')

      let record = false
      transcribeBtn.addEventListener('mousedown', function () {
        record = true
        recognition.start() // 开始语音识别
        console.log('开始语音识别')
        transcribeBtn.textContent = '正在录音...'
      })
      transcribeBtn.addEventListener('mouseup', function () {
        transcribeBtn.textContent = '按住说话'
        record = false

        recognition.stop()
      })
      recognition.onend = () => {
        console.log('停止语音识别')
        transcribeBtn.textContent = '开始'
        record = false
      }

      recognition.onerror = function (event) {
        console.log(event.error)
      }

      recognition.onresult = function (event) {
        console.log(event)
        let result = ''
        for (let i = event.resultIndex; i < event.results.length; i++) {
          result += event.results[i][0].transcript
        }
        console.log(result)
        const transcript = document.getElementById('transcription')
        const p = document.createElement('p')
        p.textContent = result
        transcript.appendChild(p) // 显示语音识别结果
        requestOpenAI(result).then((res) => {
          const p = document.createElement('p')
          p.textContent = res
          transcript.appendChild(p)
          synthesizeSpeech(res)
        })
      }
    script>
  body>
html>

上面代码中

以上就是本文的全部内容,如果对你有帮助,记得给个三连,感谢你的阅读。

本文正在参加「金石计划」

相关文章

AI智能机器人的兴起:探索当下科技发展新趋势

AI智能机器人的兴起:探索当下科技发展新趋势

  随着科技的不断进步,AI智能机器人成为了当下的热门话题。它们被用于各种领域,从设备保养到医疗保健再到商业流程优化,无所不包。这些智能机器人呈现出了很多独特的功能和特点,变...

盘点掘金 2021 高赞 React 文章

「React 进阶」 React 全部 api 解读+基础实践大全(夯实基础 2 万字总结)作者: 我不是外星人点赞 2556收藏 3283阅读 48506分类 前端React 开发必须知道的 34...

react + antd 实现

上面的代码只是实现了一个最简单的版本,样式也不够美观,因此我们可以使用 webpack + react + antd 来实现一个现代化的插件, 这里我使用一个之前创建的模版tampermonkey-starter

使用 antd 的 Popover 组件来显示,使用 react 重构下 js 代码,我们就可以实现如下效果。

image.png

基于 ChatGPT API 的划词翻译浏览器脚本实现

前言最近 GitHub 上有个基于 ChatGPT API 的浏览器脚本,openai-translator, 短时间内 star 冲到了 9.7k, 功能上除了支持翻译外,还支持润色和总结功能,除了...

语音合成技术:改变人机交互方式的创新

语音合成技术:改变人机交互方式的创新

  语音合成技术是一项用来将文本转化为声音的创新技术。随着科技的不断发展,语音合成技术正被广泛应用于各个领域,改变着人机交互的方式。本文将探讨语音合成技术的应用和前景,并对其...

智能智慧系统:打造智能化社会和智慧行业

智能智慧系统:打造智能化社会和智慧行业

  智能智慧系统是利用人工智能技术开发的智能化管理系统的核心机构。通过集成各类传感器、数据分析技术和自动控制系统,实现对智能设备及系统的智能化管理和优化。在智能智慧系统,我们...

人工智能培训:揭秘未来科技的智慧之路

人工智能培训:揭秘未来科技的智慧之路

  随着科技的飞速发展,人工智能已经成为改变世界的关键驱动力之一。无论是在工业生产、医疗保健、金融服务还是交通领域,人工智能的应用都取得了巨大的突破。为了满足这一需求,越来越...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。