-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Testing trained models in UI (#128359)
* [ML] Testing trained models in UI * folder rename * code clean up * translations * adding comments * endpoint comments * small changes based on review * removing testing text * refactoring to remove duplicate code * changing misc entities * probably is now 3 sig figs * class refactor * another refactor * fixing enitiy highlighting * adding infer timeout * show class name for known types * refactoring highlighting * moving unknown entity type * removing default badge tooltips * fixing linting error * small import changes Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
ac4e96c
commit f4ed8e1
Showing
23 changed files
with
1,007 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/ml/public/application/trained_models/models_management/test_models/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { TestTrainedModelFlyout } from './test_flyout'; | ||
export { isTestable } from './utils'; |
30 changes: 30 additions & 0 deletions
30
...ns/ml/public/application/trained_models/models_management/test_models/inference_error.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { FC } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiCallOut } from '@elastic/eui'; | ||
|
||
interface Props { | ||
errorText: string | null; | ||
} | ||
|
||
export const ErrorMessage: FC<Props> = ({ errorText }) => { | ||
return errorText === null ? null : ( | ||
<> | ||
<EuiCallOut | ||
title={i18n.translate('xpack.ml.trainedModels.testModelsFlyout.inferenceError', { | ||
defaultMessage: 'An error occurred', | ||
})} | ||
color="danger" | ||
iconType="cross" | ||
> | ||
<p>{errorText}</p> | ||
</EuiCallOut> | ||
</> | ||
); | ||
}; |
30 changes: 30 additions & 0 deletions
30
.../public/application/trained_models/models_management/test_models/models/inference_base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
|
||
import { trainedModelsApiProvider } from '../../../../services/ml_api_service/trained_models'; | ||
|
||
const DEFAULT_INPUT_FIELD = 'text_field'; | ||
|
||
export type FormattedNerResp = Array<{ | ||
value: string; | ||
entity: estypes.MlTrainedModelEntities | null; | ||
}>; | ||
|
||
export abstract class InferenceBase<TInferResponse> { | ||
protected readonly inputField: string; | ||
|
||
constructor( | ||
protected trainedModelsApi: ReturnType<typeof trainedModelsApiProvider>, | ||
protected model: estypes.MlTrainedModelConfig | ||
) { | ||
this.inputField = model.input?.field_names[0] ?? DEFAULT_INPUT_FIELD; | ||
} | ||
|
||
protected abstract infer(inputText: string): Promise<TInferResponse>; | ||
} |
131 changes: 131 additions & 0 deletions
131
.../application/trained_models/models_management/test_models/models/inference_input_form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { FC, useState } from 'react'; | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { EuiSpacer, EuiTextArea, EuiButton, EuiTabs, EuiTab } from '@elastic/eui'; | ||
|
||
import { LangIdentInference } from './lang_ident/lang_ident_inference'; | ||
import { NerInference } from './ner/ner_inference'; | ||
import type { FormattedLangIdentResp } from './lang_ident/lang_ident_inference'; | ||
import type { FormattedNerResp } from './ner/ner_inference'; | ||
|
||
import { MLJobEditor } from '../../../../jobs/jobs_list/components/ml_job_editor'; | ||
import { extractErrorMessage } from '../../../../../../common/util/errors'; | ||
import { ErrorMessage } from '../inference_error'; | ||
import { OutputLoadingContent } from '../output_loading'; | ||
|
||
interface Props { | ||
inferrer: LangIdentInference | NerInference; | ||
getOutputComponent(output: any): JSX.Element; | ||
} | ||
|
||
enum TAB { | ||
TEXT, | ||
RAW, | ||
} | ||
|
||
export const InferenceInputForm: FC<Props> = ({ inferrer, getOutputComponent }) => { | ||
const [inputText, setInputText] = useState(''); | ||
const [isRunning, setIsRunning] = useState(false); | ||
const [output, setOutput] = useState<FormattedLangIdentResp | FormattedNerResp | null>(null); | ||
const [rawOutput, setRawOutput] = useState<string | null>(null); | ||
const [selectedTab, setSelectedTab] = useState(TAB.TEXT); | ||
const [showOutput, setShowOutput] = useState(false); | ||
const [errorText, setErrorText] = useState<string | null>(null); | ||
|
||
async function run() { | ||
setShowOutput(true); | ||
setOutput(null); | ||
setRawOutput(null); | ||
setIsRunning(true); | ||
setErrorText(null); | ||
try { | ||
const { response, rawResponse } = await inferrer.infer(inputText); | ||
setOutput(response); | ||
setRawOutput(JSON.stringify(rawResponse, null, 2)); | ||
} catch (e) { | ||
setIsRunning(false); | ||
setOutput(null); | ||
setErrorText(extractErrorMessage(e)); | ||
setRawOutput(JSON.stringify(e.body ?? e, null, 2)); | ||
} | ||
setIsRunning(false); | ||
} | ||
|
||
return ( | ||
<> | ||
<EuiTextArea | ||
placeholder={i18n.translate('xpack.ml.trainedModels.testModelsFlyout.langIdent.inputText', { | ||
defaultMessage: 'Input text', | ||
})} | ||
value={inputText} | ||
disabled={isRunning === true} | ||
fullWidth | ||
onChange={(e) => { | ||
setInputText(e.target.value); | ||
}} | ||
/> | ||
<EuiSpacer size="m" /> | ||
<div> | ||
<EuiButton | ||
onClick={run} | ||
disabled={isRunning === true || inputText === ''} | ||
fullWidth={false} | ||
> | ||
<FormattedMessage | ||
id="xpack.ml.trainedModels.testModelsFlyout.langIdent.runButton" | ||
defaultMessage="Test" | ||
/> | ||
</EuiButton> | ||
</div> | ||
{showOutput === true ? ( | ||
<> | ||
<EuiSpacer size="m" /> | ||
<EuiTabs size={'s'}> | ||
<EuiTab | ||
isSelected={selectedTab === TAB.TEXT} | ||
onClick={setSelectedTab.bind(null, TAB.TEXT)} | ||
> | ||
<FormattedMessage | ||
id="xpack.ml.trainedModels.testModelsFlyout.langIdent.markupTab" | ||
defaultMessage="Output" | ||
/> | ||
</EuiTab> | ||
<EuiTab | ||
isSelected={selectedTab === TAB.RAW} | ||
onClick={setSelectedTab.bind(null, TAB.RAW)} | ||
> | ||
<FormattedMessage | ||
id="xpack.ml.trainedModels.testModelsFlyout.langIdent.rawOutput" | ||
defaultMessage="Raw output" | ||
/> | ||
</EuiTab> | ||
</EuiTabs> | ||
|
||
<EuiSpacer size="m" /> | ||
|
||
{selectedTab === TAB.TEXT ? ( | ||
<> | ||
{errorText !== null ? ( | ||
<ErrorMessage errorText={errorText} /> | ||
) : output === null ? ( | ||
<OutputLoadingContent text={inputText} /> | ||
) : ( | ||
<>{getOutputComponent(output)}</> | ||
)} | ||
</> | ||
) : ( | ||
<MLJobEditor value={rawOutput ?? ''} readOnly={true} /> | ||
)} | ||
</> | ||
) : null} | ||
</> | ||
); | ||
}; |
10 changes: 10 additions & 0 deletions
10
...ublic/application/trained_models/models_management/test_models/models/lang_ident/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export type { FormattedLangIdentResp } from './lang_ident_inference'; | ||
export { LangIdentInference } from './lang_ident_inference'; | ||
export { LangIdentOutput } from './lang_ident_output'; |
Oops, something went wrong.