Esempio n. 1
0
def start_ms_bf_server(model_config: Path,
                       app_id: Optional[str],
                       app_secret: Optional[str],
                       port: Optional[int] = None,
                       https: Optional[bool] = None,
                       ssl_key: Optional[str] = None,
                       ssl_cert: Optional[str] = None) -> None:

    server_params = get_server_params(model_config)

    host = server_params['host']
    port = port or server_params['port']

    ssl_config = get_ssl_params(server_params, https, ssl_key=ssl_key, ssl_cert=ssl_cert)

    input_q = Queue()
    bot = MSBot(model_config, input_q, app_id, app_secret)
    bot.start()

    endpoint = '/v3/conversations'
    redirect_root_to_docs(app, 'answer', endpoint, 'post')

    @app.post(endpoint)
    async def answer(activity: dict) -> dict:
        bot.input_queue.put(activity)
        return {}

    uvicorn.run(app, host=host, port=port, logger=uvicorn_log, ssl_version=ssl_config.version,
                ssl_keyfile=ssl_config.keyfile, ssl_certfile=ssl_config.certfile)
    bot.join()
Esempio n. 2
0
def start_alice_server(model_config: Union[str, Path],
                       host: Optional[str] = None,
                       port: Optional[int] = None,
                       endpoint: Optional[str] = None,
                       https: Optional[bool] = None,
                       ssl_key: Optional[str] = None,
                       ssl_cert: Optional[str] = None) -> None:
    server_params = get_server_params(model_config)

    host = host or server_params['host']
    port = port or server_params['port']
    endpoint = endpoint or server_params['model_endpoint']

    ssl_config = get_ssl_params(server_params,
                                https,
                                ssl_key=ssl_key,
                                ssl_cert=ssl_cert)

    input_q = Queue()
    output_q = Queue()

    bot = AliceBot(model_config, input_q, output_q)
    bot.start()

    redirect_root_to_docs(app, 'answer', endpoint, 'post')

    @app.post(endpoint,
              summary='A model endpoint',
              response_description='A model response')
    async def answer(data: dict = data_body) -> dict:
        loop = asyncio.get_event_loop()
        bot.input_queue.put(data)
        response: dict = await loop.run_in_executor(None, bot.output_queue.get)
        return response

    uvicorn.run(app,
                host=host,
                port=port,
                logger=uvicorn_log,
                ssl_version=ssl_config.version,
                ssl_keyfile=ssl_config.keyfile,
                ssl_certfile=ssl_config.certfile)
    bot.join()
Esempio n. 3
0
def start_alexa_server(model_config: Union[str, Path, dict],
                       port: Optional[int] = None,
                       https: Optional[bool] = None,
                       ssl_key: Optional[str] = None,
                       ssl_cert: Optional[str] = None) -> None:
    """Initiates FastAPI web service with Alexa skill.

    Allows raise Alexa web service with DeepPavlov config in backend.

    Args:
        model_config: DeepPavlov config path.
        port: FastAPI web service port.
        https: Flag for running Alexa skill service in https mode.
        ssl_key: SSL key file path.
        ssl_cert: SSL certificate file path.

    """
    server_params = get_server_params(model_config)

    host = server_params['host']
    port = port or server_params['port']

    ssl_config = get_ssl_params(server_params,
                                https,
                                ssl_key=ssl_key,
                                ssl_cert=ssl_cert)

    input_q = Queue()
    output_q = Queue()

    bot = AlexaBot(model_config, input_q, output_q)
    bot.start()

    endpoint = '/interact'
    redirect_root_to_docs(app, 'interact', endpoint, 'post')

    @app.post(endpoint,
              summary='Amazon Alexa custom service endpoint',
              response_description='A model response')
    async def interact(
            data: dict = data_body,
            signature: str = signature_header,
            signature_chain_url: str = cert_chain_url_header) -> JSONResponse:
        # It is necessary for correct data validation to serialize data to a JSON formatted string with separators.
        request_dict = {
            'request_body': json.dumps(data,
                                       separators=(',', ':')).encode('utf-8'),
            'signature_chain_url': signature_chain_url,
            'signature': signature,
            'alexa_request': data
        }

        bot.input_queue.put(request_dict)
        loop = asyncio.get_event_loop()
        response: dict = await loop.run_in_executor(None, bot.output_queue.get)
        response_code = 400 if 'error' in response.keys() else 200
        return JSONResponse(response, status_code=response_code)

    uvicorn.run(app,
                host=host,
                port=port,
                logger=uvicorn_log,
                ssl_version=ssl_config.version,
                ssl_keyfile=ssl_config.keyfile,
                ssl_certfile=ssl_config.certfile)
    bot.join()