clarifai 11.0.4__py3-none-any.whl → 11.0.6rc1__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.
Files changed (154) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  3. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  4. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  5. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  6. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  7. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  8. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  9. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  10. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  11. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  12. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  13. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  14. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  15. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  16. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  17. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  18. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  19. clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
  20. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  21. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  22. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  23. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  24. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  25. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  26. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  27. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  28. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  29. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  30. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  31. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  32. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  33. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  34. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  35. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  36. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  37. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  38. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  39. clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
  40. clarifai/models/model_serving/README.md +158 -0
  41. clarifai/models/model_serving/__init__.py +14 -0
  42. clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
  43. clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
  44. clarifai/models/model_serving/cli/__init__.py +12 -0
  45. clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  46. clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
  47. clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
  48. clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
  49. clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
  50. clarifai/models/model_serving/cli/_utils.py +53 -0
  51. clarifai/models/model_serving/cli/base.py +14 -0
  52. clarifai/models/model_serving/cli/build.py +79 -0
  53. clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
  54. clarifai/models/model_serving/cli/create.py +171 -0
  55. clarifai/models/model_serving/cli/example_cli.py +34 -0
  56. clarifai/models/model_serving/cli/login.py +26 -0
  57. clarifai/models/model_serving/cli/upload.py +183 -0
  58. clarifai/models/model_serving/constants.py +21 -0
  59. clarifai/models/model_serving/docs/cli.md +161 -0
  60. clarifai/models/model_serving/docs/concepts.md +229 -0
  61. clarifai/models/model_serving/docs/dependencies.md +11 -0
  62. clarifai/models/model_serving/docs/inference_parameters.md +139 -0
  63. clarifai/models/model_serving/docs/model_types.md +19 -0
  64. clarifai/models/model_serving/model_config/__init__.py +16 -0
  65. clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
  66. clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
  67. clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
  68. clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
  69. clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
  70. clarifai/models/model_serving/model_config/base.py +369 -0
  71. clarifai/models/model_serving/model_config/config.py +312 -0
  72. clarifai/models/model_serving/model_config/inference_parameter.py +129 -0
  73. clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -0
  74. clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -0
  75. clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -0
  76. clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -0
  77. clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -0
  78. clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -0
  79. clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -0
  80. clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -0
  81. clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -0
  82. clarifai/models/model_serving/model_config/output.py +133 -0
  83. clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
  84. clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
  85. clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
  86. clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
  87. clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
  88. clarifai/models/model_serving/model_config/triton/serializer.py +136 -0
  89. clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
  90. clarifai/models/model_serving/model_config/triton/wrappers.py +281 -0
  91. clarifai/models/model_serving/repo_build/__init__.py +14 -0
  92. clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
  93. clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
  94. clarifai/models/model_serving/repo_build/build.py +198 -0
  95. clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
  96. clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
  97. clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
  98. clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
  99. clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
  100. clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
  101. clarifai/models/model_serving/repo_build/static_files/triton/model.py +75 -0
  102. clarifai/models/model_serving/utils.py +31 -0
  103. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  104. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  105. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  106. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  107. clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
  108. clarifai/runners/deepgram_live_transcribe.py +98 -0
  109. clarifai/runners/deepgram_live_transcribe.py~ +98 -0
  110. clarifai/runners/deepgram_runner.py +131 -0
  111. clarifai/runners/deepgram_runner.py~ +130 -0
  112. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
  113. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +79 -0
  114. clarifai/runners/dockerfile_template/Dockerfile.template +54 -29
  115. clarifai/runners/example_llama2.py~ +72 -0
  116. clarifai/runners/matt_example.py +89 -0
  117. clarifai/runners/matt_example.py~ +87 -0
  118. clarifai/runners/matt_llm_example.py +129 -0
  119. clarifai/runners/matt_llm_example.py~ +128 -0
  120. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  121. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  122. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  123. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  124. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  125. clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
  126. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  127. clarifai/runners/models/model_upload.py +9 -4
  128. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  129. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  130. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  131. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  132. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  133. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  134. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  135. clarifai/runners/utils/const.py +7 -6
  136. clarifai/runners/utils/logging.py +6 -0
  137. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  138. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  139. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  140. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  141. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  142. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  143. clarifai/utils/logging.py +1 -1
  144. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  145. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  146. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  147. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  148. {clarifai-11.0.4.dist-info → clarifai-11.0.6rc1.dist-info}/METADATA +16 -27
  149. clarifai-11.0.6rc1.dist-info/RECORD +242 -0
  150. {clarifai-11.0.4.dist-info → clarifai-11.0.6rc1.dist-info}/WHEEL +1 -1
  151. clarifai-11.0.4.dist-info/RECORD +0 -100
  152. {clarifai-11.0.4.dist-info → clarifai-11.0.6rc1.dist-info}/LICENSE +0 -0
  153. {clarifai-11.0.4.dist-info → clarifai-11.0.6rc1.dist-info}/entry_points.txt +0 -0
  154. {clarifai-11.0.4.dist-info → clarifai-11.0.6rc1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,26 @@
1
+ # User model inference script.
2
+
3
+ import os
4
+ from pathlib import Path
5
+ from typing import Dict, Union
6
+ from clarifai.models.model_serving.model_config import * # noqa
7
+
8
+
9
+ class InferenceModel():
10
+ """User model inference class."""
11
+
12
+ def __init__(self) -> None:
13
+ """
14
+ Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
15
+ in this method so they are loaded only once for faster inference.
16
+ """
17
+ # current directory
18
+ self.base_path: Path = os.path.dirname(__file__)
19
+
20
+ def predict(self,
21
+ input_data: list,
22
+ inference_parameters: Dict[str, Union[bool, str, float, int]] = {}) -> list:
23
+ """predict_docstring
24
+ """
25
+
26
+ raise NotImplementedError()
@@ -0,0 +1,25 @@
1
+ # Sample config of inference_parameters and labels
2
+ # For detail, please refer to docs
3
+ # --------------------
4
+ # inference_parameters:
5
+ # - path: boolean_var
6
+ # default_value: true
7
+ # field_type: 1
8
+ # description: a boolean variable
9
+ # - path: string_var
10
+ # default_value: "a string"
11
+ # field_type: 2
12
+ # description: a string variable
13
+ # - path: number_var
14
+ # default_value: 1
15
+ # field_type: 3
16
+ # description: a number variable
17
+ # - path: secret_string_var
18
+ # default_value: "YOUR_SECRET"
19
+ # field_type: 21
20
+ # description: a string variable contains secret like API key
21
+ # labels:
22
+ # - concept1
23
+ # - concept2
24
+ # - concept3
25
+ # - concept4
@@ -0,0 +1,40 @@
1
+ import unittest
2
+
3
+ from clarifai.models.model_serving.repo_build import BaseTest
4
+
5
+
6
+ class CustomTest(unittest.TestCase):
7
+ """
8
+ BaseTest loads the InferenceModel from the inference.py file in the current working directory.
9
+ To execute the predict method of the InferenceModel, use the predict method in BaseTest.
10
+ It takes the exact same inputs and inference parameters, returning the same outputs as InferenceModel.predict.
11
+ The difference is that BaseTest.predict verifies your_infer_parameters against config.clarifai_models.inference_parameters and checks the output values.
12
+
13
+ For example, test input value of visual-classifier
14
+
15
+ def test_input(self):
16
+ import cv2
17
+ path = "path/to/image"
18
+ img = cv2.imread(path)
19
+ outputs = self.model.predict([img], infer_param1=..., infer_param2=...)
20
+ print(outputs)
21
+ assert outputs
22
+
23
+ """
24
+
25
+ def setUp(self) -> None:
26
+ your_infer_parameter = dict(
27
+ ) # for example dict(float_var=0.12, string_var="test", _secret_string_var="secret")
28
+ self.model = BaseTest(your_infer_parameter)
29
+
30
+ def test_default_cases(self):
31
+ """Test your model with dummy inputs.
32
+ In general, you only need to run this test to check your InferneceModel implementation.
33
+ In case the default inputs makes your model failed for some reason (not because of assert in `test_with_default_inputs`),
34
+ you can comment out this test.
35
+ """
36
+ self.model.test_with_default_inputs()
37
+
38
+ def test_specific_case1(self):
39
+ """ Implement your test case"""
40
+ pass
@@ -0,0 +1,75 @@
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
+ """Triton inference server Python Backend Model."""
14
+
15
+ import os
16
+ import sys
17
+
18
+ try:
19
+ import triton_python_backend_utils as pb_utils
20
+ except ModuleNotFoundError:
21
+ pass
22
+ from clarifai.models.model_serving.model_config.inference_parameter import parse_req_parameters
23
+
24
+
25
+ class TritonPythonModel:
26
+ """
27
+ Triton Python BE Model.
28
+ """
29
+
30
+ def initialize(self, args):
31
+ """
32
+ Triton server init.
33
+ """
34
+ sys.path.append(os.path.dirname(__file__))
35
+ from inference import InferenceModel
36
+
37
+ self.inference_obj = InferenceModel()
38
+
39
+ # Read input_name from config file
40
+ self.input_names = [inp.name for inp in self.inference_obj.config.serving_backend.triton.input]
41
+
42
+ def execute(self, requests):
43
+ """
44
+ Serve model inference requests.
45
+ """
46
+ responses = []
47
+
48
+ for request in requests:
49
+ try:
50
+ parameters = request.parameters()
51
+ except Exception:
52
+ print(
53
+ "It seems this triton version does not support `parameters()` in request. "
54
+ "Please upgrade tritonserver version otherwise can not use `inference_parameters`. Error message: {e}"
55
+ )
56
+ parameters = None
57
+
58
+ parameters = parse_req_parameters(parameters) if parameters else {}
59
+
60
+ if len(self.input_names) == 1:
61
+ in_batch = pb_utils.get_input_tensor_by_name(request, self.input_names[0])
62
+ in_batch = in_batch.as_numpy()
63
+ data = in_batch
64
+ else:
65
+ data = {}
66
+ for input_name in self.input_names:
67
+ in_batch = pb_utils.get_input_tensor_by_name(request, input_name)
68
+ in_batch = in_batch.as_numpy() if in_batch is not None else []
69
+ data.update({input_name: in_batch})
70
+
71
+ inference_response = self.inference_obj._tritonserver_predict(
72
+ input_data=data, inference_parameters=parameters)
73
+ responses.append(inference_response)
74
+
75
+ return responses
@@ -0,0 +1,31 @@
1
+ import os
2
+
3
+ from clarifai.models.model_serving.constants import CLARIFAI_PAT_PATH
4
+ from clarifai.utils.constants import CLARIFAI_PAT_ENV_VAR
5
+
6
+
7
+ def _persist_pat(pat: str):
8
+ """ Write down pat to CLARIFAI_PAT_PATH """
9
+ with open(CLARIFAI_PAT_PATH, "w") as f:
10
+ f.write(pat)
11
+
12
+
13
+ def _read_pat():
14
+ if not os.path.exists(CLARIFAI_PAT_PATH) and not os.environ.get(CLARIFAI_PAT_ENV_VAR, ""):
15
+ return None
16
+ if os.path.exists(CLARIFAI_PAT_PATH):
17
+ with open(CLARIFAI_PAT_PATH, "r") as f:
18
+ return f.read().replace("\n", "").replace("\r", "").strip()
19
+ elif os.environ.get(CLARIFAI_PAT_ENV_VAR):
20
+ return os.environ.get(CLARIFAI_PAT_ENV_VAR)
21
+ else:
22
+ raise ValueError(
23
+ f"PAT not found, please run `clarifai login` to persist your PAT or set it as an environment variable under the name '{CLARIFAI_PAT_ENV_VAR}'"
24
+ )
25
+
26
+
27
+ def login(pat=None):
28
+ """ if pat provided, set pat to CLARIFAI_PAT otherwise read pat from file"""
29
+ pat = pat or _read_pat()
30
+ assert pat, Exception("PAT is not found, please run `clarifai login` to persist your PAT")
31
+ os.environ["CLARIFAI_PAT"] = pat
@@ -0,0 +1,98 @@
1
+ import os
2
+ import httpx
3
+ from dotenv import load_dotenv
4
+ import threading
5
+
6
+ from deepgram import (
7
+ DeepgramClient,
8
+ LiveTranscriptionEvents,
9
+ LiveOptions,
10
+ )
11
+
12
+ load_dotenv()
13
+
14
+ # URL for the realtime streaming audio you would like to transcribe
15
+ URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
16
+
17
+ os.environ["DG_API_KEY"] = "1d5666523f0f2fbaf3e2db6ae7717f6f87280b5e"
18
+ API_KEY = os.getenv("DG_API_KEY")
19
+
20
+
21
+ def main():
22
+ try:
23
+ # STEP 1: Create a Deepgram client using the API key
24
+ deepgram = DeepgramClient(API_KEY)
25
+
26
+ # STEP 2: Create a websocket connection to Deepgram
27
+ dg_connection = deepgram.listen.live.v("1")
28
+
29
+ # STEP 3: Define the event handlers for the connection
30
+ def on_message(self, result, **kwargs):
31
+ sentence = result.channel.alternatives[0].transcript
32
+ if len(sentence) == 0:
33
+ return
34
+ print(f"speaker: {sentence}")
35
+
36
+ def on_metadata(self, metadata, **kwargs):
37
+ print(f"\n\n{metadata}\n\n")
38
+
39
+ def on_error(self, error, **kwargs):
40
+ print(f"\n\n{error}\n\n")
41
+
42
+ # STEP 4: Register the event handlers
43
+ dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
44
+ dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
45
+ dg_connection.on(LiveTranscriptionEvents.Error, on_error)
46
+
47
+ # STEP 5: Configure Deepgram options for live transcription
48
+ options = LiveOptions(
49
+ model="nova-2",
50
+ language="en-US",
51
+ smart_format=True,
52
+ )
53
+
54
+ # STEP 6: Start the connection
55
+ dg_connection.start(options)
56
+
57
+ # STEP 7: Create a lock and a flag for thread synchronization
58
+ lock_exit = threading.Lock()
59
+ exit = False
60
+
61
+ # STEP 8: Define a thread that streams the audio and sends it to Deepgram
62
+ def myThread():
63
+ with httpx.stream("GET", URL) as r:
64
+ for data in r.iter_bytes():
65
+ lock_exit.acquire()
66
+ if exit:
67
+ break
68
+ lock_exit.release()
69
+
70
+ dg_connection.send(data)
71
+
72
+ # STEP 9: Start the thread
73
+ myHttp = threading.Thread(target=myThread)
74
+ myHttp.start()
75
+
76
+ # STEP 10: Wait for user input to stop recording
77
+ input("Press Enter to stop recording...\n\n")
78
+
79
+ # STEP 11: Set the exit flag to True to stop the thread
80
+ lock_exit.acquire()
81
+ exit = True
82
+ lock_exit.release()
83
+
84
+ # STEP 12: Wait for the thread to finish
85
+ myHttp.join()
86
+
87
+ # STEP 13: Close the connection to Deepgram
88
+ dg_connection.finish()
89
+
90
+ print("Finished")
91
+
92
+ except Exception as e:
93
+ print(f"Could not open socket: {e}")
94
+ return
95
+
96
+
97
+ if __name__ == "__main__":
98
+ main()
@@ -0,0 +1,98 @@
1
+ import os
2
+ import httpx
3
+ from dotenv import load_dotenv
4
+ import threading
5
+
6
+ from deepgram import (
7
+ DeepgramClient,
8
+ LiveTranscriptionEvents,
9
+ LiveOptions,
10
+ )
11
+
12
+ load_dotenv()
13
+
14
+ # URL for the realtime streaming audio you would like to transcribe
15
+ URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
16
+
17
+ os.environ.set("DG_API_KEY", "1d5666523f0f2fbaf3e2db6ae7717f6f87280b5e")
18
+ API_KEY = os.getenv("DG_API_KEY")
19
+
20
+
21
+ def main():
22
+ try:
23
+ # STEP 1: Create a Deepgram client using the API key
24
+ deepgram = DeepgramClient(API_KEY)
25
+
26
+ # STEP 2: Create a websocket connection to Deepgram
27
+ dg_connection = deepgram.listen.live.v("1")
28
+
29
+ # STEP 3: Define the event handlers for the connection
30
+ def on_message(self, result, **kwargs):
31
+ sentence = result.channel.alternatives[0].transcript
32
+ if len(sentence) == 0:
33
+ return
34
+ print(f"speaker: {sentence}")
35
+
36
+ def on_metadata(self, metadata, **kwargs):
37
+ print(f"\n\n{metadata}\n\n")
38
+
39
+ def on_error(self, error, **kwargs):
40
+ print(f"\n\n{error}\n\n")
41
+
42
+ # STEP 4: Register the event handlers
43
+ dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
44
+ dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
45
+ dg_connection.on(LiveTranscriptionEvents.Error, on_error)
46
+
47
+ # STEP 5: Configure Deepgram options for live transcription
48
+ options = LiveOptions(
49
+ model="nova-2",
50
+ language="en-US",
51
+ smart_format=True,
52
+ )
53
+
54
+ # STEP 6: Start the connection
55
+ dg_connection.start(options)
56
+
57
+ # STEP 7: Create a lock and a flag for thread synchronization
58
+ lock_exit = threading.Lock()
59
+ exit = False
60
+
61
+ # STEP 8: Define a thread that streams the audio and sends it to Deepgram
62
+ def myThread():
63
+ with httpx.stream("GET", URL) as r:
64
+ for data in r.iter_bytes():
65
+ lock_exit.acquire()
66
+ if exit:
67
+ break
68
+ lock_exit.release()
69
+
70
+ dg_connection.send(data)
71
+
72
+ # STEP 9: Start the thread
73
+ myHttp = threading.Thread(target=myThread)
74
+ myHttp.start()
75
+
76
+ # STEP 10: Wait for user input to stop recording
77
+ input("Press Enter to stop recording...\n\n")
78
+
79
+ # STEP 11: Set the exit flag to True to stop the thread
80
+ lock_exit.acquire()
81
+ exit = True
82
+ lock_exit.release()
83
+
84
+ # STEP 12: Wait for the thread to finish
85
+ myHttp.join()
86
+
87
+ # STEP 13: Close the connection to Deepgram
88
+ dg_connection.finish()
89
+
90
+ print("Finished")
91
+
92
+ except Exception as e:
93
+ print(f"Could not open socket: {e}")
94
+ return
95
+
96
+
97
+ if __name__ == "__main__":
98
+ main()
@@ -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()