Skip to content

Commit

Permalink
feat: add warning if input is too large (#796)
Browse files Browse the repository at this point in the history
* feat: add warning if input is too large

* test: client large input

* chore: update warning msg

* chore: update warning msg

* test: large input

* chore: update warning msg

* chore: improve warning msg
  • Loading branch information
ZiniuYu authored Aug 12, 2022
1 parent b4fb0dd commit f852dfc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions client/clip_client/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mimetypes
import os
import time
import types
import warnings
from typing import (
overload,
Expand Down Expand Up @@ -140,7 +141,7 @@ def _gather_result(self, response, results: 'DocumentArray'):
def _unboxed_result(results: 'DocumentArray'):
if results.embeddings is None:
raise ValueError(
'empty embedding returned from the server. '
'Empty embedding returned from the server. '
'This often due to a mis-config of the server, '
'restarting the server or changing the serving port number often solves the problem'
)
Expand Down Expand Up @@ -308,7 +309,11 @@ def _prepare_streaming(self, disable, total):
if total is None:
total = 500
warnings.warn(
'the length of the input is unknown, the progressbar would not be accurate.'
'The length of the input is unknown, the progressbar would not be accurate.'
)
elif total > 500:
warnings.warn(
'Please ensure all the inputs are valid, otherwise the request will be aborted.'
)

from docarray.array.mixins.io.pbar import get_pbar
Expand All @@ -335,7 +340,7 @@ def _prepare_single_doc(d: 'Document'):
elif d.tensor is not None:
return d
else:
raise TypeError(f'unsupported input type {d!r} {d.content_type}')
raise TypeError(f'Unsupported input type {d!r} {d.content_type}')

@staticmethod
def _prepare_rank_doc(d: 'Document', _source: str = 'matches'):
Expand All @@ -359,7 +364,7 @@ def _iter_rank_docs(
if isinstance(c, Document):
yield self._prepare_rank_doc(c, _source)
else:
raise TypeError(f'unsupported input type {c!r}')
raise TypeError(f'Unsupported input type {c!r}')

if hasattr(self, '_pbar'):
self._pbar.update(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ def generate_docs(tag):

for r in results:
assert len(set([d.id[:2] for d in r])) == 1


def test_client_large_input(make_flow, port_generator):
from clip_client.client import Client

inputs = ['hello' for _ in range(600)]

c = Client(server=f'grpc://0.0.0.0:{make_flow.port}')
with pytest.warns(UserWarning):
c.encode(inputs if not callable(inputs) else inputs())

0 comments on commit f852dfc

Please sign in to comment.