Skip to content

Commit

Permalink
v2.0 python (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
laves authored Nov 6, 2023
1 parent 7c2b4ad commit 670164f
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 162 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/python-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ jobs:
- name: Pre-build dependencies
run: python -m pip install --upgrade pip

# ************** REMOVE AFTER RELEASE ********************
- name: Build binding
run: |
pip install wheel && cd ../../binding/python && python setup.py sdist bdist_wheel && pip install dist/pvoctopus-2.0.0-py3-none-any.whl
# ********************************************************

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

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/python-perf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
index_performance_threshold_sec: 1.4
index_performance_threshold_sec: 2.2
search_performance_threshold_sec: 0.001
- os: windows-latest
index_performance_threshold_sec: 1.4
index_performance_threshold_sec: 2.2
search_performance_threshold_sec: 0.001
- os: macos-latest
index_performance_threshold_sec: 2.0
index_performance_threshold_sec: 2.8
search_performance_threshold_sec: 0.001

steps:
Expand Down
16 changes: 9 additions & 7 deletions binding/python/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Octopus
# Octopus Binding for Python

## Octopus

Made in Vancouver, Canada by [Picovoice](https://picovoice.ai)

Expand Down Expand Up @@ -30,8 +32,8 @@ Create an instance of the engine:
```python
import pvoctopus

access_key = "" # AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
handle = pvoctopus.create(access_key=access_key)
access_key = "${ACCESS_KEY}" # AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
octopus = pvoctopus.create(access_key=access_key)
```

Octopus consists of two steps: Indexing and Searching. Indexing transforms audio data into a `Metadata` object that
Expand All @@ -44,15 +46,15 @@ The engine accepts 16-bit linearly-encoded PCM and operates on single-channel au

```python
audio_data = [...]
metadata = handle.index(audio_data)
metadata = octopus.index(audio_data)
```

Similarly, files can be indexed by passing in the absolute file path to the audio object.
Supported file formats are mp3, flac, wav and opus:

```python
audio_file_path = "/path/to/my/audiofile.wav"
metadata = handle.index_file(audio_file_path)
metadata = octopus.index_file(audio_file_path)
```

Once the `Metadata` object has been created, it can be used for searching:
Expand Down Expand Up @@ -90,10 +92,10 @@ cached_metadata = pvoctopus.OctopusMetadata.from_bytes(metadata_bytes)
matches = octopus.search(cached_metadata, ['avocado'])
```

When done the handle resources have to be released explicitly:
When done the Octopus, resources have to be released explicitly:

```python
handle.delete()
octopus.delete()
```

## Non-English Models
Expand Down
34 changes: 4 additions & 30 deletions binding/python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2021-2022 Picovoice Inc.
# Copyright 2021-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.
Expand All @@ -9,32 +9,6 @@
# specific language governing permissions and limitations under the License.
#

from typing import *

from .octopus import *
from .util import *

LIBRARY_PATH = pv_library_path('')

MODEL_PATH = pv_model_path('', 'en')


def create(access_key: str, model_path: Optional[str] = None, library_path: Optional[str] = None) -> Octopus:
"""
Factory method for Octopus Speech-to-Index engine.
:param access_key: AccessKey provided by Picovoice Console (https://console.picovoice.ai/)
:param model_path: Absolute path to the file containing model parameters. If not set it will be set to the default
location for English model.
:param library_path: Absolute path to Octopus' dynamic library. If not set it will be set to the default
location.
:return An instance of Octopus Speech-to-Index engine.
"""

if model_path is None:
model_path = MODEL_PATH

if library_path is None:
library_path = LIBRARY_PATH

return Octopus(access_key=access_key, model_path=model_path, library_path=library_path)
from ._factory import *
from ._octopus import *
from ._util import *
42 changes: 42 additions & 0 deletions binding/python/_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Copyright 2021-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.
#

from typing import Optional

from ._octopus import Octopus
from ._util import default_library_path, default_model_path


def create(access_key: str, model_path: Optional[str] = None, library_path: Optional[str] = None) -> Octopus:
"""
Factory method for Octopus Speech-to-Index engine.
:param access_key: AccessKey provided by Picovoice Console (https://console.picovoice.ai/)
:param model_path: Absolute path to the file containing model parameters. If not set it will be set to the default
location for English model.
:param library_path: Absolute path to Octopus' dynamic library. If not set it will be set to the default
location.
:return An instance of Octopus Speech-to-Index engine.
"""

if model_path is None:
model_path = default_model_path()

if library_path is None:
library_path = default_library_path()

return Octopus(
access_key=access_key,
model_path=model_path,
library_path=library_path)


__all__ = ['create']
Loading

0 comments on commit 670164f

Please sign in to comment.