def get_abstractions(): ''' Returns all the supported abstractions for the system @returns: Supported abstractions ''' logger.info('Get Supported Abstractions.') return [value for _, value in abstractions.ABSTRACTIONS.items()]
def get_example(abstraction): ''' Returns an example of the abstraction given, if the abstraction is invalid it will return a 400 error ''' logger.info('REQUEST: /concrete/example/{}'.format(abstraction)) concrete_example = get_concrete_example(abstraction) if concrete_example is not None: return jsonify({ 'example': concrete_example, 'abstraction': abstraction }) else: raise InvalidUsage('The abstraction {} does not exist.'.format(abstraction.upper()), status_code=400)
def get_example_abstraction(): ''' Returns the abstraction for a given example, will return 201 if found abstraction else it will return 200 ''' logger.info('REQUEST: /') body = request.get_json() abstraction = get_abstraction(body) if abstraction is not None: return jsonify({ 'abstraction': abstraction, 'target': body['value'] }), 201 else: raise InvalidUsage('Could not find an abstraction for: {}.'.format(body['value']), status_code=200)
def get_abstraction(concrete_example): ''' Gets an abstraction for the given example @concrete_example: dict containing a value and label for the example @returns: String abstraction name ''' logger.info('Get Abstraction: {}'.format(concrete_example)) example_str = '{} {}'.format(concrete_example['label'], concrete_example['value']) logger.info('Formatted example: {}'.format(example_str)) abstract = abstractor.Abstractor() return abstract.get_abstraction(example_str)
def get_concrete_example(abstraction): ''' Returns an example given a valid abstraction @abstraction: string abstraction @returns: random concrete example of abstraction ''' logger.info('Get Concrete Example: {}'.format(abstraction)) data_frame = None if abstractions.ABSTRACTIONS['FIRST_NAME'].lower() == abstraction.lower(): data_frame = model_utils.load_first_names() elif abstractions.ABSTRACTIONS['LAST_NAME'].lower() == abstraction.lower(): data_frame = model_utils.load_last_names() elif abstractions.ABSTRACTIONS['EMAIL'].lower() == abstraction.lower(): data_frame = model_utils.load_emails() if data_frame is None: logger.info('No examples found for: {}'.format(abstraction)) return None word = data_frame.ix[np.random.randint(0, data_frame.shape[0]), 'word'] logger.info('Found example: {}'.format(word)) return word
def create_app(config=None, environment=None): ''' Flask App Factory method @config: Flask configuration dict @environment: Flask environment @returns: Flask app instance ''' logger.info('Creating Flask app.') app = Flask(__name__) logger.info('Setting Flask configs.') app.config['ENVIRONMENT'] = environment app.config.update(config or {}) logger.info('Registering Flask blueprints.') app.register_blueprint(abstraction_bp) @app.errorhandler(InvalidUsage) def handle_invalid_usage(error): response = jsonify(error.to_dict()) response.status_code = error.status_code return response return app
def get_abstractions_supported(): ''' Returns supported abstractions ''' logger.info('REQUEST: /supported') return jsonify(get_abstractions())
def health(): logger.info("Service in good health!") return "I'am doing great!"
APP = create_app() if __name__ == "__main__": ''' API entry point with configurable port ''' PORT = int(environ.get("PORT", 8080)) APP.run(host='0.0.0.0', port=PORT) app = 'spacyservice' eureka_url = 'http://localhost:8761' heartbeat = 5.0 instance = { 'ipAddr': '127.0.0.1', 'app': app, 'instanceId': 'spacyservice' } logger.info("Eureka address: {}".format(eureka_url)) logger.info("Service definition") logger.info(instance) service_wrapper = SimpleEurekaServiceWrapper(eureka_url, instance, heartbeat) logger.info("Registering service") service_wrapper.run() logger.info("Stopping service") service_wrapper.stop()
from api.resources import AblationMask, SegmentationTask, SegmentationTaskCollection from api.task_manager import TaskManager from api.utils import logger DEFAULT_DATA_PATH = './data/tasks' def create_app(data_path): task_manager = TaskManager(data_path) api = falcon.API() # api.add_route('/masks', am_masks.AblationMaskCollection(mask_store)) api.add_route('/masks/{task_id}', AblationMask(task_manager)) api.add_route('/tasks', SegmentationTaskCollection(task_manager)) api.add_route('/tasks/{task_id}', SegmentationTask(task_manager)) return api def get_app(): data_path = Path(os.environ.get('AM_DATA_PATH', DEFAULT_DATA_PATH)) return create_app(data_path) if __name__ == '__main__': logger.info('Creating app...') app = get_app() from wsgiref import simple_server httpd = simple_server.make_server('127.0.0.1', 8000, app) logger.info('Running debug server...') httpd.serve_forever()
def save_mask(mask, path): logger.info(f'Saving mask to {path}') mask = (mask * 255).astype(np.uint8) image = Image.fromarray(mask, mode='L') image.save(path)