dream

Форк
0
54 строки · 2.1 Кб
1
import logging
2
import time
3
from os import getenv
4

5
import sentry_sdk
6
from common.robot import send_robot_command_to_perform
7
from flask import Flask, request, jsonify
8

9

10
sentry_sdk.init(getenv("SENTRY_DSN"))
11

12
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
13
logger = logging.getLogger(__name__)
14

15
app = Flask(__name__)
16

17
ROS_FLASK_SERVER = getenv("ROS_FLASK_SERVER")
18
SKILL_NAMES_SENDING_COMMANDS = ["intent_responder", "dff_intent_responder_skill", "dff_command_selector_skill"]
19

20

21
@app.route("/send", methods=["POST"])
22
def respond():
23
    st_time = time.time()
24
    results = []
25
    ann_human_utterances = request.json.get("last_human_utterances", [])
26
    ann_bot_utterances = request.json.get("bot_utterances", [])
27
    dialog_ids = request.json.get("dialog_ids", [])
28

29
    for ann_human_uttr, ann_bot_uttr, dialog_id in zip(ann_human_utterances, ann_bot_utterances, dialog_ids):
30
        active_skill = ann_bot_uttr.get("active_skill", {})
31
        hyps = ann_human_uttr.get("hypotheses", [])
32
        current_skill_hyp = [hyp for hyp in hyps if hyp.get("skill_name", "") == active_skill][0]
33
        command = current_skill_hyp.get("command_to_perform", "")
34
        if active_skill in SKILL_NAMES_SENDING_COMMANDS and len(command):
35
            logger.info(f"robot_command_sender: command `{command}` is being sent to robot")
36
            result = False
37
            try:
38
                result = send_robot_command_to_perform(command, ROS_FLASK_SERVER, dialog_id)
39
            except Exception as e:
40
                sentry_sdk.capture_exception(e)
41
                logger.exception(e)
42

43
            if result:
44
                results += [{"human_attributes": {"performing_command": command}}]
45
            else:
46
                results += [{"human_attributes": {}}]
47
            logger.info(f"robot_command_sender: status of sending command `{command}`: `{result}`")
48
        else:
49
            logger.info("robot_command_sender: NO command found in prev bot uttr")
50
            results += [{"human_attributes": {}}]
51

52
    total_time = time.time() - st_time
53
    logger.info(f"robot_command_sender exec time: {total_time:.3f}s")
54
    return jsonify(results)
55

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.