Chulinz's picture
ICJ Citation Graph release with Parquet and Docker runtime
0a50834
Raw History Blame Contribute Delete
2.47 kB
import argparse
import json
import os
from ._graph import Graph
from ._state import locked_state
from .config import TARGETS
from .download import fetch
from .embedding import embed_graph
from .indexes import create_indexes, inspect_graph
from .parquet import load_vectors
def positive(value):
value = int(value)
if value < 1:
raise argparse.ArgumentTypeError('must be positive')
return value
def main():
parser = argparse.ArgumentParser(description='Restore vectors and manage the ICJ graph. API calls are explicit.')
commands = parser.add_subparsers(dest='command', required=True)
download = commands.add_parser('fetch')
download.add_argument('--data-dir', default='/dataset')
download.add_argument('--revision', default=os.getenv('ICJ_REVISION', 'main'))
download.add_argument('--vectors', action='store_true')
commands.add_parser('inspect')
for name in ['vectors', 'embed', 'indexes']:
child = commands.add_parser(name)
child.add_argument('--targets', nargs='+', choices=list(TARGETS), default=list(TARGETS))
if name == 'indexes':
child.add_argument('--property')
child.add_argument('--timeout', type=positive, default=3600)
else:
child.add_argument('--batch-size', type=positive, default=64)
child.add_argument('--limit', type=positive, help='Maximum newly added vectors per target in this invocation; omit to process all.')
if name == 'vectors':
child.add_argument('--data-dir', default='/dataset')
else:
child.add_argument('--property', default='api_embedding')
args = parser.parse_args()
if args.command == 'fetch':
fetch(args.data_dir, args.revision, args.vectors)
return
graph = Graph()
try:
if args.command == 'inspect':
print(json.dumps(inspect_graph(graph), indent=2))
return
with locked_state(os.getenv('STATE_DIR', '/state')) as cache:
if args.command == 'vectors':
load_vectors(graph, args.targets, args.data_dir, args.batch_size, args.limit)
elif args.command == 'embed':
embed_graph(graph, args.targets, cache, args.property, args.batch_size, args.limit)
else:
create_indexes(graph, args.targets, args.property, args.timeout)
finally:
graph.close()
if __name__ == '__main__':
main()