Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Nov 17, 2024
1 parent 1549e0c commit 6d17325
Show file tree
Hide file tree
Showing 35 changed files with 653 additions and 1,037 deletions.
203 changes: 86 additions & 117 deletions commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys
import time
import sys

print = c.print
def determine_type(x):
x = str(x)
Expand All @@ -28,6 +27,7 @@ def determine_type(x):
except:
# if conversion fails, return as string
return x

elif x.startswith('{') and x.endswith('}'):
# this is a dictionary
if len(x) == 2:
Expand All @@ -41,124 +41,93 @@ def determine_type(x):
return x
else:
# try to convert to int or float, otherwise return as string
try:
return int(x)
except ValueError:

for type_fn in [int, float]:
try:
return float(x)
return type_fn(x)
except ValueError:
return x

class cli:
"""
Create and init the CLI class, which handles the coldkey, hotkey and tao transfer
"""
def __init__(self,
base = 'module',
fn_splitters = [':', '/', '//', '::'],
helper_fns = ['code', 'schema', 'fn_schema', 'help', 'fn_info', 'fn_hash'],
sep = '--',
ai_catch = True,
):
self.set_kwargs(locals())

def set_kwargs(self, kwargs, avoid=['self']):
# remove self from kwargs
for key, value in kwargs.items():
if key in avoid:
continue
setattr(self, key, value)


def forward(self, *argv):
t0 = time.time()
argv = list(*argv)
if len(argv) == 0:
argv = sys.argv[1:]
output = None
init_kwargs = {}
if any([arg.startswith(self.sep) for arg in argv]):
for arg in c.copy(argv):
if arg.startswith(self.sep):
key = arg[len(self.sep):].split('=')[0]
if key in self.helper_fns:
# is it a helper function
return self.forward([key , argv[0]])
else:
value = arg.split('=')[-1] if '=' in arg else True
argv.remove(arg)
init_kwargs[key] = determine_type(value)

# any of the --flags are init kwargs
fn = argv.pop(0).replace('-', '_')
module = c.module(self.base)
fs = [fs for fs in self.fn_splitters if fs in fn]
if len(fs) == 1:
module, fn = fn.split(fs[0])
module = c.shortcuts.get(module, module)
modules = c.modules()
module_options = []
for m in modules:
if module == m:
module_options = [m]
break
if module in m:
module_options.append(m)
if len(module_options)>0:
module = module_options[0]
module = c.module(module)
else:
raise AttributeError(f'Function {fn} not found in {module}')
if hasattr(module, 'fn2module') and not hasattr(module, fn):
c.print(f'ROUTE_ACTIVATED({fn} from {module})')
fn2module = module.fn2module()
if not fn in fn2module:
functions = c.get_functions(module)
return c.print(f'FN({fn}) not found {module}', color='red')
module = c.module(fn2module[fn])

fn_obj = getattr(module, fn)

if c.is_property(fn_obj) or c.classify_fn(fn_obj) == 'self':
fn_obj = getattr(module(**init_kwargs), fn)


if callable(fn_obj):
args = []
kwargs = {}
parsing_kwargs = False
for arg in argv:
if '=' in arg:
parsing_kwargs = True
key, value = arg.split('=')
kwargs[key] = determine_type(value)
pass
return x
def forward(sep = '--',
fn_splitters = [':', '/', '//', '::'],
base = 'module',
helper_fns = ['code', 'schema', 'fn_schema', 'help', 'fn_info', 'fn_hash']):
t0 = time.time()
argv = sys.argv[1:]
output = None
init_kwargs = {}
if any([arg.startswith(sep) for arg in argv]):
for arg in c.copy(argv):
if arg.startswith(sep):
key = arg[len(sep):].split('=')[0]
if key in helper_fns:
# is it a helper function
return forward([key , argv[0]])
else:
assert parsing_kwargs is False, 'Cannot mix positional and keyword arguments'
args.append(determine_type(arg))
output = fn_obj(*args, **kwargs)
else:
output = fn_obj
buffer = '⚡️'*4
c.print(buffer+fn+buffer, color='yellow')
latency = time.time() - t0
is_error = c.is_error(output)
msg = f'❌Error({latency:.3f}sec)❌' if is_error else f'✅Result({latency:.3f}s)✅'
c.print(msg)
is_generator = c.is_generator(output)
if is_generator:
for item in output:
if isinstance(item, dict):
c.print(item)
else:
c.print(item, end='')
else:
c.print(output)
return output
value = arg.split('=')[-1] if '=' in arg else True
argv.remove(arg)
init_kwargs[key] = determine_type(value)

def is_property(self, obj):
return isinstance(obj, property)



# any of the --flags are init kwargs
fn = argv.pop(0).replace('-', '_')
module = c.module(base)
fs = [fs for fs in fn_splitters if fs in fn]
if len(fs) == 1:
module, fn = fn.split(fs[0])
module = c.shortcuts.get(module, module)
modules = c.modules()
module_options = []
for m in modules:
if module == m:
module_options = [m]
break
if module in m:
module_options.append(m)
if len(module_options)>0:
module = module_options[0]
print('Module:', module)
module = c.module(module)
else:
raise AttributeError(f'Function {fn} not found in {module}')
if hasattr(module, 'fn2module') and not hasattr(module, fn):
c.print(f'ROUTE_ACTIVATED({fn} from {module})')
fn2module = module.fn2module()
if not fn in fn2module:
return c.print(f'FN({fn}) not found {module}', color='red')
module = c.module(fn2module[fn])
fn_obj = getattr(module, fn)
if c.is_property(fn_obj) or c.classify_fn(fn_obj) == 'self':
fn_obj = getattr(module(**init_kwargs), fn)
if callable(fn_obj):
args = []
kwargs = {}
parsing_kwargs = False
for arg in argv:
if '=' in arg:
parsing_kwargs = True
key, value = arg.split('=')
kwargs[key] = determine_type(value)
else:
assert parsing_kwargs is False, 'Cannot mix positional and keyword arguments'
args.append(determine_type(arg))
output = fn_obj(*args, **kwargs)
else:
output = fn_obj
buffer = '⚡️'*4
c.print(buffer+fn+buffer, color='yellow')
latency = time.time() - t0
is_error = c.is_error(output)
msg = f'❌Error({latency:.3f}sec)❌' if is_error else f'✅Result({latency:.3f}s)✅'
c.print(msg)
is_generator = c.is_generator(output)
if is_generator:
for item in output:
if isinstance(item, dict):
c.print(item)
else:
c.print(item, end='')
else:
c.print(output)
return output
def main():
cli().forward()
forward()
1 change: 0 additions & 1 deletion commune/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,3 @@ def status(self):
is_full = self.is_full
)


2 changes: 0 additions & 2 deletions commune/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from Crypto.Cipher import AES
import nacl.bindings
import nacl.public


from eth_keys.datatypes import PrivateKey
from scalecodec.utils.ss58 import ss58_encode, ss58_decode, get_ss58_format
from scalecodec.base import ScaleBytes
Expand Down
Loading

0 comments on commit 6d17325

Please sign in to comment.