Skip to content

Commit

Permalink
added Python API and "run via docker" in CM GUI based on mlcommons#1074
Browse files Browse the repository at this point in the history
  • Loading branch information
gfursin committed Jan 29, 2024
1 parent 12a6a99 commit 53a1da5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
51 changes: 44 additions & 7 deletions cm-mlops/automation/script/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def run(self, i):
* state (dict): global state (updated by this script - includes new_state)
"""

from cmind import utils
import copy
import time
Expand Down Expand Up @@ -455,9 +455,10 @@ def run(self, i):

cm_script_info += '"'

if verbose:
print ('')
# if verbose:
# print ('')

print ('')
print (recursion_spaces + '* ' + cm_script_info)


Expand Down Expand Up @@ -2162,7 +2163,7 @@ def add(self, i):

console = i.get('out') == 'con'

# Try to find experiment artifact by alias and/or tags
# Try to find script artifact by alias and/or tags
ii = utils.sub_input(i, self.cmind.cfg['artifact_keys'])

parsed_artifact = i.get('parsed_artifact',[])
Expand Down Expand Up @@ -3416,7 +3417,7 @@ def get_default_path_list(self, i):
############################################################
def doc(self, i):
"""
Add CM automation.
Document CM script.
Args:
(CM input dict):
Expand All @@ -3441,11 +3442,47 @@ def doc(self, i):

return utils.call_internal_module(self, __file__, 'module_misc', 'doc', i)

############################################################
def gui(self, i):
"""
Run GUI for CM script.
Args:
(CM input dict):
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""

artifact = i.get('artifact', '')
tags = ''
if artifact != '':
if ' ' in artifact:
tags = artifact.replace(' ',',')

if tags=='':
tags = i.get('tags','')

if 'tags' in i:
del(i['tags'])

i['action']='run'
i['artifact']='gui'
i['parsed_artifact']=[('gui','605cac42514a4c69')]
i['script']=tags.replace(',',' ')

return self.cmind.access(i)



############################################################
def dockerfile(self, i):
"""
Add CM automation.
Generate Dockerfile for CM script.
Args:
(CM input dict):
Expand Down Expand Up @@ -3473,7 +3510,7 @@ def dockerfile(self, i):
############################################################
def docker(self, i):
"""
Add CM automation.
Run CM script in an automatically-generated container.
Args:
(CM input dict):
Expand Down
54 changes: 52 additions & 2 deletions cm-mlops/script/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,26 +217,38 @@ def main():
tags = '--tags={}'.format(x)

# Check flags
flags_dict = {}
flags = ''
for key in st_inputs:
value = st_inputs[key]
key2 = key[1:]

if value!='' and (type(value)!=bool or value==True):
flags+=' '+var1+'\n --'+key2

z = True
if type(value)!=bool:
x = str(value)
z = x

if ' ' in x or ':' in x or '/' in x or '\\' in x:
x='"'+x+'"'
flags+='='+x

flags_dict[key2]=z

########################################################
# Extra CMD
st.markdown("""---""")
cmd_extension = st.text_input("Add extra to CM script command line").strip()

run_via_docker = False
if len(meta.get('docker',{}))>0:
run_via_docker = st.checkbox('Run via Docker', key='run_via_docker', value=False)

# Prepare CLI
cli = 'cm run script {} {} '.format(tags, flags,)
action = 'docker' if run_via_docker else 'run'
cli = 'cm {} script {} {} '.format(action, tags, flags,)

x = '' if cmd_extension=='' else var1+'\n '+cmd_extension

Expand All @@ -248,6 +260,7 @@ def main():
# if no_run=='':
# cli+=' --pause\n'


# Print CLI
st.markdown("""---""")

Expand Down Expand Up @@ -278,7 +291,44 @@ def main():

st.text_area('**Install [CM interface](https://github.com/mlcommons/ck) with a few dependencies:**', x, height=170)

cli = st.text_area('**Run CM script:**', cli, height=500)

st.markdown("**Run CM script from Python:**")

# Python API
dd = {
'action':action,
'automation':'script,5b4e0237da074764',
'tags':script_tags
}

x = script_tags
if len(selected_variations)>0:
for sv in selected_variations:
x+=' '+sv

dd['tags']=x.replace(' ',',')

dd.update(flags_dict)

import json
dd_json=json.dumps(dd, indent=2)

y = 'import cmind\n'
y+= 'r = cmind.access('+dd_json+')\n'
y+= 'if r[\'return\']>0: print (r)\n'

x='''
```python
{}
'''.format(y)

# st.write(x.replace('\n','<br>\n'), unsafe_allow_html=True)

st.markdown(x)


cli = st.text_area('**Run CM script from command line:**', cli, height=500)



# Add explicit button "Run"
Expand Down

0 comments on commit 53a1da5

Please sign in to comment.