Skip to content

Commit

Permalink
docker fix
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Aug 24, 2024
1 parent f636713 commit c4480b2
Show file tree
Hide file tree
Showing 28 changed files with 398 additions and 414 deletions.
25 changes: 7 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
# THE GENERAL CONTAINER FOR CONNECTING ALL THE ENVIRONMENTS 😈
FROM ubuntu:22.04

#SYSTEM
ENV LIBNAME=commune
WORKDIR /app
ARG DEBIAN_FRONTEND=noninteractive
RUN usermod -s /bin/bash root
RUN apt-get update

#RUST
RUN apt-get install curl nano build-essential cargo libstd-rust-dev -y

#JS
RUN apt-get install -y nodejs npm
RUN npm install -g pm2
ENV LIBNAME commune
ENV PWD /app
WORKDIR /app
RUN git clone https://github.com/commune-ai/commune.git /commune
RUN pip install -e /commune

#PYTHON
RUN apt-get install python3 python3-pip python3-venv -y
RUN git clone -b main https://github.com/commune-ai/commune.git /commune
RUN pip install -e /commune

WORKDIR /app
# TODO DOCKER

# WANT TO HAVE TO REBUILD THE WHOLE IMAGE EVERY TIME WE CHANGE THE REQUIREMENTS
COPY ./requirements.txt /app/requirements.txt
COPY ./setup.py /app/setup.py
COPY ./README.md /app/README.md

RUN pip install -e ./
# THIS IS FOR THE LOCAL PACKAG
COPY ./ /app
# git safety for app
RUN git config --global --add safe.directory /app
RUN git config pull.rebase false
# IMPORT EVERYTHING ELSE
ENTRYPOINT [ "tail", "-f", "/dev/null"]
85 changes: 52 additions & 33 deletions commune/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ class App(c.Module):
port_range = [8501, 8600]
name_prefix = 'app::'


def get_free_port(self, module, port=None, update=False):
app2info = self.get('app2info', {})
if update:
return c.free_port()
port = app2info.get(module, {}).get('port', None)
if port == None:
port = c.free_port()
return port

def start(self,
module:str = 'server',
name : Optional[str] = None,
Expand All @@ -18,52 +28,46 @@ def start(self,
kwargs:dict=None,
cmd = None,
update:bool=False,
cwd = None,
**extra_kwargs):


module = c.shortcuts().get(module, module)
app2info = self.get('app2info', {})
kwargs = kwargs or {}
name = name or module
port = port or app2info.get(name, {}).get('port', c.free_port())
if update:
port = c.free_port()
process_name = self.name_prefix + name
if c.port_used(port):
c.kill_port(port)
c.pm2_kill(process_name)
if c.module_exists(module + '.app'):
module = module + '.app'
kwargs_str = json.dumps(kwargs or {}).replace('"', "'")
module_class = c.module(module)
cmd = cmd or f'streamlit run {module_class.filepath()} --server.port {port} -- --fn {fn} --kwargs "{kwargs_str}"'


cwd = cwd or os.path.dirname(module_class.filepath())

process_name:str=None,
cwd = None):
port = self.get_free_port(module=module, port=port, update=update)
if remote:
if self.app_exists(name):
self.kill_app(name)
rkwargs = c.locals2kwargs(locals())
rkwargs['remote'] = False
del rkwargs['module_class']
del rkwargs['app2info']
del rkwargs['process_name']
self.remote_fn(
fn='start',
name=self.name_prefix + name ,
name=self.name_prefix + module ,
kwargs= rkwargs)

return {
'name': name,
'cwd': cwd,
'fn': fn,
'success': True,
'module': module,
'address': {
'local': f'http://localhost:{port}',
'public': f'http://{c.ip()}:{port}',
}
} ,
'kwargs': rkwargs

}


module = c.shortcuts().get(module, module)

kwargs = kwargs or {}
name = name or module
port = port or self.get_free_port(module)
# if the process is already running, kill it
# if the module is an app, we need to add the .app to the module name
if c.module_exists(module + '.app'):
module = module + '.app'
app2info = self.app2info()
kwargs_str = json.dumps(kwargs or {}).replace('"', "'")
module_class = c.module(module)
cmd = cmd or f'streamlit run {module_class.filepath()} --server.port {port} -- --fn {fn} --kwargs "{kwargs_str}"'
cwd = cwd or os.path.dirname(module_class.filepath())

module = c.module(module)
app_info= {
'name': name,
Expand All @@ -85,13 +89,23 @@ def app2info(self):
app2info = self.get('app2info', {})
if not isinstance(app2info, dict):
app2info = {}
changed = False
og_app2info = app2info.copy()
for name, info in og_app2info.items():
if not c.port_used(info['port']):
c.print(f'Port {info["port"]} is not used. Killing {name}')
changed = True
del app2info[name]
if changed:
self.put('app2info', app2info)

return app2info


def kill_all(self):
return c.module('pm2').kill_many(self.apps())

def kill(self, name):
def kill_app(self, name):
return c.module('pm2').kill(self.name_prefix+name)

def filter_name(self, name:str) -> bool:
Expand All @@ -104,6 +118,11 @@ def apps(self, remove_prefix = True):
apps = [n[len(self.name_prefix):] for n in apps]
return apps


def app_exists(self, name):
return name in self.apps()


def app_modules(self, **kwargs):
return list(set([m.replace('.app','') for m in self.modules() if self.has_app(m, **kwargs)]))

Expand Down
54 changes: 27 additions & 27 deletions commune/chat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def generate(self, data):
params = c.copy(data['data'])
input = params.pop('input')
system_prompt = params.pop('system_prompt', None)

if system_prompt:
input = system_prompt + '\n' + input
r = self.model.generate( input,stream=1, **params)
Expand All @@ -71,19 +70,17 @@ def save_data(self, data):

def get_params(self):
model = st.selectbox('Model', self.models)
with st.sidebar.expander('Parameters', False):


temperature = st.slider('Temperature', 0.0, 1.0, 0.5)
temperature = st.slider('Temperature', 0.0, 1.0, 0.5)

if hasattr(self.model, 'get_model_info'):
model_info = self.model.get_model_info(model)
max_tokens = min(int(model_info['context_length']*0.9), self.max_tokens)
else:
model_info = {}
max_tokens = self.max_tokens
max_tokens = st.number_input('Max Tokens', 1, max_tokens, max_tokens)
system_prompt = st.text_area('System Prompt',self.system_prompt, height=200)
if hasattr(self.model, 'get_model_info'):
model_info = self.model.get_model_info(model)
max_tokens = min(int(model_info['context_length']*0.9), self.max_tokens)
else:
model_info = {}
max_tokens = self.max_tokens
max_tokens = st.number_input('Max Tokens', 1, max_tokens, max_tokens)
system_prompt = st.text_area('System Prompt',self.system_prompt, height=200)

input = st.text_area('Text',self.text, height=100)

Expand Down Expand Up @@ -111,8 +108,7 @@ def sidebar(self, user='user', password='password', seperator='::'):
'path': self.resolve_path('history', self.key.ss58_address ),
'history': self.history(self.key.ss58_address)
})
with st.expander('History', expanded=False):
self.search_hsitory()

def search_history(self):
search = st.text_input('Search')
# if the search is in any of the columns
Expand All @@ -134,7 +130,9 @@ def app(self):
self.history_page()

def chat_page(self):
data = c.ticket(self.get_params(), key=self.key)
with st.sidebar.expander('Params', expanded=True):
params = self.get_params()
data = c.ticket(params, key=self.key)

# make the buttons cover the whole page
cols = st.columns([1,1])
Expand All @@ -147,18 +145,20 @@ def chat_page(self):
reverse_emojis = emojis[::-1]
with st.spinner(f'{emojis} Generating {reverse_emojis}'):
st.write_stream(r)

with st.expander('Post Processing', False):
lambda_string = st.text_area('fn(x={model_output})', 'x', height=100)
prefix = 'lambda x: '
lambda_string = prefix + lambda_string if not lambda_string.startswith(prefix) else lambda_string
lambda_fn = eval(lambda_string)
print(data)
try:
output = data['data']['output']
output = lambda_fn(output)
except Exception as e:
st.error(e)

self.post_processing(data)


def post_processing(self, data):
lambda_string = st.text_area('fn(x={model_output})', 'x', height=100)
prefix = 'lambda x: '
lambda_string = prefix + lambda_string if not lambda_string.startswith(prefix) else lambda_string
lambda_fn = eval(lambda_string)
try:
output = data['data']['output']
output = lambda_fn(output)
except Exception as e:
st.error(e)

def history_page(self):
history = self.data.history
Expand Down
20 changes: 14 additions & 6 deletions commune/module/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,17 @@ def module_exists(cls, module:str, **kwargs) -> bool:
def has_app(cls, module:str, **kwargs) -> bool:
return cls.module_exists(module + '.app', **kwargs)

@classmethod
def simplify_paths(cls, paths):
paths = [cls.simplify_path(p) for p in paths]
paths = [p for p in paths if p]
return paths

@classmethod
def simplify_path(cls, p, avoid_terms=['modules']):
chunks = p.split('.')
if len(chunks) < 2:
return None
file_name = chunks[-2]
chunks = chunks[:-1]
path = ''
Expand Down Expand Up @@ -448,18 +456,18 @@ def simplify_path(cls, p, avoid_terms=['modules']):

@classmethod
def local_modules(cls, search=None):
objects = cls.find_classes(cls.pwd())
object_paths = [cls.simplify_path(obj) for obj in objects]
object_paths = cls.find_classes(cls.pwd())
object_paths = cls.simplify_paths(object_paths)
if search != None:
object_paths = [obj for obj in object_paths if search in obj]
object_paths = [p for p in object_paths if search in p]
return sorted(list(set(object_paths)))

@classmethod
def lib_modules(cls, search=None):
objects = cls.find_classes(cls.libpath, )
object_paths = [cls.simplify_path(obj) for obj in objects]
object_paths = cls.find_classes(cls.libpath )
object_paths = cls.simplify_paths(object_paths)
if search != None:
object_paths = [obj for obj in object_paths if search in obj]
object_paths = [p for p in object_paths if search in p]
return sorted(list(set(object_paths)))

@classmethod
Expand Down
4 changes: 1 addition & 3 deletions commune/module/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,9 @@ def set_env(cls, key:str, value:str)-> None:

@classmethod
def pwd(cls):
pwd = os.getenv('PWD', '/app') # the current wor king directory from the process starts
pwd = os.getenv('PWD', '/commune') # the current wor king directory from the process starts
return pwd



@classmethod
def choice(cls, options:Union[list, dict])->list:
options = deepcopy(options) # copy to avoid changing the original
Expand Down
16 changes: 2 additions & 14 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,33 +925,21 @@ def remote_fn(cls,

kwargs = kwargs if kwargs else {}
args = args if args else []
if name == None:
module_path = cls.resolve_object(module).module_name()
name = f"{module_path}{tag_seperator}{fn}"
if tag != None:
name = f'{name}{tag_seperator}{tag}'

if 'remote' in kwargs:
kwargs['remote'] = False

cwd = cwd or cls.dirpath()

cwd = cwd or cls.dirpath()
kwargs = kwargs or {}
args = args or []

module = cls.resolve_object(module)

# resolve the name
if name == None:
# if the module has a module_path function, use that as the name
if hasattr(module, 'module_path'):
name = module.module_name()
else:
name = module.__name__.lower()
# resolve the tag
if tag != None:
name = f'{name}{tag_seperator}{tag}'


c.print(f'[bold cyan]Launching --> <<[/bold cyan][bold yellow]class:{module.__name__}[/bold yellow] [bold white]name[/bold white]:{name} [bold white]fn[/bold white]:{fn} [bold white]mode[/bold white]:{mode}>>', color='green')

launch_kwargs = dict(
Expand Down
3 changes: 0 additions & 3 deletions commune/modules/pm2/pm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def kill_many(cls, search=None, verbose:bool = True, timeout=10):

@classmethod
def kill_all(cls, verbose:bool = True, timeout=10, trials=10):
results = {}
while len(cls.servers()) > 0:
results = cls.kill_many(search=None, verbose=verbose, timeout=timeout)
trials -= 1
Expand All @@ -122,7 +121,6 @@ def servers(cls, search=None, verbose:bool = False) -> List[str]:
if 'errored' in line:
cls.kill(server_name, verbose=True)
continue

module_list += [server_name]

if search != None:
Expand All @@ -135,7 +133,6 @@ def servers(cls, search=None, verbose:bool = False) -> List[str]:

pm2ls = servers


# commune.run_command('pm2 status').stdout.split('\n')[5].split(' │')[0].split(' │ ')[-1]commune.run_command('pm2 status').stdout.split('\n')[5].split(' │')[0].split(' │ ')[-1]

@classmethod
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit c4480b2

Please sign in to comment.