Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] apply updated proto and sync fields with database #37

Merged
merged 1 commit into from
Feb 20, 2024
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
2 changes: 1 addition & 1 deletion config/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions gen/proto/feedback.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 27 additions & 17 deletions gen/proto/sentence.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion idl
Submodule idl updated from 8f2925 to c63025
7 changes: 4 additions & 3 deletions src/converter/sentence.converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"github.com/gdsc-ys/fluentify-server/src/model"
)

func ToSentenceDTO(sentence model.Sentence) *pb.SentenceDTO {
func ToSentenceDTO(sentence model.Sentence, exampleVideoUrl string) *pb.SentenceDTO {
return &pb.SentenceDTO{
Id: sentence.Id,
Text: sentence.Text,
Id: sentence.Id,
Text: sentence.Text,
ExampleVideoUrl: exampleVideoUrl,
}
}
80 changes: 57 additions & 23 deletions src/handler/feedback.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"math"
"net/http"
"os"
"strings"
"unicode"
"unicode/utf8"
)

type FeedbackHandler interface {
Expand All @@ -23,19 +26,29 @@ type FeedbackHandler interface {
}

type FeedbackHandlerImpl struct {
storageService service.StorageService
sentenceService service.SentenceService
sceneService service.SceneService
storageService service.StorageService
}

func (handler *FeedbackHandlerImpl) GetPronunciationFeedback(c echo.Context) error {
var request = pb.GetPronunciationFeedbackRequest{}
if err := c.Bind(&request); err != nil {
return model.NewCustomHTTPError(http.StatusBadRequest, err)
}
if request.GetSentenceId() == "" {
return model.NewCustomHTTPError(http.StatusBadRequest, "sentence_id is required")
}
sentence, err := handler.sentenceService.GetSentence(request.GetSentenceId())
if err != nil {
return model.NewCustomHTTPError(http.StatusInternalServerError, err)
}

hardCodedAudioFile := "example1.m4a"
sharedFilePath := constant.SharedAudioPath + "/" + hardCodedAudioFile
bucketPath := request.GetAudioFileUrl()
splitPath := strings.Split(bucketPath, "/")
fileName := splitPath[len(splitPath)-1]
sharedFilePath := constant.SharedAudioPath + "/" + fileName
if !existsFile(sharedFilePath) {
bucketPath := "audio/" + hardCodedAudioFile
bytes, err := handler.storageService.GetFile(bucketPath)
if err != nil {
return model.NewCustomHTTPError(http.StatusInternalServerError, err)
Expand All @@ -60,29 +73,31 @@ func (handler *FeedbackHandlerImpl) GetPronunciationFeedback(c echo.Context) err
defer cancel()

grpcRequest := pb.PronunciationFeedbackRequest{
Sentence: "It's autumn now, and the leaves are turning beautiful colors.",
AudioPath: hardCodedAudioFile,
Tip: "Say 'aw-tum,' not 'ay-tum.'",
Sentence: sentence.Text,
AudioPath: fileName,
Tip: sentence.Tip,
}

response, err := client.PronunciationFeedback(ctx, &grpcRequest)
feedbackResponse, err := client.PronunciationFeedback(ctx, &grpcRequest)
if err != nil {
log.Printf("grpc request failed: %v", err)
return model.NewCustomHTTPError(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}

pronunciationScore := int32(math.Round(response.GetPronunciationScore() * 100))
decibel := int32(math.Round(response.GetDecibel()))
speechRate := int32(math.Round(response.GetSpeechRate()))
pronunciationScore := int32(math.Round(feedbackResponse.GetPronunciationScore()))
decibel := int32(math.Round(feedbackResponse.GetDecibel()))
speechRate := int32(math.Round(feedbackResponse.GetSpeechRate()))
transcript := firstToUpper(feedbackResponse.GetTranscript())

result := pb.GetPronunciationFeedbackResponse{
PronunciationFeedback: &pb.PronunciationFeedbackDTO{
SentenceId: request.GetSentenceId(),
IncorrectIndexes: response.GetIncorrectIndexes(),
Transcript: transcript,
IncorrectIndexes: feedbackResponse.GetIncorrectIndexes(),
PronunciationScore: pronunciationScore,
VolumeScore: decibel,
SpeedScore: speechRate,
OverallFeedback: response.GetPositiveFeedback(),
OverallFeedback: feedbackResponse.GetPositiveFeedback(),
},
}

Expand All @@ -94,11 +109,19 @@ func (handler *FeedbackHandlerImpl) GetCommunicationFeedback(c echo.Context) err
if err := c.Bind(&request); err != nil {
return model.NewCustomHTTPError(http.StatusBadRequest, err)
}
if request.GetSceneId() == "" {
return model.NewCustomHTTPError(http.StatusBadRequest, "scene_id is required")
}
scene, err := handler.sceneService.GetScene(request.GetSceneId())
if err != nil {
return model.NewCustomHTTPError(http.StatusInternalServerError, err)
}

hardCodedAudioFile := "example1.m4a"
sharedFilePath := constant.SharedAudioPath + "/" + hardCodedAudioFile
bucketPath := request.GetAudioFileUrl()
splitPath := strings.Split(bucketPath, "/")
fileName := splitPath[len(splitPath)-1]
sharedFilePath := constant.SharedAudioPath + "/" + fileName
if !existsFile(sharedFilePath) {
bucketPath := "audio/" + hardCodedAudioFile
bytes, err := handler.storageService.GetFile(bucketPath)
if err != nil {
return model.NewCustomHTTPError(http.StatusInternalServerError, err)
Expand All @@ -125,11 +148,11 @@ func (handler *FeedbackHandlerImpl) GetCommunicationFeedback(c echo.Context) err
defer cancel()

grpcRequest := pb.CommunicationFeedbackRequest{
Context: "Let's imagine that you are a brave captain of a big ship. You are sailing on the high seas. Suddenly, you see a beautiful sunset. Look at this picture and tell me...",
Question: "What colors can you see in the sky?",
ExpectedAnswer: "I can see red, orange, and yellow.",
AudioPath: "example1.m4a",
ImgPath: "img/1070.jpg",
Context: scene.Context,
Question: scene.Question,
ExpectedAnswer: scene.ExpectedAnswer,
AudioPath: fileName,
ImgPath: scene.ImageUrl,
}

response, err := client.CommunicationFeedback(ctx, &grpcRequest)
Expand Down Expand Up @@ -178,8 +201,19 @@ func writeFile(fileName string, bytes []byte) error {
return nil
}

func FeedbackHandlerInit(storageService service.StorageService) *FeedbackHandlerImpl {
func firstToUpper(s string) string {
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError && size <= 1 {
return s
}
lowerCase := strings.ToLower(s)
return string(unicode.ToUpper(r)) + lowerCase[size:]
}

func FeedbackHandlerInit(sentenceService service.SentenceService, sceneService service.SceneService, storageService service.StorageService) *FeedbackHandlerImpl {
return &FeedbackHandlerImpl{
storageService: storageService,
sentenceService: sentenceService,
sceneService: sceneService,
storageService: storageService,
}
}
8 changes: 7 additions & 1 deletion src/handler/sentence.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type SentenceHandler interface {

type SentenceHandlerImpl struct {
sentenceService service.SentenceService
storageService service.StorageService
}

func (handler *SentenceHandlerImpl) GetSentence(c echo.Context) error {
Expand All @@ -31,7 +32,12 @@ func (handler *SentenceHandlerImpl) GetSentence(c echo.Context) error {
return model.NewCustomHTTPError(http.StatusInternalServerError, err)
}

response := &pb.GetSentenceResponse{Sentence: converter.ToSentenceDTO(sentence)}
exampleVideoUrl, err := handler.storageService.GetFileUrl(sentence.VideoPath)
if err != nil {
return model.NewCustomHTTPError(http.StatusInternalServerError, err)
}

response := &pb.GetSentenceResponse{Sentence: converter.ToSentenceDTO(sentence, exampleVideoUrl)}
return c.JSON(http.StatusOK, response)
}

Expand Down
10 changes: 6 additions & 4 deletions src/model/scene.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package model

type Scene struct {
Id string `firestore:"-"`
Question string `firestore:"question"`
ImageUrl string `firestore:"image_url"`
TopicId string `firestore:"topic_id"`
Id string `firestore:"-"`
Question string `firestore:"question"`
ImageUrl string `firestore:"image_url"`
Context string `firestore:"context"`
ExpectedAnswer string `firestore:"expected_answer"`
TopicId string `firestore:"topic_id"`
}
8 changes: 5 additions & 3 deletions src/model/sentence.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package model

type Sentence struct {
Id string `firestore:"-"`
Text string `firestore:"text"`
TopicId string `firestore:"topic_id"`
Id string `firestore:"-"`
Text string `firestore:"text"`
Tip string `firestore:"tip"`
TopicId string `firestore:"topic_id"`
VideoPath string `firestore:"video_path"`
}
Loading