Skip to content

Commit

Permalink
Merge branch 'python-sdk-fixes' into python-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed May 21, 2023
2 parents de17719 + cb3d3bf commit 8a60b3d
Show file tree
Hide file tree
Showing 50 changed files with 2,296 additions and 2,228 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ end_of_line = lf
insert_final_newline = true

[*.py]
indent_style = tab
indent_size = 4
2 changes: 2 additions & 0 deletions bridges/nodejs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { INTENT_OBJECT } from '@bridge/constants'
domain,
skill,
action,
lang,
utterance,
current_entities,
entities,
Expand All @@ -17,6 +18,7 @@ import { INTENT_OBJECT } from '@bridge/constants'
} = INTENT_OBJECT

const params: ActionParams = {
lang,
utterance,
current_entities,
entities,
Expand Down
13 changes: 6 additions & 7 deletions bridges/python/src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
INTENT_OBJECT = json.load(f)

SKILLS_ROOT_PATH = os.path.join(
os.getcwd(),
'skills'
os.getcwd(),
'skills'
)

SKILL_PATH = os.path.join(
SKILLS_ROOT_PATH,
INTENT_OBJECT['domain'],
INTENT_OBJECT['skill']
SKILLS_ROOT_PATH,
INTENT_OBJECT['domain'],
INTENT_OBJECT['skill']
)

SKILLS_PATH = SKILLS_ROOT_PATH
Expand All @@ -24,5 +24,4 @@
SKILL_CONFIG = json.load(f)

with open(os.path.join(SKILL_PATH, 'src', 'config.json'), 'r') as f:
SKILL_SRC_CONFIG = json.load(f)['configurations']

SKILL_SRC_CONFIG = json.load(f)['configurations']
42 changes: 22 additions & 20 deletions bridges/python/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@
from json import dumps, loads
from importlib import import_module


def main():
"""Dynamically import skills related to the args and print the output"""
"""Dynamically import skills related to the args and print the output"""

path.append('.')

path.append('.')
intent_obj = utils.get_intent_obj()

intent_obj = utils.get_intent_obj()
skill = import_module('skills.' + intent_obj['domain'] + '.' + intent_obj['skill'] + '.src.actions.' + intent_obj['action'])

skill = import_module('skills.' + intent_obj['domain'] + '.' + intent_obj['skill'] + '.src.actions.' + intent_obj['action'])
params = {
'lang': intent_obj['lang'],
'utterance': intent_obj['utterance'],
'current_entities': intent_obj['current_entities'],
'entities': intent_obj['entities'],
'current_resolvers': intent_obj['current_resolvers'],
'resolvers': intent_obj['resolvers'],
'slots': intent_obj['slots']
}

params = {
'lang': intent_obj['lang'],
'utterance': intent_obj['utterance'],
'current_entities': intent_obj['current_entities'],
'entities': intent_obj['entities'],
'current_resolvers': intent_obj['current_resolvers'],
'resolvers': intent_obj['resolvers'],
'slots': intent_obj['slots']
}
return getattr(skill, intent_obj['action'])(params)

return getattr(skill, intent_obj['action'])(params)

if __name__ == '__main__':
try:
raise main()
except Exception as e:
# Print full traceback error report if skills triggers an error from the call stack
if 'exceptions must derive from BaseException' not in str(e):
print_exc()
try:
raise main()
except Exception as e:
# Print full traceback error report if skills triggers an error from the call stack
if 'exceptions must derive from BaseException' not in str(e):
print_exc()
48 changes: 26 additions & 22 deletions bridges/python/src/sdk/leon.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import random
import sys
from typing import Union
from time import sleep
import json

from types import
from .types import AnswerInput, AnswerData, AnswerConfig
from ..constants import SKILL_SRC_CONFIG, SKILL_CONFIG, INTENT_OBJECT


class Leon:
instance: 'Leon' = None
instance: 'Leon'

def __init__(self) -> None:
if not Leon.instance:
Leon.instance = self
def __init__(self) -> None:
if not Leon.instance:
Leon.instance = self

def get_src_config(self, key: str = None):
"""
Get source configuration
"""
def get_src_config(self, key: Union[str, None] = None):
"""
Get source configuration
"""
try:
if key:
return SKILL_SRC_CONFIG[key]
Expand All @@ -25,10 +28,10 @@ def get_src_config(self, key: str = None):
print('Error while getting source configuration:', e)
return {}

def set_answer_data(self, answer_key: str, data = None):
"""
Apply data to the answer
"""
def set_answer_data(self, answer_key: Union[str, None], data: Union[AnswerData, None] = None) -> Union[str, AnswerConfig, None]:
"""
Apply data to the answer
"""
if answer_key:
try:
# In case the answer key is a raw answer
Expand Down Expand Up @@ -56,21 +59,21 @@ def set_answer_data(self, answer_key: str, data = None):
if not isinstance(answer, str) and answer.get('text'):
answer['text'] = answer['text'].replace('%{}%'.format(key), str(value))
answer['speech'] = answer['speech'].replace('%{}%'.format(key), str(value))
else:
else:
answer = answer.replace('%{}%'.format(key), str(value))

return answer
except Exception as e:
print('Error while setting answer data:', e)
except Exception as e:
print('Error while setting answer data:', e)

return None
return None

return None

def answer(self, answer_input: AnswerInput) -> None:
"""
Send an answer to the core
"""
"""
Send an answer to the core
"""
try:
output = {
'output': {
Expand All @@ -85,8 +88,8 @@ def answer(self, answer_input: AnswerInput) -> None:
output['output']['widget'] = answer_input['widget']

answer_object = {
**INTENT_OBJECT,
**output
**INTENT_OBJECT,
**output
}

# Temporize for the data buffer output on the core
Expand All @@ -97,4 +100,5 @@ def answer(self, answer_input: AnswerInput) -> None:
except Exception as e:
print('Error while creating answer:', e)


leon = Leon()
30 changes: 17 additions & 13 deletions bridges/python/src/sdk/types.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from typing import Dict, Any, Optional, List
from typing import Dict, Any, Optional, List, TypedDict

# TODO

class Answer:
key: Optional[str]

class AnswerData(TypedDict):
pass


class Answer(TypedDict):
key: Optional[str]
widget: Optional[Any]
data: Optional[AnswerData]
core: Optional[Dict[str, Any]]

class AnswerInput:
key: Optional[str]
widget: Any
data: Optional[AnswerData]
core: Dict[str, Any]

class AnswerData:
pass
class AnswerInput(TypedDict):
key: Optional[str]
widget: Any
data: Optional[AnswerData]
core: Dict[str, Any]


class AnswerConfig:
text: str
speech: str
class AnswerConfig(TypedDict):
text: str
speech: str
14 changes: 7 additions & 7 deletions bridges/python/src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

options = {
'build_exe': {
# Add common dependencies for skills
# Add common dependencies for skills
'includes': [
'bs4',
'pytube'
'bs4',
'pytube'
],
'include_files': [(requests.certs.where(), 'cacert.pem')]
}
}
}

executables = [
Executable(
script='bridges/python/src/main.py',
target_name='leon-python-bridge'
Executable(
script='bridges/python/src/main.py',
target_name='leon-python-bridge'
)
]

Expand Down
Loading

0 comments on commit 8a60b3d

Please sign in to comment.