Skip to content

Commit

Permalink
add c demo test in python
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 committed Aug 25, 2023
1 parent f13201c commit c15c26e
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 65 deletions.
40 changes: 0 additions & 40 deletions .github/actions/c-demo/action.yml

This file was deleted.

65 changes: 41 additions & 24 deletions .github/workflows/c-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@ name: C Demos
on:
workflow_dispatch:
push:
branches: [ main ]
branches: [ master ]
paths:
- 'demo/c/**'
- '!demo/c/README.md'
- 'res/audio/**'
- '.github/workflows/c-demos.yml'

- 'demo/c/**'
- 'lib/beaglebone/**'
- 'lib/common/**'
- 'lib/jetson/**'
- 'lib/linux/**'
- 'lib/mac/**'
- 'lib/raspberry-pi/**'
- 'lib/windows/**'
- 'res/audio_samples/**'
pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
branches: [ master, 'v[0-9]+.[0-9]+' ]
paths:
- 'demo/c/**'
- '!demo/c/README.md'
- 'res/audio/**'
- '.github/workflows/c-demos.yml'
- 'demo/c/**'
- 'lib/beaglebone/**'
- 'lib/common/**'
- 'lib/jetson/**'
- 'lib/linux/**'
- 'lib/mac/**'
- 'lib/raspberry-pi/**'
- 'lib/windows/**'
- 'res/audio_samples/**'


defaults:
run:
working-directory: demo/c

jobs:
build-demos-github-hosted:
build-demo-github-hosted:
runs-on: ${{ matrix.os }}

strategy:
Expand All @@ -29,32 +47,31 @@ jobs:
- os: ubuntu-latest
platform: linux
arch: x86_64
ext: so
- os: windows-latest
platform: windows
arch: amd64
ext: dll
- os: macos-latest
platform: mac
arch: x86_64
ext: dylib

steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Build Search Demo
run: |
cmake -S demo/c/. -B demo/c/build
cmake --build demo/c/build
- uses: ./.github/actions/c-demo
name: Run demo
- name: Set up Python '3.10'
uses: actions/setup-python@v4
with:
access_key: ${{ secrets.PV_VALID_ACCESS_KEY }}
library_path: ../../lib/${{ matrix.platform }}/${{ matrix.arch }}/libpv_octopus.${{ matrix.ext }}
model_path: ../../lib/common/param/octopus_params.pv
audio_path: ../../res/audio/multiple_keywords.wav
index_path: ../../index.oif
search_phrase: picovoice
python-version: '3.10'

- name: Create build directory
run: cmake -B ./build

- name: Build demo
run: cmake --build ./build

- name: Install dependencies
run: pip install -r test/requirements.txt

- name: Test
run: python test/test_octopus_c.py ${{secrets.PV_VALID_ACCESS_KEY}} ${{ matrix.platform }} ${{ matrix.arch }}
4 changes: 3 additions & 1 deletion demo/c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build
cmake-build-debug
cmake-build-debug
test/__pycache__
test/index.oif
Empty file added demo/c/test/requirements.txt
Empty file.
85 changes: 85 additions & 0 deletions demo/c/test/test_octopus_c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#
# Copyright 2023 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

import os.path
import subprocess
import sys
import unittest

from test_util import *


class OctopusCTestCase(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls._access_key = sys.argv[1]
cls._platform = sys.argv[2]
cls._arch = "" if len(sys.argv) != 4 else sys.argv[3]
cls._root_dir = os.path.join(os.path.dirname(__file__), "../../..")
cls._index_path = os.path.join(os.path.dirname(__file__), "index.oif")

def _get_library_file(self):
return os.path.join(
self._root_dir,
"lib",
self._platform,
self._arch,
"libpv_octopus." + get_lib_ext(self._platform)
)

def _get_model_path_by_language(self, language):
model_path_subdir = append_language('lib/common/param/octopus_params', language)
return os.path.join(self._root_dir, '%s.pv' % model_path_subdir)

def _get_audio_file_by_language(self, language, audio_file_name=None):
if audio_file_name is None:
audio_file_name = "%s.wav" % append_language('multiple_keywords', language)
return os.path.join(self._root_dir, 'res/audio', audio_file_name)


def run_octopus(self, language, audio_file_name=None):
args = [
os.path.join(os.path.dirname(__file__), "../build/octopus_index_demo"),
"-a", self._access_key,
"-l", self._get_library_file(),
"-m", self._get_model_path_by_language(language),
"-w", self._get_audio_file_by_language(language, audio_file_name),
"-i", self._index_path
]
process = subprocess.Popen(args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
self.assertEqual(process.poll(), 0)
self.assertEqual(stderr.decode('utf-8'), '')

args = [
os.path.join(os.path.dirname(__file__), "../build/octopus_search_demo"),
"-a", self._access_key,
"-l", self._get_library_file(),
"-m", self._get_model_path_by_language(language),
"-i", self._index_path,
"-s", "picovoice"
]
process = subprocess.Popen(args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
self.assertEqual(process.poll(), 0)
self.assertEqual(stderr.decode('utf-8'), '')


def test_octopus(self):
self.run_octopus(language="en")


if __name__ == '__main__':
if len(sys.argv) < 3 or len(sys.argv) > 4:
print("usage: test_octopus_c.py ${AccessKey} ${Platform} [${Arch}]")
exit(1)
unittest.main(argv=sys.argv[:1])
28 changes: 28 additions & 0 deletions demo/c/test/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright 2023 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

import json
import os


def get_lib_ext(platform):
if platform == "windows":
return "dll"
elif platform == "mac":
return "dylib"
else:
return "so"


def append_language(s, language):
if language == 'en':
return s
return "%s_%s" % (s, language)

0 comments on commit c15c26e

Please sign in to comment.