Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
use python3, python3 imports, docstring formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AGiantSquid committed Dec 13, 2018
1 parent bbbf616 commit b2464bf
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 90 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -31,21 +31,21 @@ Feel free to use these examples as a jumping off point in your own code.
### Requirements

- Docker 17+ (or Discovery Binaries)
- Python 2.7 with `pip`
- Python 3 with `pip`

### Dependencies

To ensure your dependencies install correctly, we recommend that you upgrade your `setuptools` before proceeding further.

```sh
pip install --upgrade setuptools
python3 -m pip install --upgrade setuptools
```

Now you are ready to install the required dependencies with `pip`.
This will provide you with the packages needed to run the `test_discovery.py` script, as well as the Discovery CLI.

```sh
pip install -r requirements.txt
python3 -m pip install -r requirements.txt
```

### Obtaining credentials
Expand Down Expand Up @@ -73,7 +73,7 @@ Ensure that your *transcripts are unformatted text with numbers spelled out*. Fo

5) Edit the credentials and other configuration parameters in `discovery_config.py`

6) Run `python test_discovery.py folder_name` to test your interpreter. For example:
6) Run `python3 test_discovery.py folder_name` to test your interpreter. For example:

```
python test_discovery.py examples/directions
Expand All @@ -90,7 +90,7 @@ You should end up with a structure like the following for the `test_discovery.py

```
└───greenkey-discovery-sdk
└───discovery_binaries_windows_10_64bit__python27_32bit
└───discovery_binaries_windows_10_64bit__python37_64bit
└───examples
└───gkcli
│ discovery_config.py
Expand Down Expand Up @@ -131,7 +131,7 @@ Then, run these files through SVTServer [following our documentation](https://tr

For example, you can launch a single job container with the following command for a file called `test.wav`. Be sure to set `$USERNAME` and `$SECRETKEY`

```
```bash
docker run \
-it \
--rm \
Expand All @@ -140,7 +140,7 @@ docker run \
-e GKT_USERNAME="$USERNAME" \
-e GKT_SECRETKEY="$SECRETKEY" \
-e TARGET_FILE="/files/test.wav" \
-v $(pwd):/files \
-v "$PWD":/files \
-e ENABLE_CLOUD="False" \
-e PROCS=1 \
docker.greenkeytech.com/svtserver
Expand Down
2 changes: 1 addition & 1 deletion discovery_sdk_utils/discovery_sdk_utils/built_in_tokens.py
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3

ONTOLOGICAL_TAGS = (
'IN',
Expand Down
@@ -1,7 +1,7 @@
#!/usr/bin/python
'''
#!/usr/bin/env python3
"""
Utility functions for discovery
'''
"""
from __future__ import print_function
from inspect import isfunction
from .built_in_tokens import BUILT_IN_TOKENS
Expand Down Expand Up @@ -221,8 +221,7 @@ def _type_is_correct(item, type):


def find_errors_in_entity_definition(entity_definition, validation_functions=VALIDATION_FUNCTIONS):
''' Accumulates a list of errors to return to the user.
'''
"""Accumulates a list of errors to return to the user."""
errors = []
for func in validation_functions:
# Minor errors are returned as strings and added to the list.
Expand Down
6 changes: 5 additions & 1 deletion examples/directions/custom/entities/direction.py
@@ -1,5 +1,9 @@
''' This contains the entity definition for a cardinal directions '''
from cleaning_functions import capitalize
try:
from cleaning_functions import capitalize
except ImportError:
from .cleaning_functions import capitalize


DIRECTION = {
'label': 'DIRECTION',
Expand Down
9 changes: 5 additions & 4 deletions examples/directions/custom/entities/street_name.py
Expand Up @@ -2,9 +2,11 @@
This contains the entity definition for the most popular street names in Chicago, Illinois.
Replace these streets with whatever street names you please.
'''
import sys
sys.path.append('../../nlp/')
from cleaning_functions import capitalize
from nlp.cleanText import text2int
try:
from cleaning_functions import capitalize
except ImportError:
from .cleaning_functions import capitalize

STREET_NAME = {
'label':
Expand Down Expand Up @@ -134,7 +136,6 @@ def format_ordinal(wordList, spacer):
>>> format_ordinal("five three", ' ')
'5th 3rd'
"""
from cleanText import text2int

def add_suffix(n):
return str(n) + 'tsnrhtdd' [n % 5 * (n % 100 ^ 15 > 4 > n % 10)::4]
Expand Down
6 changes: 4 additions & 2 deletions examples/directions/custom/entities/street_type.py
@@ -1,8 +1,10 @@
'''
This contains the entity definition for types of streets.
'''

from cleaning_functions import capitalize
try:
from cleaning_functions import capitalize
except ImportError:
from .cleaning_functions import capitalize

STREET_TYPE = {
'label':
Expand Down
4 changes: 2 additions & 2 deletions examples/directions/send_transcript_to_discovery.sh
@@ -1,5 +1,5 @@
#!/bin/bash

curl -X POST http://localhost:1234/discover \
-H "Content-type: multipart/form-data" \
-F 'data={"json_lattice": {"transcript": "I need directions to fifty five west monroe avenue by car"}};type=application/json'
-H "Content-Type: application/json" \
-d '{"transcript": "I need directions to fifty five west monroe avenue by car"}'
4 changes: 2 additions & 2 deletions examples/room_dialing/send_transcript_to_discovery.sh
@@ -1,5 +1,5 @@
#!/bin/bash

curl -X POST http://localhost:1234/discover \
-H "Content-type: multipart/form-data" \
-F 'data={"json_lattice": {"transcript": "dial one eight"}};type=application/json'
-H "Content-Type: application/json" \
-d '{"transcript": "dial one eight"}'
4 changes: 2 additions & 2 deletions examples/room_number/send_transcript_to_discovery.sh
@@ -1,5 +1,5 @@
#!/bin/bash

curl -X POST http://localhost:1234/discover \
-H "Content-type: multipart/form-data" \
-F 'data={"json_lattice": {"transcript": "five a is calling thirteen c"}};type=application/json'
-H "Content-Type: application/json" \
-d '{"transcript": "five a is calling thirteen c"}'
4 changes: 2 additions & 2 deletions examples/telephone_number/send_transcript_to_discovery.sh
@@ -1,5 +1,5 @@
#!/bin/bash

curl -X POST http://localhost:1234/discover \
-H "Content-type: multipart/form-data" \
-F 'data={"json_lattice": {"transcript": "You can reach me at five five five four three two five nine eight one"}};type=application/json'
-H "Content-Type: application/json" \
-d '{"transcript": "You can reach me at five five five four three two five nine eight one"}'
30 changes: 15 additions & 15 deletions launch_discovery.py
@@ -1,25 +1,28 @@
#!/usr/bin/env python
'''This module will launch Discovery on http://localhost:1234
"""This module will launch Discovery on http://localhost:1234
This file will automatically scan for a folder of discovery binaries.
If the binaries are not detected, the script will launch the Discovery docker container.
Available functions:
- launch_discovery: Starts discovery container or binaries
'''
"""
import fnmatch
from multiprocessing import Process
import os
import subprocess
import sys

from discovery_config import DISCOVERY_CONFIG, DISCOVERY_PORT, DISCOVERY_IMAGE_NAME


def launch_discovery(custom_directory=None, type=None, port=None, discovery_config=None):
'''Launches the Discovery engine either via docker container or via compiled binaries.
"""Launches the Discovery engine either via docker container or via compiled binaries.
Args:
custom_directory: File path to the custom directory would like uploaded to discovery.
type: String, can either be 'docker' or 'binaries'
'''
"""
if custom_directory is None:
custom_directory = os.getcwd()
if port is None:
Expand All @@ -43,7 +46,7 @@ def _determine_discovery_launch_type():


def _launch_container(custom_directory, port, discovery_config):
'''launches the Discovery docker container.'''
"""launches the Discovery docker container."""
dico_dir = ["-v", "{}/dico:/dico".format(custom_directory)] if os.path.isdir(custom_directory + '/dico') else []

try:
Expand All @@ -68,31 +71,28 @@ def _launch_container(custom_directory, port, discovery_config):


def _launch_binaries(custom_directory, port, discovery_config):
'''launches Discovery from the compiled binaries.'''
"""launches Discovery from the compiled binaries."""
binaries_directory = _detect_binaries_file()
if binaries_directory:
import sys
sys.path.append(os.path.abspath(binaries_directory))
from run import find_definition_files_and_copy_them_to_appropriate_location
find_definition_files_and_copy_them_to_appropriate_location(
os.path.join(custom_directory), os.path.abspath(binaries_directory)
)

sys.path.append(os.path.join(binaries_directory, 'discovery'))
os.chdir(os.path.join(binaries_directory, 'discovery', 'server'))

print("Launching Discovery from Binaries: {}\n".format(binaries_directory))

sdk_directory = os.path.abspath(os.path.join(binaries_directory, '..'))
# remove any instances of the SDK directory from the path to prevent dummy modules from getting loaded in
while sdk_directory in sys.path:
sys.path.remove(sdk_directory)
os.environ["SERVICE_NAME"] = "discovery"
from discovery.server import main

from server.server import main
main()
proc = Process(target=main, args=())
proc.start()


def _detect_binaries_file():
'''Returns the absolute path of the binaries directory.'''
"""Returns the absolute path of the binaries directory."""
for file in os.listdir('.'):
if fnmatch.fnmatch(file, 'discovery_binaries_*') and not file.endswith('.tar.gz') and not file.endswith('.zip'):
return os.path.abspath(file)
Expand Down

0 comments on commit b2464bf

Please sign in to comment.