Пример #1
0
def make_stub(client, stub_factory, host, port):
    """Makes a stub for an RPC service.

    Uses / depends on the beta implementation of gRPC.

    :type client: :class:`.client.Client`
    :param client: The client that owns the cluster. Provides authorization and
                   user agent.

    :type stub_factory: callable
    :param stub_factory: A factory which will create a gRPC stub for
                         a given service.

    :type host: str
    :param host: The host for the service.

    :type port: int
    :param port: The port for the service.

    :rtype: :class:`grpc.beta._stub._AutoIntermediary`
    :returns: The stub object used to make gRPC requests to a given API.
    """
    root_certificates = get_certs()
    client_credentials = implementations.ssl_client_credentials(
        root_certificates, private_key=None, certificate_chain=None)
    channel = implementations.secure_channel(
        host, port, client_credentials)
    custom_metadata_transformer = MetadataTransformer(client)
    return stub_factory(channel,
                        metadata_transformer=custom_metadata_transformer)
Пример #2
0
def _make_stub(client, stub_factory, host, port):
    """Makes a stub for an RPC service.

    Uses / depends on the beta implementation of gRPC.

    :type client: :class:`.client.Client`
    :param client: The client that owns the instance.
                   Provides authorization and user agent.

    :type stub_factory: callable
    :param stub_factory: A factory which will create a gRPC stub for
                         a given service.

    :type host: str
    :param host: The host for the service.

    :type port: int
    :param port: The port for the service.

    :rtype: :class:`grpc.beta._stub._AutoIntermediary`
    :returns: The stub object used to make gRPC requests to a given API.
    """
    # Leaving the first argument to ssl_channel_credentials() as None
    # loads root certificates from `grpc/_adapter/credentials/roots.pem`.
    transport_creds = implementations.ssl_channel_credentials(None, None, None)
    custom_metadata_plugin = _MetadataPlugin(client)
    auth_creds = implementations.metadata_call_credentials(
        custom_metadata_plugin, name='google_creds')
    channel_creds = implementations.composite_channel_credentials(
        transport_creds, auth_creds)
    channel = implementations.secure_channel(host, port, channel_creds)
    return stub_factory(channel)
def do_inference():
    # get deployed model details
    model_name = request.path[1:]
    query_string = {"modelName": model_name}
    headers = {
        'Authorization': get_access_token(),
        'Cache-Control': "no-cache"
    }
    res = requests.request("GET",
                           deployment_url,
                           headers=headers,
                           params=query_string)
    model_info = json.loads(res.text)
    # check model is available
    if int(model_info["count"]) < 1:
        return Response('404 Not Found: Model ' + model_name +
                        ' is unavailable.',
                        status=404)
    else:
        # get details for the latest model version
        latest_version = [0, 0]
        for index, model in enumerate(model_info["modelServers"]):
            if int(model["specs"]["models"][0]
                   ["modelVersion"]) > latest_version[0]:
                latest_version = [
                    int(model["specs"]["models"][0]["modelVersion"]), index
                ]
        model_host = model_info["modelServers"][
            latest_version[1]]["endpoints"][0]
        credentials = implementations.ssl_channel_credentials(
            root_certificates=str(model_host["caCrt"]))
        channel = implementations.secure_channel(str(model_host["host"]),
                                                 int(model_host["port"]),
                                                 credentials)
        stub = prediction_service_pb2.beta_create_PredictionService_stub(
            channel, metadata_transformer=metadata_transformer)
        # check file was uploaded
        if not 'file' in globals.request.files:
            return Response('404 Not Found: File not provided.', status=404)
        else:
            # perform inference on the file
            data = skimage.io.imread(globals.request.files['file'])
            req = predict_pb2.PredictRequest()
            req.model_spec.name = model_name
            req.model_spec.signature_name = 'predict_images'
            req.inputs["images"].CopyFrom(
                tf.contrib.util.make_tensor_proto(data,
                                                  shape=[1, data.size],
                                                  dtype="float32"))
            res = stub.Predict(req, 150)
            # convert scores to JSON
            res = str(res).split('}')[3].split('\n')
            res.pop(11)
            res.pop(0)
            scores = {}
            for i, estimate in enumerate(res):
                scores[str(i)] = float(estimate[14:])
    return Response(json.dumps(scores),
                    status=200,
                    mimetype='application/json')
Пример #4
0
def make_stub(client, stub_factory, host, port):
    """Makes a stub for an RPC service.

    Uses / depends on the beta implementation of gRPC.

    :type client: :class:`.client.Client`
    :param client: The client that owns the cluster. Provides authorization and
                   user agent.

    :type stub_factory: callable
    :param stub_factory: A factory which will create a gRPC stub for
                         a given service.

    :type host: str
    :param host: The host for the service.

    :type port: int
    :param port: The port for the service.

    :rtype: :class:`grpc.beta._stub._AutoIntermediary`
    :returns: The stub object used to make gRPC requests to a given API.
    """
    root_certificates = get_certs()
    client_credentials = implementations.ssl_client_credentials(
        root_certificates, private_key=None, certificate_chain=None)
    channel = implementations.secure_channel(host, port, client_credentials)
    custom_metadata_transformer = MetadataTransformer(client)
    return stub_factory(channel,
                        metadata_transformer=custom_metadata_transformer)
Пример #5
0
def _make_stub(client, stub_factory, host, port):
    """Makes a stub for an RPC service.

    Uses / depends on the beta implementation of gRPC.

    :type client: :class:`.client.Client`
    :param client: The client that owns the cluster. Provides authorization and
                   user agent.

    :type stub_factory: callable
    :param stub_factory: A factory which will create a gRPC stub for
                         a given service.

    :type host: str
    :param host: The host for the service.

    :type port: int
    :param port: The port for the service.

    :rtype: :class:`grpc.beta._stub._AutoIntermediary`
    :returns: The stub object used to make gRPC requests to a given API.
    """
    # Leaving the first argument to ssl_channel_credentials() as None
    # loads root certificates from `grpc/_adapter/credentials/roots.pem`.
    transport_creds = implementations.ssl_channel_credentials(None, None, None)
    custom_metadata_plugin = _MetadataPlugin(client)
    auth_creds = implementations.metadata_call_credentials(
        custom_metadata_plugin, name='google_creds')
    channel_creds = implementations.composite_channel_credentials(
        transport_creds, auth_creds)
    channel = implementations.secure_channel(host, port, channel_creds)
    return stub_factory(channel)
def create_trace_stub(host=TRACE_ENDPOINT, port=SSL_PORT):
    """Creates a secure channel."""
    ssl_creds = implementations.ssl_channel_credentials(None, None, None)
    call_creds = implementations.metadata_call_credentials(make_auth_func())
    channel_creds = implementations.composite_channel_credentials(ssl_creds, call_creds)
    channel = implementations.secure_channel(host, port, channel_creds)
    return trace_pb2.beta_create_TraceService_stub(channel)
Пример #7
0
def make_channel(host, port):
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)
    creds = get_credentials().create_scoped(args.speech_scope)
    auth_header = ("authorization", "Bearer " + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(lambda _, func: func([auth_header], None), name="google_creds")
    composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin)
    return implementations.secure_channel(host, port, composite_channel)
Пример #8
0
def make_stub(credentials, user_agent, stub_factory, host, port):
    """Makes a stub for an RPC service.

    Uses / depends on the beta implementation of gRPC.

    :type credentials: :class:`oauth2client.client.OAuth2Credentials`
    :param credentials: The OAuth2 Credentials to use for creating
                        access tokens.

    :type user_agent: str
    :param user_agent: (Optional) The user agent to be used with API requests.

    :type stub_factory: callable
    :param stub_factory: A factory which will create a gRPC stub for
                         a given service.

    :type host: str
    :param host: The host for the service.

    :type port: int
    :param port: The port for the service.

    :rtype: :class:`grpc.beta._stub._AutoIntermediary`
    :returns: The stub object used to make gRPC requests to a given API.
    """
    # Leaving the first argument to ssl_channel_credentials() as None
    # loads root certificates from `grpc/_adapter/credentials/roots.pem`.
    transport_creds = implementations.ssl_channel_credentials(None, None, None)
    custom_metadata_plugin = MetadataPlugin(credentials, user_agent)
    auth_creds = implementations.metadata_call_credentials(
        custom_metadata_plugin, name='google_creds')
    channel_creds = implementations.composite_channel_credentials(
        transport_creds, auth_creds)
    channel = implementations.secure_channel(host, port, channel_creds)
    return stub_factory(channel)
Пример #9
0
def create_stub(generated_create_stub, service_path, port, ssl_creds=None,
                channel=None, metadata_transformer=None, scopes=None):
    """Creates a gRPC client stub.

    Args:
        generated_create_stub: The generated gRPC method to create a stub.
        service_path: The domain name of the API remote host.
        port: The port on which to connect to the remote host.
        ssl_creds: A ClientCredentials object for use with an SSL-enabled
            Channel. If none, credentials are pulled from a default location.
        channel: A Channel object through which to make calls. If none, a secure
            channel is constructed.
        metadata_transformer: A function that transforms the metadata for
            requests, e.g., to give OAuth credentials.
        scopes: The OAuth scopes for this service. This parameter is ignored if
            a custom metadata_transformer is supplied.

    Returns:
        A gRPC client stub.
    """
    if channel is None:
        if ssl_creds is None:
            ssl_creds = implementations.ssl_channel_credentials(
                None, None, None)
        if metadata_transformer is None:
            if scopes is None:
                scopes = []
            metadata_transformer = auth.make_auth_func(scopes)

        channel_creds = _make_channel_creds(metadata_transformer, ssl_creds)
        channel = implementations.secure_channel(
            service_path, port, channel_creds)

    return generated_create_stub(channel)
Пример #10
0
def run():

    # TSL连接方式 >>>
    with open(
            'G:\DotNet\SevenTiny.Cloud.FaaS\Code\Python\SevenTiny.Cloud.FaaS.GRpc\ca\client.pem',
            'rb') as f:
        pem = f.read()

    creds = implementations.ssl_channel_credentials(pem, None, None)

    channel = implementations.secure_channel('localhost', 5001, creds)
    # TSL连接方式 <<<

    # 连接 rpc 服务器
    # channel = grpc.insecure_channel('localhost:5001')
    # 调用 rpc 服务
    stub = seventiny_cloud_faas_pb2_grpc.DynamicScriptExecutorStub(channel)
    response = stub.CheckScript(
        seventiny_cloud_faas_proto_pb2.DynamicScript(TenantId=100000,
                                                     Script='123123123'))
    print("CheckScript client received: " + response.Message)

    response = stub.Execute(
        seventiny_cloud_faas_proto_pb2.DynamicScript(TenantId=100000,
                                                     Script='123123123'))
    print("Execute client received: " + response.Message)
Пример #11
0
def run():
  creds = implementations.ssl_channel_credentials(open('../../../cert.pem'
                                                       ).read(), None, None)
  channel = implementations.secure_channel('localhost', 8000, creds)
  stub = search_pb2.beta_create_Google_stub(channel)
  search = raw_input('Google: ')
  query = search_pb2.Request(query=search)
  results = stub.Search.future(query, _TIMEOUT_)
  for result in results:
    print result.title + '\n' + result.url + '\n' + result.content
Пример #12
0
 def channel(self):
     ssl_channel = implementations.ssl_channel_credentials(None, None, None)
     creds = get_credentials().create_scoped([SPEECH_SCOPE])
     auth_header = ('Authorization',
                    'Bearer ' + creds.get_access_token().access_token)
     auth_plugin = implementations.metadata_call_credentials(
         lambda _, cb: cb([auth_header], None), name='google_creds')
     composite_channel = implementations.composite_channel_credentials(
         ssl_channel, auth_plugin)
     return implementations.secure_channel(SPEECH_API_HOST, SPEECH_API_PORT,
                                           composite_channel)
Пример #13
0
def run(host, port):
    _TIMEOUT_SECONDS = 1

    cc = ssl_credentials = grpc.ssl_channel_credentials(
        open('CA_crt.pem').read())
    channel = implementations.secure_channel(host, port, cc)
    #channel = implementations.insecure_channel(host,port)
    stub = echo_pb2.beta_create_EchoServer_stub(channel)
    response = stub.SayHello(
        echo_pb2.EchoRequest(firstname='john', lastname='doe'),
        _TIMEOUT_SECONDS)
    print response
Пример #14
0
  def dial(self):
    if self.stub:
      self.stub.close()

    p = urlparse('http://' + self.addr)

    if self.root_certificates or self.private_key or self.certificate_chain:
      creds = implementations.ssl_channel_credentials(
          self.root_certificates, self.private_key, self.certificate_chain)
      channel = implementations.secure_channel(p.hostname, p.port, creds)
    else:
      channel = implementations.insecure_channel(p.hostname, p.port)
    self.stub = vtgateservice_pb2.beta_create_Vitess_stub(channel)
Пример #15
0
  def dial(self):
    if self.stub:
      self.stub.close()

    p = urlparse('http://' + self.addr)

    if self.root_certificates or self.private_key or self.certificate_chain:
      creds = implementations.ssl_channel_credentials(
          self.root_certificates, self.private_key, self.certificate_chain)
      channel = implementations.secure_channel(p.hostname, p.port, creds)
    else:
      channel = implementations.insecure_channel(p.hostname, p.port)
    self.stub = vtgateservice_pb2.beta_create_Vitess_stub(channel)
def make_channel(host, port):
    """Creates an SSL channel with auth credentials from the environment."""
    # In order to make an https call, use an ssl channel with defaults
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)

    # Grab application default credentials from the environment
    creds = get_credentials().create_scoped([SPEECH_SCOPE])
    # Add a plugin to inject the creds into the header
    auth_header = ("Authorization", "Bearer " + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(lambda _, cb: cb([auth_header], None), name="google_creds")

    # compose the two together for both ssl and google auth
    composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin)

    return implementations.secure_channel(host, port, composite_channel)
Пример #17
0
def EstablishChannel(address, port, client_id, user, password):
    # Open a grpc channel to the device
    creds = implementations.ssl_channel_credentials(open('/tmp/host.pem').read(), None, None)
    channel = implementations.secure_channel(address, port, creds)

    # Create stub for authentication
    login_stub = authentication_service_pb2.beta_create_Login_stub(channel)

    # Fill the login request message structure
    login_request = authentication_service_pb2.LoginRequest(user_name=user, password=password, client_id=client_id)

    # Invoke the login check API
    login_response = login_stub.LoginCheck(login_request, _TIMEOUT_SECONDS)
    print login_response

    return channel
Пример #18
0
    def __init__(self,
                 host,
                 port,
                 creds=None,
                 channel_type="insecure_channel"):

        if channel_type == "insecure_channel":
            channel = implementations.insecure_channel(host, int(port))
        elif channel_type == "secure_channel":
            channel = implementations.secure_channel(host, int(port), cred)
        else:
            print 'Invalid channel_type'
            exit(1)

        self._stub = prediction_service_pb2.beta_create_PredictionService_stub(
            channel)
Пример #19
0
    def __init__(self, host, port, channel_credentials=None):
        """
        :type host: str
        :param host: The name of the remote host to which to connect.

        :type port: int
        :param port: The port of the remote host to which to connect.
        """
        if channel_credentials is None:
            # Create an `insecure_channel`.
            channel = implementations.insecure_channel(host, port)
        else:
            # Create a `secure_channel`.
            channel = implementations.secure_channel(host, port,
                                                     channel_credentials)
        self._channel = channel
        self._stubs = []
Пример #20
0
    def __init__(self, host, port, channel_credentials=None):
        """
        :type host: str
        :param host: The name of the remote host to which to connect.

        :type port: int
        :param port: The port of the remote host to which to connect.
        """
        if channel_credentials is None:
            # Create an `insecure_channel`.
            channel = implementations.insecure_channel(host, port)
        else:
            # Create a `secure_channel`.
            channel = implementations.secure_channel(host, port,
                                                     channel_credentials)
        self._channel = channel
        self._stubs = []
def run():
    # 连接 rpc 服务器
    # TSL连接方式 >>>
    with open('G:\\DotNet\\SevenTiny.Cloud.FaaS\\Code\\Python\\SevenTiny.Cloud.FaaS.GRpc\\ca\\client.pem', 'rb') as f:
        pem = f.read()

    creds = implementations.ssl_channel_credentials(
        pem, None, None)

    channel = implementations.secure_channel('localhost', 5001, creds)
    # TSL连接方式 <<<
    # channel = grpc.insecure_channel('localhost:39901')
    # 调用 rpc 服务
    stub = helloworld_pb2_grpc.GreeterStub(channel)
    response = stub.SayHello(helloworld_pb2.HelloRequest(name='czl'))
    print("Greeter client received: " + response.message)
    response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='daydaygo'))
    print("Greeter client received: " + response.message)
Пример #22
0
def EstablishChannel(address, port, client_id, user, password):
    # Open a grpc channel to the device
    creds = implementations.ssl_channel_credentials(
        open('/tmp/host.pem').read(), None, None)
    channel = implementations.secure_channel(address, port, creds)

    # Create stub for authentication
    login_stub = authentication_service_pb2.beta_create_Login_stub(channel)

    # Fill the login request message structure
    login_request = authentication_service_pb2.LoginRequest(
        user_name=user, password=password, client_id=client_id)

    # Invoke the login check API
    login_response = login_stub.LoginCheck(login_request, _TIMEOUT_SECONDS)
    print login_response

    return channel
Пример #23
0
def make_channel(host, port):
    """Creates an SSL channel with auth credentials from the environment."""
    # In order to make an https call, use an ssl channel with defaults
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)

    # Grab application default credentials from the environment
    creds = get_credentials().create_scoped([SPEECH_SCOPE])
    # Add a plugin to inject the creds into the header
    auth_header = ('Authorization',
                   'Bearer ' + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(
        lambda _, cb: cb([auth_header], None), name='google_creds')

    # compose the two together for both ssl and google auth
    composite_channel = implementations.composite_channel_credentials(
        ssl_channel, auth_plugin)

    return implementations.secure_channel(host, port, composite_channel)
Пример #24
0
def main():
    deployment_url = DEPLOYMENT_URL
    deployment_url += "/api/v2/modelServers"

    token = globals.request.headers.get("Authorization")
    headers = {'Authorization': token, 'Cache-Control': "no-cache"}
    querystring = {"modelName": MODEL_NAME, "modelVersion": MODEL_VERSION}
    response = requests.request("GET",
                                deployment_url,
                                headers=headers,
                                params=querystring)
    model_info = json.loads(response.text)

    model_info = model_info["modelServers"][0]
    endpoint = model_info["endpoints"][0]
    credentials = implementations.ssl_channel_credentials(
        root_certificates=str(endpoint["caCrt"]))
    channel = implementations.secure_channel(str(endpoint["host"]),
                                             int(endpoint["port"]),
                                             credentials)

    stub = prediction_service_pb2.beta_create_PredictionService_stub(
        channel, metadata_transformer=metadata_transformer)

    data_str = globals.request.get_data().split(",")
    data = []
    for idx in range(len(data_str)):
        data.append(float(data_str[idx]))

    request = predict_pb2.PredictRequest()
    request.model_spec.name = MODEL_NAME
    request.model_spec.signature_name = 'serving_default'
    tfutil = tf.contrib.util
    t_proto = tfutil.make_tensor_proto(data,
                                       shape=[1, len(data)],
                                       dtype="float")

    request.inputs["input_image"].CopyFrom(t_proto)

    predict = stub.Predict(request, 1500)
    print predict
    res = predict.outputs['dense_2/Sigmoid:0'].float_val[0]
    print res
    return str(res)
Пример #25
0
    def google_grpc_channel(self, host, port):
        """Creates an SSL channel with auth credentials from the environment."""
        # In order to make an https call, use an ssl channel with defaults
        ssl_channel = implementations.ssl_channel_credentials(None, None, None)

        # Grab application default credentials from the environment
        creds = GoogleCredentials.from_stream(os.path.join(app_dir,"audio_creds.json")).create_scoped([SPEECH_SCOPE])

        # Add a plugin to inject the creds into the header
        auth_header = (
            'Authorization',
            'Bearer ' + creds.get_access_token().access_token)
        auth_plugin = implementations.metadata_call_credentials(
            lambda _, cb: cb([auth_header], None),
            name='google_creds')

        # compose the two together for both ssl and google auth
        composite_channel = implementations.composite_channel_credentials(
            ssl_channel, auth_plugin)

        return implementations.secure_channel(host, port, composite_channel)
Пример #26
0
def main():
    credentials = implementations.ssl_channel_credentials(
        root_certificates=ROOT_CERT)
    channel = implementations.secure_channel(MODEL_SERVER_HOST,
                                             MODEL_SERVER_PORT, credentials)
    stub = prediction_service_pb2.beta_create_PredictionService_stub(
        channel, metadata_transformer=metadata_transformer)

    # process the first file only
    uploaded_files = globals.request.files.getlist('file')
    data = uploaded_files[0].read()

    # See prediction_service.proto for gRPC request/response details.
    # change input type and data shapes according to your model
    request = predict_pb2.PredictRequest()
    request.model_spec.name = MODEL_NAME
    request.model_spec.signature_name = 'predict_images'
    request.inputs['images'].CopyFrom(
        tf.contrib.util.make_tensor_proto(data, shape=[1]))

    return str(stub.Predict(request, 120))
Пример #27
0
    def init(self):
        if not self._initialized:
            if self._config['root_certificates'] != '':
                self._creds = implementations.ssl_channel_credentials(
                    utils.resource_string(self._config['root_certificates']),
                    None, None)

            self._channels = {}
            self._stubs = {}
            for label, server in self._servers.items():
                if server['ssl']:
                    chan = implementations.secure_channel(
                        server['host'], server['port'], self._creds)
                else:
                    chan = implementations.insecure_channel(
                        server['host'], server['port'])
                st = message_pb2.beta_create_Communication_stub(chan)

                self._channels[label] = chan
                self._stubs[label] = st
            self._initialized = True
Пример #28
0
def create_stub(generated_create_stub,
                service_path,
                port,
                ssl_creds=None,
                channel=None,
                metadata_transformer=None,
                scopes=None):
    """Creates a gRPC client stub.

    Args:
        generated_create_stub: The generated gRPC method to create a stub.
        service_path: The domain name of the API remote host.
        port: The port on which to connect to the remote host.
        ssl_creds: A ClientCredentials object for use with an SSL-enabled
            Channel. If none, credentials are pulled from a default location.
        channel: A Channel object through which to make calls. If none, a secure
            channel is constructed.
        metadata_transformer: A function that transforms the metadata for
            requests, e.g., to give OAuth credentials.
        scopes: The OAuth scopes for this service. This parameter is ignored if
            a custom metadata_transformer is supplied.

    Returns:
        A gRPC client stub.
    """
    if channel is None:
        if ssl_creds is None:
            ssl_creds = implementations.ssl_channel_credentials(
                None, None, None)
        if metadata_transformer is None:
            if scopes is None:
                scopes = []
            metadata_transformer = auth.make_auth_func(scopes)

        channel_creds = _make_channel_creds(metadata_transformer, ssl_creds)
        channel = implementations.secure_channel(service_path, port,
                                                 channel_creds)

    return generated_create_stub(channel)
Пример #29
0
 def __init__(self, host, port, channel_credentials=None):
     if channel_credentials is None:
         # Instantiate insecure channel object.
         channel = implementations.insecure_channel(host, port)
     else:
         # Instantiate secure channel object.
         channel = implementations.secure_channel(host, port,
                                                  channel_credentials)
     self._stubs = (
         # 0
         sl_route_ipv4_pb2.beta_create_SLRoutev4Oper_stub(channel),
         # 1
         sl_route_ipv6_pb2.beta_create_SLRoutev6Oper_stub(channel),
         # 2
         sl_global_pb2.beta_create_SLGlobal_stub(channel),
         # 3
         sl_mpls_pb2.beta_create_SLMplsOper_stub(channel),
         # 4
         sl_bfd_ipv4_pb2.beta_create_SLBfdv4Oper_stub(channel),
         # 5
         sl_bfd_ipv6_pb2.beta_create_SLBfdv6Oper_stub(channel),
         # 6
         sl_interface_pb2.beta_create_SLInterfaceOper_stub(channel),
     )
Пример #30
0
from grpc.beta import implementations
from gen import query_pb2
from os import path

_TIMEOUT_SECONDS = 30

DIR=path.dirname(path.dirname(path.abspath(__file__)))


creds = implementations.ssl_channel_credentials(
    open(path.join(DIR, "ssl/domain.crt")).read(),
    None,
    None
)
channel = implementations.secure_channel('localhost', 50051, creds)
stub = query_pb2.beta_create_SearchService_stub(channel)

search_request = query_pb2.SearchRequest()
search_request.query = "search"
search_request.page_number = 1
search_request.result_per_page = 30


print(stub.Search(search_request, _TIMEOUT_SECONDS))

hello_request = query_pb2.HelloRequest()
hello_request.name = "python"

print(stub.SayHello(hello_request, _TIMEOUT_SECONDS))
Пример #31
0
def main():
    deployment_url = "https://mlftrial-deployment-api.cfapps.eu10.hana.ondemand.com" + "/api/v2/modelServers"
    querystring = {"modelName": MODEL_NAME}
    headers = {
        'Authorization': get_access_token(),
        'Cache-Control': "no-cache"
    }
    response = requests.request("GET",
                                deployment_url,
                                headers=headers,
                                params=querystring)
    #print(response.text)
    model_info = json.loads(response.text)
    latest_version = [0, 0]
    for index, model in enumerate(model_info["modelServers"]):
        if int(model["specs"]["models"][0]
               ["modelVersion"]) > latest_version[0]:
            latest_version = [
                int(model["specs"]["models"][0]["modelVersion"]), index
            ]
    model_host = model_info["modelServers"][latest_version[1]]["endpoints"][0]
    credentials = implementations.ssl_channel_credentials(
        root_certificates=str(model_host["caCrt"]))
    channel = implementations.secure_channel(str(model_host["host"]),
                                             int(model_host["port"]),
                                             credentials)
    stub = prediction_service_pb2.beta_create_PredictionService_stub(
        channel)  #, metadata_transformer=metadata_transformer)
    #uploaded_files = globals.request.files.getlist('file')
    #data = skimage.io.imread(uploaded_files[0])

    feature_dict = {
        'DIFGRIRV':
        tf.train.Feature(int64_list=tf.train.Int64List(value=[-38100])),
        'NODLIR': tf.train.Feature(int64_list=tf.train.Int64List(value=[90])),
        'VSTATU':
        tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"1"])),
        'NODLGR': tf.train.Feature(int64_list=tf.train.Int64List(value=[0])),
        'DIFGRIRD':
        tf.train.Feature(int64_list=tf.train.Int64List(value=[-80])),
        'VPATD': tf.train.Feature(int64_list=tf.train.Int64List(value=[30])),
        'WERKS':
        tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"ML01"])),
        'EKORG': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"1"])),
        'TOTGRQTY': tf.train.Feature(int64_list=tf.train.Int64List(value=[0])),
        'SCENARIO':
        tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"3"])),
        'TOTIRQTY':
        tf.train.Feature(int64_list=tf.train.Int64List(value=[80])),
        'KTOKK': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"1"])),
        'EKGRP': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b"A"])),
    }
    example = tf.train.Example(features=tf.train.Features(
        feature=feature_dict))
    data = example.SerializeToString()
    # data = {"DIFGRIRV": [-38100],"NODLIR": [90],"VSTATU": ["1"],"NODLGR": [0],"DIFGRIRD": [-80],"VPATD": [30],
    #           "WERKS": ["ML01"],
    #           "EKORG": ["1"],"TOTGRQTY": [0],"SCENARIO": ["3"],"TOTIRQTY": [80],"KTOKK": ["1"],"EKGRP": ["A"]}

    req = predict_pb2.PredictRequest()
    req.model_spec.name = MODEL_NAME
    #req.model_spec.signature_name = 'predict_images'
    req.inputs["inputs"].CopyFrom(
        tf.contrib.util.make_tensor_proto(data, shape=[1, 1]))
    res = str(stub.Predict(req, 150))
    # res = str(stub.Predict(req, 150)).split('}')[3].split('\n')
    print(res)
    res.pop(11)
    res.pop(0)
    out_val = 0.0
    out = 0
    for i, estimate in enumerate(res):
        if float(estimate[14:]) > out_val:
            out_val = float(estimate[14:])
            out = i
    return "Result: " + str(out)
Пример #32
0
def secure_channel():
  with open(FLAGS.certificate) as f:
    trusted_certs = f.read()
  credentials = implementations.ssl_channel_credentials(
          trusted_certs, None, None)
  return implementations.secure_channel(FLAGS.address, FLAGS.port, credentials)
def create_pubsub_stub(host=PUBSUB_ENDPOINT, port=SSL_PORT):
    """Creates a secure pubsub channel."""
    ssl_creds = implementations.ssl_channel_credentials(None, None, None)
    channel_creds = make_channel_creds(ssl_creds, auth_func)
    channel = implementations.secure_channel(host, port, channel_creds)
    return pubsub_pb2.beta_create_Publisher_stub(channel)
Пример #34
0
def create_sub_stub(host=PUBSUB_ENDPOINT, port=SSL_PORT):
    """Creates a secure pubsub channel."""
    ssl_creds = implementations.ssl_channel_credentials(None, None, None)
    channel_creds = make_channel_creds(ssl_creds, auth_func)
    channel = implementations.secure_channel(host, port, channel_creds)
    return pubsub_pb2.beta_create_Subscriber_stub(channel)