@chengsokdara/use-whisper

中文文档
0.2.0发布于 2023/03/18MITTypeScriptESM + CJS近 30 天下载 4427

反应 Hook for OpenAI Whisper API 配有语音录音机并内置去声去声.

中文使用文档
文档目录8 节
  1. 演示
  2. 通知
  3. 安装
  4. 使用量
  5. 实例
  6. 依赖关系
  7. API 密码
  8. 路线图

使用低声

反应 Hook for OpenAI Whisper API 有语音记录器,实时转录以及内置的静音去除


  • 演示

  • 实时复制演示

https://user-images.githubusercontent.com/2707253/224465747-0b1ee159-21dd-4cd0-af9d-6fc9b882d716.mp4 请检查isbn=值 (帮助)


  • 通知

    正在开发使用Whisper for React Independent.

仓库 : https://github.com/cungsokdara/use-whiper-native 互联网档案馆的存檔,存档日期2013-12-22

进展: https://github.com/cungsokdara/use-whiper-native/issues/1 (中文(简体) )

  • 安装

npm i @chengsokdara/use-whisper
yarn add @chengsokdara/use-whisper
  • 使用量

import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  const {
    recording,
    speaking,
    transcribing,
    transcript,
    pauseRecording,
    startRecording,
    stopRecording,
  } = useWhisper({
    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
  })

  return (
    <div>
      <p>Recording: {recording}</p>
      <p>Speaking: {speaking}</p>
      <p>Transcribing: {transcribing}</p>
      <p>Transcribed Text: {transcript.text}</p>
      <button onClick={() => startRecording()}>Start</button>
      <button onClick={() => pauseRecording()}>Pause</button>
      <button onClick={() => stopRecording()}>Stop</button>
    </div>
  )
}
  • 自定义服务器( 保留 OpenAI API 令牌安全)
import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  /**
   * you have more control like this
   * do whatever you want with the recorded speech
   * send it to your own custom server
   * and return the response back to useWhisper
   */
  const onTranscribe = (blob: Blob) => {
    const base64 = await new Promise<string | ArrayBuffer | null>(
      (resolve) => {
        const reader = new FileReader()
        reader.onloadend = () => resolve(reader.result)
        reader.readAsDataURL(blob)
      }
    )
    const body = JSON.stringify({ file: base64, model: 'whisper-1' })
    const headers = { 'Content-Type': 'application/json' }
    const { default: axios } = await import('axios')
    const response = await axios.post('/api/whisper', body, {
      headers,
    })
    const { text } = await response.data
    // you must return result from your server in Transcript format
    return {
      blob,
      text,
    }
  }

  const { transcript } = useWhisper({
    // callback to handle transcription with custom server
    onTranscribe,
  })

  return (
    <div>
      <p>{transcript.text}</p>
    </div>
  )
}
  • 实例

  • 实时流线记录
import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  const { transcript } = useWhisper({
    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
    streaming: true,
    timeSlice: 1_000, // 1 second
    whisperConfig: {
      language: 'en',
    },
  })

  return (
    <div>
      <p>{transcript.text}</p>
    </div>
  )
}
  • 在发送到 Whisper 以节省费用之前删除沉默
import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  const { transcript } = useWhisper({
    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
    // use ffmpeg-wasp to remove silence from recorded speech
    removeSilence: true,
  })

  return (
    <div>
      <p>{transcript.text}</p>
    </div>
  )
}
  • 在挂载的组件上自动开始记录
import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  const { transcript } = useWhisper({
    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
    // will auto start recording speech upon component mounted
    autoStart: true,
  })

  return (
    <div>
      <p>{transcript.text}</p>
    </div>
  )
}
  • 只要用户在说话,就继续录制
import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  const { transcript } = useWhisper({
    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
    nonStop: true, // keep recording as long as the user is speaking
    stopTimeout: 5000, // auto stop after 5 seconds
  })

  return (
    <div>
      <p>{transcript.text}</p>
    </div>
  )
}
  • 自定义自定义自定义 API 配置, 当 autoTransis 属实时
import { useWhisper } from '@chengsokdara/use-whisper'

const App = () => {
  const { transcript } = useWhisper({
    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN
    autoTranscribe: true,
    whisperConfig: {
      prompt: 'previous conversation', // you can pass previous conversation for context
      response_format: 'text', // output text instead of json
      temperature: 0.8, // random output
      language: 'es', // Spanish
    },
  })

  return (
    <div>
      <p>{transcript.text}</p>
    </div>
  )
}
  • 依赖关系

    • @cungsokdara/反应-呼-sync 同步反应钩
    • 记录曲c: 交叉浏览器录音机
    • 跛脚 将 wav 编码为 mp3 用于交叉浏览器支持
    • @ffmpeg/ffmpeg: (中文(简体) ) 用于消音功能
    • 汉克: 用于语音检测
    • 轴 : 因为抓取与 Whisper 结束点无关

这些依赖性大多是懒惰地装入的,所以只有在需要时才导入

  • API 密码

  • 配置对象
名称类型默认值说明
键字符串''您的 OpenAI API 令牌
自动启动布尔虚假在组件挂载上自动启动语音记录
自动编译布尔真实停止录制后应自动转录
模式字符串转录控制 Whisper 模式或翻录或翻译,目前只支持向英语翻译
不停止布尔虚假如果真实, 记录会在停止时自动停止 。 但是,如果用户继续说话,录音机会继续录音
删除静态布尔虚假在发送文件到 OpenAI API 之前去除沉默
停止超时编号5 000毫瓦时如果非停止是真实的,则需要这样做。 记录器自动停止时的此控制
流线布尔虚假基于时间切换的实时语音
时间编号1,000 毫秒( x)数据辅助事件之间的间隔
微声配置微声阿皮克未定义微声 API 复制配置
可读数据(blob: Blob) 无效未定义获取时间间隔中记录blob的回调功能Slich
打开(blob:blob) => 承诺 <语录> 翻译:未定义在自定义服务器上处理转录的调用功能
  • 微声阿皮克
名称类型默认值说明
提示字符串未定义一个可选文本来指导模型的风格或延续之前的音频段. 提示应当与音频语言相匹配.
响应( f)字符串乔森笔录输出的格式,在其中一种选项:json,文本,srt,动词-json,或vtt.
温度编号0 个采样温度在0到1之间. 像0.8这样的更高值会使输出更加随机,而像0.2这样的更低值会使它更有重点和决定力. 如果设置为 0, 模型将使用 日志概率 以自动提高温度,直到某些阈值被击中.
语言字符串内输入音频的语言. 提供输入语言 ISO-639-1号标准 格式将提高准确性和耐久性.
  • 返回对象
名称类型说明
录音布尔语音录音状态
说话布尔检测用户说话时
转录布尔并同时删除语音中的静音, 并向 OpenAI Whisper API 发送请求
记录语录读取完成后返回对象
暂停记录答应暂停语音录音
开始记录答应开始语音录音
停止记录答应停止语音录音
  • 语录
名称类型说明
块块在 JavaScript Blob 中录制的语音
文本字符串从 Whisper API 返回的转写文本
  • 路线图

    • 反应-内在支持,将导出为 use-whiper/内在

使用 React 或 React Natural 联系我开发网络或移动应用程序
https://cungsokdara.github.io (英语)

版本与发布记录

@chengsokdara/use-whisper 共发布 16 个版本,首次发布于 2023/03/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.0latest2023/03/183 年前
0.1.22023/03/153 年前
0.1.12023/03/143 年前
0.1.02023/03/113 年前
0.0.122023/03/093 年前
0.0.112023/03/093 年前

关于 @chengsokdara/use-whisper 的常见问题

@chengsokdara/use-whisper 是什么?

反应 Hook for OpenAI Whisper API 配有语音录音机并内置去声去声.

如何安装 @chengsokdara/use-whisper?

在项目目录运行 npm install @chengsokdara/use-whisper,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chengsokdara/use-whisper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chengsokdara/use-whisper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chengsokdara/use-whisper 可以商用吗?

@chengsokdara/use-whisper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chengsokdara/use-whisper 的下载量有多少?

根据 npm 官方下载统计,@chengsokdara/use-whisper 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,427 次,日均约 148 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chengsokdara/use-whisper 还在维护吗?

最新版本 0.2.0 发布于 2023/03/18(3 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/03/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。