clarifai 11.4.1__py3-none-any.whl → 11.4.3rc1__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.
- clarifai/__init__.py +1 -1
- clarifai/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/__pycache__/errors.cpython-312.pyc +0 -0
- clarifai/__pycache__/errors.cpython-39.pyc +0 -0
- clarifai/__pycache__/versions.cpython-312.pyc +0 -0
- clarifai/__pycache__/versions.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-312.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-312.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-312.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-312.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-312.pyc +0 -0
- clarifai/cli/base.py +8 -0
- clarifai/cli/model.py +6 -6
- clarifai/client/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/model_client.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/runner.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-312.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-312.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-312.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-39.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-312.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-39.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-312.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-39.pyc +0 -0
- clarifai/client/dataset.py +6 -0
- clarifai/constants/__pycache__/base.cpython-312.pyc +0 -0
- clarifai/constants/__pycache__/base.cpython-39.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-312.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-312.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-312.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-312.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-312.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-312.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-312.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-312.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-312.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-312.pyc +0 -0
- clarifai/modules/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/modules/__pycache__/css.cpython-312.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-312.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-312.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/__pycache__/server.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/mcp_class.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/model_servicer.cpython-312.pyc +0 -0
- clarifai/runners/models/__pycache__/test_model_builder.cpython-312-pytest-8.3.5.pyc +0 -0
- clarifai/runners/models/base_typed_model.py +238 -0
- clarifai/runners/models/example_mcp_server.py +44 -0
- clarifai/runners/models/mcp_class.py +143 -0
- clarifai/runners/models/mcp_class.py~ +149 -0
- clarifai/runners/models/model_builder.py +167 -38
- clarifai/runners/models/model_class.py +5 -22
- clarifai/runners/models/model_run_locally.py +0 -4
- clarifai/runners/models/test_model_builder.py +89 -0
- clarifai/runners/models/visual_classifier_class.py +75 -0
- clarifai/runners/models/visual_detector_class.py +79 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/code_script.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_types.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/method_signatures.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/serializers.cpython-312.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-312.pyc +0 -0
- clarifai/runners/utils/code_script.py +41 -44
- clarifai/runners/utils/const.py +15 -0
- clarifai/runners/utils/data_handler.py +231 -0
- clarifai/runners/utils/data_types/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/runners/utils/data_types/__pycache__/data_types.cpython-312.pyc +0 -0
- clarifai/runners/utils/data_utils.py +33 -5
- clarifai/runners/utils/loader.py +23 -2
- clarifai/runners/utils/method_signatures.py +4 -4
- clarifai/schema/__pycache__/search.cpython-312.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/config.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-312.pyc +0 -0
- clarifai/utils/__pycache__/protobuf.cpython-312.pyc +0 -0
- clarifai/utils/config.py +19 -0
- clarifai/utils/config.py~ +145 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/helpers.cpython-312.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-312.pyc +0 -0
- clarifai/utils/logging.py +22 -5
- clarifai/workflows/__pycache__/__init__.cpython-312.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-312.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-312.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-312.pyc +0 -0
- {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/METADATA +2 -14
- clarifai-11.4.3rc1.dist-info/RECORD +230 -0
- {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/WHEEL +1 -1
- clarifai-11.4.1.dist-info/RECORD +0 -109
- {clarifai-11.4.1.dist-info/licenses → clarifai-11.4.3rc1.dist-info}/LICENSE +0 -0
- {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/entry_points.txt +0 -0
- {clarifai-11.4.1.dist-info → clarifai-11.4.3rc1.dist-info}/top_level.txt +0 -0
clarifai/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
__version__ = "11.4.
|
1
|
+
__version__ = "11.4.3rc1"
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
clarifai/cli/base.py
CHANGED
@@ -143,6 +143,14 @@ def dump(ctx_obj, output_format):
|
|
143
143
|
json.dump(ctx_obj.to_dict(), sys.stdout, indent=2)
|
144
144
|
|
145
145
|
|
146
|
+
@config.command(['cat'])
|
147
|
+
@click.option('-o', '--output-format', default='yaml', type=click.Choice(['yaml', 'json']))
|
148
|
+
@click.pass_obj
|
149
|
+
def env(ctx_obj, output_format):
|
150
|
+
"""Print env vars. Use: eval "$(clarifai config env)" """
|
151
|
+
ctx_obj.current.print_env_vars()
|
152
|
+
|
153
|
+
|
146
154
|
@cli.command()
|
147
155
|
@click.argument('api_url', default="https://api.clarifai.com")
|
148
156
|
@click.option('--user_id', required=False, help='User ID')
|
clarifai/cli/model.py
CHANGED
@@ -504,6 +504,10 @@ def local_dev(ctx, model_path):
|
|
504
504
|
|
505
505
|
logger.info(f"Current deployment_id: {deployment_id}")
|
506
506
|
|
507
|
+
logger.info(
|
508
|
+
f"Full url for the model: /users/{user_id}/apps/{app_id}/models/{model.id}/versions/{version.id}"
|
509
|
+
)
|
510
|
+
|
507
511
|
# Now that we have all the context in ctx.obj, we need to update the config.yaml in
|
508
512
|
# the model_path directory with the model object containing user_id, app_id, model_id, version_id
|
509
513
|
config_file = os.path.join(model_path, 'config.yaml')
|
@@ -526,13 +530,9 @@ def local_dev(ctx, model_path):
|
|
526
530
|
ModelBuilder._backup_config(config_file)
|
527
531
|
ModelBuilder._save_config(config_file, config)
|
528
532
|
|
529
|
-
# client_model = Model(
|
530
|
-
# TODO: once we can generate_client_script from ModelBuilder or similar
|
531
|
-
# we should be able to put the exact function call in place.
|
532
|
-
# model_script = model.generate_client_script()
|
533
|
-
|
534
533
|
builder = ModelBuilder(model_path, download_validation_only=True)
|
535
|
-
|
534
|
+
# don't mock for local dev since you need the dependencies to run the code anyways.
|
535
|
+
method_signatures = builder.get_method_signatures(mocking=False)
|
536
536
|
|
537
537
|
from clarifai.runners.utils import code_script
|
538
538
|
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
clarifai/client/dataset.py
CHANGED
@@ -685,6 +685,12 @@ class Dataset(Lister, BaseClient):
|
|
685
685
|
Note:
|
686
686
|
This is a beta feature and is subject to change.
|
687
687
|
"""
|
688
|
+
try:
|
689
|
+
import rich # noqa: F401
|
690
|
+
except ImportError:
|
691
|
+
raise UserError(
|
692
|
+
"Rich library is not installed. Please install it using pip install rich>=13.4.2"
|
693
|
+
)
|
688
694
|
self.logger.info("Getting dataset upload status...")
|
689
695
|
dataset_version_id = uuid.uuid4().hex
|
690
696
|
_ = self.create_version(id=dataset_version_id, description="SDK Upload Status")
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,238 @@
|
|
1
|
+
import itertools
|
2
|
+
from typing import Any, Dict, Iterator, List, Tuple
|
3
|
+
|
4
|
+
import numpy as np
|
5
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
6
|
+
from clarifai_grpc.grpc.api.service_pb2 import PostModelOutputsRequest
|
7
|
+
from google.protobuf import json_format
|
8
|
+
|
9
|
+
from ..utils.data_handler import InputDataHandler, OutputDataHandler
|
10
|
+
from .model_class import ModelClass
|
11
|
+
|
12
|
+
|
13
|
+
class AnyAnyModel(ModelClass):
|
14
|
+
|
15
|
+
def load_model(self):
|
16
|
+
"""
|
17
|
+
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
18
|
+
in this method so they are loaded only once for faster inference.
|
19
|
+
"""
|
20
|
+
raise NotImplementedError
|
21
|
+
|
22
|
+
def parse_input_request(
|
23
|
+
self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
|
24
|
+
list_input_dict = [
|
25
|
+
InputDataHandler.from_proto(input).to_python() for input in input_request.inputs
|
26
|
+
]
|
27
|
+
inference_params = json_format.MessageToDict(
|
28
|
+
input_request.model.model_version.output_info.params)
|
29
|
+
|
30
|
+
return list_input_dict, inference_params
|
31
|
+
|
32
|
+
def convert_output_to_proto(self, outputs: list):
|
33
|
+
assert (isinstance(outputs, Iterator) or isinstance(outputs, list) or
|
34
|
+
isinstance(outputs, tuple)), "outputs must be an Iterator"
|
35
|
+
output_protos = []
|
36
|
+
for output in outputs:
|
37
|
+
if isinstance(output, OutputDataHandler):
|
38
|
+
output = output.proto
|
39
|
+
elif isinstance(output, resources_pb2.Output):
|
40
|
+
pass
|
41
|
+
else:
|
42
|
+
raise NotImplementedError
|
43
|
+
output_protos.append(output)
|
44
|
+
|
45
|
+
return service_pb2.MultiOutputResponse(outputs=output_protos)
|
46
|
+
|
47
|
+
def predict_wrapper(
|
48
|
+
self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
|
49
|
+
list_dict_input, inference_params = self.parse_input_request(request)
|
50
|
+
outputs = self.predict(list_dict_input, inference_parameters=inference_params)
|
51
|
+
return self.convert_output_to_proto(outputs)
|
52
|
+
|
53
|
+
def generate_wrapper(
|
54
|
+
self, request: PostModelOutputsRequest) -> Iterator[service_pb2.MultiOutputResponse]:
|
55
|
+
list_dict_input, inference_params = self.parse_input_request(request)
|
56
|
+
outputs = self.generate(list_dict_input, inference_parameters=inference_params)
|
57
|
+
for output in outputs:
|
58
|
+
yield self.convert_output_to_proto(output)
|
59
|
+
|
60
|
+
def _preprocess_stream(
|
61
|
+
self, request: Iterator[PostModelOutputsRequest]) -> Iterator[Tuple[List[Dict], List[Dict]]]:
|
62
|
+
"""Return generator of processed data (from proto to python) and inference parameters like predict and generate"""
|
63
|
+
for i, req in enumerate(request):
|
64
|
+
input_data, _ = self.parse_input_request(req)
|
65
|
+
yield input_data
|
66
|
+
|
67
|
+
def stream_wrapper(self, request: Iterator[PostModelOutputsRequest]
|
68
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
69
|
+
first_request = next(request)
|
70
|
+
_, inference_params = self.parse_input_request(first_request)
|
71
|
+
request_iterator = itertools.chain([first_request], request)
|
72
|
+
outputs = self.stream(self._preprocess_stream(request_iterator), inference_params)
|
73
|
+
for output in outputs:
|
74
|
+
yield self.convert_output_to_proto(output)
|
75
|
+
|
76
|
+
def predict(self, input_data: List[Dict],
|
77
|
+
inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
|
78
|
+
"""
|
79
|
+
Prediction method.
|
80
|
+
|
81
|
+
Args:
|
82
|
+
-----
|
83
|
+
- input_data: is list of dict where key is input type name.
|
84
|
+
* image: np.ndarray
|
85
|
+
* text: str
|
86
|
+
* audio: bytes
|
87
|
+
|
88
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
89
|
+
|
90
|
+
Returns:
|
91
|
+
--------
|
92
|
+
List of OutputDataHandler
|
93
|
+
"""
|
94
|
+
raise NotImplementedError
|
95
|
+
|
96
|
+
def generate(self, input_data: List[Dict],
|
97
|
+
inference_parameters: Dict[str, Any] = {}) -> Iterator[List[OutputDataHandler]]:
|
98
|
+
"""
|
99
|
+
Generate method.
|
100
|
+
|
101
|
+
Args:
|
102
|
+
-----
|
103
|
+
- input_data: is list of dict where key is input type name.
|
104
|
+
* image: np.ndarray
|
105
|
+
* text: str
|
106
|
+
* audio: bytes
|
107
|
+
|
108
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
109
|
+
|
110
|
+
Yield:
|
111
|
+
--------
|
112
|
+
List of OutputDataHandler
|
113
|
+
"""
|
114
|
+
raise NotImplementedError
|
115
|
+
|
116
|
+
def stream(self, inputs: Iterator[List[Dict[str, Any]]],
|
117
|
+
inference_params: Dict[str, Any]) -> Iterator[List[OutputDataHandler]]:
|
118
|
+
"""
|
119
|
+
Stream method.
|
120
|
+
|
121
|
+
Args:
|
122
|
+
-----
|
123
|
+
input_request: is an Iterator of Tuple which
|
124
|
+
- First element (List[Dict[str, Union[np.ndarray, str, bytes]]]) is list of dict input data type which keys and values are:
|
125
|
+
* image: np.ndarray
|
126
|
+
* text: str
|
127
|
+
* audio: bytes
|
128
|
+
|
129
|
+
- Second element (Dict[str, Union[bool, str, float, int]]): is a dict of inference_parameters
|
130
|
+
|
131
|
+
Yield:
|
132
|
+
--------
|
133
|
+
List of OutputDataHandler
|
134
|
+
"""
|
135
|
+
raise NotImplementedError
|
136
|
+
|
137
|
+
|
138
|
+
class VisualInputModel(AnyAnyModel):
|
139
|
+
|
140
|
+
def parse_input_request(
|
141
|
+
self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
|
142
|
+
list_input_dict = [
|
143
|
+
InputDataHandler.from_proto(input).image(format="np") for input in input_request.inputs
|
144
|
+
]
|
145
|
+
inference_params = json_format.MessageToDict(
|
146
|
+
input_request.model.model_version.output_info.params)
|
147
|
+
|
148
|
+
return list_input_dict, inference_params
|
149
|
+
|
150
|
+
def load_model(self):
|
151
|
+
"""
|
152
|
+
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
153
|
+
in this method so they are loaded only once for faster inference.
|
154
|
+
"""
|
155
|
+
raise NotImplementedError
|
156
|
+
|
157
|
+
def predict(self, input_data: List[np.ndarray],
|
158
|
+
inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
|
159
|
+
"""
|
160
|
+
Prediction method.
|
161
|
+
|
162
|
+
Args:
|
163
|
+
-----
|
164
|
+
- input_data(List[np.ndarray]): is list of image as np.ndarray type
|
165
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
166
|
+
|
167
|
+
Returns:
|
168
|
+
--------
|
169
|
+
List of OutputDataHandler
|
170
|
+
"""
|
171
|
+
raise NotImplementedError
|
172
|
+
|
173
|
+
|
174
|
+
class TextInputModel(AnyAnyModel):
|
175
|
+
|
176
|
+
def load_model(self):
|
177
|
+
"""
|
178
|
+
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
179
|
+
in this method so they are loaded only once for faster inference.
|
180
|
+
"""
|
181
|
+
raise NotImplementedError
|
182
|
+
|
183
|
+
def parse_input_request(
|
184
|
+
self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
|
185
|
+
list_input_text = [InputDataHandler.from_proto(input).text for input in input_request.inputs]
|
186
|
+
inference_params = json_format.MessageToDict(
|
187
|
+
input_request.model.model_version.output_info.params)
|
188
|
+
|
189
|
+
return list_input_text, inference_params
|
190
|
+
|
191
|
+
def predict(self, input_data: List[str],
|
192
|
+
inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
|
193
|
+
"""
|
194
|
+
Prediction method.
|
195
|
+
|
196
|
+
Args:
|
197
|
+
-----
|
198
|
+
- input_data(List[str]): is list of text as str type
|
199
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
200
|
+
|
201
|
+
Returns:
|
202
|
+
--------
|
203
|
+
List of OutputDataHandler
|
204
|
+
"""
|
205
|
+
raise NotImplementedError
|
206
|
+
|
207
|
+
def generate(self, input_data: List[str],
|
208
|
+
inference_parameters: Dict[str, Any] = {}) -> Iterator[List[OutputDataHandler]]:
|
209
|
+
"""
|
210
|
+
Prediction method.
|
211
|
+
|
212
|
+
Args:
|
213
|
+
-----
|
214
|
+
- input_data(List[str]): is list of text as str type
|
215
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
216
|
+
|
217
|
+
Yield:
|
218
|
+
--------
|
219
|
+
List of OutputDataHandler
|
220
|
+
"""
|
221
|
+
raise NotImplementedError
|
222
|
+
|
223
|
+
def stream(self, inputs: Iterator[List[str]],
|
224
|
+
inference_params: Dict[str, Any]) -> Iterator[List[OutputDataHandler]]:
|
225
|
+
"""
|
226
|
+
Stream method.
|
227
|
+
|
228
|
+
Args:
|
229
|
+
-----
|
230
|
+
input_request: is an Iterator of Tuple which
|
231
|
+
- First element (List[str]) is list of input text:
|
232
|
+
- Second element (Dict[str, Union[bool, str, float, int]]): is a dict of inference_parameters
|
233
|
+
|
234
|
+
Yield:
|
235
|
+
--------
|
236
|
+
List of OutputDataHandler
|
237
|
+
"""
|
238
|
+
raise NotImplementedError
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"""Example of how to create an MCP server using MCPClass."""
|
2
|
+
|
3
|
+
from typing import Dict
|
4
|
+
from fastmcp import Tool
|
5
|
+
|
6
|
+
from clarifai.runners.models.mcp_class import MCPClass
|
7
|
+
|
8
|
+
|
9
|
+
class ExampleMCPServer(MCPClass):
|
10
|
+
"""Example MCP server that provides a simple calculator tool."""
|
11
|
+
|
12
|
+
def __init__(self):
|
13
|
+
super().__init__()
|
14
|
+
|
15
|
+
# Define and register a calculator tool
|
16
|
+
calculator_tool = Tool(
|
17
|
+
name="calculator",
|
18
|
+
description="A simple calculator that can add two numbers",
|
19
|
+
parameters={
|
20
|
+
"type": "object",
|
21
|
+
"properties": {
|
22
|
+
"a": {"type": "number", "description": "First number"},
|
23
|
+
"b": {"type": "number", "description": "Second number"}
|
24
|
+
},
|
25
|
+
"required": ["a", "b"]
|
26
|
+
}
|
27
|
+
)
|
28
|
+
|
29
|
+
@calculator_tool
|
30
|
+
async def add(params: Dict[str, float]) -> float:
|
31
|
+
"""Add two numbers together."""
|
32
|
+
return params["a"] + params["b"]
|
33
|
+
|
34
|
+
# Register the tool with the MCP server
|
35
|
+
self.add_tool(calculator_tool)
|
36
|
+
|
37
|
+
|
38
|
+
# Usage example:
|
39
|
+
if __name__ == "__main__":
|
40
|
+
# Create and run the server
|
41
|
+
server = ExampleMCPServer()
|
42
|
+
|
43
|
+
# The server is now ready to handle MCP requests through the mcp_transport method
|
44
|
+
# For example, a client could send a request to list tools or call the calculator
|