Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This page offers an overview about what a service could be (or not) and the (detailed) steps of how to integrate it 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/.

What Is (Not) a Service?

Do’s

A service is required to:

  • use input from a device and/or another service

  • do more than 1 line of code in processing

  • give output that is useful for another service and/or an application

Additionally, it should be reusable/generic (i.e. more than 1 specific application can make use of the service).

Do’nts

A service should only relate to the software aspect of the SIC framework. An example of what would NOT count as a service is integrating both a third-party device (e.g.: smartwatch) and the data collected from it into an application. In this case, the device should be added separately as one, and the data collection and processing should be turned into a service itself.

Summary How to Add a Service to the SIC Framework

...

Summary How to Add a Service to the SIC Framework

  1. create a new folder in https://bitbucket.org/socialroboticshub/processing/src/master/ with the name of the service

  2. copy the certificate file https://bitbucket.org/socialroboticshub/docker/src/master/sic/beamforming/cert.pem from any of the other services' folders into the service’s folder

  3. copy any additional files that the services may need into the service’s folder

  4. create a factory file inheriting from the SICfactory in the service’s folder, and override the superclass’s methods

  5. create a service file inheriting from SICservice in the service’s folder, and override the superclass’s methods

  6. update the https://bitbucket.org/socialroboticshub/processing/src/master/ with the name of the servicecopy the certificate file deploy_to_docker.sh file in the root folder with the new service files

  7. deploy the new service to the https://bitbucket.org/socialroboticshub/docker/src/master/cbsr/beamforming/cert.pem from any of the other services' folders into the service’s folder

  8. copy any additional files that the services may need into the service’s folder

  9. create a factory file inheriting from the CBSRFactory in the service’s folder, and override the superclass’s methods

  10. create a service file inheriting from CBSRService in the service’s folder, and override the superclass’s methods

  11. update the folder by running the deploy_to_docker.sh file

  12. update the https://bitbucket.org/socialroboticshub/docker/src/master/docker-compose.yml file in the docker folder with the new service

  13. update the https://bitbucket.org/socialroboticshub/processingdocker/src/master/deploy_to_docker.shDockerfile.python3 file in the root docker folder with the new service filesdeploy the new service to service’s dependencies

  14. update the topics in the constructor of the Abstract Connector from the https://bitbucket.org/socialroboticshub/dockerconnectors/src/master/ folder by running the deploy_to_docker.sh file

  15. update the https://bitbucket.org/socialroboticshub/docker/src/master/docker-compose.yml file in the docker folder with the new service

  16. update the https://bitbucket.org/socialroboticshub/docker/src/master/Dockerfile.python3 file in the docker folder with the new service’s dependencies

  17. update 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

  18. update the device listeners in enable_service in the Abstract Connector with the service

  19. update the listened to channels in __listen in the Abstract Connector with the service

  20. create the corresponding event handler method for the service in the Abstract Connector

  21. create the corresponding event handler method for the service in the Basic Connector

  22. use the service in a new file

...

  1. /python/social_interaction_cloud/ folder with the name of the new service

  2. update the device listeners in enable_service in the Abstract Connector with the service

  3. update the listened to channels in __listen in the Abstract Connector with the service

  4. create the corresponding event handler method for the service in the Abstract Connector

  5. create the corresponding event handler method for the service in the Basic Connector

  6. use the service in a new file

The detailed explanation of these steps with a sentiment analysis example can be found below:

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.

  1. create a new folder in https://bitbucket.org/socialroboticshub/processing/src/master/ with the name of the service. sentiment_analysis (https://bitbucket.org/socialroboticshub/docker/src/master/cbsrsic/sentiment/ ) will be used as the example folder and service in this case

  2. copy the certificate file https://bitbucket.org/socialroboticshub/docker/src/master/cbsrsic/beamforming/cert.pem from any of the other services' folders into the sentimentfolder

  3. copy the https://bitbucket.org/socialroboticshub/docker/src/master/cbsrsic/sentiment/classifier.pickle into the sentiment folder

  4. create a https://bitbucket.org/socialroboticshub/docker/src/master/cbsrsic/sentiment/sentiment_factory.py file in the sentiment folder

Code Block
breakoutModewide
languagepy
from os import getcwd

from cbsrsic.factory import CBSRfactorySICfactory
from nltk import download
from nltk.data import path

from sentiment_service import SentimentAnalysisService


class SentimentAnalysisFactory(CBSRfactorySICfactory):
    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/cbsrsic/sentiment/sentiment_service.py file in the sentiment folder

Code Block
breakoutModewide
languagepy
""" 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 cbsrsic.service import CBSRserviceSICservice
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize


class SentimentAnalysisService(CBSRserviceSICservice):
    def __init__(self, connect, identifier, disconnect):
        super(SentimentAnalysisService, self).__init__(connect, identifier, disconnect)
        with open('classifier.pickle', 'rb') as pickle:
            self.classifier = load(pickle)
        self.lemmatizer = WordNetLemmatizer()

    def get_device_types(self):
        return ['mic']

    def get_channel_action_mapping(self):
        return {self.get_full_channel('text_transcript'): self.execute}

    def execute(self, message):
        sentence = message['data'].decode()
        tokens = self.remove_noise(word_tokenize(sentence))
        sentiment = self.classifier.classify(dict([token, True] for token in tokens))
        print(sentiment)
        self.publish('text_sentiment', sentiment)
...

...

Code Block
breakoutModewide
languagebash
...
echo Deploying mysentiment_serviceanalysis...
cd ../myaudio_servicesentiment
cp -f {cert.pem,classifier.pickle,  *.py, classifier.pickle} ../../docker/cbsrsic/my_servicesentiment    # extra files canshould also be copiedinclduded ifhere
used
...

7. deploy the new service to the https://bitbucket.org/socialroboticshub/docker/src/master/ folder by running the deploy_to_docker.sh file

8. update the https://bitbucket.org/socialroboticshub/docker/src/master/docker-compose.yml file in the docker folder with the new service

wide
Code Block
breakoutMode
...
  # ------------------------------------------------------------
  # Sentiment Analysis service
  # ------------------------------------------------------------
  sentiment_analysis:
    image: sic_python3
    build:
      context: .
      dockerfile: Dockerfile.python3

 hostname: sentiment_analysis   user: "${NEW_UID}:${NEW_GID}"
    env_file:
      - ./.env

    working_dir: /my_servicesentiment
    command: python3 sentiment_factory.py
  volumes:
    - ./cbsr/mock:/sentiment:rw${MOUNT_OPTIONS}

  tty: true
  stdin_open: false.py
    networks:
    app_netvolumes:
      ipv4_address:- 172.16.238.x./sic/sentiment:/sentiment:rw,delegated

 # address has totty: differtrue
from those of the already existing services'stdin_open: false

    depends_on:
      - redis
      - dialogflow

  # - any other services the service depends on
...

...