handleResponse()

in frontends/web/src/common/Annotation/CreateInterface.js [265:455]


  handleResponse(e) {
    e.preventDefault();
    var incompleteExample = false;
    this.state.annotationConfig.input.forEach((annotationConfigObj) => {
      if (this.state.data[annotationConfigObj.name] === null) {
        this.setState({ submitWithoutFullExample: true });
        incompleteExample = true;
      }
    });
    if (incompleteExample) {
      return;
    }

    this.setState({ submitWithoutFullExample: false });
    this.setState({ submitDisabled: true, refreshDisabled: true }, () => {
      this.manageTextInput("blur");
      const url = this.state.selectedModel
        ? this.state.selectedModel.endpointUrl
        : this.state.randomTargetModel;

      if (url === null) {
        // In this case, there is no target model. Just store the example without model data.
        this.storeExampleWrapper();
        return;
      }

      // Begin hack that can be removed upon full dynalab integration
      const endpoint = url.split("predict?model=")[1];

      if (
        !endpoint.startsWith("ts") &&
        (this.state.task.task_code === "hs" ||
          this.state.task.task_code === "sentiment")
      ) {
        this.state.data["hypothesis"] = this.state.data["statement"];
      }
      if (!endpoint.startsWith("ts") && this.state.task.task_code === "qa") {
        this.state.data["hypothesis"] = this.state.data["question"];
      }
      // End hack that can be removed upon full dynalab integration
      this.context.api
        .convertToModelIO(this.state.task.id, this.state.data)
        .then((model_io_result) => {
          // Begin hack that can be removed upon full dynalab integration
          if (
            !endpoint.startsWith("ts") &&
            this.state.task.task_code === "vqa"
          ) {
            model_io_result = this.state.data;
            model_io_result["image_url"] = model_io_result["image"];
          }
          // End hack that can be removed upon full dynalab integration
          this.context.api.getModelResponse(url, model_io_result).then(
            (modelResponseResult) => {
              // Begin hack that can be removed upon full dynalab integration
              if (
                !endpoint.startsWith("ts") &&
                this.state.task.task_code === "hs"
              ) {
                modelResponseResult["label"] =
                  modelResponseResult["prob"][0] >
                  modelResponseResult["prob"][1]
                    ? "not-hateful"
                    : "hateful";
                modelResponseResult["prob"] = {
                  "not-hateful": modelResponseResult["prob"][0],
                  hateful: modelResponseResult["prob"][1],
                };
              }
              if (
                !endpoint.startsWith("ts") &&
                this.state.task.task_code === "sentiment"
              ) {
                modelResponseResult["label"] =
                  modelResponseResult["prob"][0] >
                    modelResponseResult["prob"][1] &&
                  modelResponseResult["prob"][0] >
                    modelResponseResult["prob"][2]
                    ? "negative"
                    : modelResponseResult["prob"][1] >
                      modelResponseResult["prob"][2]
                    ? "positive"
                    : "neutral";
                modelResponseResult["prob"] = {
                  negative: modelResponseResult["prob"][0],
                  positive: modelResponseResult["prob"][1],
                  neutral: modelResponseResult["prob"][2],
                };
              }
              if (
                !endpoint.startsWith("ts") &&
                this.state.task.task_code === "nli"
              ) {
                modelResponseResult["label"] =
                  modelResponseResult["prob"][0] >
                    modelResponseResult["prob"][1] &&
                  modelResponseResult["prob"][0] >
                    modelResponseResult["prob"][2]
                    ? "entailed"
                    : modelResponseResult["prob"][1] >
                      modelResponseResult["prob"][2]
                    ? "neutral"
                    : "contradictory";
                modelResponseResult["prob"] = {
                  entailed: modelResponseResult["prob"][0],
                  neutral: modelResponseResult["prob"][1],
                  contradictory: modelResponseResult["prob"][2],
                };
              }
              if (
                !endpoint.startsWith("ts") &&
                this.state.task.task_code === "qa"
              ) {
                modelResponseResult["answer"] = modelResponseResult["text"];
                modelResponseResult["conf"] = modelResponseResult["prob"];
              }
              // End hack that can be removed upon full dynalab integration

              if (modelResponseResult.errorMessage) {
                this.setState({
                  submitDisabled: false,
                  refreshDisabled: false,
                  fetchPredictionError: true,
                });
              } else {
                if (this.state.fetchPredictionError) {
                  this.setState({
                    fetchPredictionError: false,
                  });
                }

                const output = deepCopyJSON(modelResponseResult);

                // Get the target, which is the user input that is expected to
                // be in the model's output.
                const target = {};
                for (const annotationConfigObj of this.state.annotationConfig
                  .output) {
                  if (
                    this.state.data.hasOwnProperty(annotationConfigObj.name)
                  ) {
                    target[annotationConfigObj.name] =
                      this.state.data[annotationConfigObj.name];
                  }
                }

                this.context.api
                  .getModelWrong(this.state.task.id, target, output)
                  .then(
                    (modelWrongResult) =>
                      this.storeExampleWrapper(
                        modelResponseResult["signed"]
                          ? modelResponseResult["signed"]
                          : modelResponseResult["signature"], // TODO: pre-dynatask models use signed, post-dynatask models use signature. Make this cleaner somehow?
                        modelWrongResult.model_wrong,
                        output,
                        endpoint
                      ),
                    (error) => {
                      console.log(error);
                      this.setState({
                        submitDisabled: false,
                        refreshDisabled: false,
                        fetchPredictionError: true,
                      });
                    }
                  )
                  .then(() => this.smoothlyAnimateToBottom());
              }
            },
            (error) => {
              console.log(error);
              if (error && error.message && error.message === "Unauthorized") {
                this.props.history.push(
                  "/login?msg=" +
                    encodeURIComponent(
                      "You need to login to use this feature."
                    ) +
                    "&src=" +
                    encodeURIComponent(`/tasks/${this.state.taskCode}/create`)
                );
              }
              this.setState({
                submitDisabled: false,
                refreshDisabled: false,
              });
            }
          );
        });
    });
  }