Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.

Commit 8ca5ef0

Browse files
authored
Merge pull request #319 from gnes-ai/fix-flow-2
fix(frontend): fix frontend blocking behavior
2 parents f7e7791 + c23ea61 commit 8ca5ef0

File tree

11 files changed

+57
-20
lines changed

11 files changed

+57
-20
lines changed

docs/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
# documentation root, use os.path.abspath to make it absolute, like shown here.
1414
#
1515
import os
16-
from os import path
1716
import sys
17+
from os import path
18+
1819
sys.path.insert(0, os.path.abspath('..'))
1920

2021
# -- Project information -----------------------------------------------------
@@ -51,6 +52,7 @@
5152
# ones.
5253
extensions = [
5354
'sphinx.ext.autodoc',
55+
'sphinx_autodoc_typehints',
5456
'sphinx.ext.viewcode',
5557
'sphinxcontrib.apidoc',
5658
'sphinxarg.ext',

docs/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
sphinx-argparse
2-
sphinxcontrib-apidoc
2+
sphinxcontrib-apidoc
3+
sphinx-autodoc-typehints

gnes/base/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def dump_full_path(self):
217217
def yaml_full_path(self):
218218
"""
219219
Get the file path of the yaml config
220+
220221
:return:
221222
"""
222223
return os.path.join(self.work_dir, '%s.yml' % self.name)
@@ -247,6 +248,7 @@ def train(self, *args, **kwargs):
247248
def dump(self, filename: str = None) -> None:
248249
"""
249250
Serialize the object to a binary file
251+
250252
:param filename: file path of the serialized file, if not given then :py:attr:`dump_full_path` is used
251253
"""
252254
f = filename or self.dump_full_path
@@ -260,6 +262,7 @@ def dump(self, filename: str = None) -> None:
260262
def dump_yaml(self, filename: str = None) -> None:
261263
"""
262264
Serialize the object to a yaml file
265+
263266
:param filename: file path of the yaml file, if not given then :py:attr:`dump_yaml_path` is used
264267
"""
265268
f = filename or self.yaml_full_path

gnes/client/cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import sys
1818
import time
1919
import zipfile
20-
from typing import Generator
20+
from typing import Iterator
2121

2222
from termcolor import colored
2323

@@ -78,6 +78,7 @@ def query_callback(self, req: 'gnes_pb2.Request', resp: 'gnes_pb2.Response'):
7878
"""
7979
callback after get the query result
8080
override this method to customize query behavior
81+
8182
:param resp: response
8283
:param req: query
8384
:return:
@@ -86,14 +87,14 @@ def query_callback(self, req: 'gnes_pb2.Request', resp: 'gnes_pb2.Response'):
8687
print(resp)
8788

8889
@property
89-
def bytes_generator(self) -> Generator[bytes, None, None]:
90+
def bytes_generator(self) -> Iterator[bytes]:
9091
if self._bytes_generator:
9192
return self._bytes_generator
9293
else:
9394
raise ValueError('bytes_generator is empty or not set')
9495

9596
@bytes_generator.setter
96-
def bytes_generator(self, bytes_gen: Generator[bytes, None, None]):
97+
def bytes_generator(self, bytes_gen: Iterator[bytes]):
9798
if self._bytes_generator:
9899
self.logger.warning('bytes_generator is not empty, overrided')
99100
self._bytes_generator = bytes_gen

gnes/flow/__init__.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import OrderedDict, defaultdict
44
from contextlib import ExitStack
55
from functools import wraps
6-
from typing import Union, Tuple, List, Optional, Generator
6+
from typing import Union, Tuple, List, Optional, Iterator
77

88
from ..cli.parser import set_router_parser, set_indexer_parser, \
99
set_frontend_parser, set_preprocessor_parser, \
@@ -78,9 +78,10 @@ class Flow:
7878
flow.index()
7979
...
8080
81-
You can also use the shortcuts, e.g. :py:meth:add_encoder , :py:meth:add_preprocessor
81+
You can also use the shortcuts, e.g. :py:meth:`add_encoder`, :py:meth:`add_preprocessor`.
8282
8383
It is recommend to use flow in the context manner as showed above.
84+
8485
Note the different default copy behaviors in :py:meth:`.add()` and :py:meth:`.build()`:
8586
:py:meth:`.add()` always copy the flow by default, whereas :py:meth:`.build()` modify the flow in place.
8687
You can change this behavior by giving an argument `copy_flow=False`.
@@ -169,18 +170,38 @@ def to_mermaid(self, left_right: bool = True):
169170
'copy-paste the output and visualize it with: https://mermaidjs.github.io/mermaid-live-editor/')
170171
return mermaid_str
171172

172-
def train(self, bytes_gen: Generator[bytes, None, None] = None, **kwargs):
173+
def train(self, bytes_gen: Iterator[bytes] = None, **kwargs):
174+
"""Do training on the current flow
175+
176+
It will start a :py:class:`CLIClient` and call :py:func:`train`.
177+
178+
:param bytes_gen: An iterator of bytes. If not given, then you have to specify it in `kwargs`.
179+
:param kwargs: accepts all keyword arguments of `gnes client` CLI
180+
"""
173181
self._call_client(bytes_gen, mode='train', **kwargs)
174182

175-
def index(self, bytes_gen: Generator[bytes, None, None] = None, **kwargs):
183+
def index(self, bytes_gen: Iterator[bytes] = None, **kwargs):
184+
"""Do indexing on the current flow
185+
186+
It will start a :py:class:`CLIClient` and call :py:func:`index`.
187+
188+
:param bytes_gen: An iterator of bytes. If not given, then you have to specify it in `kwargs`.
189+
:param kwargs: accepts all keyword arguments of `gnes client` CLI
190+
"""
176191
self._call_client(bytes_gen, mode='index', **kwargs)
177192

178-
def query(self, bytes_gen: Generator[bytes, None, None] = None, **kwargs):
193+
def query(self, bytes_gen: Iterator[bytes] = None, **kwargs):
194+
"""Do indexing on the current flow
195+
196+
It will start a :py:class:`CLIClient` and call :py:func:`query`.
197+
198+
:param bytes_gen: An iterator of bytes. If not given, then you have to specify it in `kwargs`.
199+
:param kwargs: accepts all keyword arguments of `gnes client` CLI
200+
"""
179201
self._call_client(bytes_gen, mode='query', **kwargs)
180202

181203
@_build_level(BuildLevel.RUNTIME)
182-
def _call_client(self, bytes_gen: Generator[bytes, None, None] = None, **kwargs):
183-
204+
def _call_client(self, bytes_gen: Iterator[bytes] = None, **kwargs):
184205
os.unsetenv('http_proxy')
185206
os.unsetenv('https_proxy')
186207
args, p_args = self._get_parsed_args(self, set_client_cli_parser, kwargs)
@@ -192,26 +213,26 @@ def _call_client(self, bytes_gen: Generator[bytes, None, None] = None, **kwargs)
192213
c.start()
193214

194215
def add_frontend(self, *args, **kwargs) -> 'Flow':
195-
"""Add a frontend to the current flow, a shortcut of add(Service.Frontend)
216+
"""Add a frontend to the current flow, a shortcut of :py:meth:`add(Service.Frontend)`.
196217
Usually you dont need to call this function explicitly, a flow object contains a frontend service by default.
197218
This function is useful when you build a flow without the frontend and want to customize the frontend later.
198219
"""
199220
return self.add(Service.Frontend, *args, **kwargs)
200221

201222
def add_encoder(self, *args, **kwargs) -> 'Flow':
202-
"""Add an encoder to the current flow, a shortcut of add(Service.Encoder)"""
223+
"""Add an encoder to the current flow, a shortcut of :py:meth:`add(Service.Encoder)`"""
203224
return self.add(Service.Encoder, *args, **kwargs)
204225

205226
def add_indexer(self, *args, **kwargs) -> 'Flow':
206-
"""Add an indexer to the current flow, a shortcut of add(Service.Indexer)"""
227+
"""Add an indexer to the current flow, a shortcut of :py:meth:`add(Service.Indexer)`"""
207228
return self.add(Service.Indexer, *args, **kwargs)
208229

209230
def add_preprocessor(self, *args, **kwargs) -> 'Flow':
210-
"""Add a router to the current flow, a shortcut of add(Service.Preprocessor)"""
231+
"""Add a preprocessor to the current flow, a shortcut of :py:meth:`add(Service.Preprocessor)`"""
211232
return self.add(Service.Preprocessor, *args, **kwargs)
212233

213234
def add_router(self, *args, **kwargs) -> 'Flow':
214-
"""Add a preprocessor to the current flow, a shortcut of add(Service.Router)"""
235+
"""Add a router to the current flow, a shortcut of :py:meth:`add(Service.Router)`"""
215236
return self.add(Service.Router, *args, **kwargs)
216237

217238
def add(self, service: 'Service',
@@ -416,7 +437,8 @@ def _build_graph(self, copy_flow: bool) -> 'Flow':
416437
def build(self, backend: Optional[str] = 'thread', copy_flow: bool = False, *args, **kwargs) -> 'Flow':
417438
"""
418439
Build the current flow and make it ready to use
419-
:param backend: supported 'thread', 'process', 'swarm', 'k8s', 'shell'
440+
441+
:param backend: supported 'thread', 'process', 'swarm', 'k8s', 'shell', if None then only build graph only
420442
:param copy_flow: return the copy of the current flow
421443
:return: the current flow (by default)
422444
"""

gnes/indexer/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self, helper_indexer: 'BaseChunkIndexerHelper' = None, *args, **kwa
7575
def add(self, keys: List[Tuple[int, int]], vectors: np.ndarray, weights: List[float], *args, **kwargs):
7676
"""
7777
adding new chunks and their vector representations
78+
7879
:param keys: list of (doc_id, offset) tuple
7980
:param vectors: vector representations
8081
:param weights: weight of the chunks
@@ -160,6 +161,7 @@ class BaseDocIndexer(BaseIndexer):
160161
def add(self, keys: List[int], docs: List['gnes_pb2.Document'], *args, **kwargs):
161162
"""
162163
adding new docs and their protobuf representation
164+
163165
:param keys: list of doc_id
164166
:param docs: list of protobuf Document objects
165167
"""

gnes/indexer/chunk/annoy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AnnoyIndexer(BCI):
2727
def __init__(self, num_dim: int, data_path: str, metric: str = 'angular', n_trees: int = 10, *args, **kwargs):
2828
"""
2929
Initialize an AnnoyIndexer
30+
3031
:param num_dim: when set to -1, then num_dim is auto decided on first .add()
3132
:param data_path: index data file managed by the annoy indexer
3233
:param metric:

gnes/indexer/chunk/faiss.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class FaissIndexer(BCI):
2828
def __init__(self, num_dim: int, index_key: str, data_path: str, *args, **kwargs):
2929
"""
3030
Initialize an FaissIndexer
31+
3132
:param num_dim: when set to -1, then num_dim is auto decided on first .add()
3233
:param data_path: index data file managed by the faiss indexer
3334
"""

gnes/indexer/doc/filesys.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def add(self, keys: List[int], docs: List['gnes_pb2.Document'], *args, **kwargs)
3838
"""
3939
write GIFs of each document into disk
4040
folder structure: /data_path/doc_id/0.gif, 1.gif...
41+
4142
:param keys: list of doc id
4243
:param docs: list of docs
4344
"""
@@ -55,6 +56,8 @@ def add(self, keys: List[int], docs: List['gnes_pb2.Document'], *args, **kwargs)
5556

5657
def query(self, keys: List[int], *args, **kwargs) -> List['gnes_pb2.Document']:
5758
"""
59+
Find the doc according to the keys
60+
5861
:param keys: list of doc id
5962
:return: list of documents whose chunks field contain all the GIFs of this doc(one GIF per chunk)
6063
"""

gnes/router/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def reduce_embedding(self, accum_msgs: List['gnes_pb2.Message'], msg_type: str,
101101
def apply(self, msg: 'gnes_pb2.Message', accum_msgs: List['gnes_pb2.Message'], *args, **kwargs) -> None:
102102
"""
103103
reduce embeddings from encoders (means, concat ....)
104+
104105
:param msg: the current message
105106
:param accum_msgs: accumulated messages
106107
"""

0 commit comments

Comments
 (0)