안녕하세요 제가 karlo를 사용해보고싶은데 예제가 전부다 phyton이나 curl밖에없더라고요 이걸 java에서 사용하고싶은데 어떤식으로 적용해야하는지 가이드 라인이있을까요??
안녕하세요.
별도 가이드라인은 없고 curl 과 동일하게 API 호출 하시면 됩니다.
URL url = new URL("https://api.kakaobrain.com/v2/inference/karlo/t2i");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Authorization", "KakaoAK " + REST_API_KEY);
httpConn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write("{\n" +
" \"prompt\": \"A cat with white fur\",\n" +
" \"negative_prompt\": \"sleeping cat, human, ugly face, cropped\"\n" +
" }");
writer.flush();
writer.close();
httpConn.getOutputStream().close();
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
try (Scanner scanner = new Scanner(responseStream).useDelimiter("\\A")) {
String response = scanner.hasNext() ? scanner.next() : "";
System.out.println(response);
}
참고, image URL to base64
//image URL to Base64
String exampleUrl="https://devtalk.kakao.com/user_avatar/devtalk.kakao.com/tim.l/96/14645_2.png";
URL imageUrl = new URL(exampleUrl);
BufferedImage img = ImageIO.read(imageUrl);
File file = new File("downloaded.jpg");
ImageIO.write(img, "jpg", file);
InputStream finput = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
finput.read(imageBytes, 0, imageBytes.length);
finput.close();
String imageStr = Base64.getEncoder().encodeToString(imageBytes);
감사합니다 한번 해볼께요!