-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathModelTraining.js
More file actions
169 lines (151 loc) · 5.09 KB
/
Copy pathModelTraining.js
File metadata and controls
169 lines (151 loc) · 5.09 KB
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
// global localStorage
import React, { useContext, useState, useEffect, useRef } from "react";
import { Button, Space } from "antd";
import {
startModelTraining,
stopModelTraining,
getTrainingStatus,
} from "../api";
import Configurator from "../components/Configurator";
import { AppContext } from "../contexts/GlobalContext";
function ModelTraining() {
const context = useContext(AppContext);
const [isTraining, setIsTraining] = useState(false);
const [trainingStatus, setTrainingStatus] = useState("");
const pollingIntervalRef = useRef(null);
// Poll training status when training is active
useEffect(() => {
if (isTraining) {
console.log("Starting training status polling...");
pollingIntervalRef.current = setInterval(async () => {
try {
const status = await getTrainingStatus();
console.log("Training status:", status);
if (!status.isRunning) {
// Training has finished
console.log("Training completed!");
setIsTraining(false);
if (status.exitCode === 0) {
setTrainingStatus("Training completed successfully! ✓");
} else if (status.exitCode !== null) {
setTrainingStatus(
`Training finished with exit code: ${status.exitCode}`,
);
} else {
setTrainingStatus("Training stopped.");
}
// Clear the polling interval
if (pollingIntervalRef.current) {
clearInterval(pollingIntervalRef.current);
pollingIntervalRef.current = null;
}
}
} catch (error) {
console.error("Error polling training status:", error);
}
}, 2000); // Poll every 2 seconds
}
// Cleanup on unmount or when training stops
return () => {
if (pollingIntervalRef.current) {
console.log("Clearing polling interval");
clearInterval(pollingIntervalRef.current);
pollingIntervalRef.current = null;
}
};
}, [isTraining]);
// const [tensorboardURL, setTensorboardURL] = useState(null);
const handleStartButton = async () => {
try {
// TODO: Validate required context values before starting
if (!context.uploadedYamlFile) {
setTrainingStatus(
"Error: Please upload a YAML configuration file first.",
);
return;
}
if (!context.outputPath) {
setTrainingStatus("Error: Please set output path first in Step 1.");
return;
}
if (!context.logPath) {
setTrainingStatus("Error: Please set log path first in Step 1.");
return;
}
console.log(context.uploadedYamlFile);
const trainingConfig = context.trainingConfig;
setIsTraining(true);
setTrainingStatus(
"Starting training... Please wait, this may take a while.",
);
const getPath = (val) => {
if (!val) return "";
if (typeof val === "string") return val;
return val.path || "";
};
// TODO: The API call should be non-blocking and return immediately
// Real training status should be polled separately
const res = await startModelTraining(
trainingConfig,
getPath(context.logPath),
getPath(context.outputPath),
);
console.log(res);
// TODO: Don't set training complete here - implement proper status polling
setTrainingStatus(
"Training started successfully. Monitoring progress...",
);
} catch (e) {
console.error("Training start error:", e);
setTrainingStatus(
`Training error: ${e.message || "Please check console for details."}`,
);
setIsTraining(false);
}
};
const handleStopButton = async () => {
try {
setTrainingStatus("Stopping training...");
await stopModelTraining();
setIsTraining(false);
setTrainingStatus("Training stopped successfully.");
} catch (e) {
console.error("Training stop error:", e);
setTrainingStatus(
`Error stopping training: ${e.message || "Please check console for details."}`,
);
}
};
// const handleTensorboardButton = async () => {
// try {
// const res = await startTensorboard();
// console.log(res);
// setTensorboardURL(res);
// } catch (e) {
// console.log(e);
// }
// };
// const [componentSize, setComponentSize] = useState("default");
// const onFormLayoutChange = ({ size }) => {
// setComponentSize(size);
// };
return (
<>
<div>
{/* {"ModelTraining"} */}
<Configurator fileList={context.files} type="training" />
<Space wrap style={{ marginTop: 12 }}>
<Button onClick={handleStartButton} disabled={isTraining}>
Start Training
</Button>
<Button onClick={handleStopButton} disabled={!isTraining}>
Stop Training
</Button>
</Space>
{/* <Button onClick={handleTensorboardButton}>Tensorboard</Button> */}
<p style={{ marginTop: 4 }}>{trainingStatus}</p>
</div>
</>
);
}
export default ModelTraining;