示例#1
0
        public async Task <IActionResult> PostAsync([FromBody] IntentModificationRequest intentModificationRequest)
        {
            try
            {
                _speechToTextService.Initialize(intentModificationRequest.GetLanguage());
                var textServiceRespone = await _speechToTextService.Invork(new VoiceRequest()
                {
                    Id = intentModificationRequest.Id, VoiceData = intentModificationRequest.VoiceData
                });

                PersinalIntentData persinalIntentData = new PersinalIntentData()
                {
                    Id = textServiceRespone.Id
                };
                await persinalIntentData.WriteData(new KeyValuePair <string, string>(intentModificationRequest.IntentName, textServiceRespone.TextData));

                _textToSpeechService.Initialize(intentModificationRequest.GetLanguage());

                string textAns = "";
                if (intentModificationRequest.GetLanguage() == Models.Enums.Language.English)
                {
                    textAns = $"You have personalized command: {intentModificationRequest.IntentName}.";
                }
                else
                {
                    textAns = $"Вы персонализировали команду: {intentModificationRequest.IntentName}.";
                }
                var speechServiceRespone = await _textToSpeechService.Invork(new TextRequest()
                {
                    Id = textServiceRespone.Id, TextData = textAns
                });

                IntentModificationRespone intentModificationRespone = new IntentModificationRespone()
                {
                    Id         = speechServiceRespone.Id,
                    IntentName = intentModificationRequest.IntentName,
                    VoiceData  = speechServiceRespone.VoiceData
                };
                return(Ok(intentModificationRespone));
            }
            catch (Exception ex)
            {
                Log.LogError(intentModificationRequest.Id.Value, 0, this.GetType().ToString(), ex.Message);
                Console.WriteLine(ex);
                string outputFailText = "Я не расслышала, вашу команду. Пожалуйста, повторите ваш запрос.";
                if (intentModificationRequest.GetLanguage() == Enums.Language.English)
                {
                    outputFailText = "I did not hear what you said. Please repeat your request.";
                }
                _textToSpeechService.Initialize(intentModificationRequest.GetLanguage());
                var speechServiceRespone = await _textToSpeechService.Invork(new TextRequest()
                {
                    Id = intentModificationRequest.Id, TextData = outputFailText
                });

                IntentModificationRespone intentModificationRespone = new IntentModificationRespone
                {
                    Id         = speechServiceRespone.Id,
                    VoiceData  = speechServiceRespone.VoiceData,
                    IntentName = "none"
                };
                speechServiceRespone = null;
                return(Ok(intentModificationRespone));
            }
        }
示例#2
0
        public async Task <IActionResult> PostAsync([FromBody] ApiRequest apiRequest)
        {
            try
            {
                var time = DateTime.Now;
                //ApiRequest apiRequest = JsonConvert.DeserializeObject<ApiRequest>(requestStr);
                //convert speech to text
                if (apiRequest.GetRequestType() == Enums.RequestType.Voice)
                {
                    Log.LogInformation(apiRequest.Id.Value, 0, this.GetType().ToString(), "processing voice request");

                    _speechToTextService.Initialize(apiRequest.GetLanguage());
                    var textServiceRespone = await _speechToTextService.Invork(new VoiceRequest()
                    {
                        Id = apiRequest.Id, VoiceData = apiRequest.VoiceData
                    });

                    //recognize text and make text answer
                    _textProcessingService.Initialize(apiRequest.GetLanguage());
                    var textProcessingRespone = await _textProcessingService.Invork(new TextRequest()
                    {
                        Id = textServiceRespone.Id, TextData = textServiceRespone.TextData
                    });

                    //convert text to speech
                    _textToSpeechService.Initialize(apiRequest.GetLanguage());
                    var speechServiceRespone = await _textToSpeechService.Invork(new TextRequest()
                    {
                        Id = textProcessingRespone.Id, TextData = textProcessingRespone.TextData
                    });

                    //make a respone
                    ApiRespone apiRespone = new ApiRespone
                    {
                        Id         = speechServiceRespone.Id,
                        VoiceData  = speechServiceRespone.VoiceData,
                        InputText  = textServiceRespone.TextData,
                        OutputText = textProcessingRespone.TextData,
                        IntentName = textProcessingRespone.IntentName,
                        Entities   = textProcessingRespone.Entities,
                        WayPoint   = textProcessingRespone.WayPoint
                    };

                    textServiceRespone    = null;
                    textProcessingRespone = null;
                    speechServiceRespone  = null;

                    Log.LogInformation(apiRequest.Id.Value, 0, this.GetType().ToString(), $"voice request processed in {(DateTime.Now - time).TotalMilliseconds} ms");

                    Console.WriteLine("WayPoint:" + apiRespone.WayPoint);

                    return(Ok(apiRespone));
                }
                else
                {
                    Log.LogInformation(apiRequest.Id.Value, 0, this.GetType().ToString(), "processing text request");

                    //recognize text and make text answer
                    _textProcessingService.Initialize(apiRequest.GetLanguage());
                    var textProcessingRespone = await _textProcessingService.Invork(new TextRequest()
                    {
                        Id = apiRequest.Id, TextData = apiRequest.TextData
                    });

                    //make a respone
                    ApiRespone apiRespone = new ApiRespone
                    {
                        Id         = apiRequest.Id.Value,
                        VoiceData  = null,
                        InputText  = apiRequest.TextData,
                        OutputText = textProcessingRespone.TextData,
                        IntentName = textProcessingRespone.IntentName,
                        Entities   = textProcessingRespone.Entities,
                        WayPoint   = textProcessingRespone.WayPoint
                    };

                    textProcessingRespone = null;

                    Log.LogInformation(apiRequest.Id.Value, 0, this.GetType().ToString(), "text request processed");

                    return(Ok(apiRespone));
                }
            }
            catch (Exception ex)
            {
                Log.LogError(apiRequest.Id.Value, 0, this.GetType().ToString(), ex.Message);
                Console.WriteLine(ex);
                string outputFailText = "Я не расслышала, что вы сказали. Пожалуйста, повторите ваш запрос";
                if (apiRequest.GetLanguage() == Enums.Language.English)
                {
                    outputFailText = "I did not hear what you said. Please repeat your request";
                }
                _textToSpeechService.Initialize(apiRequest.GetLanguage());
                var speechServiceRespone = await _textToSpeechService.Invork(new TextRequest()
                {
                    Id = apiRequest.Id, TextData = outputFailText
                });

                ApiRespone apiRespone = new ApiRespone
                {
                    Id         = speechServiceRespone.Id,
                    VoiceData  = speechServiceRespone.VoiceData,
                    InputText  = "",
                    OutputText = outputFailText,
                    IntentName = "none",
                    Entities   = null,
                    WayPoint   = null
                };
                speechServiceRespone = null;
                return(Ok(apiRespone));
            }
        }