-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
329 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.splanet.splanet.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { //์ธํฐํ์ด์ค WebMvcConfigurer ์์ | ||
registry.addMapping("/**") //๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ํ์ฉํด์ค๊ฒ์ด๋ฏ๋ก | ||
.allowedOrigins("*") //๋ฆฌ์์ค ๊ณต์ ํ๋ฝํ origin ์ง์ | ||
.allowedMethods("*"); //๋ชจ๋ ๋ฉ์๋๋ฅผ ํ์ฉ | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/splanet/splanet/config/WebSocketConfig.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,24 @@ | ||
package com.splanet.splanet.config; | ||
|
||
import com.splanet.splanet.core.handler.SpeechWebSocketHandler; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocket; | ||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; | ||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; | ||
|
||
@Configuration | ||
@EnableWebSocket | ||
public class WebSocketConfig implements WebSocketConfigurer { | ||
|
||
private final SpeechWebSocketHandler speechWebSocketHandler; | ||
|
||
public WebSocketConfig(SpeechWebSocketHandler speechWebSocketHandler) { | ||
this.speechWebSocketHandler = speechWebSocketHandler; | ||
} | ||
|
||
@Override | ||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { | ||
registry.addHandler(speechWebSocketHandler, "/ws/stt") | ||
.setAllowedOrigins("*"); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/com/splanet/splanet/core/handler/SpeechWebSocketHandler.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,100 @@ | ||
package com.splanet.splanet.core.handler; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.google.protobuf.ByteString; | ||
import com.nbp.cdncp.nest.grpc.proto.v1.NestResponse; | ||
import com.splanet.splanet.stt.service.ClovaSpeechGrpcService; | ||
import io.grpc.stub.StreamObserver; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.BinaryMessage; | ||
import org.springframework.web.socket.CloseStatus; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
import org.springframework.web.socket.handler.BinaryWebSocketHandler; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class SpeechWebSocketHandler extends BinaryWebSocketHandler { | ||
|
||
private final ClovaSpeechGrpcService clovaSpeechGrpcService; | ||
private final Map<String, StreamObserver<ByteString>> clientObservers = new ConcurrentHashMap<>(); | ||
|
||
public SpeechWebSocketHandler(ClovaSpeechGrpcService clovaSpeechGrpcService) { | ||
this.clovaSpeechGrpcService = clovaSpeechGrpcService; | ||
} | ||
|
||
@Override | ||
public void afterConnectionEstablished(WebSocketSession session) throws Exception { | ||
// ์ธ์ ์ด ์ด๋ฆด ๋๋ง๋ค ์๋ก์ด gRPC ์คํธ๋ฆผ์ ์์ฑ | ||
StreamObserver<NestResponse> responseObserver = new StreamObserver<NestResponse>() { | ||
@Override | ||
public void onNext(NestResponse value) { | ||
// ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ์๋ต ์ฒ๋ฆฌ | ||
try { | ||
String contents = value.getContents(); // JSON ๋ฌธ์์ด | ||
|
||
// JSON ํ์ฑ | ||
JsonParser parser = new JsonParser(); | ||
JsonObject jsonObject = parser.parse(contents).getAsJsonObject(); | ||
|
||
if (jsonObject.has("transcription")) { | ||
JsonObject transcription = jsonObject.getAsJsonObject("transcription"); | ||
String text = transcription.get("text").getAsString(); | ||
// ํด๋ผ์ด์ธํธ๋ก text ํ๋๋ง ์ ์ก | ||
session.sendMessage(new TextMessage(text)); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
t.printStackTrace(); | ||
try { | ||
session.sendMessage(new TextMessage("์ค๋ฅ ๋ฐ์: " + t.getMessage())); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
// ์คํธ๋ฆผ ์๋ฃ ์ฒ๋ฆฌ | ||
try { | ||
session.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}; | ||
|
||
// ์ค๋์ค ๋ฐ์ดํฐ๋ฅผ ์ ์กํ StreamObserver ์์ฑ | ||
StreamObserver<ByteString> requestObserver = clovaSpeechGrpcService.recognize(responseObserver); | ||
clientObservers.put(session.getId(), requestObserver); | ||
} | ||
|
||
@Override | ||
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception { | ||
// ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ ์ค๋์ค ๋ฐ์ดํฐ๋ฅผ gRPC ์๋น์ค๋ก ์ ๋ฌ | ||
StreamObserver<ByteString> requestObserver = clientObservers.get(session.getId()); | ||
if (requestObserver != null) { | ||
byte[] audioData = message.getPayload().array(); | ||
ByteString audioChunk = ByteString.copyFrom(audioData); | ||
requestObserver.onNext(audioChunk); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { | ||
// ์ธ์ ์ด ์ข ๋ฃ๋๋ฉด gRPC ์คํธ๋ฆผ๋ ์ข ๋ฃ | ||
StreamObserver<ByteString> requestObserver = clientObservers.remove(session.getId()); | ||
if (requestObserver != null) { | ||
requestObserver.onCompleted(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/splanet/splanet/core/properties/ClovaProperties.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,15 @@ | ||
package com.splanet.splanet.core.properties; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Configuration | ||
@ConfigurationProperties(prefix = "clova.speech") | ||
public class ClovaProperties { | ||
private String clientSecret; | ||
private String language; | ||
} |
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
89 changes: 89 additions & 0 deletions
89
src/main/java/com/splanet/splanet/stt/service/ClovaSpeechGrpcService.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,89 @@ | ||
package com.splanet.splanet.stt.service; | ||
|
||
import com.google.protobuf.ByteString; | ||
import com.nbp.cdncp.nest.grpc.proto.v1.*; | ||
import com.splanet.splanet.core.properties.ClovaProperties; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.Metadata; | ||
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; | ||
import io.grpc.stub.MetadataUtils; | ||
import io.grpc.stub.StreamObserver; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ClovaSpeechGrpcService implements ClovaSpeechService { | ||
|
||
private final NestServiceGrpc.NestServiceStub nestServiceStub; | ||
private final ClovaProperties clovaProperties; | ||
|
||
public ClovaSpeechGrpcService(ClovaProperties clovaProperties) { | ||
this.clovaProperties = clovaProperties; | ||
|
||
// gRPC ์ฑ๋ ์์ฑ | ||
ManagedChannel channel = NettyChannelBuilder | ||
.forAddress("clovaspeech-gw.ncloud.com", 50051) | ||
.useTransportSecurity() | ||
.build(); | ||
|
||
// Stub ์์ฑ ๋ฐ ์ธ์ฆ ์ ๋ณด ์ค์ | ||
NestServiceGrpc.NestServiceStub stub = NestServiceGrpc.newStub(channel); | ||
Metadata metadata = new Metadata(); | ||
metadata.put(Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER), "Bearer " + clovaProperties.getClientSecret()); | ||
this.nestServiceStub = MetadataUtils.attachHeaders(stub, metadata); | ||
} | ||
|
||
@Override | ||
public StreamObserver<ByteString> recognize(StreamObserver<NestResponse> responseObserver) { | ||
StreamObserver<NestRequest> requestObserver = nestServiceStub.recognize(responseObserver); | ||
|
||
// Config ๋ฉ์์ง ์ ์ก | ||
requestObserver.onNext(createConfigRequest(clovaProperties.getLanguage())); | ||
|
||
return new StreamObserver<ByteString>() { | ||
private int sequenceId = 0; | ||
|
||
@Override | ||
public void onNext(ByteString audioChunk) { | ||
NestRequest dataRequest = createDataRequest(audioChunk, sequenceId, false); | ||
requestObserver.onNext(dataRequest); | ||
sequenceId++; | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
t.printStackTrace(); | ||
requestObserver.onError(t); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
requestObserver.onCompleted(); | ||
} | ||
}; | ||
} | ||
|
||
// Config ์ค์ | ||
private NestRequest createConfigRequest(String language) { | ||
NestConfig config = NestConfig.newBuilder() | ||
.setConfig("{\"transcription\":{\"language\":\"" + language + "\"}}") | ||
.build(); | ||
|
||
return NestRequest.newBuilder() | ||
.setType(RequestType.CONFIG) | ||
.setConfig(config) | ||
.build(); | ||
} | ||
|
||
// ๋ฐ์ดํฐ ๊ตฌ์ฑ | ||
private NestRequest createDataRequest(ByteString audioChunk, int sequenceId, boolean epFlag) { | ||
NestData data = NestData.newBuilder() | ||
.setChunk(audioChunk) | ||
.setExtraContents("{\"seqId\":" + sequenceId + ",\"epFlag\":" + epFlag + "}") | ||
.build(); | ||
|
||
return NestRequest.newBuilder() | ||
.setType(RequestType.DATA) | ||
.setData(data) | ||
.build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/splanet/splanet/stt/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,9 @@ | ||
package com.splanet.splanet.stt.service; | ||
|
||
import com.google.protobuf.ByteString; | ||
import com.nbp.cdncp.nest.grpc.proto.v1.NestResponse; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
public interface ClovaSpeechService { | ||
StreamObserver<ByteString> recognize(StreamObserver<NestResponse> responseObserver); | ||
} |
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,34 @@ | ||
syntax = "proto3"; | ||
option java_multiple_files = true; | ||
|
||
package com.nbp.cdncp.nest.grpc.proto.v1; | ||
|
||
enum RequestType { | ||
CONFIG = 0; | ||
DATA = 1; | ||
} | ||
|
||
message NestConfig { | ||
string config = 1; | ||
} | ||
|
||
message NestData { | ||
bytes chunk = 1; | ||
string extra_contents = 2; | ||
} | ||
|
||
message NestRequest { | ||
RequestType type = 1; | ||
oneof part { | ||
NestConfig config = 2; | ||
NestData data = 3; | ||
} | ||
} | ||
|
||
message NestResponse { | ||
string contents = 1; | ||
} | ||
|
||
service NestService { | ||
rpc recognize(stream NestRequest) returns (stream NestResponse) {} | ||
} |