-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
287 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import json | ||
import base64 | ||
import typing as t | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
|
||
from starwhale.core.dataset.type import MIMEType, ArtifactType, BaseArtifact | ||
|
||
|
||
@dataclass | ||
class Api: | ||
input: t.Any | ||
output: t.Any | ||
func: t.Callable | ||
uri: str | ||
|
||
def to_yaml(self): | ||
return self.func.__qualname__ | ||
|
||
|
||
class Request(ABC): | ||
@abstractmethod | ||
def to_dataset_type(self, req: t.Any) -> BaseArtifact: | ||
raise NotImplementedError() | ||
|
||
|
||
class Response(ABC): | ||
@abstractmethod | ||
def to_json(self, resp: t.Any) -> bytes: | ||
raise NotImplementedError() | ||
|
||
|
||
class GrayscaleImageRequest(Request): | ||
def to_dataset_type(self, req: t.Any) -> BaseArtifact: | ||
if isinstance(req, (str, bytes)): | ||
raw = base64.b64decode(req) | ||
elif isinstance(req, dict): | ||
if "img" not in req: | ||
raise Exception("can not get image") | ||
raw = base64.b64decode(req["img"]) | ||
else: | ||
raise Exception("can not get image from unknown request type") | ||
return BaseArtifact.reflect( | ||
raw, | ||
{ | ||
"type": ArtifactType.Image.value, | ||
"mime_type": MIMEType.GRAYSCALE, | ||
"shape": [28, 28, 1], | ||
}, | ||
) | ||
|
||
|
||
class JsonResponse(Response): | ||
def to_json(self, resp: t.Any) -> bytes: | ||
return json.dumps(resp).encode("utf8") | ||
|
||
|
||
class Service: | ||
def __init__(self): | ||
self.apis = {} | ||
|
||
def api(self, input: t.Any, output: t.Any, uri: str = None): | ||
def decorator(func: t.Any) -> t.Any: | ||
self.add_api(input, output, func, uri or func.__name__) | ||
return func | ||
|
||
return decorator | ||
|
||
# TODO: support checking duplication | ||
def add_api(self, input: t.Any, output: t.Any, func: t.Any, uri: str): | ||
_api = Api(input, output, func, uri) | ||
self.apis[uri] = _api | ||
|
||
def add_api_instance(self, api: Api) -> None: | ||
self.apis[api.uri] = api | ||
|
||
def serve(self, addr: str, port: int, handler_list: t.Optional[t.List[str]] = None): | ||
""" | ||
Default serve implementation, users can override this method | ||
:param addr | ||
:param port | ||
:param handler_list, use all handlers if None | ||
:return: None | ||
""" | ||
apis = self.apis | ||
|
||
import flask | ||
|
||
app = flask.Flask(__name__) | ||
if handler_list: | ||
apis = {uri: apis[uri] for uri in apis if uri in handler_list} | ||
for api in apis: | ||
app.add_url_rule( | ||
rule=api, | ||
endpoint=None, | ||
view_func=self.apis[api].func, | ||
methods=["POST"], | ||
) | ||
app.run(addr, port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._impl.service import Request, Service, JsonResponse, GrayscaleImageRequest | ||
|
||
__all__ = ["Service", "Request", "GrayscaleImageRequest", "JsonResponse"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.