Skip to content

Commit

Permalink
feat: dalle-3를 이용한 이미지 생성 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
oU-Ua committed Jul 20, 2024
1 parent 6b07d94 commit 970f23c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public ResponseDTO getEasyToReadCustom(String text) throws JsonProcessingExcepti
HttpRequest request = gptService.requestGPTCustom(text);
return gptConverter.toResponseDTO(gptService.responseGPT(request));
}

public ResponseDTO getImage(String keyword) throws JsonProcessingException {
HttpRequest request = gptService.requestGPTImage(keyword);
return gptConverter.toResponseDTO(gptService.responseDalle(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@ public ResponseEntity<ResponseDTO> easyLeadCustom(@RequestParam String text)

}

@GetMapping("/image")
public ResponseEntity<ResponseDTO> ImageGenerate(@RequestParam String keyword)
throws JsonProcessingException {
return ResponseEntity.ok(gptBusiness.getImage(keyword));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.easylead.easylead.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@JsonPropertyOrder({"model", "prompt", "n","size"})
public class DalleRequestDTO {
String model;
String prompt;
int n;
String size;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.easylead.easylead.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class DalleResData {
String url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.easylead.easylead.domain.gpt.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class DalleResponseDTO {
int created;


List<DalleResData> data;

}
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,51 @@ public HttpRequest requestGPTCustom(String text) throws JsonProcessingException


}

public HttpRequest requestGPTImage(String keyword) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List<Message> messages = new ArrayList<>();

messages.add(new Message(keyword, "user"));

DalleRequestDTO dalleRequest = new DalleRequestDTO("dall-e-3", keyword, 1,"1024x1024");
String input = null;
input = mapper.writeValueAsString(dalleRequest);
System.out.println(input);
System.out.println("apikey : " + gptApiCustomkey);

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.openai.com/v1/images/generations"))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + gptApiCustomkey)
.POST(HttpRequest.BodyPublishers.ofString(input))
.build();

return request;

}

public String responseDalle(HttpRequest request) throws JsonProcessingException {
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = null;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(response.body());
ObjectMapper objectMapper = new ObjectMapper();
DalleResponseDTO dalleResponseDTO = objectMapper.readValue(response.body(), DalleResponseDTO.class);
if(dalleResponseDTO.getData() ==null){
throw new ApiException(ErrorCode.SERVER_ERROR);
}
List<DalleResData> data = dalleResponseDTO.getData();

String url = data.get(0).getUrl();
String subject = "";

System.out.println("content = " + url);
return url;

}
}

0 comments on commit 970f23c

Please sign in to comment.