clarifai 9.6.2__py3-none-any.whl → 9.6.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -203,6 +203,13 @@ code
203
203
  border-color: #d0d5dd;
204
204
  box-shadow: 0 1px 2px rgba(16,24,40,.05);
205
205
  }
206
+
207
+ .stTextInput > div > div > input {
208
+ background-color: white;
209
+ }
210
+
211
+
212
+
206
213
  div[data-testid="stFileUploader"]>section {
207
214
  border: 1px dashed #d0d5dd;
208
215
  border-radius: 8px;
@@ -0,0 +1,140 @@
1
+ # Copyright 2023 Clarifai, Inc.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ """Interface to Clarifai Runners API."""
14
+
15
+ from typing import Type
16
+
17
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2
18
+ from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
19
+ from google.protobuf import json_format
20
+
21
+ from clarifai.auth.helper import ClarifaiAuthHelper
22
+ from clarifai.client import create_stub
23
+
24
+
25
+ class BaseRunner:
26
+ """
27
+ Base class for remote inference runners. This should be subclassed with the run_input method
28
+ implemented to process each input in the request.
29
+
30
+ Then on the subclass call start() to start the run loop.
31
+ """
32
+
33
+ def __init__(self, auth: Type[ClarifaiAuthHelper], runner_id: str) -> None:
34
+ """
35
+ Args:
36
+ auth: ClarifaiAuthHelper - the auth object to use
37
+ runner_id: str - the id of the runner to use. Create the runner in the Clarifai API first
38
+
39
+ """
40
+ self.auth = auth
41
+ self.stub = create_stub(self.auth)
42
+ self.runner_id = runner_id
43
+
44
+ # Check that the runner exists.
45
+ response = self.stub.GetRunner(
46
+ service_pb2.GetRunnerRequest(
47
+ user_app_id=self.auth.get_user_app_id_proto(), runner_id=self.runner_id))
48
+ if work_response.status.code != status_code_pb2.SUCCESS:
49
+ raise Exception(
50
+ f"Error getting runner, are you use this is a valid runner id {runner_id} at the user_id/app_id {self.auth.get_user_app_id_proto().user_id}/{self.auth.get_user_app_id_proto().app_id}. Error: {response.status.description}"
51
+ )
52
+
53
+ def start(self):
54
+ """
55
+ Start the run loop. This will ask the Clarifai API for work, and when it gets work, it will run
56
+ the model on the inputs and post the results back to the Clarifai API. It will then ask for more
57
+ work again.
58
+ """
59
+ self._long_poll_loop()
60
+
61
+ def _run(self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
62
+ """
63
+ Run the model on the given request. You shouldn't need to override this method, see run_input
64
+ for the implementation to process each input in the request.
65
+
66
+ Args:
67
+ request: service_pb2.PostModelOutputsRequest - the request to run the model on
68
+
69
+ Returns:
70
+ service_pb2.MultiOutputResponse - the response from the model's run_input implementation.
71
+ """
72
+ outputs = []
73
+ # TODO: parallelize this
74
+ for inp in request.inputs:
75
+ # TODO: handle errors
76
+ outputs.append(self.run_input(inp))
77
+
78
+ return service_pb2.MultiOutputResponse(
79
+ status=status_pb2.Status(
80
+ code=status_code_pb2.SUCCESS,
81
+ description="Success",
82
+ ),
83
+ outputs=outputs,
84
+ )
85
+
86
+ def run_input(self, input: resources_pb2.Input) -> resources_pb2.Output:
87
+ """
88
+ Run the model on the given input in the request. This is the method you should override to
89
+ process each input in the request.
90
+
91
+ Args:
92
+ input: resources_pb2.Input - the input to run the model on
93
+
94
+ Returns:
95
+ resources_pb2.Output - the response from the model's run_input implementation.
96
+ """
97
+ raise NotImplementedError("run_input() not implemented")
98
+
99
+ def _long_poll_loop(self):
100
+ """
101
+ This method will long poll for work, and when it gets work, it will run the model on the inputs
102
+ and post the results back to the Clarifai API. It will then long poll again for more work.
103
+ """
104
+ c = 0
105
+ # TODO: handle more errors within this loop so it never stops.
106
+ # TODO: perhaps have multiple processes running this loop to handle more work.
107
+ while True:
108
+ # Long poll waiting for work.
109
+ print("Loop iteration: {}".format(c))
110
+ work_response = self.stub.ListRunnerItems(
111
+ service_pb2.ListRunnerItemsRequest(
112
+ user_app_id=self.auth.get_user_app_id_proto(), runner_id=self.runner_id))
113
+ if work_response.status.code == status_code_pb2.RUNNER_NEEDS_RETRY:
114
+ c += 1
115
+ continue # immediate restart the long poll
116
+ if work_response.status.code != status_code_pb2.SUCCESS:
117
+ raise Exception("Error getting work: {}".format(work_response.status.description))
118
+ if len(work_response.items) == 0:
119
+ print("No work to do. Waiting...")
120
+ continue
121
+
122
+ # We have work to do. Run the model on the inputs.
123
+ for item in work_response.items:
124
+ if not item.HasField('post_model_outputs_request'):
125
+ raise Exception("Unexpected work item type: {}".format(item))
126
+ print(
127
+ f"Working on item: {item.id} with inputs {len(item.post_model_outputs_request.inputs)}"
128
+ )
129
+ result = self._run(item.post_model_outputs_request)
130
+
131
+ result_response = self.stub.PostRunnerItemOutputs(
132
+ service_pb2.PostRunnerItemOutputsRequest(
133
+ user_app_id=self.auth.get_user_app_id_proto(),
134
+ item_id=item.id,
135
+ runner_id=self.runner_id,
136
+ runner_item_outputs=[service_pb2.RunnerItemOutput(multi_output_response=result)]))
137
+ if result_response.status.code != status_code_pb2.SUCCESS:
138
+ raise Exception(
139
+ json_format.MessageToJson(result_response, preserving_proto_field_name=True))
140
+ # raise Exception("Error posting result: {}".format(result_response.status.description))
@@ -0,0 +1,36 @@
1
+ from clarifai_grpc.grpc.api import resources_pb2
2
+
3
+ from clarifai.auth.helper import ClarifaiAuthHelper
4
+ from clarifai.runners.base import BaseRunner
5
+
6
+
7
+ class MyRunner(BaseRunner):
8
+ """ A custom runner that adds "Hello World" to the end of the text and replaces the domain of the
9
+ image URL as an example.
10
+ """
11
+
12
+ def run_input(self, input: resources_pb2.Input) -> resources_pb2.Output:
13
+ """ This is the method that will be called when the runner is run. It takes in an input and
14
+ returns an output.
15
+ """
16
+
17
+ output = resources_pb2.Output()
18
+
19
+ data = input.data
20
+
21
+ if data.text.raw != "":
22
+ output.data.text.raw = data.text.raw + "Hello World"
23
+ if data.image.url != "":
24
+ output.data.text.raw = data.image.url.replace("samples.clarifai.com", "newdomain.com")
25
+ return output
26
+
27
+
28
+ if __name__ == '__main__':
29
+ # Make sure you set these env vars before running the example.
30
+ # CLARIFAI_PAT
31
+ # CLARIFAI_USER_ID
32
+ # CLARIFAI_APP_ID
33
+ auth = ClarifaiAuthHelper.from_env()
34
+
35
+ # You need to first create a runner in the Clarifai API and then use the ID here.
36
+ MyRunner(auth, runner_id="laptop_runner").start()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: clarifai
3
- Version: 9.6.2
3
+ Version: 9.6.3
4
4
  Summary: Clarifai Python Utilities
5
5
  Home-page: https://github.com/Clarifai/clarifai-python-utils
6
6
  Author: Clarifai
@@ -81,9 +81,11 @@ clarifai/models/model_serving/models/pb_model.py,sha256=gLIP0dNXLD9lfBQmXEnGgjDo
81
81
  clarifai/modules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
82
82
  clarifai/modules/css.py,sha256=kadCEunmyh5h2yf0-4aysE3ZcZ6qaQcxuAgDXS96yF8,2020
83
83
  clarifai/modules/pages.py,sha256=iOoM3RNRMgXlV0qBqcdQofxoXo2RuRQh0h9c9BIS0-I,1383
84
- clarifai/modules/style.css,sha256=ANCqydlU3YA_JKyMCyE99-AiSDGmWq87UWNy53oPT8A,6003
84
+ clarifai/modules/style.css,sha256=j7FNPZVhLPj35vvBksAJ90RuX5sLuqzDR5iM2WIEhiA,6073
85
85
  clarifai/runners/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
86
86
  clarifai/runners/api.py,sha256=ikjLONGzmkekjXoEvqXD9pv1kfh1OtHAbAh-3j-sfMc,3578
87
+ clarifai/runners/base.py,sha256=s9xXgyyvKkVlA4ypGUVdMZFTziBC-RLeBHuoDLMdGD0,5713
88
+ clarifai/runners/example.py,sha256=s_t2-7aZqXtV9MX7cZNIXJm0gSKCGd1bejbI3ag7a8o,1129
87
89
  clarifai/urls/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
88
90
  clarifai/urls/helper.py,sha256=RHDsZhXE_Vp4QGWmSjatn3j0k6Zu2O1b2gmvI-F_rHY,4276
89
91
  clarifai_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -169,14 +171,16 @@ clarifai_utils/models/model_serving/models/pb_model.py,sha256=gLIP0dNXLD9lfBQmXE
169
171
  clarifai_utils/modules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
170
172
  clarifai_utils/modules/css.py,sha256=kadCEunmyh5h2yf0-4aysE3ZcZ6qaQcxuAgDXS96yF8,2020
171
173
  clarifai_utils/modules/pages.py,sha256=iOoM3RNRMgXlV0qBqcdQofxoXo2RuRQh0h9c9BIS0-I,1383
172
- clarifai_utils/modules/style.css,sha256=ANCqydlU3YA_JKyMCyE99-AiSDGmWq87UWNy53oPT8A,6003
174
+ clarifai_utils/modules/style.css,sha256=j7FNPZVhLPj35vvBksAJ90RuX5sLuqzDR5iM2WIEhiA,6073
173
175
  clarifai_utils/runners/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
174
176
  clarifai_utils/runners/api.py,sha256=ikjLONGzmkekjXoEvqXD9pv1kfh1OtHAbAh-3j-sfMc,3578
177
+ clarifai_utils/runners/base.py,sha256=s9xXgyyvKkVlA4ypGUVdMZFTziBC-RLeBHuoDLMdGD0,5713
178
+ clarifai_utils/runners/example.py,sha256=s_t2-7aZqXtV9MX7cZNIXJm0gSKCGd1bejbI3ag7a8o,1129
175
179
  clarifai_utils/urls/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
176
180
  clarifai_utils/urls/helper.py,sha256=RHDsZhXE_Vp4QGWmSjatn3j0k6Zu2O1b2gmvI-F_rHY,4276
177
- clarifai-9.6.2.dist-info/LICENSE,sha256=GuQZ4iPZUwh44duTbVr7ZzYp_SaJDLR9MvzU7YqlZXM,555
178
- clarifai-9.6.2.dist-info/METADATA,sha256=Gjat6WZxP8vWHO409bYRj5A6M4-v6dlAP_ftN7iDeyU,2985
179
- clarifai-9.6.2.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
180
- clarifai-9.6.2.dist-info/entry_points.txt,sha256=cna1vVlFIZZZlxHy1AbhooFGy-dw1W2xRfbOVRSWSKg,255
181
- clarifai-9.6.2.dist-info/top_level.txt,sha256=w3e8wx1HuK3_huGQosppv1_FSoNjBUd09KBKMK3wR-U,24
182
- clarifai-9.6.2.dist-info/RECORD,,
181
+ clarifai-9.6.3.dist-info/LICENSE,sha256=GuQZ4iPZUwh44duTbVr7ZzYp_SaJDLR9MvzU7YqlZXM,555
182
+ clarifai-9.6.3.dist-info/METADATA,sha256=OsPBVEV14RHZoCYHyfJFQEc7tTwqlb5SNAp09OE7upc,2985
183
+ clarifai-9.6.3.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
184
+ clarifai-9.6.3.dist-info/entry_points.txt,sha256=cna1vVlFIZZZlxHy1AbhooFGy-dw1W2xRfbOVRSWSKg,255
185
+ clarifai-9.6.3.dist-info/top_level.txt,sha256=w3e8wx1HuK3_huGQosppv1_FSoNjBUd09KBKMK3wR-U,24
186
+ clarifai-9.6.3.dist-info/RECORD,,
@@ -203,6 +203,13 @@ code
203
203
  border-color: #d0d5dd;
204
204
  box-shadow: 0 1px 2px rgba(16,24,40,.05);
205
205
  }
206
+
207
+ .stTextInput > div > div > input {
208
+ background-color: white;
209
+ }
210
+
211
+
212
+
206
213
  div[data-testid="stFileUploader"]>section {
207
214
  border: 1px dashed #d0d5dd;
208
215
  border-radius: 8px;
@@ -0,0 +1,140 @@
1
+ # Copyright 2023 Clarifai, Inc.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ """Interface to Clarifai Runners API."""
14
+
15
+ from typing import Type
16
+
17
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2
18
+ from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
19
+ from google.protobuf import json_format
20
+
21
+ from clarifai.auth.helper import ClarifaiAuthHelper
22
+ from clarifai.client import create_stub
23
+
24
+
25
+ class BaseRunner:
26
+ """
27
+ Base class for remote inference runners. This should be subclassed with the run_input method
28
+ implemented to process each input in the request.
29
+
30
+ Then on the subclass call start() to start the run loop.
31
+ """
32
+
33
+ def __init__(self, auth: Type[ClarifaiAuthHelper], runner_id: str) -> None:
34
+ """
35
+ Args:
36
+ auth: ClarifaiAuthHelper - the auth object to use
37
+ runner_id: str - the id of the runner to use. Create the runner in the Clarifai API first
38
+
39
+ """
40
+ self.auth = auth
41
+ self.stub = create_stub(self.auth)
42
+ self.runner_id = runner_id
43
+
44
+ # Check that the runner exists.
45
+ response = self.stub.GetRunner(
46
+ service_pb2.GetRunnerRequest(
47
+ user_app_id=self.auth.get_user_app_id_proto(), runner_id=self.runner_id))
48
+ if work_response.status.code != status_code_pb2.SUCCESS:
49
+ raise Exception(
50
+ f"Error getting runner, are you use this is a valid runner id {runner_id} at the user_id/app_id {self.auth.get_user_app_id_proto().user_id}/{self.auth.get_user_app_id_proto().app_id}. Error: {response.status.description}"
51
+ )
52
+
53
+ def start(self):
54
+ """
55
+ Start the run loop. This will ask the Clarifai API for work, and when it gets work, it will run
56
+ the model on the inputs and post the results back to the Clarifai API. It will then ask for more
57
+ work again.
58
+ """
59
+ self._long_poll_loop()
60
+
61
+ def _run(self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
62
+ """
63
+ Run the model on the given request. You shouldn't need to override this method, see run_input
64
+ for the implementation to process each input in the request.
65
+
66
+ Args:
67
+ request: service_pb2.PostModelOutputsRequest - the request to run the model on
68
+
69
+ Returns:
70
+ service_pb2.MultiOutputResponse - the response from the model's run_input implementation.
71
+ """
72
+ outputs = []
73
+ # TODO: parallelize this
74
+ for inp in request.inputs:
75
+ # TODO: handle errors
76
+ outputs.append(self.run_input(inp))
77
+
78
+ return service_pb2.MultiOutputResponse(
79
+ status=status_pb2.Status(
80
+ code=status_code_pb2.SUCCESS,
81
+ description="Success",
82
+ ),
83
+ outputs=outputs,
84
+ )
85
+
86
+ def run_input(self, input: resources_pb2.Input) -> resources_pb2.Output:
87
+ """
88
+ Run the model on the given input in the request. This is the method you should override to
89
+ process each input in the request.
90
+
91
+ Args:
92
+ input: resources_pb2.Input - the input to run the model on
93
+
94
+ Returns:
95
+ resources_pb2.Output - the response from the model's run_input implementation.
96
+ """
97
+ raise NotImplementedError("run_input() not implemented")
98
+
99
+ def _long_poll_loop(self):
100
+ """
101
+ This method will long poll for work, and when it gets work, it will run the model on the inputs
102
+ and post the results back to the Clarifai API. It will then long poll again for more work.
103
+ """
104
+ c = 0
105
+ # TODO: handle more errors within this loop so it never stops.
106
+ # TODO: perhaps have multiple processes running this loop to handle more work.
107
+ while True:
108
+ # Long poll waiting for work.
109
+ print("Loop iteration: {}".format(c))
110
+ work_response = self.stub.ListRunnerItems(
111
+ service_pb2.ListRunnerItemsRequest(
112
+ user_app_id=self.auth.get_user_app_id_proto(), runner_id=self.runner_id))
113
+ if work_response.status.code == status_code_pb2.RUNNER_NEEDS_RETRY:
114
+ c += 1
115
+ continue # immediate restart the long poll
116
+ if work_response.status.code != status_code_pb2.SUCCESS:
117
+ raise Exception("Error getting work: {}".format(work_response.status.description))
118
+ if len(work_response.items) == 0:
119
+ print("No work to do. Waiting...")
120
+ continue
121
+
122
+ # We have work to do. Run the model on the inputs.
123
+ for item in work_response.items:
124
+ if not item.HasField('post_model_outputs_request'):
125
+ raise Exception("Unexpected work item type: {}".format(item))
126
+ print(
127
+ f"Working on item: {item.id} with inputs {len(item.post_model_outputs_request.inputs)}"
128
+ )
129
+ result = self._run(item.post_model_outputs_request)
130
+
131
+ result_response = self.stub.PostRunnerItemOutputs(
132
+ service_pb2.PostRunnerItemOutputsRequest(
133
+ user_app_id=self.auth.get_user_app_id_proto(),
134
+ item_id=item.id,
135
+ runner_id=self.runner_id,
136
+ runner_item_outputs=[service_pb2.RunnerItemOutput(multi_output_response=result)]))
137
+ if result_response.status.code != status_code_pb2.SUCCESS:
138
+ raise Exception(
139
+ json_format.MessageToJson(result_response, preserving_proto_field_name=True))
140
+ # raise Exception("Error posting result: {}".format(result_response.status.description))
@@ -0,0 +1,36 @@
1
+ from clarifai_grpc.grpc.api import resources_pb2
2
+
3
+ from clarifai.auth.helper import ClarifaiAuthHelper
4
+ from clarifai.runners.base import BaseRunner
5
+
6
+
7
+ class MyRunner(BaseRunner):
8
+ """ A custom runner that adds "Hello World" to the end of the text and replaces the domain of the
9
+ image URL as an example.
10
+ """
11
+
12
+ def run_input(self, input: resources_pb2.Input) -> resources_pb2.Output:
13
+ """ This is the method that will be called when the runner is run. It takes in an input and
14
+ returns an output.
15
+ """
16
+
17
+ output = resources_pb2.Output()
18
+
19
+ data = input.data
20
+
21
+ if data.text.raw != "":
22
+ output.data.text.raw = data.text.raw + "Hello World"
23
+ if data.image.url != "":
24
+ output.data.text.raw = data.image.url.replace("samples.clarifai.com", "newdomain.com")
25
+ return output
26
+
27
+
28
+ if __name__ == '__main__':
29
+ # Make sure you set these env vars before running the example.
30
+ # CLARIFAI_PAT
31
+ # CLARIFAI_USER_ID
32
+ # CLARIFAI_APP_ID
33
+ auth = ClarifaiAuthHelper.from_env()
34
+
35
+ # You need to first create a runner in the Clarifai API and then use the ID here.
36
+ MyRunner(auth, runner_id="laptop_runner").start()