-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
html.go
121 lines (107 loc) · 2.79 KB
/
html.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
114
115
116
117
118
119
120
121
// Copyright 2020 spaGO Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ner
import (
"bytes"
"github.com/nlpodyssey/spago/pkg/webui"
"html/template"
"log"
"net/http"
)
const htmlTemplate = `
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<title>spaGO</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
{{.CommonStyle}}
{{.CustomStyle}}
</style>
</head>
<body class="flex flex-col">
<header class="p-2 shadow z-10">
<a
class="text-xl font-bold italic"
href="https://github.com/nlpodyssey/spago"
target="_blank"
>
spa<em class="text-blue">GO</em>
</a>
</header>
<main class="flex-grow overflow-hidden flex">
<form
class="bg-gray-200 flex-grow p-4 flex flex-col"
onsubmit="analyze(); return false;"
>
<div class="flex-grow max-h-96 bg-white rounded shadow flex overflow-hidden relative">
<div
id="highlightable-text"
class="absolute inset-0 text-transparent p-2 overflow-auto"
></div>
<textarea
id="text"
placeholder="Text..."
class="flex-grow resize-none bg-transparent p-2 z-10 overflow-auto"
oninput="handleTextareaInput()"
onscroll="handleTextareaScroll()"
></textarea>
</div>
<div class="mt-4 flex">
<div class="flex-grow py-2">
<input id="merge-entities" type="checkbox" checked>
<label for="merge-entities" class="mr-2">Merge entities</label>
<input id="filter-not-entities" type="checkbox" checked>
<label for="filter-not-entities">Filter non-entities</label>
</div>
<input
type="submit"
id="submit"
value="Analyze"
class="rounded shadow cursor-pointer py-2 px-4 bg-blue hover:bg-light-blue"
>
<div id="loader" class="hidden"></div>
</div>
</form>
<aside id="entities" class="bg-gray-300 shadow p-4 overflow-auto flex flex-col">
</aside>
</main>
<script>
{{.Script}}
</script>
</body>
</html>
`
var html []byte
func init() {
t, err := template.New("NER Web UI").Parse(htmlTemplate)
if err != nil {
log.Fatal(err)
}
data := struct {
CommonStyle template.CSS
CustomStyle template.CSS
Script template.JS
}{
CommonStyle: webui.CommonStyle,
CustomStyle: style,
Script: script,
}
buf := bytes.NewBuffer([]byte{})
err = t.Execute(buf, data)
if err != nil {
log.Fatal(err)
}
html = buf.Bytes()
}
// Handler is the server handler function for Named-Entity Recognition web UI
func Handler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") // that's intended for testing purposes only
w.Header().Set("Content-Type", "text/html")
_, err := w.Write(html)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}