-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from SSU-Plector/issue/141-clova-speech
♻️ [Refactor] ClovaSpeech API 사용방식으로 변경
- Loading branch information
Showing
9 changed files
with
110 additions
and
229 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/main/java/ssuPlector/ai/naverCloud/service/ClovaService.java
This file was deleted.
Oops, something went wrong.
150 changes: 0 additions & 150 deletions
150
src/main/java/ssuPlector/ai/naverCloud/service/ClovaServiceImpl.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/main/java/ssuPlector/ai/naverCloud/service/ClovaSpeechService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ssuPlector.ai.naverCloud.service; | ||
|
||
import java.io.File; | ||
|
||
public interface ClovaSpeechService { | ||
|
||
String convert(File file); | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/ssuPlector/ai/naverCloud/service/ClovaSpeechServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ssuPlector.ai.naverCloud.service; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.FileSystemResource; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import reactor.core.publisher.Mono; | ||
import ssuPlector.global.exception.GlobalException; | ||
import ssuPlector.global.response.code.GlobalErrorCode; | ||
|
||
@Component | ||
public class ClovaSpeechServiceImpl implements ClovaSpeechService { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final WebClient webClient; | ||
|
||
@Value("${naver.cloud.clovaSpeech.client-secret}") | ||
String clientSecret; | ||
|
||
public ClovaSpeechServiceImpl(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
this.webClient = | ||
WebClient.builder() | ||
.baseUrl( | ||
"https://clovaspeech-gw.ncloud.com/external/v1/8450/a2c84d3e94793711c615c18275ecf83ebe3be19059abd2d901513e1a749035da") | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String convert(File file) { | ||
|
||
try { | ||
String language = "ko-KR"; | ||
|
||
Mono<String> responseMono = | ||
webClient | ||
.post() | ||
.uri("/recognizer/upload") | ||
.header("X-CLOVASPEECH-API-KEY", clientSecret) | ||
.contentType(MediaType.MULTIPART_FORM_DATA) | ||
.bodyValue(generateMultipartBody(file, language)) | ||
.retrieve() | ||
.bodyToMono(String.class) | ||
.map(this::getTextFromResponse); | ||
|
||
return responseMono.block(); | ||
} catch (Exception e) { | ||
throw new GlobalException(GlobalErrorCode._INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
private MultiValueMap<String, Object> generateMultipartBody(File file, String language) { | ||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); | ||
body.add("media", new FileSystemResource(file)); | ||
|
||
Map<String, Object> params = new HashMap<>(); | ||
params.put("language", language); | ||
params.put("completion", "sync"); | ||
params.put("wordAlignment", true); | ||
params.put("fullText", true); | ||
params.put("noiseFiltering", true); | ||
params.put("diarization", Map.of("enable", true)); | ||
params.put("format", "JSON"); | ||
|
||
body.add("params", new HttpEntity<>(params, createJsonHeaders())); | ||
return body; | ||
} | ||
|
||
private HttpHeaders createJsonHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
return headers; | ||
} | ||
|
||
private String getTextFromResponse(String responseStr) { | ||
try { | ||
return objectMapper.readTree(responseStr).get("text").asText(); | ||
} catch (Exception e) { | ||
throw new GlobalException(GlobalErrorCode._INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.