Picovoice / porcupine

On-device wake word detection powered by deep learning
https://picovoice.ai/
Apache License 2.0
3.76k stars 498 forks source link

Getting 'INVALID_ARGUMENT' in React binding #958

Closed DukeFerdinand closed 1 year ago

DukeFerdinand commented 1 year ago

Make sure you have read the documentation, and have put forth a reasonable effort to find an existing answer.

Expected behaviour

usePorcupine hook's init should properly initialize porcupine on the frontend and allow me to use this service.

Actual behaviour

The WASM module throws an INVALID_ARGUMENT error every single way I've tried to initialize when calling init(). I was initially using Sveltekit with the generic Web SDK, but I switched to React because Pico officially supports React.

The actual error is pretty vague, so I can't give you more detail on what exactly is going wrong, but please read the "Notes/already tried" section below as I can tell you what's probably not going wrong.

[ERROR] loading parameters failed with 'INVALID_ARGUMENT'

Notes / already tried solutions

My full package.json:

{
  "name": "twitch-vox",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview",
    "tauri": "tauri"
  },
  "dependencies": {
    "@picovoice/porcupine-react": "^2.2.0",
    "@picovoice/web-voice-processor": "^4.0.6",
    "@tauri-apps/api": "^1.2.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@tauri-apps/cli": "^1.2.3",
    "@types/node": "^18.7.10",
    "@types/react": "^18.0.15",
    "@types/react-dom": "^18.0.6",
    "@vitejs/plugin-react": "^3.0.0",
    "typescript": "^4.9.5",
    "vite": "^4.2.1"
  }
}

Steps to reproduce the behaviour

Click the dropdown(s) to see code snippets

VoiceTest.tsx ```typescript import React, { useEffect } from "react"; import { usePorcupine } from "@picovoice/porcupine-react"; export function VoiceWidget() { const { keywordDetection, isLoaded, isListening, error, init, start, stop, release, } = usePorcupine(); const porcupineModel = { publicPath: "hey-bot-suero_en_wasm_v2_2_0.ppn", forceWrite: true, }; useEffect(() => { init( "YOUR OWN API KEY :)", [ { publicPath: "hey-bot-suero_en_wasm_v2_2_0.ppn", label: "Hey botsuro", }, ], porcupineModel ); }, []); useEffect(() => { if (keywordDetection !== null) { // ... use keyword detection result console.log(keywordDetection); } }, [keywordDetection]); // ... render component return (
{isLoaded &&

loaded

} {error &&

{error?.message}

}
); } ```
App.tsx ```typescript // Note: this import {VoiceWidget} from "./VoiceWidget" function App() { return (
{/* ...redacted for brevity ... */}
); } export default App; ```

Please let me know if you need any more details, or need a copy of my PPN file and I'll help as I'm able to! I really need this to work for a client of mine, and there don't seem to be any helpful tips for this in the FAQ. :)

mrrostam commented 1 year ago

@DukeFerdinand Could you please consider starting with our React demo project and modifying it to accept a custom keyword model, as explained in our documentation, instead of us attempting to replicate your project? This way we can identify any potential issues with our React binding more quickly and easily.

DukeFerdinand commented 1 year ago

Sure! I'll try out the official solution just to make sure it's not on me. I'll update this issue when I've done that :)

DukeFerdinand commented 1 year ago

I was not able to reproduce the INVALID_ARGUMENT problem using the official React demo. The official React demo code also works when copy-pasting the VoiceWidget into an unrelated standard Next.js app, so any issues here are not caused by Porcupine :)

Are PRs welcome in the off chance I can get a Tauri (Rust + React) app working for a native desktop app project? I feel like that could be a good demo for future users.

Also as a stretch goal would it be possible to identify permission problems when calling the init function from Porcupine? I'm not 100% sure, but that INVALID_ARGUMENT feels like it's caused by incorrect permissions or missing window properties when running React in a "native" window.

mrrostam commented 1 year ago

We always welcome PRs here. To ensure consistency, we request that you refer to our existing demos and follow the same format. Additionally, for each demo, we do have Github actions that build and test it after every release or any updates to maintain their quality.