in experiments/babel/main.go [164:223]
func handleSynthesis(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "unable to process body", http.StatusInternalServerError)
return
}
if len(body) == 0 {
http.Error(w, "no content provided", http.StatusBadRequest)
return
}
log.Printf("%s", body)
var babelRequest BabelRequest
err = json.NewDecoder(bytes.NewReader(body)).Decode(&babelRequest)
if err != nil {
http.Error(w, "error decoding Fabulae Request", http.StatusInternalServerError)
return
}
log.Print("synthesizing... ")
// core babel functionality
// languages
languages := getAllLanguages()
// translations
translations := translate(babelRequest.Statement, languages)
// generate speech
outputmetadata := generateSpeech(voices, translations)
// service additional functionality
// move to storage bucket
outputfiles := []string{}
for _, translation := range outputmetadata {
outputfiles = append(outputfiles, translation.AudioPath)
}
err = moveFilesToAudioBucket(outputfiles)
if err != nil {
http.Error(w, "error writing to Storage", http.StatusInternalServerError)
return
}
log.Printf("%d files written to gs://%s/%s", len(outputfiles), babelbucket, babelpath)
revisedOutput := []BabelOutput{}
for _, o := range outputmetadata {
if o.Length > 0 {
revisedOutput = append(revisedOutput, o)
}
}
response := BabelResponse{}
response.AudioMetadata = revisedOutput
w.Header().Set("Content-Type", "application/json")
//fmt.Fprintf(w, "%s", body)
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Print(err)
}
}