/// <summary>Snippet for Predict</summary> public void PredictResourceNames() { // Snippet: Predict(PlacementName, UserEvent, string, int?, CallSettings) // Create client PredictionServiceClient predictionServiceClient = PredictionServiceClient.Create(); // Initialize request argument(s) PlacementName name = PlacementName.FromProjectLocationCatalogEventStorePlacement("[PROJECT]", "[LOCATION]", "[CATALOG]", "[EVENT_STORE]", "[PLACEMENT]"); UserEvent userEvent = new UserEvent(); // Make the request PagedEnumerable <PredictResponse, PredictResponse.Types.PredictionResult> response = predictionServiceClient.Predict(name, userEvent); // Iterate over all response items, lazily performing RPCs as required foreach (PredictResponse.Types.PredictionResult item in response) { // Do something with each item Console.WriteLine(item); } // Or iterate over pages (of server-defined size), performing one RPC per page foreach (PredictResponse page in response.AsRawResponses()) { // Do something with each page of items Console.WriteLine("A page of results:"); foreach (PredictResponse.Types.PredictionResult item in page) { // Do something with each item Console.WriteLine(item); } } // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required int pageSize = 10; Page <PredictResponse.Types.PredictionResult> singlePage = response.ReadPage(pageSize); // Do something with the page of items Console.WriteLine($"A page of {pageSize} results (unless it's the final page):"); foreach (PredictResponse.Types.PredictionResult item in singlePage) { // Do something with each item Console.WriteLine(item); } // Store the pageToken, for when the next page is required. string nextPageToken = singlePage.NextPageToken; // End snippet }
public ActionResult <ServiceResponse> Post([Bind("Url")][FromBody] ServiceRequest request) { // Boilerpipe.net code below string html = ""; string headline = ""; string source = ""; string text = ""; string img = ""; try { if (Uri.IsWellFormedUriString(request.Url, UriKind.Absolute)) { using (WebClient webClient = new WebClient()) { html = webClient.DownloadString(request.Url); } text = RemoveSpecialCharacters(CommonExtractors.LargestContentExtractor.GetText(html)); headline = RemoveSpecialCharacters(Regex.Match(html, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value); var domainParser = new DomainParser(new WebTldRuleProvider()); var domainName = domainParser.Get(request.Url); source = domainName.RegistrableDomain; img = "http://" + FetchLinksFromSource(html)[0].AbsolutePath; } else { return(BadRequest(new ServiceResponse { Success = false, Error = "Bad URL." })); } // Google Cloud API code below var credential = GoogleCredential.FromJson(ServiceAccountJSON) .CreateScoped(LanguageServiceClient.DefaultScopes); var channel = new Grpc.Core.Channel( AutoMlClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials()); PredictionServiceClient client = PredictionServiceClient.Create(channel); string modelFullId = ModelName.Format("partem-1579924523879", "us-central1", "TCN7494917767758348288"); PredictRequest predictRequest = new PredictRequest { Name = modelFullId, Payload = new ExamplePayload { TextSnippet = new TextSnippet { Content = text, MimeType = "text/plain" } } }; PredictResponse response = client.Predict(predictRequest); var leftPerc = response.Payload.First(x => x.DisplayName == "left").Classification.Score; var rightPerc = response.Payload.First(x => x.DisplayName == "right").Classification.Score; return(Ok(new ServiceResponse { LeftPercentage = leftPerc, RightPercentage = rightPerc, CenterPercentage = 1 - Math.Abs(leftPerc - rightPerc), Headline = headline, Source = source, Image = img, Success = true, })); } catch (Exception ex) { return(BadRequest(new ServiceResponse { Success = false, Error = "An error has occured. Please try again later." })); } }