dream
54 строки · 2.1 Кб
1import logging2import time3from os import getenv4
5import sentry_sdk6from common.robot import send_robot_command_to_perform7from flask import Flask, request, jsonify8
9
10sentry_sdk.init(getenv("SENTRY_DSN"))11
12logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)13logger = logging.getLogger(__name__)14
15app = Flask(__name__)16
17ROS_FLASK_SERVER = getenv("ROS_FLASK_SERVER")18SKILL_NAMES_SENDING_COMMANDS = ["intent_responder", "dff_intent_responder_skill", "dff_command_selector_skill"]19
20
21@app.route("/send", methods=["POST"])22def respond():23st_time = time.time()24results = []25ann_human_utterances = request.json.get("last_human_utterances", [])26ann_bot_utterances = request.json.get("bot_utterances", [])27dialog_ids = request.json.get("dialog_ids", [])28
29for ann_human_uttr, ann_bot_uttr, dialog_id in zip(ann_human_utterances, ann_bot_utterances, dialog_ids):30active_skill = ann_bot_uttr.get("active_skill", {})31hyps = ann_human_uttr.get("hypotheses", [])32current_skill_hyp = [hyp for hyp in hyps if hyp.get("skill_name", "") == active_skill][0]33command = current_skill_hyp.get("command_to_perform", "")34if active_skill in SKILL_NAMES_SENDING_COMMANDS and len(command):35logger.info(f"robot_command_sender: command `{command}` is being sent to robot")36result = False37try:38result = send_robot_command_to_perform(command, ROS_FLASK_SERVER, dialog_id)39except Exception as e:40sentry_sdk.capture_exception(e)41logger.exception(e)42
43if result:44results += [{"human_attributes": {"performing_command": command}}]45else:46results += [{"human_attributes": {}}]47logger.info(f"robot_command_sender: status of sending command `{command}`: `{result}`")48else:49logger.info("robot_command_sender: NO command found in prev bot uttr")50results += [{"human_attributes": {}}]51
52total_time = time.time() - st_time53logger.info(f"robot_command_sender exec time: {total_time:.3f}s")54return jsonify(results)55