-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathinference_api.py
More file actions
105 lines (86 loc) · 2.68 KB
/
inference_api.py
File metadata and controls
105 lines (86 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
"""SingleStoreDB Cloud Inference API."""
import os
from typing import Any
from typing import Dict
from typing import Optional
from .utils import vars_to_str
from singlestoredb.exceptions import ManagementError
from singlestoredb.management.manager import Manager
class InferenceAPIInfo(object):
"""
Inference API definition.
This object is not directly instantiated. It is used in results
of API calls on the :class:`InferenceAPIManager`. See :meth:`InferenceAPIManager.get`.
"""
service_id: str
model_name: str
name: str
connection_url: str
project_id: str
hosting_platform: str
def __init__(
self,
service_id: str,
model_name: str,
name: str,
connection_url: str,
project_id: str,
hosting_platform: str,
):
self.service_id = service_id
self.connection_url = connection_url
self.model_name = model_name
self.name = name
self.project_id = project_id
self.hosting_platform = hosting_platform
@classmethod
def from_dict(
cls,
obj: Dict[str, Any],
) -> 'InferenceAPIInfo':
"""
Construct a Inference API from a dictionary of values.
Parameters
----------
obj : dict
Dictionary of values
Returns
-------
:class:`Job`
"""
out = cls(
service_id=obj['serviceID'],
project_id=obj['projectID'],
model_name=obj['modelName'],
name=obj['name'],
connection_url=obj['connectionURL'],
hosting_platform=obj['hostingPlatform'],
)
return out
def __str__(self) -> str:
"""Return string representation."""
return vars_to_str(self)
def __repr__(self) -> str:
"""Return string representation."""
return str(self)
class InferenceAPIManager(object):
"""
SingleStoreDB Inference APIs manager.
This class should be instantiated using :attr:`Organization.inference_apis`.
Parameters
----------
manager : InferenceAPIManager, optional
The InferenceAPIManager the InferenceAPIManager belongs to
See Also
--------
:attr:`InferenceAPI`
"""
def __init__(self, manager: Optional[Manager]):
self._manager = manager
self.project_id = os.environ.get('SINGLESTOREDB_PROJECT')
def get(self, model_name: str) -> InferenceAPIInfo:
if self._manager is None:
raise ManagementError(msg='Manager not initialized')
res = self._manager._get(f'inferenceapis/{self.project_id}/{model_name}').json()
return InferenceAPIInfo.from_dict(res)