opennvr-adapter-sdk 1.0.0__tar.gz
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.
- opennvr_adapter_sdk-1.0.0/CHANGELOG.md +50 -0
- opennvr_adapter_sdk-1.0.0/LICENSE +201 -0
- opennvr_adapter_sdk-1.0.0/MANIFEST.in +30 -0
- opennvr_adapter_sdk-1.0.0/PKG-INFO +209 -0
- opennvr_adapter_sdk-1.0.0/README.md +169 -0
- opennvr_adapter_sdk-1.0.0/RELEASING.md +146 -0
- opennvr_adapter_sdk-1.0.0/__init__.py +102 -0
- opennvr_adapter_sdk-1.0.0/adapter_app.py +624 -0
- opennvr_adapter_sdk-1.0.0/auth.py +154 -0
- opennvr_adapter_sdk-1.0.0/contract.py +719 -0
- opennvr_adapter_sdk-1.0.0/metrics.py +144 -0
- opennvr_adapter_sdk-1.0.0/opennvr_adapter_sdk.egg-info/SOURCES.txt +22 -0
- opennvr_adapter_sdk-1.0.0/pyproject.toml +98 -0
- opennvr_adapter_sdk-1.0.0/service.py +214 -0
- opennvr_adapter_sdk-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `opennvr-adapter-sdk` are documented here.
|
|
4
|
+
Format loosely follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/);
|
|
5
|
+
the SDK uses semantic versioning aligned with the AI Adapter Contract major version (SDK v1.x targets contract v1).
|
|
6
|
+
|
|
7
|
+
## [1.0.0] — 2026-05
|
|
8
|
+
|
|
9
|
+
First public release. Extracted from the three reference adapters
|
|
10
|
+
(Piper TTS, YOLOv8 object detection, Whisper ASR) shipped in
|
|
11
|
+
`open-nvr/ai-adapter`. All §3 endpoints, §5 result conventions,
|
|
12
|
+
§6 WebSocket streaming protocol, §7 failure envelope, and §11.3
|
|
13
|
+
fingerprint drift detection are exercised by the three references.
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
|
|
17
|
+
- `AdapterService` ABC with four required abstract methods
|
|
18
|
+
(`load`, `is_ready`, `fingerprint`, `model_info`,
|
|
19
|
+
`hardware_evaluation`, `infer`) plus optional `handle_stream`
|
|
20
|
+
for streaming adapters.
|
|
21
|
+
- `AdapterApp` builder that wraps an `AdapterService` in a
|
|
22
|
+
FastAPI app implementing all six mandatory contract endpoints
|
|
23
|
+
(`/health`, `/capabilities`, `/hardware/evaluation`,
|
|
24
|
+
`/metrics`, `/infer`, `/infer/stream`), plus auth +
|
|
25
|
+
correlation_id middleware, Prometheus metrics, lifespan
|
|
26
|
+
startup, and body parsing for `BodyShape.{TEXT, IMAGE, AUDIO, GENERIC}`.
|
|
27
|
+
- `ServiceError` exception that translates to the §7 failure
|
|
28
|
+
envelope.
|
|
29
|
+
- `BODY_BYTES_KEY` constant for the binary body payload key;
|
|
30
|
+
caller-supplied params that shadow it are rejected with
|
|
31
|
+
`malformed_input` rather than being silently overwritten.
|
|
32
|
+
- `opennvr_adapter_sdk.contract` submodule with every Pydantic
|
|
33
|
+
wire type the contract defines; commonly-used types are
|
|
34
|
+
re-exported at the package root.
|
|
35
|
+
- Oversize bodies (`max_body_bytes`) return HTTP 413, not 400.
|
|
36
|
+
- Streaming adapters automatically get `inc/dec_stream_connection`
|
|
37
|
+
metrics; per-frame metrics via `self.metrics`.
|
|
38
|
+
- `service_factory` constructor parameter for lazy service
|
|
39
|
+
construction at lifespan startup — useful for test fixtures
|
|
40
|
+
that monkey-patch `__init__`.
|
|
41
|
+
|
|
42
|
+
### Notes
|
|
43
|
+
|
|
44
|
+
- Apache-2.0 licensed so third parties can write closed-source
|
|
45
|
+
production adapters. The reference adapters and the ai-adapter
|
|
46
|
+
app are AGPL-3.0; the SDK boundary stays permissive.
|
|
47
|
+
- Pinned to FastAPI ≥0.115 / Pydantic ≥2.7 / Python ≥3.10. Loosen
|
|
48
|
+
the upper bounds in your fork if you need wider compatibility.
|
|
49
|
+
|
|
50
|
+
[1.0.0]: https://github.com/open-nvr/ai-adapter/releases/tag/sdk-v1.0.0
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may accept and charge a
|
|
167
|
+
fee for, or grant indemnities for, additional or similar liability
|
|
168
|
+
obligations and/or rights consistent with this License. However,
|
|
169
|
+
in accepting such obligations, You may act only on Your own behalf
|
|
170
|
+
and on Your sole responsibility, not on behalf of any other
|
|
171
|
+
Contributor, and only if You agree to indemnify, defend, and hold
|
|
172
|
+
each Contributor harmless for any liability incurred by, or claims
|
|
173
|
+
asserted against, such Contributor by reason of your accepting any
|
|
174
|
+
such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 OpenNVR
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# sdist-only inclusions. The wheel picks these up via
|
|
2
|
+
# [tool.setuptools.package-data] in pyproject.toml; setuptools' default
|
|
3
|
+
# sdist heuristics skip non-Python files at the package root unless we
|
|
4
|
+
# spell them out here.
|
|
5
|
+
#
|
|
6
|
+
# Why every file is listed explicitly:
|
|
7
|
+
# - README.md is referenced via ``readme = "README.md"`` in
|
|
8
|
+
# pyproject.toml and setuptools auto-includes it. Listing it again
|
|
9
|
+
# here makes sdist contents independent of that backend behavior.
|
|
10
|
+
# - LICENSE is NOT auto-included by ``license = { text = "Apache-2.0" }``
|
|
11
|
+
# in modern setuptools — that key only sets metadata, it doesn't
|
|
12
|
+
# copy a file. (PEP 639 ``license-files = ["LICENSE"]`` would, but
|
|
13
|
+
# we're not on that yet.) So LICENSE ships ONLY because it's
|
|
14
|
+
# listed here (sdist) and in package-data (wheel).
|
|
15
|
+
# - CHANGELOG.md and RELEASING.md have no auto-include path; listed
|
|
16
|
+
# here is the only way they make it into the sdist.
|
|
17
|
+
#
|
|
18
|
+
# If you remove any of these lines, ``twine check`` will still pass
|
|
19
|
+
# but ``tar tzf dist/*.tar.gz`` will be silently missing files.
|
|
20
|
+
include LICENSE
|
|
21
|
+
include README.md
|
|
22
|
+
include CHANGELOG.md
|
|
23
|
+
include RELEASING.md
|
|
24
|
+
|
|
25
|
+
# Belt-and-suspenders: keep build leftovers and old sdist extractions
|
|
26
|
+
# out of the next sdist build.
|
|
27
|
+
prune dist
|
|
28
|
+
prune build
|
|
29
|
+
prune *.egg-info
|
|
30
|
+
prune opennvr_adapter_sdk-*
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opennvr-adapter-sdk
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Boilerplate-free SDK for writing AI Adapter Contract v1 services for OpenNVR.
|
|
5
|
+
Author-email: OpenNVR <contact@open-nvr.org>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/open-nvr/ai-adapter
|
|
8
|
+
Project-URL: Documentation, https://github.com/open-nvr/ai-adapter/tree/main/opennvr_adapter_sdk
|
|
9
|
+
Project-URL: Repository, https://github.com/open-nvr/ai-adapter
|
|
10
|
+
Project-URL: Issues, https://github.com/open-nvr/ai-adapter/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/open-nvr/ai-adapter/blob/main/opennvr_adapter_sdk/CHANGELOG.md
|
|
12
|
+
Keywords: ai,inference,adapter,sdk,fastapi,websocket,computer-vision,nvr,object-detection,asr,tts
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: FastAPI
|
|
15
|
+
Classifier: Framework :: Pydantic :: 2
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Topic :: Multimedia :: Video
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: fastapi<1.0,>=0.115
|
|
31
|
+
Requires-Dist: pydantic<3.0,>=2.7
|
|
32
|
+
Requires-Dist: python-multipart>=0.0.18
|
|
33
|
+
Provides-Extra: serve
|
|
34
|
+
Requires-Dist: uvicorn[standard]<1.0,>=0.30; extra == "serve"
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
37
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
38
|
+
Requires-Dist: httpx>=0.27; extra == "dev"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# opennvr-adapter-sdk
|
|
42
|
+
|
|
43
|
+
The boilerplate-free way to write an [AI Adapter Contract v1](https://github.com/open-nvr/open-nvr/blob/main/docs/AI_ADAPTER_CONTRACT.md) service.
|
|
44
|
+
|
|
45
|
+
A minimal adapter is around thirty lines of FastAPI. The SDK gives you `AdapterService` — the ABC every adapter implements with four abstract methods — and `AdapterApp`, the builder that wraps your service in a FastAPI app with the six mandatory endpoints, auth, correlation-ID propagation, Prometheus metrics, and multipart plus JSON body parsing. `ServiceError` is the typed failure envelope from §7 of the contract. Every contract Pydantic type is re-exported from the package root, so adapter authors get one import line.
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
uv add opennvr-adapter-sdk
|
|
51
|
+
# with the uvicorn server bundled too:
|
|
52
|
+
uv add 'opennvr-adapter-sdk[serve]'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
If you're not on [uv](https://docs.astral.sh/uv/) yet, `pip install opennvr-adapter-sdk` works the same — the package is a single wheel with FastAPI, Pydantic, and python-multipart as its only runtime dependencies. We recommend `uv` because adapter projects tend to grow heavy ML deps fast, and `uv sync` keeps resolve time and the lockfile manageable as they do.
|
|
56
|
+
|
|
57
|
+
## The minimum viable adapter
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
# my_adapter/main.py
|
|
61
|
+
from datetime import datetime, timezone
|
|
62
|
+
|
|
63
|
+
from opennvr_adapter_sdk import (
|
|
64
|
+
AdapterApp, AdapterService, BodyShape, ErrorCategory,
|
|
65
|
+
HardwareEvaluationResponse, HardwareVerdict, InferResponse,
|
|
66
|
+
ModelInfo, ServiceError,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
class MyService(AdapterService):
|
|
70
|
+
def __init__(self):
|
|
71
|
+
self._ready = False
|
|
72
|
+
|
|
73
|
+
def load(self):
|
|
74
|
+
# Heavy lifting goes here.
|
|
75
|
+
self._ready = True
|
|
76
|
+
|
|
77
|
+
def is_ready(self): return self._ready
|
|
78
|
+
|
|
79
|
+
def fingerprint(self):
|
|
80
|
+
return "sha256:..."
|
|
81
|
+
|
|
82
|
+
def model_info(self):
|
|
83
|
+
return ModelInfo(
|
|
84
|
+
name="my-model", version="1.0",
|
|
85
|
+
framework="numpy", modalities_in=["text"],
|
|
86
|
+
modalities_out=["text"], fingerprint=self.fingerprint(),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
def hardware_evaluation(self):
|
|
90
|
+
return HardwareEvaluationResponse(
|
|
91
|
+
verdict=HardwareVerdict.OK, reasoning="ready",
|
|
92
|
+
checked_at=datetime.now(timezone.utc), details={},
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def infer(self, payload):
|
|
96
|
+
if "text" not in payload:
|
|
97
|
+
raise ServiceError(
|
|
98
|
+
ErrorCategory.TRANSPORT_ERROR, code="malformed_input",
|
|
99
|
+
message="'text' required", transient=False, http_status=400,
|
|
100
|
+
)
|
|
101
|
+
return InferResponse(
|
|
102
|
+
model_name="my-model", model_version="1.0",
|
|
103
|
+
inference_ms=1, result={"echoed": payload["text"]},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
app = AdapterApp(
|
|
107
|
+
service=MyService(),
|
|
108
|
+
name="my-adapter", version="1.0.0",
|
|
109
|
+
vendor="me", license="MIT",
|
|
110
|
+
tasks_advertised=["echo"],
|
|
111
|
+
body_shape=BodyShape.TEXT,
|
|
112
|
+
).fastapi_app
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Run it:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
OPENNVR_ADAPTER_TOKEN=dev-token \
|
|
119
|
+
uvicorn my_adapter.main:app --host 0.0.0.0 --port 9001
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Verify conformance:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
python -m conformance http://localhost:9001 --token dev-token
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
That's a complete contract-compliant adapter. The SDK handles `/health`, `/capabilities`, `/hardware/evaluation`, `/metrics`, `/infer`, `/infer/stream` (HTTP 501 refusal), auth, correlation_id, multipart + JSON body parsing, Prometheus metrics, lifespan startup. You write only the model wrapper.
|
|
129
|
+
|
|
130
|
+
## Body shapes
|
|
131
|
+
|
|
132
|
+
| Shape | Wire | Use for |
|
|
133
|
+
|---|---|---|
|
|
134
|
+
| `BodyShape.TEXT` | JSON + multipart (text-only fields) | TTS, LLM chat, any text-only adapter |
|
|
135
|
+
| `BodyShape.IMAGE` | multipart `frame` file + JSON `frame_b64` | Vision detection, classification, OCR |
|
|
136
|
+
| `BodyShape.AUDIO` | multipart `audio` file + JSON `audio_b64` | ASR, audio classification, TTS post-process |
|
|
137
|
+
| `BodyShape.GENERIC` | multipart `data` file + JSON `data_b64` | Anything else with binary input |
|
|
138
|
+
|
|
139
|
+
For non-TEXT shapes, the SDK puts the binary content at `payload[BODY_BYTES_KEY]` (bytes) and merges any `params` JSON into the dict. `BODY_BYTES_KEY` is re-exported from the SDK root — import it rather than hard-coding the literal so future renames don't silently break adapters. Caller-supplied params that shadow this key are rejected with `malformed_input` so collisions surface at the wire, not as silently-overwritten values.
|
|
140
|
+
|
|
141
|
+
## Streaming adapters
|
|
142
|
+
|
|
143
|
+
Add `supports_stream=True` to `AdapterApp(...)` and override `AdapterService.handle_stream(websocket)`:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
class MyDetector(AdapterService):
|
|
147
|
+
async def handle_stream(self, websocket):
|
|
148
|
+
await websocket.accept()
|
|
149
|
+
# ... §6 protocol ...
|
|
150
|
+
|
|
151
|
+
app = AdapterApp(
|
|
152
|
+
service=MyDetector(),
|
|
153
|
+
...
|
|
154
|
+
supports_stream=True,
|
|
155
|
+
stream_max_concurrent=16,
|
|
156
|
+
stream_supports_shared_memory=False,
|
|
157
|
+
).fastapi_app
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
The SDK handles auth on the WebSocket upgrade (§6.5 close code 4001 on auth failure) and delegates to your handler. The §6 protocol itself — handshake → frame_meta + binary → result_message — is the adapter's responsibility (YOLOv8 has the reference implementation under `adapters/yolov8/`).
|
|
161
|
+
|
|
162
|
+
## Constructor reference
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
AdapterApp(
|
|
166
|
+
# Service — exactly one required:
|
|
167
|
+
service: AdapterService | None, # eager construction
|
|
168
|
+
service_factory: Callable[[], AdapterService] | None, # lazy (lifespan startup)
|
|
169
|
+
|
|
170
|
+
# Adapter identity (required):
|
|
171
|
+
name: str, version: str,
|
|
172
|
+
vendor: str, license: str,
|
|
173
|
+
tasks_advertised: Sequence[str],
|
|
174
|
+
|
|
175
|
+
# Body shape + size cap:
|
|
176
|
+
body_shape: BodyShape = BodyShape.TEXT,
|
|
177
|
+
max_body_bytes: int = 32 * 1024 * 1024,
|
|
178
|
+
|
|
179
|
+
# Capabilities metadata (optional, with defaults):
|
|
180
|
+
permissions: Permissions = Permissions(),
|
|
181
|
+
scheduling: Scheduling = Scheduling(), # default max_inflight=1
|
|
182
|
+
cost: Cost = Cost(),
|
|
183
|
+
model_card_url: str | None = None,
|
|
184
|
+
supported_contract_versions: Sequence[str] = ("1",),
|
|
185
|
+
|
|
186
|
+
# Streaming (default off):
|
|
187
|
+
supports_stream: bool = False,
|
|
188
|
+
stream_max_concurrent: int = 0,
|
|
189
|
+
stream_supports_shared_memory: bool = False,
|
|
190
|
+
|
|
191
|
+
# Tuning:
|
|
192
|
+
latency_buckets_seconds: tuple[float, ...] = (...), # Prometheus buckets
|
|
193
|
+
cors_origins: Sequence[str] = ("*",),
|
|
194
|
+
)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Real-world examples
|
|
198
|
+
|
|
199
|
+
`opennvr-adapter-sdk` is the production runtime for the eight adapters shipped in this repo: `adapters/yolov8/` for object detection (`BodyShape.IMAGE` with WebSocket streaming); `adapters/piper/` for text-to-speech (`BodyShape.TEXT` with a custom `/voices` route and inline-audio response); `adapters/whisper/` for speech-to-text (`BodyShape.AUDIO`, multipart decode); `adapters/fast_plate_ocr/` for license-plate text recognition on a pre-cropped plate image (`BodyShape.IMAGE`, designed to chain downstream of YOLOv8); `adapters/insightface/` for face detection plus recognition with a REST face DB; `adapters/blip/` for scene captioning, used by the OpenNVR camera-agent; `adapters/vlm/` for open-vocabulary detection (OWL-ViT v2, detects free-text queries like "red truck"); and `adapters/bytetrack/` for stateful multi-object tracking as a post-processor over an upstream detector's results. Their `main.py` files are non-trivial reference implementations worth reading before authoring your own.
|
|
200
|
+
|
|
201
|
+
## Versioning
|
|
202
|
+
|
|
203
|
+
SDK ships with the same major version as the contract. SDK v1.x targets contract v1; a future contract v2 ships SDK v2.x. `AdapterApp.supported_contract_versions` defaults to `["1"]` — bump when you implement multi-version support.
|
|
204
|
+
|
|
205
|
+
## Why this isn't a "framework"
|
|
206
|
+
|
|
207
|
+
`AdapterService` is an ABC, not a metaclass. `AdapterApp` is a builder, not a base class. The SDK lives between you and FastAPI — your service code never imports FastAPI directly, but you can still drop down to `app.add_route(...)` for adapter-specific endpoints (see Piper's `/voices` route).
|
|
208
|
+
|
|
209
|
+
The contract is the source of truth. The SDK is a convenience layer on top of it. If the SDK gets in your way, write the service hand-rolled the way the early reference adapters did before the SDK was extracted — the contract is implementable without it, just more boilerplate.
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# opennvr-adapter-sdk
|
|
2
|
+
|
|
3
|
+
The boilerplate-free way to write an [AI Adapter Contract v1](https://github.com/open-nvr/open-nvr/blob/main/docs/AI_ADAPTER_CONTRACT.md) service.
|
|
4
|
+
|
|
5
|
+
A minimal adapter is around thirty lines of FastAPI. The SDK gives you `AdapterService` — the ABC every adapter implements with four abstract methods — and `AdapterApp`, the builder that wraps your service in a FastAPI app with the six mandatory endpoints, auth, correlation-ID propagation, Prometheus metrics, and multipart plus JSON body parsing. `ServiceError` is the typed failure envelope from §7 of the contract. Every contract Pydantic type is re-exported from the package root, so adapter authors get one import line.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
uv add opennvr-adapter-sdk
|
|
11
|
+
# with the uvicorn server bundled too:
|
|
12
|
+
uv add 'opennvr-adapter-sdk[serve]'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
If you're not on [uv](https://docs.astral.sh/uv/) yet, `pip install opennvr-adapter-sdk` works the same — the package is a single wheel with FastAPI, Pydantic, and python-multipart as its only runtime dependencies. We recommend `uv` because adapter projects tend to grow heavy ML deps fast, and `uv sync` keeps resolve time and the lockfile manageable as they do.
|
|
16
|
+
|
|
17
|
+
## The minimum viable adapter
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
# my_adapter/main.py
|
|
21
|
+
from datetime import datetime, timezone
|
|
22
|
+
|
|
23
|
+
from opennvr_adapter_sdk import (
|
|
24
|
+
AdapterApp, AdapterService, BodyShape, ErrorCategory,
|
|
25
|
+
HardwareEvaluationResponse, HardwareVerdict, InferResponse,
|
|
26
|
+
ModelInfo, ServiceError,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
class MyService(AdapterService):
|
|
30
|
+
def __init__(self):
|
|
31
|
+
self._ready = False
|
|
32
|
+
|
|
33
|
+
def load(self):
|
|
34
|
+
# Heavy lifting goes here.
|
|
35
|
+
self._ready = True
|
|
36
|
+
|
|
37
|
+
def is_ready(self): return self._ready
|
|
38
|
+
|
|
39
|
+
def fingerprint(self):
|
|
40
|
+
return "sha256:..."
|
|
41
|
+
|
|
42
|
+
def model_info(self):
|
|
43
|
+
return ModelInfo(
|
|
44
|
+
name="my-model", version="1.0",
|
|
45
|
+
framework="numpy", modalities_in=["text"],
|
|
46
|
+
modalities_out=["text"], fingerprint=self.fingerprint(),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def hardware_evaluation(self):
|
|
50
|
+
return HardwareEvaluationResponse(
|
|
51
|
+
verdict=HardwareVerdict.OK, reasoning="ready",
|
|
52
|
+
checked_at=datetime.now(timezone.utc), details={},
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def infer(self, payload):
|
|
56
|
+
if "text" not in payload:
|
|
57
|
+
raise ServiceError(
|
|
58
|
+
ErrorCategory.TRANSPORT_ERROR, code="malformed_input",
|
|
59
|
+
message="'text' required", transient=False, http_status=400,
|
|
60
|
+
)
|
|
61
|
+
return InferResponse(
|
|
62
|
+
model_name="my-model", model_version="1.0",
|
|
63
|
+
inference_ms=1, result={"echoed": payload["text"]},
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
app = AdapterApp(
|
|
67
|
+
service=MyService(),
|
|
68
|
+
name="my-adapter", version="1.0.0",
|
|
69
|
+
vendor="me", license="MIT",
|
|
70
|
+
tasks_advertised=["echo"],
|
|
71
|
+
body_shape=BodyShape.TEXT,
|
|
72
|
+
).fastapi_app
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Run it:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
OPENNVR_ADAPTER_TOKEN=dev-token \
|
|
79
|
+
uvicorn my_adapter.main:app --host 0.0.0.0 --port 9001
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Verify conformance:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
python -m conformance http://localhost:9001 --token dev-token
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
That's a complete contract-compliant adapter. The SDK handles `/health`, `/capabilities`, `/hardware/evaluation`, `/metrics`, `/infer`, `/infer/stream` (HTTP 501 refusal), auth, correlation_id, multipart + JSON body parsing, Prometheus metrics, lifespan startup. You write only the model wrapper.
|
|
89
|
+
|
|
90
|
+
## Body shapes
|
|
91
|
+
|
|
92
|
+
| Shape | Wire | Use for |
|
|
93
|
+
|---|---|---|
|
|
94
|
+
| `BodyShape.TEXT` | JSON + multipart (text-only fields) | TTS, LLM chat, any text-only adapter |
|
|
95
|
+
| `BodyShape.IMAGE` | multipart `frame` file + JSON `frame_b64` | Vision detection, classification, OCR |
|
|
96
|
+
| `BodyShape.AUDIO` | multipart `audio` file + JSON `audio_b64` | ASR, audio classification, TTS post-process |
|
|
97
|
+
| `BodyShape.GENERIC` | multipart `data` file + JSON `data_b64` | Anything else with binary input |
|
|
98
|
+
|
|
99
|
+
For non-TEXT shapes, the SDK puts the binary content at `payload[BODY_BYTES_KEY]` (bytes) and merges any `params` JSON into the dict. `BODY_BYTES_KEY` is re-exported from the SDK root — import it rather than hard-coding the literal so future renames don't silently break adapters. Caller-supplied params that shadow this key are rejected with `malformed_input` so collisions surface at the wire, not as silently-overwritten values.
|
|
100
|
+
|
|
101
|
+
## Streaming adapters
|
|
102
|
+
|
|
103
|
+
Add `supports_stream=True` to `AdapterApp(...)` and override `AdapterService.handle_stream(websocket)`:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
class MyDetector(AdapterService):
|
|
107
|
+
async def handle_stream(self, websocket):
|
|
108
|
+
await websocket.accept()
|
|
109
|
+
# ... §6 protocol ...
|
|
110
|
+
|
|
111
|
+
app = AdapterApp(
|
|
112
|
+
service=MyDetector(),
|
|
113
|
+
...
|
|
114
|
+
supports_stream=True,
|
|
115
|
+
stream_max_concurrent=16,
|
|
116
|
+
stream_supports_shared_memory=False,
|
|
117
|
+
).fastapi_app
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The SDK handles auth on the WebSocket upgrade (§6.5 close code 4001 on auth failure) and delegates to your handler. The §6 protocol itself — handshake → frame_meta + binary → result_message — is the adapter's responsibility (YOLOv8 has the reference implementation under `adapters/yolov8/`).
|
|
121
|
+
|
|
122
|
+
## Constructor reference
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
AdapterApp(
|
|
126
|
+
# Service — exactly one required:
|
|
127
|
+
service: AdapterService | None, # eager construction
|
|
128
|
+
service_factory: Callable[[], AdapterService] | None, # lazy (lifespan startup)
|
|
129
|
+
|
|
130
|
+
# Adapter identity (required):
|
|
131
|
+
name: str, version: str,
|
|
132
|
+
vendor: str, license: str,
|
|
133
|
+
tasks_advertised: Sequence[str],
|
|
134
|
+
|
|
135
|
+
# Body shape + size cap:
|
|
136
|
+
body_shape: BodyShape = BodyShape.TEXT,
|
|
137
|
+
max_body_bytes: int = 32 * 1024 * 1024,
|
|
138
|
+
|
|
139
|
+
# Capabilities metadata (optional, with defaults):
|
|
140
|
+
permissions: Permissions = Permissions(),
|
|
141
|
+
scheduling: Scheduling = Scheduling(), # default max_inflight=1
|
|
142
|
+
cost: Cost = Cost(),
|
|
143
|
+
model_card_url: str | None = None,
|
|
144
|
+
supported_contract_versions: Sequence[str] = ("1",),
|
|
145
|
+
|
|
146
|
+
# Streaming (default off):
|
|
147
|
+
supports_stream: bool = False,
|
|
148
|
+
stream_max_concurrent: int = 0,
|
|
149
|
+
stream_supports_shared_memory: bool = False,
|
|
150
|
+
|
|
151
|
+
# Tuning:
|
|
152
|
+
latency_buckets_seconds: tuple[float, ...] = (...), # Prometheus buckets
|
|
153
|
+
cors_origins: Sequence[str] = ("*",),
|
|
154
|
+
)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Real-world examples
|
|
158
|
+
|
|
159
|
+
`opennvr-adapter-sdk` is the production runtime for the eight adapters shipped in this repo: `adapters/yolov8/` for object detection (`BodyShape.IMAGE` with WebSocket streaming); `adapters/piper/` for text-to-speech (`BodyShape.TEXT` with a custom `/voices` route and inline-audio response); `adapters/whisper/` for speech-to-text (`BodyShape.AUDIO`, multipart decode); `adapters/fast_plate_ocr/` for license-plate text recognition on a pre-cropped plate image (`BodyShape.IMAGE`, designed to chain downstream of YOLOv8); `adapters/insightface/` for face detection plus recognition with a REST face DB; `adapters/blip/` for scene captioning, used by the OpenNVR camera-agent; `adapters/vlm/` for open-vocabulary detection (OWL-ViT v2, detects free-text queries like "red truck"); and `adapters/bytetrack/` for stateful multi-object tracking as a post-processor over an upstream detector's results. Their `main.py` files are non-trivial reference implementations worth reading before authoring your own.
|
|
160
|
+
|
|
161
|
+
## Versioning
|
|
162
|
+
|
|
163
|
+
SDK ships with the same major version as the contract. SDK v1.x targets contract v1; a future contract v2 ships SDK v2.x. `AdapterApp.supported_contract_versions` defaults to `["1"]` — bump when you implement multi-version support.
|
|
164
|
+
|
|
165
|
+
## Why this isn't a "framework"
|
|
166
|
+
|
|
167
|
+
`AdapterService` is an ABC, not a metaclass. `AdapterApp` is a builder, not a base class. The SDK lives between you and FastAPI — your service code never imports FastAPI directly, but you can still drop down to `app.add_route(...)` for adapter-specific endpoints (see Piper's `/voices` route).
|
|
168
|
+
|
|
169
|
+
The contract is the source of truth. The SDK is a convenience layer on top of it. If the SDK gets in your way, write the service hand-rolled the way the early reference adapters did before the SDK was extracted — the contract is implementable without it, just more boilerplate.
|