-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
113 lines (92 loc) · 2.53 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
package main
/*
This program runs an unencrypted http server on a local port.
The server uses http.ServeMux to make it easy to add more endpoints in the future.
Each endpoint should require POST requests with a JSON body. The request
includes many rows of data, and the response should include result of
running some computation on each row of data..
Every endpoint will receive a JSON object structured like this:
{
"data": [
[ ROWNUM, VALUE, VALUE, ... ],
[ ROWNUM, VALUE, VALUE, ... ],
...
]
}
Every endpoint will respond with a JSON object structured like this:
{
"data": [
[ ROWNUM, RESPONSE ],
...
]
}
If an endpoint wants to return a JSON object as the RESPONSE, it must
encode it as a string. This is because SingleStore external functions
doesn't yet support nested JSON. This limitation will be resolved in a
future release.
*/
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/sugarme/tokenizer/pretrained"
)
type ReqResp struct {
Data [][]interface{} `json:"data"`
}
var (
BertModel = pretrained.BertBaseUncased()
)
func TokenizeText(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST requests are allowed", http.StatusMethodNotAllowed)
return
}
var req ReqResp
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
out := make([][]interface{}, len(req.Data))
for i, row := range req.Data {
valueString, ok := row[1].(string)
if !ok {
http.Error(w, "value must be a string", http.StatusBadRequest)
return
}
encoded, err := BertModel.EncodeSingle(valueString)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// SingleStore external functions doesn't yet support nested JSON
// objects in a response so we need to encode each value as a string.
marshalled, err := json.Marshal(map[string]interface{}{
"tokens": encoded.Tokens,
"offsets": encoded.Offsets,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out[i] = []interface{}{row[0], string(marshalled)}
}
resp := ReqResp{Data: out}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
mux := http.NewServeMux()
mux.HandleFunc("/text/tokenize", TokenizeText)
err := http.ListenAndServe(":"+port, mux)
if err != nil {
log.Fatal(err)
}
}