diff --git a/commune/cli.py b/commune/cli.py index 8f35b98b0..720f13584 100644 --- a/commune/cli.py +++ b/commune/cli.py @@ -146,13 +146,18 @@ def determine_type(cls, x): @classmethod def history_module(cls, path='history'): - return c.m('history')(cls.resolve_path(path)) + return c.m('history')(folder_path=cls.resolve_path(path)) @classmethod def history(cls,**kwargs): history = cls.history_module().history(**kwargs) return history + @classmethod + def rm_history(cls,*args, **kwargs): + history = cls.history_module().rm_history(*args, **kwargs) + return history + @classmethod def history_paths(cls, **kwargs): diff --git a/commune/history.py b/commune/history.py index 404fe3218..51fac6aca 100644 --- a/commune/history.py +++ b/commune/history.py @@ -43,6 +43,12 @@ def history(self, search=None, n=100, reverse=True, idx=None): return history[idx] return history + def rm_history(self, search=None, n=100, reverse=True): + history_paths = self.history_paths(n=n, reverse=reverse, search=search) + for path in history_paths: + c.rm(path) + return history_paths + def last_n(self, n=1): return self.history(n=n) diff --git a/commune/key/key.py b/commune/key/key.py index 490cbacfe..296d75b44 100644 --- a/commune/key/key.py +++ b/commune/key/key.py @@ -294,7 +294,7 @@ def load_keys(cls, path='saved_keys.json', refresh=False, **kwargs): @classmethod def mems(cls, search=None): mems = {} - for key in cls.keys(): + for key in cls.keys(search): try: mems[key] = cls.getmem(key) except Exception as e: diff --git a/commune/module/module.py b/commune/module/module.py index 38eeac9a9..fd484499b 100755 --- a/commune/module/module.py +++ b/commune/module/module.py @@ -3289,6 +3289,9 @@ def module(cls,module: Any = 'module' , **kwargs): ''' Wraps a python class as a module ''' + shortcuts = c.shortcuts() + if module in shortcuts: + module = shortcuts[module] module_class = c.get_module(module,**kwargs) return module_class m = mod = module diff --git a/commune/remote/app.py b/commune/remote/app.py index 5368d0b55..318ba307c 100644 --- a/commune/remote/app.py +++ b/commune/remote/app.py @@ -148,6 +148,7 @@ def ssh(self): host_map = self.host_map cols = st.columns([5,1]) + cmd = cols[0].text_input('Command', 'ls') [cols[1].write('') for i in range(2)] if 'x' not in fn_code: diff --git a/commune/remote/remote.py b/commune/remote/remote.py index f49adf776..34212c842 100644 --- a/commune/remote/remote.py +++ b/commune/remote/remote.py @@ -114,10 +114,8 @@ def print_output(): yield line # if there is an stderr, print it - cnt = 0 + for line in stderr.readlines(): - if cnt == 0: - yield '---- ERROR ----' if verbose: c.print(f'[bold]{host["name"]}[/bold]', line.strip('\n')) yield line diff --git a/commune/subspace/subspace.py b/commune/subspace/subspace.py index ef1742170..39b787f73 100644 --- a/commune/subspace/subspace.py +++ b/commune/subspace/subspace.py @@ -2517,28 +2517,19 @@ def register( if isinstance(netuid, str): subnet = netuid netuid = subnet2netuid.get(netuid, 0) - - c.print(f"Registering {name} with {stake} stake on {subnet} subnet") - - if netuid not in netuid2subnet: + + if netuid in netuid2subnet: + subnet = netuid2subnet[netuid] + else: netuid = 0 response = input(f"Do you want to create a new subnet ({subnet}) (yes or y or dope): ") if response.lower() not in ["yes", 'y', 'dope']: return {'success': False, 'msg': 'Subnet not found and not created'} - + # require prompt to create new subnet - stake = stake or 0 min_register_stake = self.min_register_stake(netuid=netuid, network=network) stake = max(min_register_stake, stake) - - if c.key_exists(name): - mkey = c.get_key(name) - mkey_balance = self.get_balance(key=mkey.ss58_address, network=network) - if mkey_balance > stake: - c.print(f'Using {name} key to register {name} with {stake} stake') - key = mkey - stake = stake * 1e9 if '0.0.0.0' in address: @@ -3314,8 +3305,12 @@ def set_weights( if modules == None: modules = c.shuffle(self.uids(netuid=netuid, update=update)) # checking if the "uids" are passed as names -> strings + key2name, name2uid = None, None for i, module in enumerate(modules): if isinstance(module, str): + if key2name == None: + key2name = self.key2name(netuid=netuid, update=update) + name2uid = self.name2uid(netuid=netuid, update=update) if module in key2name: modules[i] = key2name[module] elif module in name2uid: @@ -3325,7 +3320,6 @@ def set_weights( if weights is None: weights = [1 for _ in uids] - max_weight = max(weights) if len(uids) < subnet_params['min_allowed_weights']: n = self.n(netuid=netuid) while len(uids) < subnet_params['min_allowed_weights']: @@ -3335,13 +3329,24 @@ def set_weights( weights.append(min_value) uid2weight = dict(sorted(zip(uids, weights), key=lambda item: item[1], reverse=True)) + + self_uid = self.key2uid(netuid=netuid).get(key.ss58_address, None) + uid2weight.pop(self_uid, None) + uids = list(uid2weight.keys()) weights = list(uid2weight.values()) + + + if len(uids) > subnet_params['max_allowed_weights']: + uids = uids[:subnet_params['max_allowed_weights']] + weights = weights[:subnet_params['max_allowed_weights']] + + + c.print(f'Voting for {len(uids)} modules') assert len(uids) == len(weights), f"Length of uids {len(uids)} must be equal to length of weights {len(weights)}" - uids = torch.tensor(uids)[:n] - weights = torch.tensor(weights)[:n] + uids = torch.tensor(uids) + weights = torch.tensor(weights) weights = weights / weights.sum() # normalize the weights between 0 and 1 - # STEP 2: CLAMP THE WEIGHTS BETWEEN 0 AND 1 WITH MIN AND MAX VALUES assert min_value >= 0 and max_value <= 1, f"min_value and max_value must be between 0 and 1" weights = torch.clamp(weights, min_value, max_value) # min_value and max_value are between 0 and 1 @@ -3350,6 +3355,8 @@ def set_weights( weights = list(map(lambda x : int(min(x, U16_MAX)), weights.tolist())) uids = list(map(int, uids.tolist())) + + params = {'uids': uids, 'weights': weights, 'netuid': netuid} @@ -3409,6 +3416,7 @@ def register_servers(self, c.print(f"Registering {info['name']} with module_key {info['ss58_address']} using launcher {launcher_key}") f = c.submit(c.register, kwargs=dict(name=info['name'], address= info['address'], + netuid = netuid, module_key=info['ss58_address'], key=launcher_key), timeout=timeout) futures+= [f] diff --git a/commune/vali/vali.py b/commune/vali/vali.py index fe1aad026..48a5c31b5 100644 --- a/commune/vali/vali.py +++ b/commune/vali/vali.py @@ -162,7 +162,7 @@ def epoch(self, batch_size = None, network=None, **kwargs): module_addresses = c.shuffle(list(self.namespace.values())) c.print(f'Epoch {self.epochs} with {len(module_addresses)} modules', color='yellow') batch_size = min(self.config.batch_size, len(module_addresses)) - self.executor = c.module('executor.thread')(max_workers=batch_size) + self.executor = c.module('executor.thread')(max_workers=min(1, batch_size)) batch_size = self.config.batch_size for module_address in module_addresses: diff --git a/commune/vali/vali.yaml b/commune/vali/vali.yaml index 033d48066..d6c1b3636 100644 --- a/commune/vali/vali.yaml +++ b/commune/vali/vali.yaml @@ -1,7 +1,7 @@ # network network: subspace # the network to connect to (bittensor, commune, local) to specify subnet, (subspace.0 or subspace.text) -netuid: 0 # (OPTIONAL) if you have a voting network with subnets [bittensor, commune] +netuid: null # (OPTIONAL) if you have a voting network with subnets [bittensor, commune] sync_interval: 60 sample_interval: 0.005 # the sleep interval between sending samples search: null # the search for modules in the namespace diff --git a/commune/watchdog/watchdog.py b/commune/watchdog/watchdog.py index 8bb47e1cc..4ca473674 100644 --- a/commune/watchdog/watchdog.py +++ b/commune/watchdog/watchdog.py @@ -1,26 +1,28 @@ import commune as c class Watchdog(c.Module): - def __init__(self): + def __init__(self, modules=['module', 'subspace']): + self.modules = {m: c.module(m) for m in modules} + self.module = c.module('watchdog') c.thread(self.run_loop) def sync(self): + self.modules = {m: c.module(m) for m in self.modules} c.print('syncing...') c.ip(update=1) c.tree(update=1) c.namespace(update=1) c.print('synced') - self.subspace = c.module('subspace') + self.subspace = c.get_module('subspace') self.subspace.stake_from(netuid='all') - def run_loop(self, sleep_time=30): + def run_loop(self, sleep_time=30): while True: try: self.sync() except Exception as e: - print(e) - pass - c.sleep(sleep_time) - print(f'sleeping for {sleep_time} seconds') + e = c.detailed_error(e) + c.print(f'Error syncing, restarting {e}') + diff --git a/modules/comchat/comchat.py b/modules/comchat/comchat.py new file mode 100644 index 000000000..ac37d1206 --- /dev/null +++ b/modules/comchat/comchat.py @@ -0,0 +1,10 @@ +import commune as c + + +class Comchat(c.Module): + def __init__(self, model='model.openrouter' , **kwargs): + self.model = c.module(model)(**kwargs) + def generate(self, text, **kwargs): + return self.model.generate(text, **kwargs) + def test(self): + return self.generate('hello world') \ No newline at end of file diff --git a/sandbox.py b/sandbox.py index 5328ee55a..ec473d22d 100644 --- a/sandbox.py +++ b/sandbox.py @@ -1 +1,26 @@ -print('fam') \ No newline at end of file +import commune as c + + +def register_servers(netuid=6, buffer = 100, timeout = 60): + subspace = c.module('subspace')() + stake = subspace.min_register_stake(netuid=netuid) + buffer + key2balance = {k:v for k,v in c.key2balance().items() if v > stake} + keys = list(key2balance.keys()) + c.print(f'Keys for registration: {keys}') + servers = c.servers() + c.print(f'Servers: {servers}') + futures = [] + for i, s in enumerate(c.servers()): + c.print(f'Registering {s}') + key = keys[i % len(keys)] + future = c.submit(c.register, kwargs=dict(name=s, netuid=netuid, stake=stake, key=key ), timeout=timeout) + futures.append(future) + + for f in c.as_completed(futures, timeout=timeout): + c.print(f.result()) + + +while True: + register_servers() +# for s in c.servers(): +# c.register(s, netuid=) \ No newline at end of file diff --git a/subnet/comchat/comchat.py b/subnet/comchat/comchat.py new file mode 100644 index 000000000..ac37d1206 --- /dev/null +++ b/subnet/comchat/comchat.py @@ -0,0 +1,10 @@ +import commune as c + + +class Comchat(c.Module): + def __init__(self, model='model.openrouter' , **kwargs): + self.model = c.module(model)(**kwargs) + def generate(self, text, **kwargs): + return self.model.generate(text, **kwargs) + def test(self): + return self.generate('hello world') \ No newline at end of file