-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from replicate/coglet
Rename to `coglet` plus reorg
- Loading branch information
Showing
20 changed files
with
234 additions
and
163 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
# Coglet | ||
|
||
Coglet provides a minimum viable [Cog] runtime primarily for use within the Replicate | ||
platform, e.g.: | ||
|
||
``` | ||
python -m coglet --working-dir path/to/code/ --module-name predict --class-name Predictor | ||
``` | ||
|
||
[Cog]: <https://github.com/replicate/cog> |
Empty file.
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,13 @@ | ||
import pathlib | ||
import sys | ||
import warnings | ||
|
||
warnings.warn( | ||
( | ||
'coglet/_compat/ is being added to the front of sys.path ' | ||
"for 'cog' import compatibility" | ||
), | ||
category=ImportWarning, | ||
stacklevel=2, | ||
) | ||
sys.path.insert(0, str(pathlib.Path(__file__).absolute().parent / '_compat')) |
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,80 @@ | ||
import argparse | ||
import asyncio | ||
import contextvars | ||
import logging | ||
import sys | ||
from typing import Optional | ||
|
||
from coglet import file_runner | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
'--working-dir', metavar='DIR', required=True, help='working directory' | ||
) | ||
parser.add_argument( | ||
'--module-name', metavar='NAME', required=True, help='Python module name' | ||
) | ||
parser.add_argument( | ||
'--class-name', metavar='NAME', required=True, help='Python class name' | ||
) | ||
|
||
_ctx_pid: contextvars.ContextVar[Optional[str]] = contextvars.ContextVar( | ||
'pid', default=None | ||
) | ||
_ctx_newline: contextvars.ContextVar[bool] = contextvars.ContextVar( | ||
'newline', default=False | ||
) | ||
|
||
logger = logging.getLogger('coglet') | ||
logger.setLevel(logging.INFO) | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setFormatter( | ||
logging.Formatter( | ||
'%(asctime)s\t%(levelname)s\t[%(name)s]\t%(filename)s:%(lineno)d\t%(message)s' | ||
) | ||
) | ||
logger.addHandler(handler) | ||
|
||
_stdout_write = sys.stdout.write | ||
_stderr_write = sys.stderr.write | ||
|
||
def _ctx_write(write_fn): | ||
def _write(s: str) -> int: | ||
pid = _ctx_pid.get() | ||
if pid is None: | ||
return write_fn(s) | ||
else: | ||
n = 0 | ||
if _ctx_newline.get(): | ||
n += write_fn(f'[pid={pid}] ') | ||
if s[-1] == '\n': | ||
_ctx_newline.set(True) | ||
s = s[:-1].replace('\n', f'\n[pid={pid}] ') + '\n' | ||
else: | ||
_ctx_newline.set(False) | ||
s = s.replace('\n', f'\n[pid={pid}] ') | ||
n += write_fn(s) | ||
return n | ||
|
||
return _write | ||
|
||
sys.stdout.write = _ctx_write(_stdout_write) # type: ignore | ||
sys.stderr.write = _ctx_write(_stderr_write) # type: ignore | ||
|
||
args = parser.parse_args() | ||
|
||
return asyncio.run( | ||
file_runner.FileRunner( | ||
logger=logger, | ||
working_dir=args.working_dir, | ||
module_name=args.module_name, | ||
class_name=args.class_name, | ||
ctx_pid=_ctx_pid, | ||
).start() | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
sys.exit(main()) |
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
File renamed without changes.
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
File renamed without changes.
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.