private async Task RealizeSetPowerState(PredictionResponse command) { var deviceType = await ExtractEntity(command, FirstLevelEntity.DeviceType, required : true); var location = await ExtractEntity(command, FirstLevelEntity.Location, required : true); var powerState = await ExtractEntity(command, FirstLevelEntity.PowerState, required : true); var device = DbContext.Device.FirstOrDefault(x => x.DeviceTypeId == deviceType.LuisEntityId && x.LocationId == location.LuisEntityId); if (device == null) { var msg = $"Not found device of type {deviceType.Name} in {location.Name}."; SpeechProcessor.TextToSpeech(msg); RealizeCancel(); return; } var stateChanged = device.PowerStateId != powerState.LuisEntityId; device.PowerStateId = powerState.LuisEntityId; DbContext.SaveChanges(); var messageAfterExecution = stateChanged ? $"Device {device.Name} {powerState.Name}" : $"Device {device.Name} is already {powerState.Name}"; await SpeechProcessor.TextToSpeech(messageAfterExecution); }
private static async Task HandleIntent(PredictionResponse predictionResponse, MessageType msgObj, MessageSender outputServiceBus) { if (!msgObj.messageType.Equals("chat")) { return; } var topIntent = predictionResponse.Prediction.TopIntent; if (topIntent != null && topIntent.Equals("OrderIn")) { // What service do you need delivered to your room. if (predictionResponse.Prediction.Intents["OrderIn"].Score > 0.75) { string destination = String.Empty; foreach (var entity in predictionResponse.Prediction.Entities) { destination = entity.Key; break; } //Detected an actionable request with an identified entity. if (!destination.Equals(String.Empty)) { var generatedMessage = $"We've sent your message '{msgObj.message}' to {destination}, and they will confirm it shortly."; await SendBotMessage(msgObj, generatedMessage, outputServiceBus); } } } }
public static PredictionResponse MapFrom(this PredictionResponse self, MlFlowPredictionResponse data) { // Assumption that we will get one and only one // response since we are not doing bulk prediction var prediction = data.Predictions.First(); var explanation = data.Explanations.First(); // Map the response structure from the MLFLow API // to the response structure for this API self.Status = RequestStatus.Ok; self.Prediction = prediction; self.Suburb = explanation.Suburb; self.Rooms = explanation.Rooms; self.Type = explanation.Type; self.Method = explanation.Method; self.Postcode = explanation.Postcode; self.Regionname = explanation.Regionname; self.Propertycount = explanation.Propertycount; self.Distance = explanation.Distance; self.CouncilArea = explanation.CouncilArea; self.Month = explanation.Month; self.Year = explanation.Year; self.GlobalMean = explanation.GlobalMean; return(self); }
private async Task RealizeGetList(PredictionResponse command) { var deviceType = await ExtractEntity(command, FirstLevelEntity.DeviceType, required : false); var location = await ExtractEntity(command, FirstLevelEntity.Location, required : false); var powerState = await ExtractEntity(command, FirstLevelEntity.PowerState, required : false); var filterText = ""; IQueryable <Device> query = DbContext.Device; if (deviceType != null) { query = query.Where(x => x.DeviceTypeId == deviceType.LuisEntityId); filterText += " of type " + deviceType.Name; } if (location != null) { query = query.Where(x => x.LocationId == location.LuisEntityId); filterText += " in " + location.Name; } if (powerState != null) { query = query.Where(x => x.PowerStateId == powerState.LuisEntityId); filterText += " with state " + powerState.Name; } var results = query.Select(x => x.Name).ToArray(); var msg = results.Count() > 0 ? $"Devices{filterText} are: {string.Join(", ", results)}" : $"Not found devices{filterText}"; await SpeechProcessor.TextToSpeech(msg); }
public async Task <PredictionResponse> Predict(PredictionRequest predictionRequest) { var guid = Guid.NewGuid().ToString(); var predictionResponseItem = new PredictionResponseItem(); var response = new PredictionResponse(); var databaseContext = this.CreateContext(); var cache = await databaseContext.PredictionRequests.Where(g => g.Guid == guid).ToListAsync(); await this.InsertRequests(predictionRequest, guid); foreach (var loopItem in predictionRequest.Items) { predictionResponseItem = new PredictionResponseItem(); await CalculateProbability(loopItem, predictionResponseItem); response.Predictions.Add(predictionResponseItem); } // Experiment if (this.serviceConfiguration.ExperimentEnabled) { await this.InsertFilePredictionForExperiment(predictionRequest, guid); response = await this.FilterResponseForExperiment(predictionRequest, response); } return(response); }
private async Task RealizeCheckInternetSpeed(PredictionResponse command) { var rnd = new Random(); var speed = rnd.NextDouble() * 150 + 20; var message = FormattableString.Invariant($"Internet speed is {speed:0.#} megabytes per second"); await SpeechProcessor.TextToSpeech(message); }
static async Task Main(string[] args) { const string PREDICTION_KEY = "PONER AQUI LA CLAVE DEL RECURSO DE PREDICCION DE LUIS"; const string ENDPOINT = "PONER AQUI LA URL DEL ENDPOINT DE PREDICCION DE LUIS"; Guid APPID = new Guid("PONER AQUI EL IDENTIFICADOR DE LA APLICACION DE LUIS"); string slot = "Staging"; //Staging o Production LUISRuntimeClient client = new LUISRuntimeClient(new ApiKeyServiceClientCredentials(PREDICTION_KEY)) { Endpoint = ENDPOINT }; while (true) { //Realizamos la petición a la API Console.Write("Introduce la orden: "); string pregunta = Console.ReadLine(); PredictionRequest peticion = new PredictionRequest { Query = pregunta }; PredictionResponse prediccion = await client.Prediction.GetSlotPredictionAsync(APPID, slot, peticion); //Procesamos el resultado Console.WriteLine($"Acción: {prediccion.Prediction.TopIntent}"); foreach (var entidad in prediccion.Prediction.Entities) { Console.WriteLine($"{entidad.Key}: {JsonConvert.DeserializeObject<List<string>>(entidad.Value.ToString())[0]}"); } Console.WriteLine(); } }
public async Task <SpeechRecognizedResult> ValidateSpeechInputAsync(string text) { if (string.IsNullOrEmpty(text)) { return(new SpeechRecognizedResult()); } PredictionResponse prediction = null; SpeechOutputResult speechOutputResult = new SpeechOutputResult(); try { prediction = await _luisClient.Prediction.GetSlotPredictionAsync(Guid.Parse(_config.LuisAppId), _config.LuisAppSlot, new PredictionRequest { Query = text }); speechOutputResult = await AnalyzePredictionAsync(prediction); } catch (Exception ex) { _logger.LogError(ex, "Error during speech recognition"); } return(new SpeechRecognizedResult { Speaker = null, Text = text, TopIntent = prediction?.Prediction?.TopIntent, Intents = prediction?.Prediction?.Intents, Entities = prediction?.Prediction?.Entities, VoiceResponse = speechOutputResult.Output }); }
private static void LogIfNecessary(PredictionResponse command) { if (Logging) { double?topIntentScore = command.Prediction.Intents.Max(x => x.Value.Score); Console.WriteLine($"Recognized intent {command.Prediction.TopIntent} with score {topIntentScore}. Found entities: {string.Join(", ", command.Prediction.Entities.Select(x => x.Key))}"); } }
private async Task <LuisEntity> ExtractEntity(PredictionResponse command, FirstLevelEntity firstLevelEntity, bool required) { LuisEntity result = null; var currentCommand = command; while (true) { if (currentCommand != command) { LogIfNecessary(currentCommand); } await CancelIfRequested(currentCommand); var mustAskToClarify = false; object nextLevelEntityObj = null; if (currentCommand.Prediction.Entities.TryGetValue(firstLevelEntity.ToString(), out nextLevelEntityObj) && nextLevelEntityObj is JArray nextLevelEntitiesContainer && nextLevelEntitiesContainer.Count == 1 ) { var nextLevelEntities = nextLevelEntitiesContainer .Select(x => (x.FirstOrDefault() as JProperty)) .Where(x => x != null) .Select(x => x.Name) .Distinct() .ToList(); if (nextLevelEntities.Count == 1) { var foundEntity = nextLevelEntities.First(); return(DbContext.LuisEntity.FirstOrDefault(x => x.Name == foundEntity)); } else if (nextLevelEntities.Count > 1) { mustAskToClarify = true; } } if (required || mustAskToClarify) { var question = $"Please specify {firstLevelEntity}"; SpeechProcessor.TextToSpeech(question); var userResponse = await SpeechProcessor.SpeechToText(); var userText = await SpeechProcessor.HandleSpeachToTextResponse(userResponse); currentCommand = await TextToCommand(userText); } else { return(null); } } RealizeCancel(); return(null); }
private async Task RealizeGetWeather(PredictionResponse command) { var rnd = new Random(); var weather = new[] { "cloudy", "rainy", "sunny", "windy" }; var temp = rnd.NextDouble() * 30; var message = FormattableString.Invariant($"It is {weather[rnd.Next(weather.Length)]} outside. The temperature is {temp:0.#} Celsius degrees."); await SpeechProcessor.TextToSpeech(message); }
public List <PredictionResult> GetButtonsInsideBox(PredictionResult box, PredictionResponse prediction) { var buttonsInside = prediction.Predictions .Where(i => i.TagName == "Button" && i.BoundingBox.Left > box.BoundingBox.Left && i.BoundingBox.Top > box.BoundingBox.Top).OrderByDescending(i => i.Probability) .Take(4).ToList(); return(buttonsInside); }
private async Task CancelIfRequested(PredictionResponse currentCommand) { if (currentCommand.Prediction.TopIntent == Intent.cancel.ToString() && currentCommand.Prediction.Intents.Max(x => x.Value.Score > cancelIntentThreshold)) { await RealizeCancel(); throw new BotException(); } }
private static PredictionResponse CreateResponse(IEnumerable <BasicDataset> predictions) { PredictionResponse response; Dictionary <string, string> dateClosePredictions = ConvertToDateCloseDictionary(predictions); response = new PredictionResponse((int)ResponseCode.OK, dateClosePredictions); return(response); }
public static byte[] GetResponse(byte[] receivedData) { FileTransferRequest request = RequestHandler.RestoreRequest <FileTransferRequest>(receivedData); IEnumerable <BasicDataset> predictions = GetPrediction(request); PredictionResponse response = CreateResponse(predictions); return(ResponseManager.CreateByteResponse(response)); }
public static CustomLuisResponse MapToCustomLuisRespone(PredictionResponse luisResponse) { return(new CustomLuisResponse { Query = luisResponse.Query, TopIntent = luisResponse.Prediction.TopIntent, Intents = MapLuisPredictionIntents(luisResponse.Prediction.Intents), Entities = MapLuisPredictionEntities(JObject.FromObject(luisResponse.Prediction.Entities)) }); }
public string RunningGrossIncomeBotWorker(PredictionResponse prediction, string language) { var year = DateTime.Now.Year; var runningGrossIncome = Database.Invoices.Where(i => i.Date.Year == year).Sum(i => i.Price); return(language switch { "it" => $"Il fatturato corrente ammonta a {runningGrossIncome:0.##} Euro", "en" => $"The running gross income is {runningGrossIncome:0.##} Euro", _ => throw new NotImplementedException() });
public async Task HandleCommand(PredictionResponse command) { double?topIntentScore = command.Prediction.Intents.Max(x => x.Value.Score); LogIfNecessary(command); if (!Enum.TryParse(command.Prediction.TopIntent, out Intent topIntent) || topIntent == Intent.None || topIntentScore < validIntentThreshold) { string textToSay = $"I dont understand command: {command.Query}"; await SpeechProcessor.TextToSpeech(textToSay); return; } switch (topIntent) { case Intent.cancel: await RealizeCancel(); break; case Intent.set_power_state: await RealizeSetPowerState(command); break; case Intent.get_list: await RealizeGetList(command); break; case Intent.get_weather: await RealizeGetWeather(command); break; case Intent.who_is_at_home: await RealizeWhoIsAtHome(command); break; case Intent.check_internet_speed: await RealizeCheckInternetSpeed(command); break; default: throw new NotSupportedException($"Intent {topIntent} not supported"); } }
public async Task HandleVoiceCommand() { var toTextResult = await this.SpeechProcessor.SpeechToText(); if (!toTextResult.Success) { await SpeechProcessor.TextToSpeech(toTextResult.Text); return; } PredictionResponse command = await TextToCommand(toTextResult.Text); await HandleCommand(command); }
public VisionResult GetResult(string imageFileName, PredictionResponse prediction) { var file = new FileInfo(imageFileName); var img = new Bitmap(file.FullName); var topBox = prediction. Predictions.Where(i => i.TagName == "Box").OrderByDescending(i => i.Probability) .FirstOrDefault(); var buttons = GetButtonsInsideBox(topBox, prediction); var result = new VisionResult() { Box = FromPrediction(file.Extension, img, topBox), Buttons = buttons.Select(i => FromPrediction(file.Extension, img, i)).ToList() }; return(null); }
public string ProduceAnswer(PredictionResponse prediction, string language) { switch (prediction.Prediction.TopIntent) { case "RunningGrossIncome": return(RunningGrossIncomeBotWorker(prediction, language)); case "YearlyGrossIncome": return(YearlyGrossIncomeBotWorker(prediction, language)); case "OutgoingInvoicePayment": return(OutgoingInvoicePaymentBotWorker(prediction, language)); default: return(null); } }
public async Task <PredictionServiceResult> GetPredictions(string projectName, string path, IEnumerable <string> filePaths) { this.logger.Info("Detecting changed files..."); var changedFiles = await this.DetectChanges(path, filePaths); if (changedFiles == null || changedFiles.Files.Count == 0) { this.logger.Info("No files changed. Nothing to do."); return(new PredictionServiceResult(0, null)); } foreach (var loopChangedFile in changedFiles.Files) { this.logger.Info($"\t({loopChangedFile.NumberOfModifiedLines} lines) {loopChangedFile.Path}"); } this.logger.Info("Calling the service..."); PredictionRequest request = BuildRequest(changedFiles); request.ProjectName = projectName; PredictionResponse response; try { response = await this.webClient.GetPredictions(request); } catch (WebServiceException webServiceException) { this.errorHandler.Handle(webServiceException.Message, webServiceException); return(new PredictionServiceResult(changedFiles.Files.Count, null)); } if (response == null) { response = new PredictionResponse(); } response.Request = request; return(new PredictionServiceResult(changedFiles.Files.Count, response)); }
private LabeledUtterance LuisResultToLabeledUtterance(PredictionResponse predictionResponse) { if (predictionResponse == null) { return(new LabeledUtterance(null, null, null)); } var mappedTypes = this.LuisSettings.PrebuiltEntityTypes .ToDictionary(pair => $"builtin.{pair.Value}", pair => pair.Key); var intent = predictionResponse.Prediction.TopIntent; var entities = GetEntities(predictionResponse.Query, predictionResponse.Prediction.Entities, mappedTypes)?.ToList(); var intentData = default(Intent); predictionResponse.Prediction.Intents?.TryGetValue(intent, out intentData); return(intentData != null && intentData.Score.HasValue ? new ScoredLabeledUtterance(predictionResponse.Query, intent, intentData.Score.Value, entities) : new LabeledUtterance(predictionResponse.Query, intent, entities)); }
public HttpResponseMessage PostPredict([FromBody] PredictionRequest pr) { var volatility = Simulations.Volatilities .TryFind(pr.Symbol).ToResult(); var price = pr.Price; if (volatility.IsOk) { var calcReq = new Simulations.CalcRequest(pr.NumTimesteps, pr.Price, volatility.Ok); price = Simulations.calcPriceCPU(calcReq); } var prediction = new PredictionResponse(price, new double[0]); return(this.Request.CreateResponse(HttpStatusCode.OK, prediction)); }
public void SetByUsingResponse(PredictionResponse predictionResponse) { if (predictionResponse == null) { this.SetReady(); return; } var numberOfPredictions = predictionResponse.Predictions.Count; var numberOfFails = predictionResponse.Predictions.Count(g => g.ProbableSuccess == false); if (numberOfFails == 0) { this.SetPredictionSuccess(numberOfPredictions); } else { this.SetPredictionFail(numberOfPredictions, numberOfFails); } }
private async Task <PredictionResponse> GetFramePrediction(VideoFrame request, Guid projectId, string publishedModelName) { var predictionResponse = new PredictionResponse { Timestamp = DateTime.Now, Millisecond = request.Millisecond }; using (var stream = File.OpenRead(request.FilePath)) { var response = await _predictionApi.DetectImageWithHttpMessagesAsync( projectId, publishedModelName, stream ); var objects = new List <PredictionObjectResponse>(); foreach (var prediction in response.Body.Predictions) { var predictionObjectResponse = new PredictionObjectResponse { Label = prediction.TagName, Confidence = prediction.Probability, BoundingBox = new BoundingBox { Height = prediction.BoundingBox.Height, Width = prediction.BoundingBox.Width, Top = prediction.BoundingBox.Top, Left = prediction.BoundingBox.Left } }; if (predictionObjectResponse.Confidence > 0.7) { objects.Add(predictionObjectResponse); } } predictionResponse.PredictionObjects = objects; } return(predictionResponse); }
private async Task RealizeWhoIsAtHome(PredictionResponse command) { var rnd = new Random(); var people = new[] { "Eva", "Julia", "Sylvia", "Adam", "John", "Barry", "Olaf" }; var peopleAtHome = new List <string>(); for (int i = 0; i < people.Length; i++) { if (rnd.NextDouble() < 0.2) { break; } peopleAtHome.Add(people[rnd.Next(people.Length)]); } var message = peopleAtHome.Any() ? $"There is {peopleAtHome.Count} people at home: {string.Join(", ", peopleAtHome)}" : "There is nobody at home"; await SpeechProcessor.TextToSpeech(message); }
public IEnumerator GetPrediction(WWWForm form) { using (UnityWebRequest www = UnityWebRequest.Post(predictionURL, form)) { yield return(www.SendWebRequest()); if (www.isNetworkError) { Debug.Log(www.error); } else { if (www.isDone) { Debug.Log(www.downloadHandler.text); serverResponse = JsonUtility.FromJson <PredictionResponse>(www.downloadHandler.text); giveTop3ToSceneManager(serverResponse.prediction); } } } }
public static void ShowInMessageBox(PredictionResponse response) { if (response == null) { Show("response == null"); return; } var builder = new StringBuilder(); for (int i = 0; i < response.Request.Items.Count; i++) { var file = response.Request.Items.ElementAtOrDefault(i); var prediction = response.Predictions.ElementAtOrDefault(i); if (file != null && prediction != null) { builder.AppendLine($"{file.Path} probable success={prediction.ProbableSuccess} success probability={prediction.SuccessProbability}"); } } Show(builder.ToString()); }
private async Task <PredictionResponse> GenerateResponse(PredictionRequest predictionRequest, string guid) { using (var databaseContext = this.CreateContext()) { var cache = await databaseContext.PredictionRequests.Where(g => g.Guid == guid).ToListAsync(); var response = new PredictionResponse(); foreach (var requestItem in predictionRequest.Items) { var cachedItem = cache.FirstOrDefault(g => g.Path == requestItem.Path); if (cachedItem == null) { throw new ApplicationException($"Cached item for path '{requestItem.Path}' not found"); } var predictionResponseItem = new PredictionResponseItem(); predictionResponseItem.ProbableSuccess = cachedItem.BuildResultPredictionClass; predictionResponseItem.SuccessProbability = 1 - cachedItem.BuildResultFaildPrediction; response.Predictions.Add(predictionResponseItem); } return(response); } }