Example #1
0
        public async Task Invokes_predict_async_call()
        {
            var expectedResultValues = new[] { 1f, 2f, 3f };

            var outputTensorProto = new TensorProto {
                Dtype = DataType.DtFloat
            };

            outputTensorProto.FloatVal.Add(expectedResultValues);

            outputTensorProto.TensorShape = new TensorShapeProto();
            outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim());
            outputTensorProto.TensorShape.Dim[0].Size = 3;

            var predictRequest  = new PredictRequest();
            var predictResponse = new PredictResponse();

            predictResponse.Outputs.Add("output_alias", outputTensorProto);

            var predictionServiceClientMock = new Mock <IPredictionServiceClient>();

            predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).ReturnsAsync(predictResponse).Verifiable();

            var scoringRequestMock = new Mock <IScoringRequest>();

            scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest);

            var scoringClient = new ScoringClient(predictionServiceClientMock.Object);
            var result        = await scoringClient.ScoreAsync(scoringRequestMock.Object);

            Assert.Equal(expectedResultValues, result);

            scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1));
            predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(1));
        }
Example #2
0
        public async Task PredictAsync()
        {
            Mock <PredictionService.PredictionServiceClient> mockGrpcClient = new Mock <PredictionService.PredictionServiceClient>(MockBehavior.Strict);

            mockGrpcClient.Setup(x => x.CreateOperationsClient())
            .Returns(new Mock <Operations.OperationsClient>().Object);
            PredictRequest expectedRequest = new PredictRequest
            {
                ModelName = new ModelName("[PROJECT]", "[LOCATION]", "[MODEL]"),
                Payload   = new ExamplePayload(),
                Params    = { },
            };
            PredictResponse expectedResponse = new PredictResponse();

            mockGrpcClient.Setup(x => x.PredictAsync(expectedRequest, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <PredictResponse>(Task.FromResult(expectedResponse), null, null, null, null));
            PredictionServiceClient client       = new PredictionServiceClientImpl(mockGrpcClient.Object, null);
            ModelName      name                  = new ModelName("[PROJECT]", "[LOCATION]", "[MODEL]");
            ExamplePayload payload               = new ExamplePayload();
            IDictionary <string, string> @params = new Dictionary <string, string>();
            PredictResponse response             = await client.PredictAsync(name, payload, @params);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
    public PredictResponse GetPrediction(int predictedZoneId, string dataSubject)
    {
        WebRequest request = WebRequest.Create(apiRoot + predictPath
                                               + "?id=" + predictedZoneId.ToString()
                                               + "&dataSubject=" + WWW.EscapeURL(dataSubject)
                                               + "&apiKey=" + apiKey);

        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;
        // Get the response.
        WebResponse response = request.GetResponse();

        // Display the status.
        Debug.Log(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();

        PredictResponse output = JsonUtility.FromJson <PredictResponse>(responseFromServer);

        Debug.Log("prediction form server age = " + output.value + " - " + output.latestDataUsed);

        reader.Close();
        response.Close();

        return(output);
    }
Example #4
0
        public async stt::Task PredictRequestObjectAsync()
        {
            moq::Mock <PredictionService.PredictionServiceClient> mockGrpcClient = new moq::Mock <PredictionService.PredictionServiceClient>(moq::MockBehavior.Strict);
            PredictRequest request = new PredictRequest
            {
                EndpointAsEndpointName = EndpointName.FromProjectLocationEndpoint("[PROJECT]", "[LOCATION]", "[ENDPOINT]"),
                Instances  = { new wkt::Value(), },
                Parameters = new wkt::Value(),
            };
            PredictResponse expectedResponse = new PredictResponse
            {
                Predictions     = { new wkt::Value(), },
                DeployedModelId = "deployed_model_idf0bd41af",
            };

            mockGrpcClient.Setup(x => x.PredictAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <PredictResponse>(stt::Task.FromResult(expectedResponse), null, null, null, null));
            PredictionServiceClient client = new PredictionServiceClientImpl(mockGrpcClient.Object, null);
            PredictResponse         responseCallSettings = await client.PredictAsync(request, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None));

            xunit::Assert.Same(expectedResponse, responseCallSettings);
            PredictResponse responseCancellationToken = await client.PredictAsync(request, st::CancellationToken.None);

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
Example #5
0
        public async Task <PredictResponse> Submit(Question question)
        {
            _httpClient.VerifyNotNull($"{nameof(PredictService)} has been disposed");

            await _limit.WaitAsync(TimeSpan.FromMinutes(5));

            try
            {
                string requestUri = _option.PropertyResolver.Resolve(_option.ServiceUri);

                _logger.LogInformation($"Sending question '{_json.Serialize(question)}' to model at {requestUri}.");

                var             sw = Stopwatch.StartNew();
                PredictResponse predictResponse = await _httpClient.PostAsJsonAsync(requestUri, question)
                                                  .GetContent <PredictResponse>();

                predictResponse.Request ??= question.Sentence;

                _logger.LogInformation($"Receive answer '{_json.Serialize(predictResponse)}' from model for question '{question.Sentence}', ms={sw.ElapsedMilliseconds}");
                return(predictResponse);
            }
            finally
            {
                _limit.Release();
            }
        }
    float getPredictionOfWindDirection()
    {
        PredictResponse predictedDirSin = api.GetPrediction(zoneId, "sin wind direction");

        Debug.Log("prediction = " + predictedDirSin.ToString());
        DateTime sinAge = Convert.ToDateTime(predictedDirSin.latestDataUsed);


        Debug.Log("prediction age  = " + predictionAge.ToString() + " - " + (DateTime.Now.Second - predictionAge.Second).ToString());

        PredictResponse predictedDirCos = api.GetPrediction(zoneId, "cos wind direction");

        Debug.Log("prediction = " + predictedDirCos.ToString());
        DateTime cosAge = Convert.ToDateTime(predictedDirCos.latestDataUsed);

        Debug.Log("prediction age  = " + predictionAge.ToString() + " - " + (DateTime.Now.Second - predictionAge.Second).ToString());

        if (sinAge == cosAge)
        {
            predictionAge = sinAge;
            arrow.color   = new Color(1f, 1f, 0f);
        }
        else
        {
            arrow.color = new Color(1f, 1f, 1f);
        }



        return(MyMaths.SinCosToDegrees(predictedDirSin.value, predictedDirCos.value));
    }
Example #7
0
        public static MultiObjectLocalizationAndLabelingResult GetImageDetectionResult(string image_url, List <string> Categories)
        {
            MultiObjectLocalizationAndLabelingResult ret = new MultiObjectLocalizationAndLabelingResult();
            int             height = 0;
            int             width  = 0;
            PredictResponse res    = ImageDetectionRequest(image_url, out height, out width);


            //float score_threshold =
            int class_counts = res.Outputs["detection_classes"].FloatVal.Count;

            float[] classes = new float[class_counts];
            res.Outputs["detection_classes"].FloatVal.CopyTo(classes, 0);
            int box_counts = res.Outputs["detection_boxes"].FloatVal.Count;

            float[] boxes = new float[box_counts];
            res.Outputs["detection_boxes"].FloatVal.CopyTo(boxes, 0);
            int score_counts = res.Outputs["detection_scores"].FloatVal.Count;

            float[] scores = new float[score_counts];
            res.Outputs["detection_scores"].FloatVal.CopyTo(scores, 0);

            double score_threshold = 0.5;

            for (int i = 0; i < score_counts; i++)
            {
                float s = scores[i];

                if (s == 0)
                {
                    break;
                }
                if (s < score_threshold)
                {
                    continue;
                }

                int c = (int)classes[i] - 1;
                // comes in as ymin, xmin, ymax, xmax
                int tlx = (int)(boxes[4 * i + 1] * width);
                int tly = (int)(boxes[4 * i] * height);
                int brx = (int)(boxes[4 * i + 3] * width);
                int bry = (int)(boxes[4 * i + 2] * height);

                MultiObjectLocalizationAndLabelingResultSingleEntry e = new MultiObjectLocalizationAndLabelingResultSingleEntry();
                e.boundingBox = new BoundingBox(tlx, tly, brx, bry);
                e.Category    = Categories[c];

                ret.objects.Add(e);
            }

            ret.imageHeight = height;
            ret.imageWidth  = width;


            return(ret);
        }
Example #8
0
        public PredictionResult Predict(byte[] byteArray)
        {
            TensorProto    tensorProto = this.GetTensorProto(byteArray);
            PredictRequest request     = this.GetPredictRequest(tensorProto);

            DateTime deadline = DateTime.UtcNow.AddSeconds(TIMEOUT);

            PredictResponse response = _client.Predict(request, new CallOptions(deadline: deadline));

            return(this.ParseResponse(response));
        }
Example #9
0
        public async Task <IEnumerable <Detection> > Predict(Bitmap bmp)
        {
            if (_client == null)
            {
                throw new ApplicationException(nameof(_client));
            }

            // Desired image format
            const int         channels = 3;
            const int         width    = 300;
            const int         height   = 300;
            const PixelFormat format   = PixelFormat.Format24bppRgb;

            var shape = new TensorShapeProto
            {
                Dim = { new []
                        {
                            new TensorShapeProto.Types.Dim {
                                Name = "", Size = 1
                            },
                            new TensorShapeProto.Types.Dim {
                                Name = nameof(height), Size = height
                            },
                            new TensorShapeProto.Types.Dim {
                                Name = nameof(width), Size = width
                            },
                            new TensorShapeProto.Types.Dim {
                                Name = nameof(channels), Size = channels
                            },
                        } }
            };

            var proto = new TensorProto
            {
                TensorShape   = shape,
                Dtype         = Tensorflow.DataType.DtUint8,
                TensorContent = ToByteString(bmp, channels, width, height, format)
            };

            var request = new PredictRequest
            {
                ModelSpec = new ModelSpec {
                    Name = _model
                }
            };

            request.Inputs.Add("data", proto);

            // Send requenst for inference
            PredictResponse response = await _client.PredictAsync(request);

            return(ToDetections(response));
        }
Example #10
0
 /// <summary>Snippet for Predict</summary>
 public void Predict()
 {
     // Snippet: Predict(string, IEnumerable<Value>, Value, CallSettings)
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     string endpoint = "projects/[PROJECT]/locations/[LOCATION]/endpoints/[ENDPOINT]";
     IEnumerable <Value> instances = new Value[] { new Value(), };
     Value parameters = new Value();
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(endpoint, instances, parameters);
     // End snippet
 }
Example #11
0
 /// <summary>Snippet for Predict</summary>
 public void PredictResourceNames()
 {
     // Snippet: Predict(EndpointName, IEnumerable<Value>, Value, CallSettings)
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     EndpointName        endpoint  = EndpointName.FromProjectLocationEndpoint("[PROJECT]", "[LOCATION]", "[ENDPOINT]");
     IEnumerable <Value> instances = new Value[] { new Value(), };
     Value parameters = new Value();
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(endpoint, instances, parameters);
     // End snippet
 }
Example #12
0
 /// <summary>Snippet for Predict</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void Predict()
 {
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     string         name    = "projects/[PROJECT]/locations/[LOCATION]/models/[MODEL]";
     ExamplePayload payload = new ExamplePayload();
     IDictionary <string, string> @params = new Dictionary <string, string> {
         { "", "" },
     };
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(name, payload, @params);
 }
        public IActionResult Serve(
            string serve, string itemIdServe, int modelIdServe
            )
        {
            PredictResponse Result = VisualInspection.Serve(new string[] { itemIdServe }, modelIdServe);

            ViewData["HttpResponse"]  = "Status code: " + (int)Result.HttpResponse.StatusCode + " " + Result.HttpResponse.StatusCode;
            ViewData["StringMessage"] = Result.HttpResponse.Content;
            ViewBag.BearerAvailable   = VisualInspection.IsAuthorized;
            ViewBag.Active            = "serve";

            return(Index());
        }
Example #14
0
 /// <summary>Snippet for Predict</summary>
 public void Predict()
 {
     // Snippet: Predict(ModelName,ExamplePayload,IDictionary<string, string>,CallSettings)
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     ModelName      name    = new ModelName("[PROJECT]", "[LOCATION]", "[MODEL]");
     ExamplePayload payload = new ExamplePayload();
     IDictionary <string, string> @params = new Dictionary <string, string>();
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(name, payload, @params);
     // End snippet
 }
 /// <summary>Snippet for Predict</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void PredictResourceNames()
 {
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     ModelName      name    = ModelName.FromProjectLocationModel("[PROJECT]", "[LOCATION]", "[MODEL]");
     ExamplePayload payload = new ExamplePayload();
     IDictionary <string, string> @params = new Dictionary <string, string> {
         { "", "" },
     };
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(name, payload, @params);
 }
Example #16
0
        public IActionResult Serve(
            string serve, string itemIdServe, int modelIdServe
            )
        {
            PredictResponse response = hacarusVisualInspection.Serve(new string[] { itemIdServe }, modelIdServe);

            ViewData["HttpResponse"]  = "Status code: " + (int)response.httpResponse.StatusCode + " " + response.httpResponse.StatusCode;
            ViewData["StringMessage"] = hacarusVisualInspection.StringMessage;
            ViewBag.BearerAvailable   = !string.IsNullOrEmpty(bearer);
            ViewBag.Active            = "serve";

            return(Index());
        }
Example #17
0
 /// <summary>Snippet for Predict</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void PredictRequestObject()
 {
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     PredictRequest request = new PredictRequest
     {
         ModelName = ModelName.FromProjectLocationModel("[PROJECT]", "[LOCATION]", "[MODEL]"),
         Payload   = new ExamplePayload(),
         Params    = { { "", "" }, },
     };
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(request);
 }
Example #18
0
        public async stt::Task PredictRequestObjectAsync()
        {
            moq::Mock <PredictionService.PredictionServiceClient> mockGrpcClient = new moq::Mock <PredictionService.PredictionServiceClient>(moq::MockBehavior.Strict);
            PredictRequest request = new PredictRequest
            {
                Placement    = "placementb440552a",
                UserEvent    = new UserEvent(),
                PageSize     = -226905851,
                PageToken    = "page_tokenf09e5538",
                Filter       = "filtere47ac9b2",
                ValidateOnly = true,
                Params       =
                {
                    {
                        "key8a0b6e3c",
                        new wkt::Value()
                    },
                },
                Labels =
                {
                    {
                        "key8a0b6e3c",
                        "value60c16320"
                    },
                },
            };
            PredictResponse expectedResponse = new PredictResponse
            {
                Results =
                {
                    new PredictResponse.Types.PredictionResult(),
                },
                AttributionToken = "attribution_token14371a88",
                MissingIds       =
                {
                    "missing_ids9e3bd4de",
                },
                ValidateOnly = true,
            };

            mockGrpcClient.Setup(x => x.PredictAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <PredictResponse>(stt::Task.FromResult(expectedResponse), null, null, null, null));
            PredictionServiceClient client = new PredictionServiceClientImpl(mockGrpcClient.Object, null);
            PredictResponse         responseCallSettings = await client.PredictAsync(request, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None));

            xunit::Assert.Same(expectedResponse, responseCallSettings);
            PredictResponse responseCancellationToken = await client.PredictAsync(request, st::CancellationToken.None);

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
Example #19
0
        public async Task Retries_transient_exceptions(StatusCode transientStatusCode)
        {
            var exception = new RpcException(new Status(transientStatusCode, string.Empty));

            var expectedResultValues = new[] { 1f, 2f, 3f };

            var outputTensorProto = new TensorProto {
                Dtype = DataType.DtFloat
            };

            outputTensorProto.FloatVal.Add(expectedResultValues);

            outputTensorProto.TensorShape = new TensorShapeProto();
            outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim());
            outputTensorProto.TensorShape.Dim[0].Size = 3;

            var predictRequest  = new PredictRequest();
            var predictResponse = new PredictResponse();

            predictResponse.Outputs.Add("output_alias", outputTensorProto);

            var predictionServiceClientMock = new Mock <IPredictionServiceClient>();

            predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).Returns(async() =>
            {
                await Task.CompletedTask;

                if (exception != null)
                {
                    var x     = exception;
                    exception = null;
                    throw x;
                }

                return(predictResponse);
            }).Verifiable();

            var scoringRequestMock = new Mock <IScoringRequest>();

            scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest);

            var scoringClient = new ScoringClient(predictionServiceClientMock.Object);
            var result        = await scoringClient.ScoreAsync(scoringRequestMock.Object);

            Assert.Equal(expectedResultValues, result);

            scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1));
            predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(2));
        }
    string resultString = "";               // final result string

    List <Tuple <string, float> > SendAndReceive(byte[] imageData)
    {
        var tf_channel = new Channel(ipAddress, Convert.ToInt32(port), ChannelCredentials.Insecure);
        var tf_client  = new PredictionService.PredictionServiceClient(tf_channel);

        try
        {
            //Create prediction request
            var request = new PredictRequest()
            {
                ModelSpec = new ModelSpec()
                {
                    Name          = "inception",
                    SignatureName = "predict_images"
                }
            };
            var imageTensor = TensorProtoBuilder.TensorProtoFromImage(imageData);

            // Add image tensor to request
            request.Inputs.Add("images", imageTensor);
            // Send request and get response
            PredictResponse predictResponse = tf_client.Predict(request);
            // Decode response
            var      classesTensor = predictResponse.Outputs["classes"];
            string[] classes       = TensorProtoDecoder.TensorProtoToStringArray(classesTensor);

            var     scoresTensor = predictResponse.Outputs["scores"];
            float[] scores       = TensorProtoDecoder.TensorProtoToFloatArray(scoresTensor);

            List <Tuple <string, float> > predictResult = new List <Tuple <string, float> >();

            for (int i = 0; i < classes.Length; i++)
            {
                predictResult.Add(new Tuple <string, float>(classes[i], scores[i]));
            }
            return(predictResult);
        }
        catch (Exception e)
        {
            if (e is RpcException)
            {
                RpcException re = (RpcException)e;
                Debug.Log(re.Status.Detail);
                Debug.Log(re.StatusCode);
            }
            Debug.Log(e.Message);
            throw;
        }
    }
Example #21
0
        public List <ImageFeature> Process(ByteString image)
        {
            List <ImageFeature> result = new List <ImageFeature>();

            try
            {
                IScoringRequest request = null;
                float[]         offsets = new float[] { 123, 117, 104 };
                using (MemoryStream stream = new MemoryStream(image.ToByteArray()))
                    using (Bitmap bitmap = new Bitmap(stream))
                    {
                        int     width     = bitmap.Width;
                        int     height    = bitmap.Height;
                        float[] values    = new float[3 * width * height];
                        int     valuesIdx = 0;

                        for (int i = 0; i < height; i++)
                        {
                            for (int j = 0; j < width; j++)
                            {
                                var pixel = bitmap.GetPixel(j, i);
                                values[valuesIdx++] = pixel.B - offsets[0];
                                values[valuesIdx++] = pixel.G - offsets[1];
                                values[valuesIdx++] = pixel.R - offsets[2];
                            }
                        }
                        int[] shape = new int[] { 1, 300, 300, 3 };
                        Tuple <float[], int[]> tuple = new Tuple <float[], int[]>(values, shape);
                        Dictionary <string, Tuple <float[], int[]> > inputs = new Dictionary <string, Tuple <float[], int[]> >
                        {
                            { "brainwave_ssd_vgg_1_Version_0.1_input_1:0", tuple }
                        };

                        request = new FloatRequest(inputs);
                    }

                PredictResponse response = Predict(request.MakePredictRequest());
                if (response != null)
                {
                    result = FpgaPostProcess.PostProcess(response, selectThreshold: 0.5F, jaccardThreshold: 0.45F);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed during FPGA Pre- or Post- Process: {ex.Message}");
            }

            return(result);
        }
Example #22
0
 /// <summary>Snippet for Predict</summary>
 public void Predict_RequestObject()
 {
     // Snippet: Predict(PredictRequest,CallSettings)
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     PredictRequest request = new PredictRequest
     {
         ModelName = new ModelName("[PROJECT]", "[LOCATION]", "[MODEL]"),
         Payload   = new ExamplePayload(),
     };
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(request);
     // End snippet
 }
        public async Task GivenTestModel_WhenUsed_ShouldResponed()
        {
            TestWebsiteHost host = await TestApplication.GetHost();

            await host.WaitForStartup();

            var request = new PredictRequest
            {
                Request = "I am sad",
            };

            PredictResponse predictResponse = (await new ModelRestApi(host.Client).PostRequest(request)).Value !;

            Verify(predictResponse, request.Request);
        }
Example #24
0
 /// <summary>Snippet for Predict</summary>
 public void PredictRequestObject()
 {
     // Snippet: Predict(PredictRequest, CallSettings)
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     PredictRequest request = new PredictRequest
     {
         Name    = "",
         Payload = new ExamplePayload(),
         Params  = { { "", "" }, },
     };
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(request);
     // End snippet
 }
Example #25
0
        /// <summary>Snippet for PredictAsync</summary>
        public async Task PredictAsync()
        {
            // Snippet: PredictAsync(ModelName,ExamplePayload,IDictionary<string, string>,CallSettings)
            // Additional: PredictAsync(ModelName,ExamplePayload,IDictionary<string, string>,CancellationToken)
            // Create client
            PredictionServiceClient predictionServiceClient = await PredictionServiceClient.CreateAsync();

            // Initialize request argument(s)
            ModelName      name    = new ModelName("[PROJECT]", "[LOCATION]", "[MODEL]");
            ExamplePayload payload = new ExamplePayload();
            IDictionary <string, string> @params = new Dictionary <string, string>();
            // Make the request
            PredictResponse response = await predictionServiceClient.PredictAsync(name, payload, @params);

            // End snippet
        }
Example #26
0
 /// <summary>Snippet for Predict</summary>
 public void PredictRequestObject()
 {
     // Snippet: Predict(PredictRequest, CallSettings)
     // Create client
     PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create();
     // Initialize request argument(s)
     PredictRequest request = new PredictRequest
     {
         EndpointAsEndpointName = EndpointName.FromProjectLocationEndpoint("[PROJECT]", "[LOCATION]", "[ENDPOINT]"),
         Instances  = { new Value(), },
         Parameters = new Value(),
     };
     // Make the request
     PredictResponse response = predictionServiceClient.Predict(request);
     // End snippet
 }
        private void Verify(PredictResponse predictResponse, string request)
        {
            predictResponse.Should().NotBeNull();

            predictResponse.Request.Should().Be(request);

            predictResponse.Model.Should().NotBeNull();
            predictResponse.Model !.Name.Should().Be("fakeModel");
            predictResponse.Model !.Version.Should().Be("1.0");

            predictResponse.Intents.Should().NotBeNull();
            predictResponse.Intents !.Count.Should().BeGreaterThan(2);
            predictResponse.Intents.First().Label.Should().Be("HAPPINESS");
            predictResponse.Intents.First().Score.Should().Be(0.9824827);
            predictResponse.Intents.Last().Label.Should().Be("LOVE");
            predictResponse.Intents.Last().Score.Should().Be(0.009116333);
        }
Example #28
0
        private PredictionResult ParseResponse(PredictResponse response)
        {
            var resultClasses = response.Outputs["classes"].Int64Val;

            long[] classes = new long[Math.Max(resultClasses.Count, 8)];
            response.Outputs["classes"].Int64Val.CopyTo(classes, 0);

            var resultScores = response.Outputs["scores"].FloatVal;

            float[] scores = new float[Math.Max(resultScores.Count, 8)];
            response.Outputs["scores"].FloatVal.CopyTo(scores, 0);

            return(new PredictionResult()
            {
                PredictedClass = (int)classes[0], Scores = scores
            });
        }
Example #29
0
        public async Task <ActionResult <PredictResponse> > Submit([FromBody] PredictRequest request)
        {
            _logger.LogInformation($"{nameof(Submit)}: {_json.Serialize(request)}");

            if (!request.IsValidRequest())
            {
                return(StatusCode((int)HttpStatusCode.BadRequest));
            }

            switch (_executionContext.State)
            {
            case ExecutionState.Booting:
            case ExecutionState.Starting:
            case ExecutionState.Restarting:
                return(ReturnNotAvailable());

            case ExecutionState.Running:
                try
                {
                    PredictResponse hostResponse = await _predict.Submit(new Question { Sentence = request.Request ?? request.Sentence });

                    _logger.LogInformation($"{nameof(Submit)} answer: {_json.Serialize(hostResponse)}");

                    var result = new PredictResponse
                    {
                        Model   = hostResponse.Model,
                        Request = hostResponse.Request,
                        Intents = hostResponse.Intents
                                  .OrderByDescending(x => x.Score)
                                  .Take(request.IntentLimit ?? int.MaxValue)
                                  .ToList(),
                    };

                    return(Ok(result));
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Exception from model.  Ex={ex}");
                    throw;
                }

            default:
                _logger.LogError($"Failed: ExecutionState={_executionContext.State}");
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Example #30
0
        /// <summary>Snippet for PredictAsync</summary>
        public async Task PredictAsync_RequestObject()
        {
            // Snippet: PredictAsync(PredictRequest,CallSettings)
            // Additional: PredictAsync(PredictRequest,CancellationToken)
            // Create client
            PredictionServiceClient predictionServiceClient = await PredictionServiceClient.CreateAsync();

            // Initialize request argument(s)
            PredictRequest request = new PredictRequest
            {
                ModelName = new ModelName("[PROJECT]", "[LOCATION]", "[MODEL]"),
                Payload   = new ExamplePayload(),
            };
            // Make the request
            PredictResponse response = await predictionServiceClient.PredictAsync(request);

            // End snippet
        }