Skip to content

Commit fdc1c7d

Browse files
authored
Merge pull request #42 from comet-ml/CM-1631-artifact-upload
[Cm-1631]: Implement Artifact Uploading
2 parents 7606e55 + 2dea450 commit fdc1c7d

File tree

105 files changed

+4133
-1015
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+4133
-1015
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ml.comet.examples;
2+
3+
import ml.comet.experiment.OnlineExperiment;
4+
import ml.comet.experiment.artifact.Artifact;
5+
import ml.comet.experiment.artifact.LoggedArtifact;
6+
7+
import java.net.URI;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Path;
10+
import java.util.Arrays;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.concurrent.CompletableFuture;
15+
16+
import static java.util.concurrent.TimeUnit.SECONDS;
17+
import static ml.comet.examples.Utils.getResourceFile;
18+
import static ml.comet.experiment.ExperimentBuilder.OnlineExperiment;
19+
20+
/**
21+
* Provides examples of working with Comet artifact.
22+
*
23+
* <p>To run from command line execute the following at the root of this module:
24+
* <pre>
25+
* COMET_API_KEY=your_api_key \
26+
* COMET_WORKSPACE_NAME=your_workspace \
27+
* COMET_PROJECT_NAME=your_project_name \
28+
* mvn exec:java -Dexec.mainClass="ml.comet.examples.ArtifactExample"
29+
* </pre>
30+
* Make sure to provide correct values above.
31+
*/
32+
public class ArtifactExample implements BaseExample {
33+
private static final Map<String, Object> SOME_METADATA = new HashMap<>();
34+
35+
static {
36+
SOME_METADATA.put("someInt", 10);
37+
SOME_METADATA.put("someString", "test string");
38+
SOME_METADATA.put("someBoolean", true);
39+
}
40+
41+
/**
42+
* The main entry point to the example.
43+
*
44+
* @param args the command line arguments if any.
45+
*/
46+
public static void main(String[] args) throws Exception {
47+
try (OnlineExperiment experiment = OnlineExperiment().interceptStdout().build()) {
48+
ArtifactExample.run(experiment);
49+
}
50+
}
51+
52+
private static void run(OnlineExperiment experiment) throws Exception {
53+
experiment.setExperimentName("Artifact Example");
54+
55+
List<String> aliases = Arrays.asList("alias1", "alias2");
56+
List<String> tags = Arrays.asList("tag1", "tag2");
57+
String artifactName = "someArtifact";
58+
String artifactType = "someType";
59+
Artifact artifact = Artifact
60+
.newArtifact(artifactName, artifactType)
61+
.withAliases(aliases)
62+
.withVersionTags(tags)
63+
.withMetadata(SOME_METADATA)
64+
.build();
65+
66+
// add remote assets
67+
//
68+
URI firstAssetLink = new URI("s3://bucket/folder/firstAssetFile.extension");
69+
String firstAssetFileName = "firstAssetFileName";
70+
artifact.addRemoteAsset(firstAssetLink, firstAssetFileName);
71+
72+
String secondAssetExpectedFileName = "secondAssetFile.extension";
73+
URI secondAssetLink = new URI("s3://bucket/folder/" + secondAssetExpectedFileName);
74+
artifact.addRemoteAsset(secondAssetLink, secondAssetExpectedFileName);
75+
76+
// add local assets
77+
//
78+
artifact.addAsset(getResourceFile(CHART_IMAGE_FILE), "amazing chart.png", false, SOME_METADATA);
79+
artifact.addAsset(getResourceFile(MODEL_FILE), false);
80+
byte[] someData = "some data".getBytes(StandardCharsets.UTF_8);
81+
String someDataName = "someDataName";
82+
artifact.addAsset(someData, someDataName);
83+
84+
// add assets folder
85+
//
86+
Path assetDir = BaseExample.copyResourcesToTmpDir();
87+
artifact.addAssetFolder(assetDir.toFile(), true, true);
88+
89+
// log artifact
90+
//
91+
CompletableFuture<LoggedArtifact> futureArtifact = experiment.logArtifact(artifact);
92+
LoggedArtifact loggedArtifact = futureArtifact.get(60, SECONDS);
93+
94+
System.out.printf("\n\nArtifact upload complete, new artifact version: %s\n\n", loggedArtifact.getVersion());
95+
96+
System.out.println("===== Experiment completed ====");
97+
}
98+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ml.comet.examples;
2+
3+
import ml.comet.experiment.OnlineExperiment;
4+
import org.apache.commons.io.file.PathUtils;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Objects;
10+
11+
import static ml.comet.examples.Utils.getResourceFile;
12+
import static ml.comet.examples.Utils.readResourceToString;
13+
14+
/**
15+
* The definition of common methods and constants to be used by examples.
16+
*/
17+
interface BaseExample {
18+
String CHART_IMAGE_FILE = "chart.png";
19+
String MODEL_FILE = "model.hd5";
20+
String HTML_REPORT_FILE = "report.html";
21+
String GRAPH_JSON_FILE = "graph.json";
22+
String CODE_FILE = "code_sample.py";
23+
24+
static void generateCharts(OnlineExperiment experiment) {
25+
long currentStep = experiment.getStep();
26+
27+
for (int i = 1; i < 15; i++) {
28+
experiment.logMetric("numMetric", 123 + i, currentStep + i, getUpdatedEpochValue(experiment));
29+
}
30+
31+
for (int i = 1; i < 15; i++) {
32+
experiment.logMetric("strMetric", "123" + i, currentStep + i, getUpdatedEpochValue(experiment));
33+
}
34+
35+
for (int i = 1; i < 15; i++) {
36+
experiment.logMetric("doubleMetric", 123.12d + i, currentStep + i, getUpdatedEpochValue(experiment));
37+
}
38+
}
39+
40+
static String generateCustomHtmlReport() throws IOException {
41+
return readResourceToString("report.html");
42+
}
43+
44+
static long getUpdatedEpochValue(OnlineExperiment experiment) {
45+
return experiment.getEpoch() + experiment.getStep() / 5;
46+
}
47+
48+
static Path copyResourcesToTmpDir() throws IOException {
49+
Path root = Files.createTempDirectory("onlineExperimentExample");
50+
PathUtils.copyFileToDirectory(
51+
Objects.requireNonNull(getResourceFile(CHART_IMAGE_FILE)).toPath(), root);
52+
PathUtils.copyFileToDirectory(
53+
Objects.requireNonNull(getResourceFile(MODEL_FILE)).toPath(), root);
54+
Files.createTempFile(root, "empty_file", ".txt");
55+
56+
Path subDir = Files.createTempDirectory(root, "subDir");
57+
PathUtils.copyFileToDirectory(
58+
Objects.requireNonNull(getResourceFile(HTML_REPORT_FILE)).toPath(), subDir);
59+
PathUtils.copyFileToDirectory(
60+
Objects.requireNonNull(getResourceFile(GRAPH_JSON_FILE)).toPath(), subDir);
61+
62+
return root;
63+
}
64+
}

comet-examples/src/main/java/ml/comet/examples/OnlineExperimentExample.java

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
import ml.comet.experiment.context.ExperimentContext;
66
import org.apache.commons.io.file.PathUtils;
77

8-
import java.io.IOException;
98
import java.net.URI;
10-
import java.nio.file.Files;
119
import java.nio.file.Path;
12-
import java.util.Objects;
1310

1411
import static ml.comet.examples.Utils.getResourceFile;
1512
import static ml.comet.examples.Utils.readResourceToString;
1613

1714
/**
18-
* Provides variety of example logging using OnlineExperiment.
15+
* Provides variety of examples of data logging using OnlineExperiment.
1916
*
2017
* <p>To run from command line execute the following at the root of this module:
2118
* <pre>
@@ -26,20 +23,14 @@
2623
* </pre>
2724
* Make sure to provide correct values above.
2825
*/
29-
public class OnlineExperimentExample {
30-
31-
private static final String CHART_IMAGE_FILE = "chart.png";
32-
private static final String MODEL_FILE = "model.hd5";
33-
private static final String HTML_REPORT_FILE = "report.html";
34-
private static final String GRAPH_JSON_FILE = "graph.json";
35-
private static final String CODE_FILE = "code_sample.py";
26+
public class OnlineExperimentExample implements BaseExample {
3627

3728
/**
3829
* The main entry point to the example.
3930
*
4031
* @param args the command line arguments if any.
4132
*/
42-
public static void main(String[] args) {
33+
public static void main(String[] args) throws Exception {
4334

4435
//this will take configs from /comet-java-sdk/comet-examples/src/main/resources/application.conf
4536
//be sure you have set up apiKey, project, workspace in defaults.conf before you start!
@@ -54,8 +45,6 @@ public static void main(String[] args) {
5445

5546
try {
5647
OnlineExperimentExample.run(experiment);
57-
} catch (Throwable e) {
58-
e.printStackTrace();
5948
} finally {
6049
experiment.end();
6150
}
@@ -73,11 +62,11 @@ private static void run(OnlineExperiment experiment) throws Exception {
7362

7463
experiment.setEpoch(3);
7564

76-
generateCharts(experiment);
65+
BaseExample.generateCharts(experiment);
7766

7867
experiment.setStep(1234);
7968

80-
experiment.logHtml(generateCustomHtmlReport(), false);
69+
experiment.logHtml(BaseExample.generateCustomHtmlReport(), false);
8170

8271
experiment.logParameter("batch_size", "500");
8372
experiment.logParameter("learning_rate", 12);
@@ -92,7 +81,7 @@ private static void run(OnlineExperiment experiment) throws Exception {
9281

9382
// upload asset files from folder
9483
//
95-
Path assetDir = copyResourcesToTmpDir();
84+
Path assetDir = BaseExample.copyResourcesToTmpDir();
9685
experiment.logAssetFolder(assetDir.toFile(), true, true);
9786

9887
// log remote assets
@@ -117,45 +106,4 @@ private static void run(OnlineExperiment experiment) throws Exception {
117106
// remove tmp directory
118107
PathUtils.deleteDirectory(assetDir);
119108
}
120-
121-
private static void generateCharts(OnlineExperiment experiment) {
122-
long currentStep = experiment.getStep();
123-
124-
for (int i = 1; i < 15; i++) {
125-
experiment.logMetric("numMetric", 123 + i, currentStep + i, getUpdatedEpochValue(experiment));
126-
}
127-
128-
for (int i = 1; i < 15; i++) {
129-
experiment.logMetric("strMetric", "123" + i, currentStep + i, getUpdatedEpochValue(experiment));
130-
}
131-
132-
for (int i = 1; i < 15; i++) {
133-
experiment.logMetric("doubleMetric", 123.12d + i, currentStep + i, getUpdatedEpochValue(experiment));
134-
}
135-
}
136-
137-
private static String generateCustomHtmlReport() throws IOException {
138-
return readResourceToString("report.html");
139-
}
140-
141-
private static long getUpdatedEpochValue(OnlineExperiment experiment) {
142-
return experiment.getEpoch() + experiment.getStep() / 5;
143-
}
144-
145-
private static Path copyResourcesToTmpDir() throws IOException {
146-
Path root = Files.createTempDirectory("onlineExperimentExample");
147-
PathUtils.copyFileToDirectory(
148-
Objects.requireNonNull(getResourceFile(CHART_IMAGE_FILE)).toPath(), root);
149-
PathUtils.copyFileToDirectory(
150-
Objects.requireNonNull(getResourceFile(MODEL_FILE)).toPath(), root);
151-
Files.createTempFile(root, "empty_file", ".txt");
152-
153-
Path subDir = Files.createTempDirectory(root, "subDir");
154-
PathUtils.copyFileToDirectory(
155-
Objects.requireNonNull(getResourceFile(HTML_REPORT_FILE)).toPath(), subDir);
156-
PathUtils.copyFileToDirectory(
157-
Objects.requireNonNull(getResourceFile(GRAPH_JSON_FILE)).toPath(), subDir);
158-
159-
return root;
160-
}
161109
}

comet-examples/src/main/java/ml/comet/examples/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.Scanner;
1010

1111
/**
12-
* Provides common utilitites used bby examples.
12+
* Provides common utilities used by examples.
1313
*/
1414
public class Utils {
1515

comet-java-client/pom.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464
<artifactId>jackson-databind</artifactId>
6565
<version>2.13.0</version>
6666
</dependency>
67+
<dependency>
68+
<groupId>com.vdurmont</groupId>
69+
<artifactId>semver4j</artifactId>
70+
<version>3.1.0</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.awaitility</groupId>
74+
<artifactId>awaitility</artifactId>
75+
<version>4.1.1</version>
76+
</dependency>
6777
<dependency>
6878
<groupId>org.slf4j</groupId>
6979
<artifactId>slf4j-api</artifactId>
@@ -81,12 +91,6 @@
8191
<artifactId>junit-jupiter</artifactId>
8292
<scope>test</scope>
8393
</dependency>
84-
<dependency>
85-
<groupId>org.awaitility</groupId>
86-
<artifactId>awaitility</artifactId>
87-
<version>4.1.1</version>
88-
<scope>test</scope>
89-
</dependency>
9094
<dependency>
9195
<groupId>org.mockito</groupId>
9296
<artifactId>mockito-inline</artifactId>

comet-java-client/src/main/java/ml/comet/experiment/CometApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ml.comet.experiment;
22

3-
import ml.comet.experiment.model.ExperimentMetadataRest;
4-
import ml.comet.experiment.model.RestProject;
3+
import ml.comet.experiment.model.ExperimentMetadata;
4+
import ml.comet.experiment.model.Project;
55

66
import java.io.Closeable;
77
import java.util.List;
@@ -26,13 +26,13 @@ public interface CometApi extends Closeable {
2626
* @param workspaceName workspace name
2727
* @return List of project DTOs
2828
*/
29-
List<RestProject> getAllProjects(String workspaceName);
29+
List<Project> getAllProjects(String workspaceName);
3030

3131
/**
3232
* Gets all experiment DTOs under specified project id.
3333
*
3434
* @param projectId Project id
3535
* @return List of experiment DTOs
3636
*/
37-
List<ExperimentMetadataRest> getAllExperiments(String projectId);
37+
List<ExperimentMetadata> getAllExperiments(String projectId);
3838
}

0 commit comments

Comments
 (0)