Skip to content

Commit

Permalink
Merge pull request #50 from SWM-SMART/feat/#49
Browse files Browse the repository at this point in the history
모든 AI 요청 SSE로 수정
  • Loading branch information
noparamin authored Nov 12, 2023
2 parents ce5be3d + ece3e37 commit c72e6ee
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class AudioFileController {
private final FileService fileService;
private final MindmapService mindmapService;
private final WhiteboardService whiteboardService;
private final KeywordService keywordService;
private final JwtService jwtService;
private final SseService sseService;

@PostMapping("/{documentID}/audio")
public ResponseEntity<?> uploadAudioFile(@PathVariable(value = "documentID") long documentId, @RequestParam("audio") MultipartFile audioFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException, DocumentException {
Expand Down Expand Up @@ -74,19 +74,22 @@ public ResponseEntity<?> uploadAudioFile(@PathVariable(value = "documentID") lon
List<SttData> data = sttService.getSTTData(sttResponseEntity);
lectureNoteService.createLectureNote(documentId, data, sttResult);

ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestSTTKeywords(textPdfPath);
List<String> keywords = keywordService.createKeywords(responseEntity, documentId);
// ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestSTTKeywords(textPdfPath);
// List<String> keywords = keywordService.createKeywords(responseEntity, documentId);
//
// // 요약본 요청
// ResponseEntity<SummaryDto> summary = requestService.requestSTTSummary(textPdfPath);
// summaryService.createSummary(documentId, summary.getBody().getSummary());
sseService.notifyKeywords(documentId, textPdfPath);
sseService.notifySummary(documentId, textPdfPath);

// 요약본 요청
ResponseEntity<SummaryDto> summary = requestService.requestSTTSummary(textPdfPath);
summaryService.createSummary(documentId, summary.getBody().getSummary());

UploadDto uploadDto = new UploadDto(keywords, data);
//UploadDto uploadDto = new UploadDto(keywords, data);

whiteboardService.setDataType(documentId, "audio");

// 응답 키워드, stt
return new ResponseEntity<>(uploadDto, HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.OK);
}

@PutMapping("/{documentID}/audio")
Expand Down Expand Up @@ -116,17 +119,20 @@ public ResponseEntity<?> updateAudioFile(@PathVariable(value = "documentID") lon
S3Dto s3DtoForSTT = new S3Dto(multipartFile, documentId, userId, "pdf");
String textPdfPath = awsS3Uploader.uploadTextPdfFile(s3DtoForSTT);

ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestSTTKeywords(textPdfPath);
List<String> keywords = keywordService.renewKeywords(responseEntity, documentId); // update
sseService.notifyKeywords(documentId, textPdfPath);
sseService.notifySummary(documentId, textPdfPath);

// 요약본 요청
ResponseEntity<SummaryDto> summary = requestService.requestSTTSummary(sttResult);
summaryService.updateSummary(documentId, summary.getBody().getSummary()); // update
UploadDto uploadDto = new UploadDto(keywords, data);
// ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestSTTKeywords(textPdfPath);
// List<String> keywords = keywordService.renewKeywords(responseEntity, documentId); // update
//
// // 요약본 요청
// ResponseEntity<SummaryDto> summary = requestService.requestSTTSummary(sttResult);
// summaryService.updateSummary(documentId, summary.getBody().getSummary()); // update
// UploadDto uploadDto = new UploadDto(keywords, data);

whiteboardService.setDataType(documentId, "audio");

return new ResponseEntity<>(uploadDto, HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.OK);
}

@DeleteMapping("/{documentID}/audio")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
public class LearningFileController {
private final AwsS3Uploader awsS3Uploader;
private final FileService fileService;
private final RequestService requestService;
private final WhiteboardService whiteboardService;
private final SummaryService summaryService;
private final KeywordService keywordService;
private final JwtService jwtService;
private final SseService sseService;

@PostMapping("/{documentID}/pdf")
public ResponseEntity<?> uploadLearningFile(@PathVariable(value = "documentID") long documentId, @RequestParam("pdf") MultipartFile pdfFile, @RequestHeader("Authorization") String accessToken) throws UnsupportedAudioFileException, IOException {
Expand All @@ -40,14 +38,17 @@ public ResponseEntity<?> uploadLearningFile(@PathVariable(value = "documentID")

S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf");
String path = awsS3Uploader.uploadFile(s3Dto);
ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
keywordService.createKeywords(responseEntity, documentId);

ResponseEntity<SummaryDto> summary = requestService.requestPdfSummary(path);
summaryService.createSummary(documentId, summary.getBody().getSummary());
sseService.notifyKeywords(documentId, path);
sseService.notifySummary(documentId, path);
// ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
// keywordService.createKeywords(responseEntity, documentId);
//
// ResponseEntity<SummaryDto> summary = requestService.requestPdfSummary(path);
// summaryService.createSummary(documentId, summary.getBody().getSummary());
whiteboardService.setDataType(documentId, "pdf");

return new ResponseEntity<>(responseEntity.getBody(), HttpStatus.OK);
//return new ResponseEntity<>(responseEntity.getBody(), HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.OK);
}

@PutMapping("/{documentID}/pdf")
Expand All @@ -57,14 +58,16 @@ public ResponseEntity<?> updateLearningFile(@PathVariable(value = "documentID")

S3Dto s3Dto = new S3Dto(pdfFile, documentId, userId, "pdf");
String path = awsS3Uploader.uploadFile(s3Dto);
ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
keywordService.renewKeywords(responseEntity, documentId);

ResponseEntity<SummaryDto> summary = requestService.requestPdfSummary(path);
summaryService.updateSummary(documentId, summary.getBody().getSummary());
sseService.notifyKeywords(documentId, path);
sseService.notifySummary(documentId, path);
// ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestPdfKeywords(path);
// keywordService.renewKeywords(responseEntity, documentId);
//
// ResponseEntity<SummaryDto> summary = requestService.requestPdfSummary(path);
// summaryService.updateSummary(documentId, summary.getBody().getSummary());
whiteboardService.setDataType(documentId, "pdf");

return new ResponseEntity<>(responseEntity.getBody(), HttpStatus.OK);
return new ResponseEntity<>(HttpStatus.OK);
}

@DeleteMapping("/{documentID}/pdf")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.smart.watchboard.repository;

import com.smart.watchboard.domain.Document;
import com.smart.watchboard.domain.Keyword;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public void updateKeywords(KeywordsDto keywordsDto, Long documentId) {
keywordRepository.save(updatedKeyword);
}

public Keyword findKeywords(Long documentId) {
Optional<Keyword> keyword = keywordRepository.findByDocumentId(documentId);

return keyword.orElse(null);
}

public void deleteAllKeywords(Keyword keyword) {
keywordRepository.delete(keyword);
}
Expand Down
76 changes: 73 additions & 3 deletions src/main/java/com/smart/watchboard/service/SseService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.smart.watchboard.service;

import com.smart.watchboard.domain.Note;
import com.smart.watchboard.dto.AnswerDto;
import com.smart.watchboard.dto.KeywordsBodyDto;
import com.smart.watchboard.dto.MindmapResponseDto;
import com.smart.watchboard.dto.SummaryDto;
import com.smart.watchboard.repository.EmitterRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,8 +22,10 @@ public class SseService {
private final FileService fileService;
private final WhiteboardService whiteboardService;
private final RequestService requestService;
private final LectureNoteService lectureNoteService;
//private final LectureNoteService lectureNoteService;
private final NoteService noteService;
private final KeywordService keywordService;
private final SummaryService summaryService;

public SseEmitter subscribe(Long documentId) {
SseEmitter emitter = createEmitter(documentId);
Expand Down Expand Up @@ -52,11 +57,11 @@ private void sendToClient(Long documentId, List<String> keywords) {
if (whiteboardService.isPdfType(documentId)) {
String path = fileService.getPdfUrl(documentId);
ResponseEntity<MindmapResponseDto> body = requestService.requestPdfMindmap(path, documentId, keywords);
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("sse").data(body.getBody()));
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("mindmap").data(body.getBody()));
} else if (whiteboardService.isAudioType(documentId)) {
Note note = noteService.findByDocument(documentId);
ResponseEntity<MindmapResponseDto> body = requestService.requestSTTMindmap(note.getPath(), documentId, keywords);
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("sse").data(body.getBody()));
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("mindmap").data(body.getBody()));
}
} catch (IOException exception) {
emitterRepository.deleteById(documentId);
Expand All @@ -65,6 +70,71 @@ private void sendToClient(Long documentId, List<String> keywords) {
}
}

public void notifyKeywords(Long documentId, String path) {
sendKeywords(documentId, path);
}

public void notifySummary(Long documentId, String path) {
sendSummary(documentId, path);
}

public void notifyAnswer(Long documentId, String path) {
sendAnswer(documentId, path);
}

private void sendKeywords(Long documentId, String path) {
SseEmitter emitter = emitterRepository.get(documentId);
if (emitter != null) {
try {
ResponseEntity<KeywordsBodyDto> responseEntity = requestService.requestSTTKeywords(path);
//List<String> keywords = keywordService.createKeywords(responseEntity, documentId);
if (keywordService.findKeywords(documentId) == null) {
keywordService.createKeywords(responseEntity, documentId);
} else {
keywordService.renewKeywords(responseEntity, documentId);
}
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("keywords").data(responseEntity.getBody().getKeywords()));
} catch (IOException exception) {
emitterRepository.deleteById(documentId);
emitter.completeWithError(exception);
}

}
}

private void sendSummary(Long documentId, String path) {
SseEmitter emitter = emitterRepository.get(documentId);
if (emitter != null) {
try {
ResponseEntity<SummaryDto> summary = requestService.requestSTTSummary(path);
if (summaryService.findSummary(documentId) == null) {
summaryService.createSummary(documentId, summary.getBody().getSummary());
} else {
summaryService.updateSummary(documentId, summary.getBody().getSummary());
}

//emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("summary").data(summary.getBody().getSummary()));
} catch (IOException exception) {
emitterRepository.deleteById(documentId);
emitter.completeWithError(exception);
}
}
}

private void sendAnswer(Long documentId, String keywordLabel) {
SseEmitter emitter = emitterRepository.get(documentId);
if (emitter != null) {
try {
ResponseEntity<AnswerDto> responseEntity = requestService.requestAnswer(documentId, keywordLabel);
emitter.send(SseEmitter.event().id(String.valueOf(documentId)).name("answer").data(responseEntity.getBody()));
} catch (IOException exception) {
emitterRepository.deleteById(documentId);
emitter.completeWithError(exception);
}
}

}

private SseEmitter createEmitter(Long documentId) {
SseEmitter emitter = new SseEmitter(DEFAULT_TIMEOUT);
emitterRepository.save(documentId, emitter);
Expand Down

0 comments on commit c72e6ee

Please sign in to comment.