mirror of
https://github.com/hexastack/hexabot
synced 2025-01-22 18:45:57 +00:00
fix: nlp form update
This commit is contained in:
parent
7425f60284
commit
d04f2f3ce4
@ -20,10 +20,11 @@ import { ContentItem } from "@/app-components/dialogs/layouts/ContentItem";
|
|||||||
import { Input } from "@/app-components/inputs/Input";
|
import { Input } from "@/app-components/inputs/Input";
|
||||||
import MultipleInput from "@/app-components/inputs/MultipleInput";
|
import MultipleInput from "@/app-components/inputs/MultipleInput";
|
||||||
import { useCreate } from "@/hooks/crud/useCreate";
|
import { useCreate } from "@/hooks/crud/useCreate";
|
||||||
|
import { useGet } from "@/hooks/crud/useGet";
|
||||||
import { useUpdate } from "@/hooks/crud/useUpdate";
|
import { useUpdate } from "@/hooks/crud/useUpdate";
|
||||||
import { DialogControlProps } from "@/hooks/useDialog";
|
import { DialogControlProps } from "@/hooks/useDialog";
|
||||||
import { useToast } from "@/hooks/useToast";
|
import { useToast } from "@/hooks/useToast";
|
||||||
import { EntityType } from "@/services/types";
|
import { EntityType, Format } from "@/services/types";
|
||||||
import { INlpValue, INlpValueAttributes } from "@/types/nlp-value.types";
|
import { INlpValue, INlpValueAttributes } from "@/types/nlp-value.types";
|
||||||
|
|
||||||
export type TNlpValueAttributesWithRequiredExpressions = INlpValueAttributes & {
|
export type TNlpValueAttributesWithRequiredExpressions = INlpValueAttributes & {
|
||||||
@ -44,11 +45,16 @@ export const NlpValueDialog: FC<NlpValueDialogProps> = ({
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const { query } = useRouter();
|
const { query } = useRouter();
|
||||||
|
const { refetch: refetchEntity } = useGet(data?.entity || String(query.id), {
|
||||||
|
entity: EntityType.NLP_ENTITY,
|
||||||
|
format: Format.FULL,
|
||||||
|
});
|
||||||
const { mutateAsync: createNlpValue } = useCreate(EntityType.NLP_VALUE, {
|
const { mutateAsync: createNlpValue } = useCreate(EntityType.NLP_VALUE, {
|
||||||
onError: () => {
|
onError: () => {
|
||||||
toast.error(t("message.internal_server_error"));
|
toast.error(t("message.internal_server_error"));
|
||||||
},
|
},
|
||||||
onSuccess(data) {
|
onSuccess(data) {
|
||||||
|
refetchEntity();
|
||||||
closeDialog();
|
closeDialog();
|
||||||
toast.success(t("message.success_save"));
|
toast.success(t("message.success_save"));
|
||||||
callback?.(data);
|
callback?.(data);
|
||||||
|
@ -23,7 +23,7 @@ import {
|
|||||||
RadioGroup,
|
RadioGroup,
|
||||||
Typography,
|
Typography,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { FC, useCallback, useMemo, useState } from "react";
|
import { FC, useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { Controller, useFieldArray, useForm } from "react-hook-form";
|
import { Controller, useFieldArray, useForm } from "react-hook-form";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useQuery } from "react-query";
|
import { useQuery } from "react-query";
|
||||||
@ -65,71 +65,40 @@ const NlpDatasetSample: FC<NlpDatasetSampleProps> = ({
|
|||||||
{
|
{
|
||||||
hasCount: false,
|
hasCount: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
onSuccess(entities) {
|
|
||||||
// By default append trait entities
|
|
||||||
if (!sample) {
|
|
||||||
removeTraitEntity();
|
|
||||||
(entities || [])
|
|
||||||
.filter(({ lookups }) => lookups.includes("trait"))
|
|
||||||
.forEach(({ name }) => {
|
|
||||||
appendTraitEntity({
|
|
||||||
entity: name,
|
|
||||||
value: "",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
const getNlpValueFromCache = useGetFromCache(EntityType.NLP_VALUE);
|
const getNlpValueFromCache = useGetFromCache(EntityType.NLP_VALUE);
|
||||||
// Default trait entities to append to the form
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
const defaultTraitEntities = useMemo(() => {
|
const defaultValues: INlpSampleFormAttributes = useMemo(
|
||||||
if (!sample || !entities) return [];
|
() => ({
|
||||||
|
|
||||||
const traitEntities = entities.filter(({ lookups }) =>
|
|
||||||
lookups.includes("trait"),
|
|
||||||
);
|
|
||||||
const sampleTraitEntities = sample.entities.filter(
|
|
||||||
(e) => "start" in e && typeof e.start === "undefined",
|
|
||||||
);
|
|
||||||
|
|
||||||
if (sampleTraitEntities.length === traitEntities.length) {
|
|
||||||
return sampleTraitEntities;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sampleEntityNames = new Set(sampleTraitEntities.map((e) => e.entity));
|
|
||||||
const missingEntities = traitEntities
|
|
||||||
.filter(({ name }) => !sampleEntityNames.has(name))
|
|
||||||
.map(({ name }) => ({
|
|
||||||
entity: name,
|
|
||||||
value: "",
|
|
||||||
}));
|
|
||||||
|
|
||||||
return [...sampleTraitEntities, ...missingEntities];
|
|
||||||
}, [entities, sample]);
|
|
||||||
const { handleSubmit, control, register, reset, setValue, watch } =
|
|
||||||
useForm<INlpSampleFormAttributes>({
|
|
||||||
defaultValues: {
|
|
||||||
type: sample?.type || NlpSampleType.train,
|
type: sample?.type || NlpSampleType.train,
|
||||||
text: sample?.text || "",
|
text: sample?.text || "",
|
||||||
language: sample?.language,
|
language: sample?.language || null,
|
||||||
traitEntities: defaultTraitEntities,
|
traitEntities: (entities || [])
|
||||||
keywordEntities:
|
.filter(({ lookups }) => {
|
||||||
sample?.entities.filter(
|
return lookups.includes("trait");
|
||||||
|
})
|
||||||
|
.map((e) => {
|
||||||
|
return {
|
||||||
|
entity: e.name,
|
||||||
|
value: sample
|
||||||
|
? sample.entities.find(({ entity }) => entity === e.name)?.value
|
||||||
|
: "",
|
||||||
|
} as INlpDatasetTraitEntity;
|
||||||
|
}),
|
||||||
|
keywordEntities: (sample?.entities || []).filter(
|
||||||
(e) => "start" in e && typeof e.start === "number",
|
(e) => "start" in e && typeof e.start === "number",
|
||||||
) || [],
|
) as INlpDatasetKeywordEntity[],
|
||||||
},
|
}),
|
||||||
|
[sample, entities],
|
||||||
|
);
|
||||||
|
const { handleSubmit, control, register, reset, setValue, watch } =
|
||||||
|
useForm<INlpSampleFormAttributes>({
|
||||||
|
defaultValues,
|
||||||
});
|
});
|
||||||
const currentText = watch("text");
|
const currentText = watch("text");
|
||||||
const currentType = watch("type");
|
const currentType = watch("type");
|
||||||
const { apiClient } = useApiClient();
|
const { apiClient } = useApiClient();
|
||||||
const {
|
const { fields: traitEntities, update: updateTraitEntity } = useFieldArray({
|
||||||
fields: traitEntities,
|
|
||||||
append: appendTraitEntity,
|
|
||||||
update: updateTraitEntity,
|
|
||||||
remove: removeTraitEntity,
|
|
||||||
} = useFieldArray({
|
|
||||||
control,
|
control,
|
||||||
name: "traitEntities",
|
name: "traitEntities",
|
||||||
});
|
});
|
||||||
@ -187,16 +156,14 @@ const NlpDatasetSample: FC<NlpDatasetSampleProps> = ({
|
|||||||
} | null>(null);
|
} | null>(null);
|
||||||
const onSubmitForm = (form: INlpSampleFormAttributes) => {
|
const onSubmitForm = (form: INlpSampleFormAttributes) => {
|
||||||
submitForm(form);
|
submitForm(form);
|
||||||
reset({
|
|
||||||
type: form?.type || NlpSampleType.train,
|
|
||||||
text: "",
|
|
||||||
language: form?.language,
|
|
||||||
traitEntities: defaultTraitEntities,
|
|
||||||
keywordEntities: [],
|
|
||||||
});
|
|
||||||
refetchEntities();
|
refetchEntities();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
reset(defaultValues);
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [JSON.stringify(defaultValues)]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="nlp-train" sx={{ position: "relative", p: 2 }}>
|
<Box className="nlp-train" sx={{ position: "relative", p: 2 }}>
|
||||||
<form onSubmit={handleSubmit(onSubmitForm)}>
|
<form onSubmit={handleSubmit(onSubmitForm)}>
|
||||||
|
@ -142,8 +142,6 @@ export const NlpValues = ({ entityId }: { entityId: string }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid container gap={2} flexDirection="column">
|
<Grid container gap={2} flexDirection="column">
|
||||||
{/* <PageHeader title={t("title.nlp_train")} icon={faGraduationCap} /> */}
|
|
||||||
|
|
||||||
<Slide direction={direction} in={true} mountOnEnter unmountOnExit>
|
<Slide direction={direction} in={true} mountOnEnter unmountOnExit>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<Box sx={{ padding: 1 }}>
|
<Box sx={{ padding: 1 }}>
|
||||||
|
Loading…
Reference in New Issue
Block a user