使用低声
反应 Hook for OpenAI Whisper API 有语音记录器,实时转录以及内置的静音去除
-
演示
-
实时复制演示
-
通知
正在开发使用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 (英语)