{"id":13523482,"url":"https://github.com/chengsokdara/use-whisper","last_synced_at":"2025-05-15T17:04:28.323Z","repository":{"id":119360476,"uuid":"610373872","full_name":"chengsokdara/use-whisper","owner":"chengsokdara","description":"React hook for OpenAI Whisper with speech recorder, real-time transcription, and silence removal built-in","archived":false,"fork":false,"pushed_at":"2024-04-30T12:12:27.000Z","size":319,"stargazers_count":767,"open_issues_count":36,"forks_count":141,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-05-11T06:41:27.775Z","etag":null,"topics":["api","hook","openai","react","real-time","whisper"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chengsokdara.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-06T16:37:33.000Z","updated_at":"2025-04-30T14:52:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"f7728612-9e63-4300-aaf4-858e3470e5d1","html_url":"https://github.com/chengsokdara/use-whisper","commit_stats":{"total_commits":36,"total_committers":1,"mean_commits":36.0,"dds":0.0,"last_synced_commit":"8d2df1000d6d1e5be947a7c21030eeee452748f2"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengsokdara%2Fuse-whisper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengsokdara%2Fuse-whisper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengsokdara%2Fuse-whisper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengsokdara%2Fuse-whisper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chengsokdara","download_url":"https://codeload.github.com/chengsokdara/use-whisper/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384987,"owners_count":22062422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","hook","openai","react","real-time","whisper"],"created_at":"2024-08-01T06:01:00.540Z","updated_at":"2025-05-15T17:04:26.083Z","avatar_url":"https://github.com/chengsokdara.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","Applications","Packages"],"sub_categories":["JavaScript"],"readme":"# useWhisper\n\nReact Hook for OpenAI Whisper API with speech recorder, real-time transcription and silence removal built-in\n\n---\n\n- ### Demo\n\n- ###### Real-Time transcription demo\n\nhttps://user-images.githubusercontent.com/2707253/224465747-0b1ee159-21dd-4cd0-af9d-6fc9b882d716.mp4\n\n---\n\n- ### Announcement\n\n  useWhisper for React Native is being developed.\n\nRepository: [https://github.com/chengsokdara/use-whisper-native](https://github.com/chengsokdara/use-whisper-native)\n\nProgress: [https://github.com/chengsokdara/use-whisper-native/issues/1](https://github.com/chengsokdara/use-whisper-native/issues/1)\n\n- ### Install\n\n```\nnpm i @chengsokdara/use-whisper\n```\n\n```\nyarn add @chengsokdara/use-whisper\n```\n\n- ### Usage\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  const {\n    recording,\n    speaking,\n    transcribing,\n    transcript,\n    pauseRecording,\n    startRecording,\n    stopRecording,\n  } = useWhisper({\n    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003eRecording: {recording}\u003c/p\u003e\n      \u003cp\u003eSpeaking: {speaking}\u003c/p\u003e\n      \u003cp\u003eTranscribing: {transcribing}\u003c/p\u003e\n      \u003cp\u003eTranscribed Text: {transcript.text}\u003c/p\u003e\n      \u003cbutton onClick={() =\u003e startRecording()}\u003eStart\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e pauseRecording()}\u003ePause\u003c/button\u003e\n      \u003cbutton onClick={() =\u003e stopRecording()}\u003eStop\u003c/button\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ###### Custom Server (keep OpenAI API token secure)\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  /**\n   * you have more control like this\n   * do whatever you want with the recorded speech\n   * send it to your own custom server\n   * and return the response back to useWhisper\n   */\n  const onTranscribe = (blob: Blob) =\u003e {\n    const base64 = await new Promise\u003cstring | ArrayBuffer | null\u003e(\n      (resolve) =\u003e {\n        const reader = new FileReader()\n        reader.onloadend = () =\u003e resolve(reader.result)\n        reader.readAsDataURL(blob)\n      }\n    )\n    const body = JSON.stringify({ file: base64, model: 'whisper-1' })\n    const headers = { 'Content-Type': 'application/json' }\n    const { default: axios } = await import('axios')\n    const response = await axios.post('/api/whisper', body, {\n      headers,\n    })\n    const { text } = await response.data\n    // you must return result from your server in Transcript format\n    return {\n      blob,\n      text,\n    }\n  }\n\n  const { transcript } = useWhisper({\n    // callback to handle transcription with custom server\n    onTranscribe,\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{transcript.text}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ### Examples\n\n- ###### Real-time streaming trascription\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  const { transcript } = useWhisper({\n    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN\n    streaming: true,\n    timeSlice: 1_000, // 1 second\n    whisperConfig: {\n      language: 'en',\n    },\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{transcript.text}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ###### Remove silence before sending to Whisper to save cost\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  const { transcript } = useWhisper({\n    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN\n    // use ffmpeg-wasp to remove silence from recorded speech\n    removeSilence: true,\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{transcript.text}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ###### Auto start recording on component mounted\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  const { transcript } = useWhisper({\n    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN\n    // will auto start recording speech upon component mounted\n    autoStart: true,\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{transcript.text}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ###### Keep recording as long as the user is speaking\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  const { transcript } = useWhisper({\n    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN\n    nonStop: true, // keep recording as long as the user is speaking\n    stopTimeout: 5000, // auto stop after 5 seconds\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{transcript.text}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ###### Customize Whisper API config when autoTranscribe is true\n\n```jsx\nimport { useWhisper } from '@chengsokdara/use-whisper'\n\nconst App = () =\u003e {\n  const { transcript } = useWhisper({\n    apiKey: process.env.OPENAI_API_TOKEN, // YOUR_OPEN_AI_TOKEN\n    autoTranscribe: true,\n    whisperConfig: {\n      prompt: 'previous conversation', // you can pass previous conversation for context\n      response_format: 'text', // output text instead of json\n      temperature: 0.8, // random output\n      language: 'es', // Spanish\n    },\n  })\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003e{transcript.text}\u003c/p\u003e\n    \u003c/div\u003e\n  )\n}\n```\n\n- ### Dependencies\n\n  - **@chengsokdara/react-hooks-async** asynchronous react hooks\n  - **recordrtc:** cross-browser audio recorder\n  - **lamejs** encode wav into mp3 for cross-browser support\n  - **@ffmpeg/ffmpeg:** for silence removal feature\n  - **hark:** for speaking detection\n  - **axios:** since fetch does not work with Whisper endpoint\n\n_most of these dependecies are lazy loaded, so it is only imported when it is needed_\n\n- ### API\n\n- ###### Config Object\n\n| Name            | Type                                               | Default Value  | Description                                                                                                          |\n| --------------- | -------------------------------------------------- | -------------- | -------------------------------------------------------------------------------------------------------------------- |\n| apiKey          | string                                             | ''             | your OpenAI API token                                                                                                |\n| autoStart       | boolean                                            | false          | auto start speech recording on component mount                                                                       |\n| autoTranscribe  | boolean                                            | true           | should auto transcribe after stop recording                                                                          |\n| mode            | string                                             | transcriptions | control Whisper mode either transcriptions or translations, currently only support translation to English            |\n| nonStop         | boolean                                            | false          | if true, record will auto stop after stopTimeout. However if user keep on speaking, the recorder will keep recording |\n| removeSilence   | boolean                                            | false          | remove silence before sending file to OpenAI API                                                                     |\n| stopTimeout     | number                                             | 5,000 ms       | if nonStop is true, this become required. This control when the recorder auto stop                                   |\n| streaming       | boolean                                            | false          | transcribe speech in real-time based on timeSlice                                                                    |\n| timeSlice       | number                                             | 1000 ms        | interval between each onDataAvailable event                                                                          |\n| whisperConfig   | [WhisperApiConfig](#whisperapiconfig)              | undefined      | Whisper API transcription config                                                                                     |\n| onDataAvailable | (blob: Blob) =\u003e void                               | undefined      | callback function for getting recorded blob in interval between timeSlice                                            |\n| onTranscribe    | (blob: Blob) =\u003e Promise\u003c[Transcript](#transcript)\u003e | undefined      | callback function to handle transcription on your own custom server                                                  |\n\n- ###### WhisperApiConfig\n\n| Name            | Type   | Default Value | Description                                                                                                                                                                                                                                                                                                                                               |\n| --------------- | ------ | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| prompt          | string | undefined     | An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.                                                                                                                                                                                                                             |\n| response_format | string | json          | The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.                                                                                                                                                                                                                                                      |\n| temperature     | number | 0             | The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit. |\n| language        | string | en            | The language of the input audio. Supplying the input language in [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) format will improve accuracy and latency.                                                                                                                                                                             |\n\n- ###### Return Object\n\n| Name           | Type                      | Description                                                               |\n| -------------- | ------------------------- | ------------------------------------------------------------------------- |\n| recording      | boolean                   | speech recording state                                                    |\n| speaking       | boolean                   | detect when user is speaking                                              |\n| transcribing   | boolean                   | while removing silence from speech and send request to OpenAI Whisper API |\n| transcript     | [Transcript](#transcript) | object return after Whisper transcription complete                        |\n| pauseRecording | Promise                   | pause speech recording                                                    |\n| startRecording | Promise                   | start speech recording                                                    |\n| stopRecording  | Promise                   | stop speech recording                                                     |\n\n- ###### Transcript\n\n| Name | Type   | Description                                |\n| ---- | ------ | ------------------------------------------ |\n| blob | Blob   | recorded speech in JavaScript Blob         |\n| text | string | transcribed text returned from Whisper API |\n\n- ### Roadmap\n\n  - react-native support, will be export as use-whisper/native\n\n---\n\n**_Contact me for web or mobile app development using React or React Native_**  \n[https://chengsokdara.github.io](https://chengsokdara.github.io)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchengsokdara%2Fuse-whisper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchengsokdara%2Fuse-whisper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchengsokdara%2Fuse-whisper/lists"}