-
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.
so that: - `python -m coglet` is the main entrypoint - `cog` compatibility imports are a thin wrapper around `coglet.api` and are only available for import once `coglet/_compat` is put into `sys.path`, thus greatly reducing the likelihood of import collisions. - `cog/internal/` directory collapsed up a level to `coglet/` since the above import collision problem is fixed.
- Loading branch information
1 parent
24c0f02
commit 610121a
Showing
20 changed files
with
152 additions
and
98 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,16 @@ | ||
import pathlib | ||
import sys | ||
import warnings | ||
|
||
# NOTE: The compatibility import provided in `./_compat/cog.py` **SHOULD NOT** be in | ||
# PYTHONPATH until `coglet` is imported. This prevents `coglet` from interfering with | ||
# normal usage of `cog` within a given python environment. | ||
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
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.