lostluck commented on code in PR #24497: URL: https://github.com/apache/beam/pull/24497#discussion_r1047860080
########## sdks/go/pkg/beam/transforms/xlang/inference/inference.go: ########## @@ -0,0 +1,142 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package inference has the cross language implementation of RunInference API implemented in Python SDK. +// An exapnsion service for python external transforms can be started by running +// +// $ python -m apache_beam.runners.portability.expansion_service_main -p $PORT_FOR_EXPANSION_SERVICE +package inference + +import ( + "reflect" + + "github.com/apache/beam/sdks/v2/go/pkg/beam" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/xlangx" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex" + "github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/xlang" + "github.com/apache/beam/sdks/v2/go/pkg/beam/transforms/xlang/python" +) + +func init() { + beam.RegisterType(reflect.TypeOf((*runInferenceConfig)(nil)).Elem()) + beam.RegisterType(reflect.TypeOf((*argsStruct)(nil)).Elem()) + beam.RegisterType(reflect.TypeOf((*sklearn)(nil)).Elem()) + beam.RegisterType(reflect.TypeOf((*PredictionResult)(nil)).Elem()) +} + +var outputT = reflect.TypeOf((*PredictionResult)(nil)).Elem() + +// PredictionResult represents the result of a prediction obtained from Python's RunInference API. +type PredictionResult struct { + Example []int64 `beam:"example"` + Inference int32 `beam:"inference"` +} + +type runInferenceConfig struct { + args argsStruct + expansionAddr string +} + +type runInferenceOption func(*runInferenceConfig) + +// WithArgs set arguments for the RunInference transform parameters. +func WithArgs(args []string) runInferenceOption { + return func(c *runInferenceConfig) { + c.args.args = append(c.args.args, args...) + } +} + +// WithExpansionAddr provides URL for Python expansion service. +func WithExpansionAddr(expansionAddr string) runInferenceOption { + return func(c *runInferenceConfig) { + c.expansionAddr = expansionAddr + } +} + +type argsStruct struct { + args []string +} + +// sklearn configures the parameters for the sklearn inference transform. +type sklearn struct { + // ModelHandlerProvider defines the model handler to be used. + ModelHandlerProvider python.CallableSource `beam:"model_handler_provider"` + // ModelURI indicates the model path to be used for Sklearn Model Handler. + ModelURI string `beam:"model_uri"` +} + +// sklearnConfig could be used to configre other optional parameters in future if necessary. Review Comment: ```suggestion // sklearnConfig could be used to configure other optional parameters in future if necessary. ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
