Skip to content

Need Java example #31

@denghuichao

Description

@denghuichao

I try to invoke the stabilty rest api in Java via apache httpclient. Following is my code:
`@Override
public byte[] generate(String input, Config config) throws Exception {
String apiUrl = String.format("%s/v1/generation/%s/image-to-image",
Constants.STABILITY_API_HOST, "stable-diffusion-512-v2-1");
CloseableHttpClient httpClient = HttpClientUtils.getHttpClient();

CloseableHttpResponse response = null;
try {
  MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  builder.setCharset(Charset.forName("UTF-8"));
  URL url = new URL(config.getBaseImgUrl());
  builder.addBinaryBody("init_image", IOUtils.toByteArray(url));
  builder.addTextBody("image_strength", "0.35", ContentType.create("text/plain", Charset.forName("UTF-8")));
  builder.addTextBody("init_image_mode", "IMAGE_STRENGTH",  ContentType.create("text/plain", Charset.forName("UTF-8")));
  builder.addTextBody("text_prompts[0][text]", config.getPrompt(),  ContentType.create("text/plain", Charset.forName("UTF-8")));
  builder.addTextBody("cfg_scale", "7",  ContentType.create("text/plain", Charset.forName("UTF-8")));
  builder.addTextBody("clip_guidance_preset", "FAST_BLUE",  ContentType.create("text/plain", Charset.forName("UTF-8")));
  builder.addTextBody("samples", "1",  ContentType.create("text/plain", Charset.forName("UTF-8")));
  builder.addTextBody("steps", "30",  ContentType.create("text/plain", Charset.forName("UTF-8")));
  HttpEntity multipart = builder.build();

  HttpPost post = new HttpPost(apiUrl);
  post.addHeader(HttpHeaders.CONTENT_TYPE, "multipart/form-data");
  post.addHeader(HttpHeaders.ACCEPT, "application/json");
  post.addHeader(HttpHeaders.AUTHORIZATION, String.format("Bearer %s", config.getStabilityApiKey()));
  post.setEntity(multipart);
  response = httpClient.execute(post);

  int statusCode = response.getStatusLine().getStatusCode();
  String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);

  if (statusCode == 200) {
    Gson gson = new Gson();
    JsonObject jsonResponse = gson.fromJson(responseBody, JsonObject.class);
    JsonArray artifactsArray = jsonResponse.getAsJsonArray("artifacts");
    JsonObject artifactObject = artifactsArray.get(0).getAsJsonObject();
    String base64EncodedImage = artifactObject.get("base64").getAsString();
    byte[] imageBytes = Base64.getDecoder().decode(base64EncodedImage);
    return imageBytes;
  } else {
    throw new ContentGenerateException(
        String.format("stability api status code: %s, response: %s", statusCode, responseBody));
  }
} finally {
  if (response != null) {
    try {
      response.close();
    } catch (Exception e) {

    }
  }
}

}`

I think every thing is OK, but it returns me an error:
{"id":"b5ad9f42296d3eea01cd2bd3307478fb","message":"can't decode multipart body","name":"bad_request"}

What's going wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions