Skip to main content

實做infrastructure Text to Speech & make a sentence

在這篇,先實做Text to Speech跟OpenAI的部分

Text to Speech

因為前面有介紹過大致怎麼使用go來跟gcp互動了,所以這邊主要是介紹我想要怎麼使用他

因為這次的需求是 將文字轉成語音,並將他放到anki中 又因為anki他需要將檔案移動到某個地方,所以我的想法是需要一個 輸入是要轉換的文字 輸出的檔案先存放到一個temp的資料夾,然後檔名就跟轉換的文字一樣,後面加上.mp3 換成程式的話就是

func (t *TextToSpeechSuite) Test_generate_japanese_file() {  
outputPath := "/Users/kevin/Developer/side-project/anki-support/temp"
expectOutput := filepath.Join(outputPath, "私の机は木製です。.mp3")
os.Remove(expectOutput)
err := t.client.GenerateAudioByText("私の机は木製です。", outputPath, "私の机は木製です。")
t.NoError(err)
t.FileExists(expectOutput)
}

然後就是將內容實做完成就附在下方連結 https://github.com/kevinyay945/anki-support/blob/9cc90f8ddea7e811058dc9764e4ed99368d515a4/infrastructure/gcp/text_to_speech.go#L13

用Open ai 造句

在openai 這邊,我這次的需求是 我會提供以前背過的單字,然後請用我新給的單字幫我造句,如果前面提供的單字可以放入句子的話請盡量放入

因此這次的Input跟output分別為 input

  1. 之前背過的單字
  2. 這次要造句的單字 output
  3. 造好的句子
  4. error

整體的使用情境會像是

func (t *MakeSentenceSuite) Test_make_japanese_sentence() {  
rememberVocabularyList := []string{
"両親", "月餅", "電池", "彼の方", "お兄さん", "高速バス", "お姉さん", "映画", "前", "乗り場", "冷蔵庫", "学校", "明後日", "チケット", "番線",
}
sentence, hiraganaSentence, chineseSentence, err := t.client.makeJapaneseSentence(rememberVocabularyList, "山")
t.NoError(err)

t.Equal("japanese origin sentence", sentence)
t.Equal("japanese hiragana sentence", hiraganaSentence)
t.Equal("traditional Chinese sentence", chineseSentence)

}

而實做的部分則是 https://github.com/kevinyay945/anki-support/blob/9cc90f8ddea7e811058dc9764e4ed99368d515a4/infrastructure/openai/make_sentence.go#L10

那這兩邊的服務就差不多告一個段落了 接下來就是Anki的部分