def main(operator: Operator, cfg: DictConfig) -> None: correct_config_answer = cfg.correct_answer def onboarding_is_valid(onboarding_data): inputs = onboarding_data["inputs"] outputs = onboarding_data["outputs"] return outputs.get("answer") == correct_config_answer shared_state = SharedStaticTaskState( onboarding_data={"correct_answer": correct_config_answer}, validate_onboarding=onboarding_is_valid, ) operator.launch_task_run(cfg.mephisto, shared_state) operator.wait_for_runs_then_shutdown(skip_input=True, log_rate=30)
def main(operator: Operator, cfg: DictConfig) -> None: def onboarding_always_valid(onboarding_data): return True shared_state = SharedStaticTaskState( static_task_data=[ { "text": "This text is good text!" }, { "text": "This text is bad text!" }, ], validate_onboarding=onboarding_always_valid, ) task_dir = cfg.task_dir build_custom_bundle(task_dir) operator.launch_task_run(cfg.mephisto, shared_state) operator.wait_for_runs_then_shutdown(skip_input=True, log_rate=30)
def main(operator: Operator, cfg: DictConfig) -> None: def onboarding_always_valid(onboarding_data): # NOTE you can make an onboarding task and validate it here print(onboarding_data) return True # Right now we're building locally, but should eventually # use non-local for the real thing tasks = build_tasks(cfg.num_tasks) context = build_local_context(cfg.num_tasks) def handle_with_model( _request_id: str, args: Dict[str, Any], agent_state: RemoteProcedureAgentState ) -> Dict[str, Any]: """Remote call to process external content using a 'model'""" # NOTE this body can be whatever you want print(f"The parsed args are {args}, you can do what you want with that") print(f"You can also use {agent_state.init_data}, to get task keys") assert agent_state.init_data is not None idx = agent_state.init_data["local_value_key"] print(f"And that may let you get local context, like {context[idx]}") return { "secret_local_value": context[idx], "update": f"this was request {args['arg3'] + 1}", } function_registry = { "handle_with_model": handle_with_model, } shared_state = SharedRemoteProcedureTaskState( static_task_data=tasks, validate_onboarding=onboarding_always_valid, function_registry=function_registry, ) task_dir = cfg.task_dir build_custom_bundle(task_dir) operator.launch_task_run(cfg.mephisto, shared_state) operator.wait_for_runs_then_shutdown(skip_input=True, log_rate=30)
def main(operator: Operator, cfg: DictConfig) -> None: tasks: List[Dict[str, Any]] = [{}] * cfg.num_tasks mnist_model = mnist(pretrained=True) def handle_with_model( _request_id: str, args: Dict[str, Any], agent_state: RemoteProcedureAgentState) -> Dict[str, Any]: """Convert the image to be read by MNIST classifier, then classify""" img_dat = args["urlData"].split("data:image/png;base64,")[1] im = Image.open(BytesIO(base64.b64decode(img_dat))) im_gray = im.convert("L") im_resized = im_gray.resize((28, 28)) im_vals = list(im_resized.getdata()) norm_vals = [(255 - x) * 1.0 / 255.0 for x in im_vals] in_tensor = torch.tensor([norm_vals]) output = mnist_model(in_tensor) pred = output.data.max(1)[1] print("Predicted digit:", pred.item()) return { "digit_prediction": pred.item(), } function_registry = { "classify_digit": handle_with_model, } shared_state = SharedRemoteProcedureTaskState( static_task_data=tasks, function_registry=function_registry, ) task_dir = cfg.task_dir build_custom_bundle(task_dir) operator.launch_task_run(cfg.mephisto, shared_state) operator.wait_for_runs_then_shutdown(skip_input=True, log_rate=30)