-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
102 lines (90 loc) · 2.45 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package snowboy
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
const (
EndpointBase = "https://snowboy.kitt.ai/api/"
EndpointVersion = "v1"
EndpointTrain = EndpointBase + EndpointVersion + "/train"
EndpointImprove = EndpointBase + EndpointVersion + "/improve"
)
type AgeGroup string
const (
AgeGroup0s AgeGroup = "0_9"
AgeGroup10s = "10_19"
AgeGroup20s = "20_29"
AgeGroup30s = "30_39"
AgeGroup40s = "40_49"
AgeGroup50s = "50_59"
AgeGroup60plus = "60+"
)
type Gender string
const (
GenderMale Gender = "M"
GenderFemale = "F"
)
type Language string
const (
LanguageArabic Language = "ar"
LanguageChinese = "zh"
LanguageDutch = "nl"
LanguageEnglish = "en"
LanguageFrench = "fr"
LanguageGerman = "dt"
LanguageHindi = "hi"
LanguageItalian = "it"
LanguageJapanese = "jp"
LanguageKorean = "ko"
LanguagePersian = "fa"
LanguagePolish = "pl"
LanguagePortuguese = "pt"
LanguageRussian = "ru"
LanguageSpanish = "es"
LanguageOther = "ot"
)
type TrainRequest struct {
VoiceSamples []VoiceSample `json:"voice_samples"`
Token string `json:"token"`
Name string `json:"name"`
Language Language `json:"language"`
AgeGroup AgeGroup `json:"age_group"`
Gender Gender `json:"gender"`
Microphone string `json:"microphone"`
}
type VoiceSample struct {
Wave string `json:"wave"`
}
func (t *TrainRequest) AddWave(filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
enc := base64.StdEncoding.EncodeToString(data)
t.VoiceSamples = append(t.VoiceSamples, VoiceSample{
Wave: enc,
})
}
func (t *TrainRequest) Train() ([]byte, error) {
data, err := json.Marshal(t)
if err != nil {
return []byte{}, err
}
fmt.Println("sending", string(data), "to ", EndpointTrain)
resp, err := http.DefaultClient.Post(EndpointTrain, "application/json", bytes.NewBuffer(data))
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
d, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp.StatusCode, resp.Status, string(d))
if resp.StatusCode != 200 {
return []byte{}, errors.New("non-200 returned from kitt.ai")
}
return ioutil.ReadAll(resp.Body)
}