Skip to content

Commit

Permalink
feat(client): auto detect input_type
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxiao committed May 27, 2020
1 parent 6702c61 commit fda5790
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions jina/clients/python/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
__license__ = "Apache-2.0"

import ctypes
import os
import random
import urllib.parse
from typing import Iterator, Union

from ...enums import ClientInputType, ClientMode
from ...enums import ClientMode
from ...helper import batch_iterator
from ...proto import jina_pb2


def _generate(data: Union[Iterator[bytes], Iterator['jina_pb2.Document'], Iterator[str]], batch_size: int = 0,
first_doc_id: int = 0, first_request_id: int = 0,
random_doc_id: bool = False, mode: ClientMode = ClientMode.INDEX, top_k: int = 50,
input_type: ClientInputType = ClientInputType.BUFFER,
mime_type: str = None,
*args, **kwargs) -> Iterator['jina_pb2.Message']:
if isinstance(mode, str):
Expand All @@ -31,18 +32,18 @@ def _generate(data: Union[Iterator[bytes], Iterator['jina_pb2.Document'], Iterat

for _raw in pi:
d = getattr(req, str(mode).lower()).docs.add()
if input_type == ClientInputType.PROTOBUF:
if isinstance(_raw, jina_pb2.Document):
d.CopyFrom(_raw)
elif input_type == ClientInputType.DATA_URI:
d.data_uri = _raw
elif input_type == ClientInputType.FILE_PATH:
d.file_path = _raw
elif input_type == ClientInputType.BUFFER:
if isinstance(_raw, str):
_raw = _raw.encode() # auto-fix for str
elif isinstance(_raw, bytes):
d.buffer = _raw
if mime_type:
d.mime_type = mime_type
elif isinstance(_raw, str):
scheme = urllib.parse.urlparse(_raw).scheme
if scheme in {'http', 'https'} or os.path.exists(_raw) or os.access(os.path.dirname(_raw), os.W_OK):
d.file_path = _raw
elif scheme == 'data':
d.data_uri = _raw
d.doc_id = first_doc_id if not random_doc_id else random.randint(0, ctypes.c_uint(-1).value)
d.weight = 1.0
first_doc_id += 1
Expand Down
4 changes: 2 additions & 2 deletions jina/executors/crafters/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def craft(self, file_path: str, data_uri: str, buffer: bytes, *args, **kwargs):
if buffer:
pass
elif file_path:
return super(FilePath2Buffer, self).craft(file_path)
return FilePath2Buffer.craft(self, file_path)
elif data_uri:
return super(DataURI2Buffer, self).craft(data_uri)
return DataURI2Buffer.craft(self, data_uri)
else:
raise ValueError('this document has no "file_path", no "data_uri" and no "buffer" set')

Expand Down

0 comments on commit fda5790

Please sign in to comment.