Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { useState } from "react";
import { Button } from "@humansignal/ui";
import { ErrorWrapper } from "../../../components/Error/Error";
import { InlineError } from "../../../components/Error/InlineError";
import { Form, Input, Select, TextArea, Toggle } from "../../../components/Form";
import "./MachineLearningSettings.scss";
 
const CustomBackendForm = ({ action, backend, project, onSubmit }) => {
  const [selectedAuthMethod, setAuthMethod] = useState("NONE");
  const [, setMLError] = useState();
 
  return (
    <Form
      action={action}
      formData={{ ...(backend ?? {}) }}
      params={{ pk: backend?.id }}
      onSubmit={async (response) => {
        if (!response.error_message) {
          onSubmit(response);
        }
      }}
    >
      <Input type="hidden" name="project" value={project.id} />
 
      <Form.Row columnCount={1}>
        <Input name="title" label="名称" placeholder="输入名称" required />
      </Form.Row>
 
      <Form.Row columnCount={1}>
        <Input name="url" label="后端 URL" required />
      </Form.Row>
 
      <Form.Row columnCount={2}>
        <Select
          name="auth_method"
          label="选择认证方式"
          options={[
            { label: "无认证", value: "NONE" },
            { label: "基础认证 (Basic Auth)", value: "BASIC_AUTH" },
          ]}
          value={selectedAuthMethod}
          onChange={setAuthMethod}
        />
      </Form.Row>
 
      {(backend?.auth_method === "BASIC_AUTH" || selectedAuthMethod === "BASIC_AUTH") && (
        <Form.Row columnCount={2}>
          <Input name="basic_auth_user" label="基础认证用户名" />
          {backend?.basic_auth_pass_is_set ? (
            <Input name="basic_auth_pass" label="基础认证密码" type="password" placeholder="********" />
          ) : (
            <Input name="basic_auth_pass" label="基础认证密码" type="password" />
          )}
        </Form.Row>
      )}
 
      <Form.Row columnCount={1}>
        <TextArea
          name="extra_params"
          label="模型连接时传递的额外参数"
          style={{ minHeight: 120 }}
        />
      </Form.Row>
 
      <Form.Row columnCount={1}>
        <Toggle
          name="is_interactive"
          label="交互式预标注"
          description="如果启用,某些标注工具将在标注过程中向 ML 后端发送交互请求。"
        />
      </Form.Row>
 
      <Form.Actions>
        <Button type="submit" look="primary" onClick={() => setMLError(null)} aria-label="保存机器学习表单">
          验证并保存
        </Button>
      </Form.Actions>
 
      <Form.ResponseParser>
        {(response) => (
          <>
            {response.error_message && (
              <ErrorWrapper
                error={{
                  response: {
                    detail: `${backend ? "保存" : "添加新"} ML 后端失败。`,
                    exc_info: response.error_message,
                  },
                }}
              />
            )}
          </>
        )}
      </Form.ResponseParser>
 
      <InlineError />
    </Form>
  );
};
 
export { CustomBackendForm };