Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public CreateChatbotResponseDto createCartoon(String graphId, CreateChatbotReque
prompt,
"dall-e-3",
"vivid",
"standard",
"1024x1024",
1
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
@AllArgsConstructor
public class ImageCreateRequestDto {
private String prompt;
private String model = "dall-e-3";
private String style = "vivid";
private String size = "1024x1024";
private String model;
private String style;
private String quality;
private String size;
private int n = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.going.server.domain.graph.entity.Graph;
import com.going.server.domain.graph.entity.GraphNode;
import com.going.server.domain.graph.repository.GraphRepository;
import com.going.server.domain.openai.dto.ImageCreateRequestDto;
import com.going.server.domain.openai.service.ImageCreateService;
import com.going.server.domain.quiz.dto.PictureQuizDto;
Expand All @@ -14,58 +15,61 @@
@AllArgsConstructor
public class PictureQuizGenerator implements QuizGenerator<PictureQuizDto> {
private final ImageCreateService imageCreateService;
private final GraphRepository graphRepository;

@Override
public PictureQuizDto generate(Graph graph) {
public PictureQuizDto generate(Graph currentGraph) {
Random random = new Random();
Set<String> shuffled = new HashSet<>(); // 보기둜 μ œκ³΅ν•  랜덀 λ¬Έμž₯λ“€(쀑볡 λ°©μ§€)
int shuffledListSize = 3; // 총 보기 수 (μ •λ‹΅ 포함)
List<String> candidateSentences = new ArrayList<>(); // μ „μ²˜λ¦¬ν•œ includeSentence
int shuffledListSize = 3;

// κ·Έλž˜ν”„ λ…Έλ“œλ₯Ό λŒλ©΄μ„œ λ¬Έμž₯ 후보 μˆ˜μ§‘
for (GraphNode node : graph.getNodes()) {
// IncludeSentenceκ°€ λΉ„μ–΄μžˆλŠ” 경우 λ„˜μ–΄κ°€κΈ°
if (node.getIncludeSentence() == null || node.getIncludeSentence().isBlank()) continue;
// μ •λ‹΅ 후보 λ¬Έμž₯: ν˜„μž¬ κ·Έλž˜ν”„μ—μ„œ μˆ˜μ§‘
List<String> answerCandidates = extractCandidateSentences(currentGraph);

// "." 으둜 λ¬Έμž₯ λ‚˜λˆ„κΈ°
String[] splitSentences = node.getIncludeSentence().split("\\.");
if (answerCandidates.isEmpty()) {
throw new IllegalStateException("ν˜„μž¬ κ·Έλž˜ν”„μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” λ¬Έμž₯이 μ—†μŠ΅λ‹ˆλ‹€.");
}

for (String rawSentence : splitSentences) {
String sentence = rawSentence.trim();
if (sentence.isBlank()) continue; // 곡백은 μŠ€ν‚΅
if (sentence.split("\\s+").length < 5) continue; // 5단어 λ―Έλ§Œμ€ μŠ€ν‚΅
// μ •λ‹΅ 1개 선택
Collections.shuffle(answerCandidates, random);
String answer = answerCandidates.get(0);

candidateSentences.add(sentence);
// μ˜€λ‹΅ 후보 λ¬Έμž₯: λ‹€λ₯Έ κ·Έλž˜ν”„λ“€μ—μ„œ μˆ˜μ§‘
List<Graph> allGraphs = graphRepository.findAll();
List<String> distractorCandidates = new ArrayList<>();
for (Graph graph : allGraphs) {
if (!graph.equals(currentGraph)) {
distractorCandidates.addAll(extractCandidateSentences(graph));
}
}

// 후보 λ¬Έμž₯이 λΆ€μ‘±ν•  경우 λ°©μ–΄
if (candidateSentences.size() < shuffledListSize) {
throw new IllegalStateException("생성 κ°€λŠ₯ν•œ λ¬Έμž₯이 λΆ€μ‘±ν•©λ‹ˆλ‹€.");
// μ˜€λ‹΅μ΄ λΆ€μ‘±ν•œ 경우: ν˜„μž¬ κ·Έλž˜ν”„μ—μ„œ μΆ”κ°€ 확보
if (distractorCandidates.size() < shuffledListSize - 1) {
distractorCandidates.addAll(answerCandidates.subList(1, Math.min(answerCandidates.size(), shuffledListSize)));
}

// 랜덀으둜 λ¬Έμž₯ 선택
Collections.shuffle(candidateSentences, random);

// 보기용 λ¬Έμž₯ μΆ”κ°€
// μ„ μ§€ μ…”ν”Œ 및 μ •λ‹΅ ν¬ν•¨ν•΄μ„œ 뽑기
Collections.shuffle(distractorCandidates, random);
Set<String> selectedSentences = new LinkedHashSet<>();
selectedSentences.add(answer);
int index = 0;
while (selectedSentences.size() < shuffledListSize && index < candidateSentences.size()) {
selectedSentences.add(candidateSentences.get(index));
while (selectedSentences.size() < shuffledListSize && index < distractorCandidates.size()) {
selectedSentences.add(distractorCandidates.get(index));
index++;
}

// λ¬΄μž‘μœ„λ‘œ μ •λ‹΅ 보기 μ„€μ •
int answerIndex = random.nextInt(shuffledListSize);
String answer = new ArrayList<>(selectedSentences).get(answerIndex);
if (selectedSentences.size() < shuffledListSize) {
throw new IllegalStateException("λ¬Έμ œκ°€ 될 λ¬Έμž₯이 μΆ©λΆ„ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
}

String prompt = buildQuizImagePrompt(answer);
ImageCreateRequestDto requestDto = new ImageCreateRequestDto(
prompt,
"dall-e-3",
"vivid",
"512x512",
1);
"standard",
"1024x1024",
1
);
String imageUrl = imageCreateService.generatePicture(requestDto);

return PictureQuizDto.builder()
Expand All @@ -75,6 +79,26 @@ public PictureQuizDto generate(Graph graph) {
.build();
}

// λ¬Έμž₯ 후보 μΆ”μΆœ λ©”μ„œλ“œ 뢄리
private List<String> extractCandidateSentences(Graph graph) {
List<String> sentences = new ArrayList<>();

for (GraphNode node : graph.getNodes()) {
String raw = node.getIncludeSentence();
if (raw == null || raw.isBlank()) continue;

String[] split = raw.split("\\.");
for (String s : split) {
String trimmed = s.trim();
if (trimmed.isBlank()) continue;
if (trimmed.split("\\s+").length < 5) continue;
sentences.add(trimmed);
}
}

return sentences;
}

// 이미지 생성 ν”„λ‘¬ν”„νŠΈ 생성 λ©”μ„œλ“œ
public static String buildQuizImagePrompt(String answer) {
return "You are given an educational description in natural language.\n\n" +
Expand Down