clarifai 10.11.1__py3-none-any.whl → 10.11.2rc1__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-310.pyc +0 -0
- clarifai/__pycache__/errors.cpython-310.pyc +0 -0
- clarifai/__pycache__/versions.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
- clarifai/client/dataset.py +4 -4
- clarifai/client/model.py +94 -13
- clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/datasets/upload/features.py +1 -1
- clarifai/datasets/upload/multimodal.py +2 -1
- clarifai/datasets/upload/text.py +3 -2
- clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/README.md +158 -0
- clarifai/models/model_serving/__init__.py +14 -0
- clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__init__.py +12 -0
- clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/_utils.py +53 -0
- clarifai/models/model_serving/cli/base.py +14 -0
- clarifai/models/model_serving/cli/build.py +79 -0
- clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
- clarifai/models/model_serving/cli/create.py +171 -0
- clarifai/models/model_serving/cli/example_cli.py +34 -0
- clarifai/models/model_serving/cli/login.py +26 -0
- clarifai/models/model_serving/cli/upload.py +183 -0
- clarifai/models/model_serving/constants.py +21 -0
- clarifai/models/model_serving/docs/cli.md +161 -0
- clarifai/models/model_serving/docs/concepts.md +229 -0
- clarifai/models/model_serving/docs/dependencies.md +11 -0
- clarifai/models/model_serving/docs/inference_parameters.md +139 -0
- clarifai/models/model_serving/docs/model_types.md +19 -0
- clarifai/models/model_serving/model_config/__init__.py +16 -0
- clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/base.py +369 -0
- clarifai/models/model_serving/model_config/config.py +312 -0
- clarifai/models/model_serving/model_config/inference_parameter.py +129 -0
- clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -0
- clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -0
- clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -0
- clarifai/models/model_serving/model_config/output.py +133 -0
- clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/serializer.py +136 -0
- clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
- clarifai/models/model_serving/model_config/triton/wrappers.py +281 -0
- clarifai/models/model_serving/repo_build/__init__.py +14 -0
- clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
- clarifai/models/model_serving/repo_build/build.py +198 -0
- clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
- clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
- clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
- clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
- clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
- clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
- clarifai/models/model_serving/repo_build/static_files/triton/model.py +75 -0
- clarifai/models/model_serving/utils.py +31 -0
- clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
- clarifai/runners/deepgram_live_transcribe.py +98 -0
- clarifai/runners/deepgram_live_transcribe.py~ +98 -0
- clarifai/runners/deepgram_runner.py +131 -0
- clarifai/runners/deepgram_runner.py~ +130 -0
- clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
- clarifai/runners/dockerfile_template/Dockerfile.cuda.template +79 -0
- clarifai/runners/example_llama2.py~ +72 -0
- clarifai/runners/matt_example.py +89 -0
- clarifai/runners/matt_example.py~ +87 -0
- clarifai/runners/matt_llm_example.py +129 -0
- clarifai/runners/matt_llm_example.py~ +128 -0
- clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
- clarifai/runners/models/model_upload.py +75 -10
- clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
- clarifai/runners/utils/const.py +27 -25
- clarifai/runners/utils/loader.py +31 -16
- clarifai/runners/utils/logging.py +6 -0
- clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
- clarifai/utils/logging.py +7 -0
- clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
- {clarifai-10.11.1.dist-info → clarifai-10.11.2rc1.dist-info}/METADATA +15 -15
- clarifai-10.11.2rc1.dist-info/RECORD +242 -0
- {clarifai-10.11.1.dist-info → clarifai-10.11.2rc1.dist-info}/WHEEL +1 -1
- clarifai-10.11.1.dist-info/RECORD +0 -100
- {clarifai-10.11.1.dist-info → clarifai-10.11.2rc1.dist-info}/LICENSE +0 -0
- {clarifai-10.11.1.dist-info → clarifai-10.11.2rc1.dist-info}/entry_points.txt +0 -0
- {clarifai-10.11.1.dist-info → clarifai-10.11.2rc1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,131 @@
|
|
1
|
+
import os
|
2
|
+
import time
|
3
|
+
import httpx
|
4
|
+
from dotenv import load_dotenv
|
5
|
+
import threading
|
6
|
+
import queue
|
7
|
+
import hashlib
|
8
|
+
|
9
|
+
from deepgram import (
|
10
|
+
DeepgramClient,
|
11
|
+
LiveTranscriptionEvents,
|
12
|
+
LiveOptions,
|
13
|
+
)
|
14
|
+
|
15
|
+
load_dotenv()
|
16
|
+
|
17
|
+
os.environ["DG_API_KEY"] = "1d5666523f0f2fbaf3e2db6ae7717f6f87280b5e"
|
18
|
+
API_KEY = os.getenv("DG_API_KEY")
|
19
|
+
|
20
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
21
|
+
from collections.abc import Iterator
|
22
|
+
from google.protobuf import json_format
|
23
|
+
|
24
|
+
from clarifai.client.runner import Runner
|
25
|
+
|
26
|
+
|
27
|
+
class MyRunner(Runner):
|
28
|
+
"""A custom runner that adds "Hello World" to the end of the text and replaces the domain of the
|
29
|
+
image URL as an example.
|
30
|
+
"""
|
31
|
+
|
32
|
+
def setup_connection(self):
|
33
|
+
print("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZz")
|
34
|
+
# STEP 2: Create a websocket connection to Deepgram
|
35
|
+
self.dg_connection = self.deepgram.listen.live.v("1")
|
36
|
+
|
37
|
+
output_q = self.output_q
|
38
|
+
|
39
|
+
# STEP 3: Define the event handlers for the connection
|
40
|
+
def on_message(self, result, **kwargs):
|
41
|
+
if result.is_final:
|
42
|
+
sentence = result.channel.alternatives[0].transcript
|
43
|
+
if len(sentence) == 0:
|
44
|
+
return
|
45
|
+
print("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS")
|
46
|
+
print(f"speaker: {sentence}")
|
47
|
+
# put it on a queue as we get responses from deepgram.
|
48
|
+
output_q.put(sentence)
|
49
|
+
|
50
|
+
def on_metadata(self, metadata, **kwargs):
|
51
|
+
print(f"\n\n{metadata}\n\n")
|
52
|
+
|
53
|
+
def on_error(self, error, **kwargs):
|
54
|
+
print(f"\n\n{error}\n\n")
|
55
|
+
|
56
|
+
# STEP 4: Register the event handlers
|
57
|
+
self.dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
|
58
|
+
self.dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
|
59
|
+
self.dg_connection.on(LiveTranscriptionEvents.Error, on_error)
|
60
|
+
|
61
|
+
# STEP 5: Configure Deepgram options for live transcription
|
62
|
+
self.options = LiveOptions(
|
63
|
+
model="nova-2",
|
64
|
+
language="en-US",
|
65
|
+
smart_format=True,
|
66
|
+
)
|
67
|
+
|
68
|
+
# STEP 6: Start the connection
|
69
|
+
self.dg_connection.start(self.options)
|
70
|
+
|
71
|
+
print("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD")
|
72
|
+
print(self.dg_connection._socket)
|
73
|
+
|
74
|
+
def __init__(self, *args, **kwargs):
|
75
|
+
print("MyRunner init")
|
76
|
+
# STEP 1: Create a Deepgram client using the API key
|
77
|
+
self.deepgram = DeepgramClient(API_KEY)
|
78
|
+
self.output_q = queue.Queue()
|
79
|
+
|
80
|
+
self.setup_connection()
|
81
|
+
|
82
|
+
super().__init__(*args, **kwargs)
|
83
|
+
|
84
|
+
def stream(self, request: service_pb2.PostModelOutputsRequest
|
85
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
86
|
+
"""Example yielding a whole batch of streamed stuff back.
|
87
|
+
"""
|
88
|
+
|
89
|
+
assert len(request.inputs) == 1, "This runner only supports one input at a time."
|
90
|
+
|
91
|
+
# Get the next chunk of data from the incoming stream.
|
92
|
+
|
93
|
+
print("Got some audio data")
|
94
|
+
|
95
|
+
data = request.inputs[0].data.audio.base64
|
96
|
+
print(hashlib.md5(data).hexdigest())
|
97
|
+
|
98
|
+
# FIXME(zeiler): this doesnt' work but isn't iportant to our system.
|
99
|
+
if not self.dg_connection._socket:
|
100
|
+
#self.dg_connection.finish()
|
101
|
+
#self.dg_connection.start(self.options)
|
102
|
+
self.setup_connection()
|
103
|
+
self.dg_connection.send(data)
|
104
|
+
print("Sent it to deepgram")
|
105
|
+
|
106
|
+
while True:
|
107
|
+
try:
|
108
|
+
item = self.output_q.get(timeout=0.1)
|
109
|
+
output = resources_pb2.Output()
|
110
|
+
output.data.text.raw = item
|
111
|
+
output.status.code = 10000
|
112
|
+
print("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY")
|
113
|
+
print("Yielding: ", output.data.text.raw)
|
114
|
+
yield service_pb2.MultiOutputResponse(outputs=[
|
115
|
+
output,
|
116
|
+
])
|
117
|
+
except queue.Empty:
|
118
|
+
print("Queue is empty, sleeping then breaking")
|
119
|
+
break
|
120
|
+
|
121
|
+
# # STEP 13: Close the connection to Deepgram
|
122
|
+
# dg_connection.finish()
|
123
|
+
|
124
|
+
|
125
|
+
if __name__ == '__main__':
|
126
|
+
# Make sure you set these env vars before running the example.
|
127
|
+
# CLARIFAI_PAT
|
128
|
+
# CLARIFAI_USER_ID
|
129
|
+
|
130
|
+
# You need to first create a runner in the Clarifai API and then use the ID here.
|
131
|
+
MyRunner(runner_id="matt-test-runner", base_url="http://q6:32013", num_parallel_polls=1).start()
|
@@ -0,0 +1,130 @@
|
|
1
|
+
import os
|
2
|
+
import time
|
3
|
+
import httpx
|
4
|
+
from dotenv import load_dotenv
|
5
|
+
import threading
|
6
|
+
import queue
|
7
|
+
import hashlib
|
8
|
+
|
9
|
+
from deepgram import (
|
10
|
+
DeepgramClient,
|
11
|
+
LiveTranscriptionEvents,
|
12
|
+
LiveOptions,
|
13
|
+
)
|
14
|
+
|
15
|
+
load_dotenv()
|
16
|
+
|
17
|
+
os.environ["DG_API_KEY"] = "1d5666523f0f2fbaf3e2db6ae7717f6f87280b5e"
|
18
|
+
API_KEY = os.getenv("DG_API_KEY")
|
19
|
+
|
20
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
21
|
+
from collections.abc import Iterator
|
22
|
+
from google.protobuf import json_format
|
23
|
+
|
24
|
+
from clarifai.client.runner import Runner
|
25
|
+
|
26
|
+
|
27
|
+
class MyRunner(Runner):
|
28
|
+
"""A custom runner that adds "Hello World" to the end of the text and replaces the domain of the
|
29
|
+
image URL as an example.
|
30
|
+
"""
|
31
|
+
|
32
|
+
def setup_connection(self):
|
33
|
+
print("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZz")
|
34
|
+
# STEP 2: Create a websocket connection to Deepgram
|
35
|
+
self.dg_connection = self.deepgram.listen.live.v("1")
|
36
|
+
|
37
|
+
output_q = self.output_q
|
38
|
+
|
39
|
+
# STEP 3: Define the event handlers for the connection
|
40
|
+
def on_message(self, result, **kwargs):
|
41
|
+
if result.is_final:
|
42
|
+
sentence = result.channel.alternatives[0].transcript
|
43
|
+
if len(sentence) == 0:
|
44
|
+
return
|
45
|
+
print("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS")
|
46
|
+
print(f"speaker: {sentence}")
|
47
|
+
# put it on a queue as we get responses from deepgram.
|
48
|
+
output_q.put(sentence)
|
49
|
+
|
50
|
+
def on_metadata(self, metadata, **kwargs):
|
51
|
+
print(f"\n\n{metadata}\n\n")
|
52
|
+
|
53
|
+
def on_error(self, error, **kwargs):
|
54
|
+
print(f"\n\n{error}\n\n")
|
55
|
+
|
56
|
+
# STEP 4: Register the event handlers
|
57
|
+
self.dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
|
58
|
+
self.dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
|
59
|
+
self.dg_connection.on(LiveTranscriptionEvents.Error, on_error)
|
60
|
+
|
61
|
+
# STEP 5: Configure Deepgram options for live transcription
|
62
|
+
self.options = LiveOptions(
|
63
|
+
model="nova-2",
|
64
|
+
language="en-US",
|
65
|
+
smart_format=True,
|
66
|
+
)
|
67
|
+
|
68
|
+
# STEP 6: Start the connection
|
69
|
+
self.dg_connection.start(self.options)
|
70
|
+
|
71
|
+
print("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD")
|
72
|
+
print(self.dg_connection._socket)
|
73
|
+
|
74
|
+
def __init__(self, *args, **kwargs):
|
75
|
+
print("MyRunner init")
|
76
|
+
# STEP 1: Create a Deepgram client using the API key
|
77
|
+
self.deepgram = DeepgramClient(API_KEY)
|
78
|
+
self.output_q = queue.Queue()
|
79
|
+
|
80
|
+
self.setup_connection()
|
81
|
+
|
82
|
+
super().__init__(*args, **kwargs)
|
83
|
+
|
84
|
+
def stream(self, request: service_pb2.PostModelOutputsRequest
|
85
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
86
|
+
"""Example yielding a whole batch of streamed stuff back.
|
87
|
+
"""
|
88
|
+
|
89
|
+
assert len(request.inputs) == 1, "This runner only supports one input at a time."
|
90
|
+
|
91
|
+
# Get the next chunk of data from the incoming stream.
|
92
|
+
|
93
|
+
print("Got some audio data")
|
94
|
+
|
95
|
+
data = request.inputs[0].data.audio.base64
|
96
|
+
print(hashlib.md5(data).hexdigest())
|
97
|
+
|
98
|
+
if not self.dg_connection._socket:
|
99
|
+
self.dg_connection.finish()
|
100
|
+
self.dg_connection.start(self.options)
|
101
|
+
#self.setup_connection()
|
102
|
+
self.dg_connection.send(data)
|
103
|
+
print("Sent it to deepgram")
|
104
|
+
|
105
|
+
while True:
|
106
|
+
try:
|
107
|
+
item = self.output_q.get(timeout=0.1)
|
108
|
+
output = resources_pb2.Output()
|
109
|
+
output.data.text.raw = item
|
110
|
+
output.status.code = 10000
|
111
|
+
print("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY")
|
112
|
+
print("Yielding: ", output.data.text.raw)
|
113
|
+
yield service_pb2.MultiOutputResponse(outputs=[
|
114
|
+
output,
|
115
|
+
])
|
116
|
+
except queue.Empty:
|
117
|
+
print("Queue is empty, sleeping then breaking")
|
118
|
+
break
|
119
|
+
|
120
|
+
# # STEP 13: Close the connection to Deepgram
|
121
|
+
# dg_connection.finish()
|
122
|
+
|
123
|
+
|
124
|
+
if __name__ == '__main__':
|
125
|
+
# Make sure you set these env vars before running the example.
|
126
|
+
# CLARIFAI_PAT
|
127
|
+
# CLARIFAI_USER_ID
|
128
|
+
|
129
|
+
# You need to first create a runner in the Clarifai API and then use the ID here.
|
130
|
+
MyRunner(runner_id="matt-test-runner", base_url="http://q6:32013", num_parallel_polls=1).start()
|
@@ -0,0 +1,31 @@
|
|
1
|
+
ARG PYTHON_VERSION=${PYTHON_VERSION}
|
2
|
+
FROM public.ecr.aws/docker/library/python:${PYTHON_VERSION}-slim-bookworm as build
|
3
|
+
|
4
|
+
# Set the working directory to /app
|
5
|
+
WORKDIR /app
|
6
|
+
|
7
|
+
COPY requirements.txt .
|
8
|
+
# Install requirements and cleanup before leaving this line.
|
9
|
+
# Note(zeiler): this could be in a future template as {{model_python_deps}}
|
10
|
+
RUN python -m pip install -r requirements.txt && rm -rf /root/.cache
|
11
|
+
|
12
|
+
# Install Clarifai SDK
|
13
|
+
RUN python -m pip install clarifai
|
14
|
+
|
15
|
+
# These will be set by the templaing system.
|
16
|
+
ENV CLARIFAI_PAT=${CLARIFAI_PAT}
|
17
|
+
ENV CLARIFAI_USER_ID=${CLARIFAI_USER_ID}
|
18
|
+
ENV CLARIFAI_RUNNER_ID=${CLARIFAI_RUNNER_ID}
|
19
|
+
ENV CLARIFAI_NODEPOOL_ID=${CLARIFAI_NODEPOOL_ID}
|
20
|
+
ENV CLARIFAI_COMPUTE_CLUSTER_ID=${CLARIFAI_COMPUTE_CLUSTER_ID}
|
21
|
+
ENV CLARIFAI_API_BASE=${CLARIFAI_API_BASE}
|
22
|
+
|
23
|
+
# Copy the current folder into /app/model_dir that the SDK will expect.
|
24
|
+
COPY . /app/model_dir/${name}
|
25
|
+
|
26
|
+
# Add the model directory to the python path.
|
27
|
+
ENV PYTHONPATH "${PYTHONPATH}:/app/model_dir/${name}"
|
28
|
+
|
29
|
+
# Finally run the clarifai entrypoint to start the runner loop and local dev server.
|
30
|
+
# Note(zeiler): we may want to make this a clarifai CLI call.
|
31
|
+
CMD ["python", "-m", "clarifai.runners.server", "--model_path", "/app/model_dir/${name}"]
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# Build a virtualenv containing necessary system libraries and Python packages
|
2
|
+
# for users to install their own packages while also being distroless.
|
3
|
+
# * Install python3-venv
|
4
|
+
# * Install gcc libpython3-dev to compile C Python modules
|
5
|
+
# * In the virtualenv: Update pip setuputils and wheel to support building new packages
|
6
|
+
# * Export environment variables to use the virtualenv by default
|
7
|
+
# * Create a non-root user with minimal privileges and use it
|
8
|
+
ARG TARGET_PLATFORM=linux/amd64
|
9
|
+
FROM --platform=$TARGET_PLATFORM public.ecr.aws/docker/library/python:${PYTHON_VERSION}-slim-bookworm as build
|
10
|
+
|
11
|
+
ENV DEBIAN_FRONTEND=noninteractive
|
12
|
+
RUN apt-get update && \
|
13
|
+
apt-get install --no-install-suggests --no-install-recommends --yes \
|
14
|
+
software-properties-common \
|
15
|
+
gcc \
|
16
|
+
libpython3-dev && \
|
17
|
+
python${PYTHON_VERSION} -m venv /venv && \
|
18
|
+
/venv/bin/pip install --disable-pip-version-check --upgrade pip setuptools wheel && \
|
19
|
+
apt-get clean && rm -rf /var/lib/apt/lists/*
|
20
|
+
|
21
|
+
# Set environment variables to use virtualenv by default
|
22
|
+
ENV VIRTUAL_ENV=/venv
|
23
|
+
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
|
24
|
+
|
25
|
+
#############################
|
26
|
+
# User specific requirements
|
27
|
+
#############################
|
28
|
+
COPY requirements.txt .
|
29
|
+
|
30
|
+
# Install requirements and cleanup before leaving this line.
|
31
|
+
# Note(zeiler): this could be in a future template as {{model_python_deps}}
|
32
|
+
RUN python -m pip install -r requirements.txt && rm -rf /root/.cache
|
33
|
+
|
34
|
+
# Install Clarifai SDK
|
35
|
+
RUN python -m pip install clarifai
|
36
|
+
|
37
|
+
#############################
|
38
|
+
# Finally copy everything we built into a distroless image for runtime.
|
39
|
+
######################>#######
|
40
|
+
ARG TARGET_PLATFORM=linux/amd64
|
41
|
+
FROM --platform=$TARGET_PLATFORM gcr.io/distroless/python3-debian12:latest
|
42
|
+
# FROM --platform=$TARGET_PLATFORM gcr.io/distroless/python3-debian12:debug
|
43
|
+
ARG PYTHON_VERSION=${PYTHON_VERSION}
|
44
|
+
# needed to call pip directly
|
45
|
+
COPY --from=build /bin/sh /bin/sh
|
46
|
+
|
47
|
+
# virtual env
|
48
|
+
COPY --from=build /venv /venv
|
49
|
+
|
50
|
+
# We have to overwrite the python3 binary that the distroless image uses
|
51
|
+
COPY --from=build /usr/local/bin/python${PYTHON_VERSION} /usr/bin/python3
|
52
|
+
# And also copy in all the lib files for it.
|
53
|
+
COPY --from=build /usr/local/lib/ /usr/lib/
|
54
|
+
|
55
|
+
# Set environment variables to use virtualenv by default
|
56
|
+
ENV VIRTUAL_ENV=/venv
|
57
|
+
ENV PYTHONPATH=${PYTHONPATH}:${VIRTUAL_ENV}/lib/python${PYTHON_VERSION}/site-packages
|
58
|
+
|
59
|
+
# These will be set by the templaing system.
|
60
|
+
ENV CLARIFAI_PAT=${CLARIFAI_PAT}
|
61
|
+
ENV CLARIFAI_USER_ID=${CLARIFAI_USER_ID}
|
62
|
+
ENV CLARIFAI_RUNNER_ID=${CLARIFAI_RUNNER_ID}
|
63
|
+
ENV CLARIFAI_NODEPOOL_ID=${CLARIFAI_NODEPOOL_ID}
|
64
|
+
ENV CLARIFAI_COMPUTE_CLUSTER_ID=${CLARIFAI_COMPUTE_CLUSTER_ID}
|
65
|
+
ENV CLARIFAI_API_BASE=${CLARIFAI_API_BASE}
|
66
|
+
|
67
|
+
# Set the working directory to /app
|
68
|
+
WORKDIR /app
|
69
|
+
|
70
|
+
# Copy the current folder into /app/model_dir that the SDK will expect.
|
71
|
+
# Note(zeiler): would be nice to exclude checkpoints in case they were pre-downloaded.
|
72
|
+
COPY . /app/model_dir/${name}
|
73
|
+
|
74
|
+
# Add the model directory to the python path.
|
75
|
+
ENV PYTHONPATH=${PYTHONPATH}:/app/model_dir/${name}
|
76
|
+
|
77
|
+
# Finally run the clarifai entrypoint to start the runner loop and local dev server.
|
78
|
+
# Note(zeiler): we may want to make this a clarifai CLI call.
|
79
|
+
CMD ["-m", "clarifai.runners.server", "--model_path", "/app/model_dir/${name}"]
|
@@ -0,0 +1,72 @@
|
|
1
|
+
from clarifai.client.runner import Runner
|
2
|
+
from clarifai_grpc.grpc.api import resources_pb2
|
3
|
+
|
4
|
+
# This example requires to run the following before running this example:
|
5
|
+
# pip install transformers
|
6
|
+
|
7
|
+
# https://huggingface.co/TheBloke/Llama-2-70B-chat-GPTQ
|
8
|
+
model_name_or_path = "TheBloke/Llama-2-7B-chat-GPTQ"
|
9
|
+
model_basename = "model"
|
10
|
+
|
11
|
+
use_triton = False
|
12
|
+
|
13
|
+
|
14
|
+
class Llama2Runner(Runner):
|
15
|
+
"""A custom runner that runs the LLama2 LLM.
|
16
|
+
"""
|
17
|
+
|
18
|
+
def __init__(self, *args, **kwargs):
|
19
|
+
print("Starting to load the model...")
|
20
|
+
st = time.time()
|
21
|
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
|
22
|
+
self.model = AutoModelForCausalLM.from_pretrained(model_name_or_path, device_map='auto')
|
23
|
+
|
24
|
+
self.logger.info("Loading model complete in (%f seconds), ready to loop for requests." %
|
25
|
+
(time.time() - st))
|
26
|
+
super(MyRunner, self).__init__(*args, **kwargs)
|
27
|
+
|
28
|
+
def run_input(self, input: resources_pb2.Input,
|
29
|
+
output_info: resources_pb2.OutputInfo) -> resources_pb2.Output:
|
30
|
+
"""This is the method that will be called when the runner is run. It takes in an input and
|
31
|
+
returns an output.
|
32
|
+
"""
|
33
|
+
|
34
|
+
output = resources_pb2.Output()
|
35
|
+
data = input.data
|
36
|
+
if data.text.raw != "":
|
37
|
+
input_text = data.text.raw
|
38
|
+
elif data.text.url != "":
|
39
|
+
input_text = str(requests.get(data.text.url).text)
|
40
|
+
else:
|
41
|
+
raise Exception("Need to include data.text.raw or data.text.url in your inputs.")
|
42
|
+
|
43
|
+
st = time.time()
|
44
|
+
max_tokens = 4096
|
45
|
+
# # Method 1
|
46
|
+
# input_ids = self.tokenizer(input_text, return_tensors='pt').input_ids.cuda()
|
47
|
+
# out = self.model.generate(inputs=input_ids, temperature=0.7, max_new_tokens=max_tokens)
|
48
|
+
# out_text = self.tokenizer.decode(out[0], skip_special_tokens=True)
|
49
|
+
# output.data.text.raw = out_text.replace(input_text, '')
|
50
|
+
|
51
|
+
# # Method 2
|
52
|
+
pipe = pipeline(
|
53
|
+
"text-generation",
|
54
|
+
model=self.model,
|
55
|
+
tokenizer=self.tokenizer,
|
56
|
+
max_new_tokens=max_tokens,
|
57
|
+
temperature=0.7,
|
58
|
+
top_p=0.95,
|
59
|
+
repetition_penalty=1.15,
|
60
|
+
return_full_text=False)
|
61
|
+
a = pipe(input_text)
|
62
|
+
output.data.text.raw = a[0]['generated_text']
|
63
|
+
return output
|
64
|
+
|
65
|
+
|
66
|
+
if __name__ == '__main__':
|
67
|
+
# Make sure you set these env vars before running the example.
|
68
|
+
# CLARIFAI_PAT
|
69
|
+
# CLARIFAI_USER_ID
|
70
|
+
|
71
|
+
# You need to first create a runner in the Clarifai API and then use the ID here.
|
72
|
+
Llama2Runner(runner_id="sdk-llama2-runner").start()
|
@@ -0,0 +1,89 @@
|
|
1
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
2
|
+
from collections.abc import Iterator
|
3
|
+
from google.protobuf import json_format
|
4
|
+
|
5
|
+
from clarifai.client.runner import Runner
|
6
|
+
|
7
|
+
|
8
|
+
class MyRunner(Runner):
|
9
|
+
"""A custom runner that adds "Hello World" to the end of the text and replaces the domain of the
|
10
|
+
image URL as an example.
|
11
|
+
"""
|
12
|
+
|
13
|
+
def run_input(self, input: resources_pb2.Input, output_info: resources_pb2.OutputInfo,
|
14
|
+
**kwargs) -> resources_pb2.Output:
|
15
|
+
"""This is the method that will be called when the runner is run. It takes in an input and
|
16
|
+
returns an output.
|
17
|
+
"""
|
18
|
+
|
19
|
+
output = resources_pb2.Output()
|
20
|
+
|
21
|
+
data = input.data
|
22
|
+
|
23
|
+
# Optional use of output_info
|
24
|
+
params_dict = {}
|
25
|
+
if "params" in output_info:
|
26
|
+
params_dict = output_info["params"]
|
27
|
+
|
28
|
+
if data.text.raw != "":
|
29
|
+
output.data.text.raw = data.text.raw + "Hello World" + params_dict.get(
|
30
|
+
"hello", "") + kwargs.get("extra", "")
|
31
|
+
if data.image.url != "":
|
32
|
+
output.data.text.raw = data.image.url.replace("samples.clarifai.com",
|
33
|
+
"newdomain.com" + params_dict.get("domain",))
|
34
|
+
return output
|
35
|
+
|
36
|
+
def generate(self, request: service_pb2.PostModelOutputsRequest
|
37
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
38
|
+
"""Example yielding a whole batch of streamed stuff back.
|
39
|
+
"""
|
40
|
+
|
41
|
+
model = request.model
|
42
|
+
output_info = None
|
43
|
+
if request.model.model_version.id != "":
|
44
|
+
output_info = json_format.MessageToDict(
|
45
|
+
model.model_version.output_info, preserving_proto_field_name=True)
|
46
|
+
|
47
|
+
for i in range(10): # fake something iterating generating 10 times.
|
48
|
+
|
49
|
+
outputs = []
|
50
|
+
for input in request.inputs:
|
51
|
+
# output = self.run_input(input, output_info, extra=f" {i}")
|
52
|
+
output = resources_pb2.Output()
|
53
|
+
output.data.text.raw = f"Generate Hello World {i}"
|
54
|
+
outputs.append(output)
|
55
|
+
resp = service_pb2.MultiOutputResponse(outputs=outputs,)
|
56
|
+
yield resp
|
57
|
+
|
58
|
+
def stream(self, request: service_pb2.PostModelOutputsRequest
|
59
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
60
|
+
"""Example yielding a whole batch of streamed stuff back.
|
61
|
+
"""
|
62
|
+
|
63
|
+
model = request.model
|
64
|
+
output_info = None
|
65
|
+
if request.model.model_version.id != "":
|
66
|
+
output_info = json_format.MessageToDict(
|
67
|
+
model.model_version.output_info, preserving_proto_field_name=True)
|
68
|
+
|
69
|
+
for i in range(10): # fake something iterating generating 10 times.
|
70
|
+
|
71
|
+
outputs = []
|
72
|
+
for input in request.inputs:
|
73
|
+
# output = self.run_input(input, output_info, extra=f" {i}")
|
74
|
+
output = resources_pb2.Output()
|
75
|
+
out_text = input.data.text.raw + f"Stream Hello World {i}"
|
76
|
+
print(out_text)
|
77
|
+
output.data.text.raw = out_text
|
78
|
+
outputs.append(output)
|
79
|
+
resp = service_pb2.MultiOutputResponse(outputs=outputs,)
|
80
|
+
yield resp
|
81
|
+
|
82
|
+
|
83
|
+
if __name__ == '__main__':
|
84
|
+
# Make sure you set these env vars before running the example.
|
85
|
+
# CLARIFAI_PAT
|
86
|
+
# CLARIFAI_USER_ID
|
87
|
+
|
88
|
+
# You need to first create a runner in the Clarifai API and then use the ID here.
|
89
|
+
MyRunner(runner_id="matt-test-runner", base_url="http://q6:32013", num_parallel_polls=1).start()
|
@@ -0,0 +1,87 @@
|
|
1
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
2
|
+
from collections.abc import Iterator
|
3
|
+
from google.protobuf import json_format
|
4
|
+
|
5
|
+
from clarifai.client.runner import Runner
|
6
|
+
|
7
|
+
|
8
|
+
class MyRunner(Runner):
|
9
|
+
"""A custom runner that adds "Hello World" to the end of the text and replaces the domain of the
|
10
|
+
image URL as an example.
|
11
|
+
"""
|
12
|
+
|
13
|
+
def run_input(self, input: resources_pb2.Input, output_info: resources_pb2.OutputInfo,
|
14
|
+
**kwargs) -> resources_pb2.Output:
|
15
|
+
"""This is the method that will be called when the runner is run. It takes in an input and
|
16
|
+
returns an output.
|
17
|
+
"""
|
18
|
+
|
19
|
+
output = resources_pb2.Output()
|
20
|
+
|
21
|
+
data = input.data
|
22
|
+
|
23
|
+
# Optional use of output_info
|
24
|
+
params_dict = {}
|
25
|
+
if "params" in output_info:
|
26
|
+
params_dict = output_info["params"]
|
27
|
+
|
28
|
+
if data.text.raw != "":
|
29
|
+
output.data.text.raw = data.text.raw + "Hello World" + params_dict.get(
|
30
|
+
"hello", "") + kwargs.get("extra", "")
|
31
|
+
if data.image.url != "":
|
32
|
+
output.data.text.raw = data.image.url.replace("samples.clarifai.com",
|
33
|
+
"newdomain.com" + params_dict.get("domain",))
|
34
|
+
return output
|
35
|
+
|
36
|
+
def generate(self, request: service_pb2.PostModelOutputsRequest
|
37
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
38
|
+
"""Example yielding a whole batch of streamed stuff back.
|
39
|
+
"""
|
40
|
+
|
41
|
+
model = request.model
|
42
|
+
output_info = None
|
43
|
+
if request.model.model_version.id != "":
|
44
|
+
output_info = json_format.MessageToDict(
|
45
|
+
model.model_version.output_info, preserving_proto_field_name=True)
|
46
|
+
|
47
|
+
for i in range(10): # fake something iterating generating 10 times.
|
48
|
+
|
49
|
+
outputs = []
|
50
|
+
for input in request.inputs:
|
51
|
+
# output = self.run_input(input, output_info, extra=f" {i}")
|
52
|
+
output = resources_pb2.Output()
|
53
|
+
output.data.text.raw = f"Generate Hello World {i}"
|
54
|
+
outputs.append(output)
|
55
|
+
resp = service_pb2.MultiOutputResponse(outputs=outputs,)
|
56
|
+
yield resp
|
57
|
+
|
58
|
+
def stream(self, request: service_pb2.PostModelOutputsRequest
|
59
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
60
|
+
"""Example yielding a whole batch of streamed stuff back.
|
61
|
+
"""
|
62
|
+
|
63
|
+
model = request.model
|
64
|
+
output_info = None
|
65
|
+
if request.model.model_version.id != "":
|
66
|
+
output_info = json_format.MessageToDict(
|
67
|
+
model.model_version.output_info, preserving_proto_field_name=True)
|
68
|
+
|
69
|
+
for i in range(10): # fake something iterating generating 10 times.
|
70
|
+
|
71
|
+
outputs = []
|
72
|
+
for input in request.inputs:
|
73
|
+
# output = self.run_input(input, output_info, extra=f" {i}")
|
74
|
+
output = resources_pb2.Output()
|
75
|
+
output.data.text.raw = input.data.text.raw + f"Stream Hello World {i}"
|
76
|
+
outputs.append(output)
|
77
|
+
resp = service_pb2.MultiOutputResponse(outputs=outputs,)
|
78
|
+
yield resp
|
79
|
+
|
80
|
+
|
81
|
+
if __name__ == '__main__':
|
82
|
+
# Make sure you set these env vars before running the example.
|
83
|
+
# CLARIFAI_PAT
|
84
|
+
# CLARIFAI_USER_ID
|
85
|
+
|
86
|
+
# You need to first create a runner in the Clarifai API and then use the ID here.
|
87
|
+
MyRunner(runner_id="matt-test-runner", base_url="http://q6:32013", num_parallel_polls=1).start()
|