@NotNull @Override public JsonElement serialize(@Nullable ClarifaiInput value, @NotNull Gson gson) { if (value == null) { return JsonNull.INSTANCE; } final JSONObjectBuilder builder = new JSONObjectBuilder() .add("id", value.id()); final JSONObjectBuilder data = new JSONObjectBuilder() .add("concepts", toJson(gson, value.concepts(), new TypeToken<List<Concept>>() {})) .add("metadata", value.metadata()); if (value.inputValue() instanceof ClarifaiVideo) { data.add("video", toJson(gson, (ClarifaiVideo) value.inputValue(), ClarifaiVideo.class)); } else { data.add("image", toJson(gson, (ClarifaiImage) value.inputValue(), ClarifaiImage.class)); } if (value.geo() != null) { data.add("geo", new JSONObjectBuilder() .add("geo_point", new JSONObjectBuilder() .add("latitude", value.geo().x()) .add("longitude", value.geo().y()))); } if (value.createdAt() != null) { builder.addIfNotNull("created_at", toJson(gson, value.createdAt(), Date.class)); } builder.add("data", data); return builder.build(); } };
@NotNull private WorkflowPredictRequest makeWorkflowPredictRequestForFoodAndGeneral() { return client.workflowPredict(FOOD_AND_GENERAL_WORKFLOW_ID) .withInputs( ClarifaiInput.forImage(METRO_NORTH_IMAGE_URL)); } }
/** * Deprecated. Use {@link #forInputValue(ClarifaiInputValue)} instead. * @param image the image to represent * @return a {@link ClarifaiInput} that represents the given image */ @Deprecated @NotNull public static ClarifaiInput forImage(@NotNull ClarifaiImage image) { return forInputValue(image); }
/** * @param geo a geographic coordinate to assign to this input * @return a copy of this {@link ClarifaiInput} with its geographic coordinate set to the specified value */ @NotNull public final ClarifaiInput withGeo(@Nullable PointF geo) { return new AutoValue_ClarifaiInput(id(), createdAt(), inputValue(), metadata(), concepts(), geo, regions()); }
/** * @param metadata the metadata to attach to this input * @return a copy of this {@link ClarifaiInput} with its metadata set to the specified value */ @NotNull public final ClarifaiInput withMetadata(@NotNull JsonObject metadata) { InternalUtil.assertMetadataHasNoNulls(metadata); return new AutoValue_ClarifaiInput(id(), createdAt(), inputValue(), metadata, concepts(), geo(), regions()); }
@Test public void addMultipleInputsWithIDs() { client.addInputs() .plus( ClarifaiInput.forImage("@@sampleTrain") .withConcepts(Concept.forID("id1")), ClarifaiInput.forImage("@@sampleWedding") .withConcepts(Concept.forID("id2")) ) .allowDuplicateURLs(true) .executeSync(); }
@Nullable private String addInputWithConcepts(ClarifaiClient client, String url, String ...conceptIds) { List<Concept> concepts = new ArrayList<>(conceptIds.length); for (String concept : conceptIds) { concepts.add(Concept.forID(concept)); } ClarifaiInput input = ClarifaiInput.forImage(url).withConcepts(concepts); ClarifaiResponse<List<ClarifaiInput>> response = client.addInputs().plus(input).allowDuplicateURLs(true).executeSync(); if(!response.isSuccessful() || response.get().size() != 1) { return null; } return response.get().get(0).id(); }
@Retry @Test public void testMergeMetadata() { final String inputID = assertSuccess(client.addInputs() .allowDuplicateURLs(true) .plus(ClarifaiInput.forImage(METRO_NORTH_IMAGE_URL) ) ).get(0).id(); assertNotNull(inputID); final JsonObject newMetadata = assertSuccess( client.addMetadataForInput( inputID, new JSONObjectBuilder() .add("foo", "bar") .build() ) ).metadata(); assertEquals(new JSONObjectBuilder().add("foo", "bar").build(), newMetadata); }
@Test public void inputsRequestShouldReturnConceptsWhenWithPermissions() { try { addInputWithConcepts(client, FAMILY_IMAGE_URL, "conceptA", "conceptB"); addInputWithConcepts(client, METRO_NORTH_IMAGE_URL, "conceptA"); GetInputsRequest request = makeClient(EnvVar.CLARIFAI_API_KEY_ALL_GET.value()).getInputs(); ClarifaiResponse<List<ClarifaiInput>> response = request.getPage(1).executeSync(); assertEquals(10000, response.getStatus().statusCode()); int maxConceptsSize = 0; for (ClarifaiInput inputResponse : response.get()) { maxConceptsSize = Math.max(inputResponse.concepts().size(), maxConceptsSize); } assertNotEquals(0, maxConceptsSize); } finally { client.deleteAllInputs().executeSync(); } }
assertSuccess(client.addInputs() .plus( ClarifaiInput.forImage(FERRARI_IMAGE_URL) .withConcepts( ferrari23.withValue(true) ), ClarifaiInput.forImage(FERRARI_IMAGE_URL2) .withConcepts( ferrari23.withValue(true), outdoors23.withValue(false) ), ClarifaiInput.forImage(HONDA_IMAGE_URL) .withConcepts( ferrari23.withValue(true), outdoors23 ).withGeo(PointF.at(30, -24)), ClarifaiInput.forImage(HONDA_IMAGE_URL2) .withConcepts( ferrari23.withValue(false), outdoors23 ), ClarifaiInput.forImage(FERRARI_IMAGE_URL3) .withConcepts( ferrari23.withValue(false), outdoors23 ), ClarifaiInput.forImage(TOYOTA_IMAGE_URL) .withConcepts( ferrari23.withValue(false),
@Retry @Test public void t01a_addInputs() throws Exception { assertSuccess(client.addInputs() .plus(ClarifaiInput.forInputValue(ClarifaiImage.of(KOTLIN_LOGO_IMAGE_FILE) .withCrop(Crop.create() .top(0.1F) .left(0.1F) .bottom(0.9F) .right(0.9F) ) ) .withID("foo1") .withConcepts(Concept.forID("concept1").withValue(false)) ) ); }
@Retry @Test public void t16d_predictBatchBase64WithModel() { List<ClarifaiInput> inputs = new ArrayList<>(); inputs.add(ClarifaiInput.forImage(KOTLIN_LOGO_IMAGE_FILE).withID("myID1")); inputs.add(ClarifaiInput.forImage(KOTLIN_LOGO_IMAGE_FILE).withID("myID2")); PredictRequest<Concept> request = client.getDefaultModels().generalModel().predict() .withInputs(inputs); assertSuccess(request); ClarifaiResponse<List<ClarifaiOutput<Concept>>> response = request.executeSync(); assertTrue(response.isSuccessful()); }
@Retry @Test public void t01c_addInputWithMetadata() { assertSuccess(client.addInputs().plus(ClarifaiInput.forImage(KOTLIN_LOGO_IMAGE_FILE) .withID("inputWithMetadata") .withMetadata(new JSONObjectBuilder() .add("foo", "bar") .build() ) )); }
@Retry @Test public void t17c_searchInputsWithModel_metadata() { final List<SearchHit> hits = assertSuccess( client.searchInputs(SearchClause.matchMetadata(new JSONObjectBuilder().add("foo", "bar").build())) ).searchHits(); final ClarifaiInput hit = hits.stream() .filter(someHit -> "inputWithMetadata".equals(someHit.input().id())) .findFirst() .orElseThrow(() -> new AssertionError("")) .input(); assertEquals("inputWithMetadata", hit.id()); assertEquals(new JSONObjectBuilder().add("foo", "bar").build(), hit.metadata()); }
ClarifaiInput.forInputValue(ClarifaiImage.of("@@samplePuppy")) .withConcepts( Concept.forID("boscoe") ), ClarifaiInput.forInputValue(ClarifaiImage.of("@@sampleWedding")) .withConcepts( Concept.forID("boscoe").withValue(false)
@Retry @Test public void t26_testGeneralVideoModel() { ClarifaiResponse<List<ClarifaiOutput<Frame>>> frames = client.getDefaultModels().generalVideoModel().predict() .withInputs(ClarifaiInput.forVideo(CONAN_GIF_URL)) .executeSync(); Assert.assertNotNull(frames.get()); Assert.assertNotNull(frames.get().get(0)); Assert.assertNotNull(frames.get().get(0).data()); Assert.assertNotNull(frames.get().get(0).data().get(0)); Assert.assertNotNull(frames.get().get(0).data().get(0).index()); Assert.assertNotNull(frames.get().get(0).data().get(0).time()); Assert.assertNotNull(frames.get().get(0).data().get(0).concepts()); }
@Test public void t17e_searchInputsWithModel_geo() { assertSuccess(client.addInputs().plus( ClarifaiInput.forImage(METRO_NORTH_IMAGE_URL) .withGeo(PointF.at(90F, 23F)) )); assertSuccess( client.searchInputs(matchConcept(Concept.forID("outdoors23").withValue(true))) .and(SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))) .and(SearchClause.matchGeo(PointF.at(90F, 23F), Radius.of(5, Radius.Unit.MILE))) .build() ); }
@NotNull @Override protected List<ClarifaiInput> unmarshalerGrpc(Object returnedObject) { InputOuterClass.MultiInputResponse inputsResponse = (InputOuterClass.MultiInputResponse) returnedObject; List<ClarifaiInput> inputs = new ArrayList<>(); for (InputOuterClass.Input input : inputsResponse.getInputsList()) { inputs.add(ClarifaiInput.deserialize(input)); } return inputs; }
@NotNull public final ClarifaiInput withConcepts(@NotNull Concept... concepts) { return withConcepts(Arrays.asList(concepts)); }
/** * A search clause that will match inputs that had metadata that matches the metadata on the given * {@link ClarifaiInput} * * @param input the input whose metadata to search by * @return a {@link SearchClause} to be given to a search endpoint * @see #matchMetadata(JsonObject) */ @NotNull public static SearchClause matchMetadata(@NotNull ClarifaiInput input) { return matchMetadata(input.metadata()); }