public async Task <string> ClassifySentiment(string usersUtterance)
        {
            try
            {
                var data = new SentimentData()
                {
                    SentimentText = usersUtterance
                };

                var blobResult = _blobService.DownloadModelAsync().Result;

                if (blobResult != null)
                {
                    var model = await PredictionModel.ReadAsync <SentimentData, SentimentPrediction>(blobResult);

                    var classification = model.Predict(data);
                    _logger.LogInfo($"Sentiment returned from model was: {classification.Sentiment}");

                    return($"{(classification.Sentiment ? "Positive" : "Negative")}");
                }

                return(null);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                throw ex;
            }
        }
Beispiel #2
0
        public async Task <Stream> DownloadModelAsync()
        {
            Stream stream = new MemoryStream();

            try
            {
                _cache.TryGetValue("trainedModel", out stream);

                if (stream != null)
                {
                    return(stream);
                }

                else
                {
                    stream = new MemoryStream();
                    await blockBlob.DownloadToStreamAsync(target : stream);

                    _cache.Set("trainedModel", stream);
                }

                return(stream);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return(null);
            }
        }
        public async Task <IActionResult> Get([FromQuery(Name = "query")] string query)
        {
            try
            {
                if (string.IsNullOrEmpty(query))
                {
                    return(BadRequest("'query' url parameter is missing."));
                }

                var classifedUtterance = await _classifyService.ClassifySentiment(query);

                if (!string.IsNullOrEmpty(classifedUtterance))
                {
                    return(Json($"Sentiment: {classifedUtterance}"));
                }

                return(Json($"Error: Could not receive response from Blob Storage."));
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return(Json("Exception occurred, please try again later."));
            }
        }