This page offers an overview about the (detailed) steps of how to integrate a service into the SIC Framework. Become familiar with what a service could (not) be by checking out Services - Restructured . The code for all the existing services can be found at https://bitbucket.org/socialroboticshub/processing/src/master/. Creating a new service can be done as follows, and then included in the repository by opening a Pull Request:
TLDR (Too Long Didn’t Read)
...
Summary How to Add a Service to the SIC Framework
create a new folder in https://bitbucket.org/socialroboticshub/processing/src/master/ with the name of the service
copy the certificate file https://bitbucket.org/socialroboticshub/docker/src/master/cbsrsic/beamforming/cert.pem from any of the other services' folders into the service’s folder
copy any additional files that the services may need into the service’s folder
create a factory file inheriting from the
CBSRFactory
SICfactory
in the service’s folder, and override the superclass’s methodscreate a service file inheriting from
CBSRService
SICservice
in the service’s folder, and override the superclass’s methodsupdate the https://bitbucket.org/socialroboticshub/processing/src/master/deploy_to_docker.sh file in the root folder with the new service files
deploy the new service to the https://bitbucket.org/socialroboticshub/docker/src/master/ folder by running the
deploy_to_docker.sh
fileupdate the https://bitbucket.org/socialroboticshub/docker/src/master/docker-compose.yml file in the
docker
folder with the new serviceupdate the topics in the https://bitbucket.org/socialroboticshub/docker/src/master/Dockerfile.python3 file in the
docker
folder with the new service’s dependenciesupdate the topics in the constructor of the Abstract Connector from the https://bitbucket.org/socialroboticshub/connectors/src/master/python/social_interaction_cloud/ folder with the name of the new service
update the device listeners in
enable_service
in the Abstract Connector with the serviceupdate the listened to channels in
__listen
in the Abstract Connector with the servicecreate the corresponding event handler method for the service in the Abstract Connector
create the corresponding event handler method for the service in the Basic Connector
use the new service in a new file
The detailed explanation of these steps with a sentiment analysis example can be found below:
Too Long Still Read
...
create a new folder in https://bitbucket.org/socialroboticshub/processing/src/master/ with the name of the service. my_service
will be used as the example folder and service name in this case
...
Detailed How to Add a (Sentiment) Service to the SIC Framework
There are two shared libraries that handle a lot of the common logic that is needed to interact with our framework: one for Python-based integrations and one for Java-based integrations (using Maven). In order to allow users to run services without worrying about compatibility and installations, Docker Compose is used.
If a service is not simply an alternative to an existing service, adding a new service will also require updates to the connectors (EIS and Python) in order to be fully integrated.
create a new folder in https://bitbucket.org/socialroboticshub/dockerprocessing/src/master/cbsr/beamforming/cert.pem from any of the other services' folders into the
my_service
foldercreate a
my_service_factory.py
file in themy_service
folder
Code Block | ||||
---|---|---|---|---|
| ||||
from cbsr.factory import CBSRfactory
from my_service import MyService
class MyServiceFactory(CBSRfactory):
def __init__(self):
""" MyServiceFactory constructor
inherit from the CBSRfactory constructor to create the new service as a service
"""
super(MyServiceFactory, self).__init__()
def get_connection_channel(self):
""" initialise the name of the service Redis channel
"""
return 'my_service'
def create_service(self, connect, identifier, disconnect):
return MyService(connect, identifier, disconnect)
if __name__ == '__main__':
my_factory = MyServiceFactory()
my_factory.run() |
4. create a my_service.py
file in the my_service
folder
...
breakoutMode | wide |
---|---|
language | py |
...
with the name of the service.
sentiment_analysis
(https://bitbucket.org/socialroboticshub/docker/src/master/sic/sentiment/ ) will be used as the example folder and service in this casecopy the certificate file https://bitbucket.org/socialroboticshub/docker/src/master/sic/beamforming/cert.pem from any of the other services' folders into the
sentiment
foldercopy the https://bitbucket.org/socialroboticshub/docker/src/master/sic/sentiment/classifier.pickle into the
sentiment
foldercreate a https://bitbucket.org/socialroboticshub/docker/src/master/sic/sentiment/sentiment_factory.py file in the
sentiment
folder
Code Block | ||||
---|---|---|---|---|
| ||||
from os import getcwd
from sic.factory import SICfactory
from nltk import download
from nltk.data import path
from sentiment_service import SentimentAnalysisService
class SentimentAnalysisFactory(SICfactory):
def __init__(self):
super(SentimentAnalysisFactory, self).__init__()
def get_connection_channel(self):
return 'sentiment_analysis'
def create_service(self, connect, identifier, disconnect):
return SentimentAnalysisService(connect, identifier, disconnect)
if __name__ == '__main__':
cwd = getcwd()
download('punkt', download_dir=cwd)
download('omw-1.4', download_dir=cwd)
download('averaged_perceptron_tagger', download_dir=cwd)
download('wordnet', download_dir=cwd)
path.append(cwd)
sentiment_analysis_factory = SentimentAnalysisFactory()
sentiment_analysis_factory.run() |
5. create a https://bitbucket.org/socialroboticshub/docker/src/master/sic/sentiment/sentiment_service.py file in the sentiment
folder
Code Block | ||||
---|---|---|---|---|
| ||||
""" This file shows an example of a sentiment service that uses the text_transcript channel result from the Dialogflow speech-to-text to get the the type of sentiment """ from pickle import load from re import sub from string import punctuation from sic.service import SICservice from nltk.stem.wordnet import WordNetLemmatizer from nltk.tag import pos_tag from nltk.tokenize import word_tokenize class SentimentAnalysisService(SICservice): def __init__(self, connect, identifier, disconnect): super(SentimentAnalysisService, self).__init__(connect, identifier, disconnect) return {self.get_full_channel('text_transcript'): self.execute}with open('classifier.pickle', 'rb') as pickle: def execute(self, message):self.classifier = load(pickle) self.lemmatizer = WordNetLemmatizer() """ :param message: the message published in the channel the method was linked to def get_device_types(self): return ['mic'] def get_channel_action_mapping(self): This method decodes the data received in the channel it was linked to and further publishes return {self.get_full_channel('text_transcript'): self.execute} def execute(self, message): sentence a= message in the new service channel['data'].decode() tokens Extra functionalities can be added to the method """ = self.remove_noise(word_tokenize(sentence)) sentiment = self.classifier.classify(dict([token, True] for token in tokens)) sentence = message['data'].decode( print(sentiment) self.publish('mytext_servicesentiment', sentencesentiment) ... |
56. update the https://bitbucket.org/socialroboticshub/processing/src/master/deploy_to_docker.sh file in the root folder with the new service files
Code Block | ||||
---|---|---|---|---|
| ||||
... echo Deploying mysentiment_serviceanalysis... cd ../myaudio_servicesentiment cp -f {cert.pem, classifier.pickle,*.py} ../../docker/cbsrsic/my_servicesentiment # extra files should canalso be copiedinclduded if usedhere ... |
67. deploy the new service to the https://bitbucket.org/socialroboticshub/docker/src/master/ folder by running the deploy_to_docker.sh
file
78. update the https://bitbucket.org/socialroboticshub/docker/src/master/docker-compose.yml file in the docker
folder with the new service
Code Block | ||
---|---|---|
| ||
... # ------------------------------------------------------------ # Sentiment MyAnalysis service # ------------------------------------------------------------- mysentiment_serviceanalysis: image: sic_python3 build: context: . dockerfile: Dockerfile.python3 hostname: my_service user: "${NEW_UID}:${NEW_GID}" env_file: - ./.env working_dir: /my_servicesentiment command: python3 mysentiment_service_factory.py volumes: - ./cbsrsic/mocksentiment:/my_service:rw${MOUNT_OPTIONS}sentiment:rw,delegated tty: true stdin_open: false networks depends_on: app_net: - redis ipv4_address: 172.16.238.x - dialogflow # address has to# differ- fromany thoseother ofservices the already existing services' depends_on: - redis - dialogflow # - any other services my_service depends on |
...
service depends on
... |
9. update the https://bitbucket.org/socialroboticshub/docker/src/master/Dockerfile.python3 file in the docker
folder with the new service’s dependencies
Code Block |
---|
RUN pip3 install --no-cache-dir --upgrade --prefer-binary \
redis~=4.1 \
hiredis~=2.0 \
simplejson~=3.17 \
Pillow~=9.0 \
numpy~=1.22 \
imutils~=0.5 \
[any other dependendencies] \
... |
10. update the topics
in the constructor of the Abstract Connector abstract_connector.py
from the https://bitbucket.org/socialroboticshub/connectors/src/master/python/social_interaction_cloud/ folder with the name of the new service
topics = [..., 'mytext_servicesentiment']
911. update the corresponding list of devices listeners in enable_service
in abstract_connector.py
with my_service
Code Block | ||
---|---|---|
| ||
... ########################### # Management # ########################### def enable_service(self, name: str) -> None: ... elif ... and name == 'mysentiment_serviceanalysis': for mic in self.devices[self.device_types['mic']]: pipe.publish(name, mic) ... |
1012. update the channels in __listen
in abstract_connector.py
with my_service
Code Block | ||
---|---|---|
| ||
... elif channel = 'mytext_servicesentiment': self.on_mytext_servicesentiment(message=data.decode('utf-8')) ... |
1113. create the corresponding event handler method on_my_service
in abstract_connector.py
Code Block | ||
---|---|---|
| ||
... ########################### # Event handlers # ########################### ... def on_mytext_servicesentiment(self, message: str) -> None: pass ... |
1214. create the corresponding inherited event handler method on_my_service
in basic_connector.py
Code Block | ||||
---|---|---|---|---|
| ||||
... ########################### # Event handlers # ########################### ... def on_mytext_servicesentiment(self, message: str) -> None: """ :param message: the message published on the mytext_servicesentiment channel This method notifies the listeners that a new message has been posted on the my_service channel; This method can be further inherited and overridden """ self.__notify_listeners('onMyService', message) ... |
13. use the new service by creating a new file my_service_example.py
and running it
Code Block | ||||
---|---|---|---|---|
| ||||
from social_interaction_cloud.action import ActionRunner from social_interaction_cloud.basic_connector import BasicSICConnector class MyConnector(BasicSICConnector): def __init__(self, server_ip: str): """ :param server_ip: This method inherits the BasicSICConnector and enables the new my_service service; my_service needs to be manually enabled in this way """ has been posted on the text_sentiment channel; This method can be further inherited and overridden """ self.__notify_listeners('onTextSentiment', message) ... |
15. use the new service by creating a new file sentiment_example.py
, overriding the on_text_sentiment
method and running it
Code Block | ||||
---|---|---|---|---|
| ||||
from enum import Enum from functools import partial from social_interaction_cloud.action import ActionRunner from social_interaction_cloud.basic_connector import BasicSICConnector class SentimentConnector(BasicSICConnector): def __init__(self, server_ip: str, dialogflow_key_file: str, dialogflow_agent_id: str): super(SentimentConnector, self).__init__(server_ip, 'en-US', dialogflow_key_file, dialogflow_agent_id) super(MyConnector, self).enable__init__(server_ipservice('sentiment_analysis') self.enable_service('my_service')sentiment = None def on_mytext_servicesentiment(self, messagesentiment: str) -> None: """ :param message: message published in the my_service channel print(sentiment) self.sentiment = sentiment class Example: def __init__(self, server_ip: str, dialogflow_key_file: str, dialogflow_agent_id: str): This methodself.sic overrides the event function on_my_service inherited from the BasicSICConnector= SentimentConnector(server_ip, dialogflow_key_file, dialogflow_agent_id) self.action_runner = ActionRunner(self.sic) """ self.recognition_manager = printRecognitionManager(message2) self.stop().user_model = {} ... |