def run_model(model_id, input_dict, output_names): """ Load the model, set the inputs and return the calculated outputs TODO get this working roughly following these steps - Get serialised model from S3 - Load model with koala - Extract inputs from payload - Set the inputs in the model - Extract required outputs from payload (all outputs if none specifically requested) - Get the required outputs from the model - Build and return response """ # see if compiled model compiled try: compliled_string = bucket.Object( 'compiled_models/{}'.format(model_id)).get()['Body'].read() except botocore.exceptions.ClientError as err: return err.response['Error']['Code'] # XXX HACK Workaround needed for koala spreadsheet loading API # - need to write the file to a temp location for koala to read it... # - FIX: update koala.Spreadsheet / koala.serialize to take the file contents in directly if not os.path.exists('/tmp'): os.mkdir('/tmp') dummy_file_name = '/tmp/temp_{}.gzip'.format(model_id) with open(dummy_file_name, 'wb') as f: f.write(compliled_string) sheet = Spreadsheet.load(dummy_file_name) for name, value in input_dict.iteritems(): sheet.set_value(name, value) results = {} for name in output_names: output_value = sheet.evaluate(name) if isinstance(output_value, ExcelError): output_value = str(output_value) results[name] = output_value # Cleanup previous workaround os.remove(dummy_file_name) if not os.listdir('/tmp'): os.rmdir('/tmp') return results
from koala.ExcelCompiler import ExcelCompiler from koala.Spreadsheet import Spreadsheet filename = "./examples/basic.xlsx" print(filename) ### Graph Generation ### c = ExcelCompiler(filename) sp = c.gen_graph() ## Graph Serialization ### print("Serializing to disk...") sp.dump(filename.replace("xlsx", "gzip")) ### Graph Loading ### print("Reading from disk...") sp = Spreadsheet.load(filename.replace("xlsx", "gzip")) ### Graph Evaluation ### sp.set_value('Sheet1!A1', 10) print('New D1 value: %s' % str(sp.evaluate('Sheet1!D1')))
from koala.ExcelCompiler import ExcelCompiler from koala.Spreadsheet import Spreadsheet file = "./examples/basic.xlsx" print file ### Graph Generation ### c = ExcelCompiler(file) sp = c.gen_graph() ## Graph Serialization ### print "Serializing to disk..." sp.dump(file.replace("xlsx", "gzip")) ### Graph Loading ### print "Reading from disk..." sp = Spreadsheet.load(file.replace("xlsx", "gzip")) ### Graph Evaluation ### sp.set_value('Sheet1!A1', 10) print 'New D1 value: %s' % str(sp.evaluate('Sheet1!D1'))