Skip to content

Commit

Permalink
fam
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Nov 16, 2024
1 parent 07c4166 commit 1549e0c
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 215 deletions.
14 changes: 7 additions & 7 deletions commune/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@



from .module import Module
from .vali import Vali
from .server import Server
from .client import Client
from .key import Key
from .module import Module # the module module
from .vali import Vali # the vali module
from .server import Server # the server module
from .client import Client # the client module
from .key import Key # the key module
# set the module functions as globalsw
IDONTGIVEAFUCKWHATYOUCALLTHIS = c = Block = Lego = M = Agent = Module # alias c.Module as c.Block, c.Lego, c.M
M = IDONTGIVEAFUCKWHATYOUCALLTHIS = c = Block = Lego = M = Agent = Module # alias c.Module as c.Block, c.Lego, c.M
c.add_to_globals(globals())
key = c.get_key # override key function with file key in commune/key.py
key = c.get_key # override key function with file key in commune/key.py TODO: remove this line with a better solution

226 changes: 202 additions & 24 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ def add_routes(cls, routes:dict=None, verbose=False, add_utils=True):
This allows you to call the function as if it were a method of the current module.
for example
"""
routes = routes or cls.get_routes()
t0 = time.time()
# WARNING : THE PLACE HOLDERS MUST NOT INTERFERE WITH THE KWARGS OTHERWISE IT WILL CAUSE A BUG IF THE KWARGS ARE THE SAME AS THE PLACEHOLDERS
# THE PLACEHOLDERS ARE NAMED AS module_ph and fn_ph AND WILL UNLIKELY INTERFERE WITH THE KWARGS
Expand All @@ -545,8 +546,7 @@ def fn(*args, **kwargs):
else:
return fn_obj
return fn(*args, **kwargs)

for module, fns in cls.get_routes().items():
for module, fns in routes.items():
for fn in fns:
if not hasattr(cls, fn):
fn_obj = partial(fn_generator, route=module + '.' + fn)
Expand Down Expand Up @@ -846,8 +846,9 @@ def resolve_path(cls, path:str = None, extension:Optional[str]=None):
def abspath(cls, path:str):
return os.path.abspath(os.path.expanduser(path))


@classmethod
def put_text(cls, path:str, text:str, key=None, bits_per_character=8) -> None:
def put_text(cls, path:str, text:str, key=None) -> None:
# Get the absolute path of the file
path = cls.resolve_path(path)
dirpath = os.path.dirname(path)
Expand All @@ -861,13 +862,10 @@ def put_text(cls, path:str, text:str, key=None, bits_per_character=8) -> None:
with open(path, 'w') as file:
file.write(text)
# get size
text_size = len(text)*bits_per_character

return {'success': True, 'path': f'{path}', 'size': text_size}
return {'success': True, 'path': f'{path}', 'size': len(text)*8}

@classmethod
def ls(cls, path:str = '',
recursive:bool = False,
search = None,
return_full_path:bool = True):
"""
Expand Down Expand Up @@ -1860,7 +1858,7 @@ def get_tree(cls, path, depth = 10, max_age=60, update=False, **kwargs):
tree_cache_path = 'tree/'+os.path.abspath(path).replace('/', '_')
tree = c.get(tree_cache_path, None, max_age=max_age, update=update)
if tree == None:
c.print(f'TREE(max_age={max_age}, depth={depth}, pat={path})', color='green')
c.print(f'TREE(max_age={max_age}, depth={depth}, path={path})', color='green')
class_paths = cls.find_classes(path, depth=depth)
simple_paths = [cls.objectpath2name(p) for p in class_paths]
tree = dict(zip(simple_paths, class_paths))
Expand Down Expand Up @@ -1894,7 +1892,6 @@ def module(cls, path:str = 'module',
try:
module = c.import_object(path)
except Exception as e:
tree = c.tree(update=1)
if trials == 0:
raise ValueError(f'Error in module {og_path} {e}')
return c.module(path, cache=cache, verbose=verbose, tree=tree, trials=trials-1)
Expand All @@ -1916,6 +1913,7 @@ def module(cls, path:str = 'module',
module.params = lambda *args, **kwargs : c.params(module)
module.key = c.get_key(module.module_name(), create_if_not_exists=True)
module.fn2code = lambda *args, **kwargs : c.fn2code(module)
module.help = lambda *args, **kwargs : c.help(*args, module=module, **kwargs)

c.print(f'Module({og_path}->{path})({latency}s)', verbose=verbose)
return module
Expand Down Expand Up @@ -2093,9 +2091,9 @@ def set_api_key(self, api_key:str, cache:bool = True):
self.add_api_key(api_key)
assert isinstance(api_key, str)

def add_api_key(self, api_key:str, path=None):
def add_api_key(self, api_key:str):
assert isinstance(api_key, str)
path = self.resolve_path(path or 'api_keys')
path = self.resolve_path('api_keys')
api_keys = self.get(path, [])
api_keys.append(api_key)
api_keys = list(set(api_keys))
Expand All @@ -2104,8 +2102,7 @@ def add_api_key(self, api_key:str, path=None):

def set_api_keys(self, api_keys:str):
api_keys = list(set(api_keys))
self.put('api_keys', api_keys)
return {'api_keys': api_keys}
return self.put('api_keys', api_keys)

def rm_api_key(self, api_key:str):
assert isinstance(api_key, str)
Expand All @@ -2115,12 +2112,11 @@ def rm_api_key(self, api_key:str):
api_keys.pop(i)
break
path = self.resolve_path('api_keys')
self.put(path, api_keys)
return {'api_keys': api_keys}
return self.put(path, api_keys)

def get_api_key(self, module=None):
if module != None:
self = self.module(module)
self = c.module(module)
api_keys = self.api_keys()
if len(api_keys) == 0:
raise
Expand Down Expand Up @@ -2155,12 +2151,7 @@ def remote_fn(cls,
if 'remote' in kwargs:
kwargs['remote'] = False
assert fn != None, 'fn must be specified for pm2 launch'
kwargs = {
'module': module,
'fn': fn,
'args': args,
'kwargs': kwargs
}
kwargs = {'module': module, 'fn': fn, 'args': args, 'kwargs': kwargs}
name = name or module
if refresh:
c.kill(name)
Expand All @@ -2184,6 +2175,8 @@ def resolve_extension( filename:str, extension = '.py') -> str:
return filename + extension

def help(self, *text, module=None, global_context=f'{rootpath}/docs', **kwargs):
if self.module_name() == 'module':
return c.module('docs')().help(*text)
text = ' '.join(map(str, text))
if global_context != None:
text = text + str(c.file2text(global_context))
Expand Down Expand Up @@ -2239,7 +2232,6 @@ def fn2module(cls, path=None):
fn2module[f] = m
return fn2module



def install(self, path ):
path = path + '/requirements.txt'
Expand All @@ -2251,9 +2243,195 @@ def epoch(self, *args, **kwargs):
return c.run_epoch(*args, **kwargs)


c.routes = c.get_routes()
c.routes = {
"vali": [
"run_epoch",
"setup_vali",
"from_module"
],
"py": [
"envs",
"env2cmd",
"create_env",
"env2path"
],
"cli": [
"parse_args"
],
"streamlit": [
"set_page_config",
"load_style",
"st_load_css"
],
"docker": [
"containers",
"dlogs",
"images"
],
"client": [
"call",
"call_search",
"connect"
],
"repo": [
"is_repo",
"repos"
],
"serializer": [
"serialize",
"deserialize",
"serializer_map",
],
"key": [
"rename_key",
"ss58_encode",
"ss58_decode",
"key2mem",
"key_info_map",
"key_info",
"valid_ss58_address",
"valid_h160_address",
"add_key",
"from_password",
"str2key",
"pwd2key",
"getmem",
"mem",
"mems",
"switch_key",
"module_info",
"rename_kefy",
"mv_key",
"add_keys",
"key_exists",
"ls_keys",
"rm_key",
"key_encrypted",
"encrypt_key",
"get_keys",
"rm_keys",
"key2address",
"key_addresses",
"address2key",
"is_key",
"new_key",
"save_keys",
"load_key",
"load_keys",
"get_signer",
"encrypt_file",
"decrypt_file",
"get_key_for_address",
"resolve_key_address",
"ticket"
],
"remote": [
"host2ssh"
],
"network": [
"networks",
"register_server",
"deregister_server",
"server_exists",
"add_server",
"has_server",
"add_servers",
"rm_servers",
"rm_server",
"namespace",
"namespace",
"infos",
"get_address",
"servers",
"name2address"
],
"app": [
"start_app",
"app",
"apps",
"app2info",
"kill_app"
],
"user": [
"role2users",
"is_user",
"get_user",
"update_user",
"get_role",
"refresh_users",
"user_exists",
"is_admin",
"admins",
"add_admin",
"rm_admin",
"num_roles",
"rm_user"
],
"server": [
"serve",
"wait_for_server",
"endpoint",
"is_endpoint",
"fleet",
"processes",
"kill",
"kill_many",
"kill_all",
"kill_all_processes",
"logs"
],

"subspace": [
"transfer_stake",
"stake_trnsfer",
"switch",
"switchnet",
"subnet",
"update_module",
"subnet_params_map",
"staketo",
"network",
"get_staketo",
"stakefrom",
"get_stakefrom",
"switch_network",
"key2balance",
"subnets",
"send",
"my_keys",
"key2value",
"transfer",
"multistake",
"stake",
"unstake",
"register",
"subnet_params",
"global_params",
"balance",
"get_balance",
"get_stak",
"get_stake_to",
"get_stake_from",
"my_stake_to",
"netuid2subnet",
"subnet2netuid",
"is_registered",
"update_subnet",
"my_subnets",
"my_netuids",
"register_subnet",
"registered_subnets",
"registered_netuids"
],
"model.openrouter": [
"generate",
"models"
],
"chat": ["ask", "models", "pricing", "model2info"]
}
c.add_routes()
Module = c # Module is alias of c
Module.run(__name__)



Loading

0 comments on commit 1549e0c

Please sign in to comment.