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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { useCallback, useContext, useEffect, useState } from "react";
import { NavLink } from "react-router-dom";
import { Button, Typography, Spinner, EmptyState, SimpleCard } from "@humansignal/ui";
import { useUpdatePageTitle, createTitleFromSegments } from "@humansignal/core";
import { Form, Label, Toggle } from "../../../components/Form";
import { modal } from "../../../components/Modal/Modal";
import { IconModels, IconExternal } from "@humansignal/icons";
import { useAPI } from "../../../providers/ApiProvider";
import { ProjectContext } from "../../../providers/ProjectProvider";
import { MachineLearningList } from "./MachineLearningList";
import { CustomBackendForm } from "./Forms";
import { TestRequest } from "./TestRequest";
import { StartModelTraining } from "./StartModelTraining";
import "./MachineLearningSettings.scss";
 
export const MachineLearningSettings = () => {
  const api = useAPI();
  const { project, fetchProject } = useContext(ProjectContext);
  const [backends, setBackends] = useState([]);
  const [loading, setLoading] = useState(false);
  const [loaded, setLoaded] = useState(false);
 
  useUpdatePageTitle(createTitleFromSegments([project?.title, "模型设置"]));
 
  const fetchBackends = useCallback(async () => {
    setLoading(true);
    const models = await api.callApi("mlBackends", {
      params: {
        project: project.id,
        include_static: true,
      },
    });
 
    if (models) setBackends(models);
    setLoading(false);
    setLoaded(true);
  }, [project, setBackends]);
 
  const startTrainingModal = useCallback(
    (backend) => {
      const modalProps = {
        title: "开始模型训练",
        style: { width: 760 },
        closeOnClickOutside: true,
        body: <StartModelTraining backend={backend} />,
      };
 
      modal(modalProps);
    },
    [project],
  );
 
  const showRequestModal = useCallback(
    (backend) => {
      const modalProps = {
        title: "测试请求",
        style: { width: 760 },
        closeOnClickOutside: true,
        body: <TestRequest backend={backend} />,
      };
 
      modal(modalProps);
    },
    [project],
  );
 
  const showMLFormModal = useCallback(
    (backend) => {
      const action = backend ? "updateMLBackend" : "addMLBackend";
      const modalProps = {
        title: `${backend ? "编辑" : "连接"} 模型`,
        style: { width: 760 },
        closeOnClickOutside: false,
        body: (
          <CustomBackendForm
            action={action}
            backend={backend}
            project={project}
            onSubmit={() => {
              fetchBackends();
              modalRef.close();
            }}
          />
        ),
      };
 
      const modalRef = modal(modalProps);
    },
    [project, fetchBackends],
  );
 
  useEffect(() => {
    if (project.id) {
      fetchBackends();
    }
  }, [project.id]);
 
  return (
    <section>
      <div className="w-[42rem]">
        <Typography variant="headline" size="medium" className="mb-base">
          模型
        </Typography>
        {loading && <Spinner size={32} />}
        {loaded && backends.length === 0 && (
          <SimpleCard title="" className="bg-primary-background border-primary-border-subtler p-base">
            <EmptyState
              size="medium"
              variant="primary"
              icon={<IconModels />}
              title="连接您的第一个模型"
              description="连接机器学习模型以生成项目的实时预测。比较预测结果,利用自动预标注加速标注,并通过主动学习引导团队处理最具影响力的任务。"
              actions={
                <Button
                  variant="primary"
                  look="filled"
                  onClick={() => showMLFormModal()}
                  aria-label="添加机器学习模型"
                >
                  连接模型
                </Button>
              }
              footer={
                !window.APP_SETTINGS?.whitelabel_is_active && (
                  <Typography variant="label" size="small" className="text-primary-link">
                    <a
                      href="https://labelstud.io/guide/ml"
                      target="_blank"
                      rel="noopener noreferrer"
                      data-testid="ml-help-link"
                      aria-label="了解更多关于机器学习模型的信息(在新窗口打开)"
                      className="inline-flex items-center gap-1 hover:underline"
                    >
                      了解更多
                      <IconExternal width={16} height={16} />
                    </a>
                  </Typography>
                )
              }
            />
          </SimpleCard>
        )}
        <MachineLearningList
          onEdit={(backend) => showMLFormModal(backend)}
          onTestRequest={(backend) => showRequestModal(backend)}
          onStartTraining={(backend) => startTrainingModal(backend)}
          fetchBackends={fetchBackends}
          backends={backends}
        />
 
        {backends.length > 0 && (
          <div className="my-wide">
            <Typography size="small" className="text-neutral-content-subtler">
              检测到已连接的模型!如果您希望从此模型获取预测结果,请按照以下步骤操作:
            </Typography>
            <Typography size="small" className="text-neutral-content-subtler mt-base">
              1. 导航到 <i>数据管理器</i>。
            </Typography>
            <Typography size="small" className="text-neutral-content-subtler mt-tighter">
              2. 选择所需的任务。
            </Typography>
            <Typography size="small" className="text-neutral-content-subtler mt-tighter">
              3. 从 <i>操作</i> 菜单中点击 <i>批量预测</i>。
            </Typography>
            <Typography size="small" className="text-neutral-content-subtler mt-base">
              如果您想使用模型预测进行预标注,请在{" "}
              <NavLink to="annotation" className="hover:underline">
                标注设置
              </NavLink>
              中进行配置。
            </Typography>
          </div>
        )}
 
        <Form
          action="updateProject"
          formData={{ ...project }}
          params={{ pk: project.id }}
          onSubmit={() => fetchProject()}
        >
          {backends.length > 0 && (
            <div className="p-wide border border-neutral-border rounded-md">
              <Form.Row columnCount={1}>
                <Label text="配置" large />
 
                <div>
                  <Toggle
                    label="在提交标注时开始模型训练"
                    description="此选项将发送包含标注信息的请求到 /train。您可以使用此功能启用主动学习循环。您也可以通过模型卡片中的菜单手动开始训练。"
                    name="start_training_on_annotation_update"
                  />
                </div>
              </Form.Row>
            </div>
          )}
 
          {backends.length > 0 && (
            <Form.Actions>
              <Form.Indicator>
                <span case="success">已保存!</span>
              </Form.Indicator>
              <Button type="submit" look="primary" className="w-[120px]" aria-label="保存机器学习设置">
                保存
              </Button>
            </Form.Actions>
          )}
        </Form>
      </div>
    </section>
  );
};
 
MachineLearningSettings.title = "模型";
MachineLearningSettings.path = "/ml";