Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed May 3, 2024
1 parent 7b38bec commit 2c87818
Show file tree
Hide file tree
Showing 75 changed files with 567 additions and 459 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ RUN pip install -r requirements.txt
RUN apt-get install -y nodejs npm
RUN npm install -g pm2
COPY . /commune
RUN pip install -e ./

ENTRYPOINT ["tail", "-f", "/dev/null"]
ENTRYPOINT ["pip", "install", "-e", "./", "tail", "-f", "/dev/null"]
File renamed without changes.
6 changes: 0 additions & 6 deletions commune/app/login/login.py → app/login/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,4 @@ def app(self, x:int = 1, y:int = 2) -> int:
st.write(self.key.ss58_address)








Login.run(__name__)
53 changes: 0 additions & 53 deletions chat/app/bro/chat_app_bro.py

This file was deleted.

11 changes: 0 additions & 11 deletions chat/app/bro/miner.py

This file was deleted.

41 changes: 0 additions & 41 deletions chat/app/bro/subnet.md

This file was deleted.

9 changes: 0 additions & 9 deletions chat/app/chat_app.py

This file was deleted.

2 changes: 0 additions & 2 deletions chat/app/chat_app.yaml

This file was deleted.

1 change: 0 additions & 1 deletion chat/app/utils.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
167 changes: 99 additions & 68 deletions commune/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def __init__(
loop: 'asyncio.EventLoop' = None,
debug: bool = False,
serializer= 'serializer',
default_fn = 'info',

**kwargs
):
self.loop = c.get_event_loop() if loop == None else loop
Expand All @@ -32,23 +34,10 @@ def __init__(
self.save_history = save_history
self.history_path = history_path
self.debug = debug
self.default_fn = default_fn

def prepare_request(self, args: list = None, kwargs: dict = None, params=None, message_type = "v0"):

async def async_forward(self,
fn: str,
args: list = None,
kwargs: dict = None,
params: dict = None,
address : str = None,
timeout: int = 10,
headers : dict ={'Content-Type': 'application/json'},
message_type = "v0",
default_fn = 'info',
verbose = False,
debug = True,
**extra_kwargs
):
if isinstance(args, dict):
kwargs = args
args = None
Expand All @@ -60,10 +49,6 @@ async def async_forward(self,
elif isinstance(params, dict):
kwargs = params
kwargs = kwargs or {}
kwargs.update(extra_kwargs)
fn = fn or default_fn

address = address or self.address
args = args if args else []
kwargs = kwargs if kwargs else {}

Expand All @@ -72,64 +57,29 @@ async def async_forward(self,
"kwargs": kwargs,
"timestamp": c.timestamp(),
}
self.count += 1

# serialize this into a json string
if message_type == "v0":
request = self.serializer.serialize(input)
request = self.key.sign(request, return_json=True)
# key emoji

elif message_type == "v1":
input['ticket'] = self.key.ticket()
request = self.serializer.serialize(input)
else:
raise ValueError(f"Invalid message_type: {message_type}")

url = f"{address}/{fn}/"
if not url.startswith('http'):
url = 'http://' + url
result = await self.process_request(url, request, headers=headers, timeout=timeout)

c.print(f"🛰️ Call {self.address}/{fn} 🛰️ (🔑{self.key.ss58_address})", color='green', verbose=verbose)

if self.save_history:
input['fn'] = fn
input['result'] = result
input['module'] = self.address
input['latency'] = c.time() - input['timestamp']
path = self.history_path+ '/' + self.key.ss58_address + '/' + self.address+ '/'+ str(input['timestamp'])
self.put(path, input)
return result

return request

def age(self):
return self.start_timestamp - c.timestamp()

async def send_request(self, url:str, request: dict, headers=None, timeout:int=10, verbose=False):
# start a client session and send the request

def set_client(self,
address : str = None,
verbose: bool = 1,
network : str = 'local',
possible_modes = ['http', 'https'],
):
# we dont want to load the namespace if we have the address
if not c.is_address(address):
module = address # we assume its a module name
assert module != None, 'module must be provided'
namespace = c.get_namespace(search=module, network=network)
if module in namespace:
address = namespace[module]
else:
address = module
if '://' in address:
mode = address.split('://')[0]
assert mode in possible_modes, f'Invalid mode {mode}'
address = address.split('://')[-1]
address = address.replace(c.ip(), '0.0.0.0')
self.address = address
return {'address': self.address}
if not url.startswith('http'):
url = 'http://' + url

c.print(f"🛰️ Call {url} 🛰️ (🔑{self.key.ss58_address})", color='green', verbose=verbose)

async def process_request(self, url:str, request: dict, headers=None, timeout:int=10):
# start a client session and send the request
async with aiohttp.ClientSession() as session:
async with session.post(url, json=request, headers=headers) as response:

Expand Down Expand Up @@ -185,11 +135,7 @@ async def process_request(self, url:str, request: dict, headers=None, timeout:in

return result

@classmethod
def history(cls, key=None, history_path='history'):
key = c.get_key(key)
return cls.ls(history_path + '/' + key.ss58_address)


def process_output(self, result):
## handles
if isinstance(result, str):
Expand All @@ -199,6 +145,91 @@ def process_output(self, result):
return result['data']
else:
return result


def resolve_key(self,key=None):
if key == None:
key = self.key
if isinstance(key, str):
key = c.get_key(key)
return key

def prepare_url(self, address, fn):
address = address or self.address
fn = fn or self.default_fn
if '/' in address.split('://')[-1]:
address = address.split('://')[-1]
url = f"{address}/{fn}/"
return url

async def async_forward(self,
fn: str,
args: list = None,
kwargs: dict = None,
params: dict = None,
address : str = None,
timeout: int = 10,
headers : dict ={'Content-Type': 'application/json'},
message_type = "v0",
key : str = None,
verbose = 1,
**extra_kwargs
):
key = self.resolve_key(key)
url = self.prepare_url(address, fn)

# resolve the kwargs at least
kwargs =kwargs or {}
kwargs.update(extra_kwargs)
request = self.prepare_request(args=args, kwargs=kwargs, params=params, message_type=message_type)
result = await self.send_request(url=url, request=request, headers=headers, timeout=timeout, verbose=verbose)
if self.save_history:
input = self.serializer.deserialize(request)
path = self.history_path+ '/' + self.key.ss58_address + '/' + self.address+ '/'+ str(input['timestamp'])

output = {
'address': address,
'fn': fn,
'input': input,
'result': result,
'latency': c.time() - input['timestamp'],
}
self.put(path, output)
return result


def age(self):
return self.start_timestamp - c.timestamp()

def set_client(self,
address : str = None,
verbose: bool = 1,
network : str = 'local',
possible_modes = ['http', 'https'],
):
# we dont want to load the namespace if we have the address
if not c.is_address(address):
module = address # we assume its a module name
assert module != None, 'module must be provided'
namespace = c.get_namespace(search=module, network=network)
if module in namespace:
address = namespace[module]
else:
address = module
if '://' in address:
mode = address.split('://')[0]
assert mode in possible_modes, f'Invalid mode {mode}'
address = address.split('://')[-1]
address = address.replace(c.ip(), '0.0.0.0')
self.address = address
return {'address': self.address}

@classmethod
def history(cls, key=None, history_path='history'):
key = c.get_key(key)
return cls.ls(history_path + '/' + key.ss58_address)



def forward(self,*args,return_future:bool=False, timeout:str=4, **kwargs):
forward_future = asyncio.wait_for(self.async_forward(*args, **kwargs), timeout=timeout)
Expand Down
Loading

0 comments on commit 2c87818

Please sign in to comment.