This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
/
interact.go
213 lines (196 loc) · 5.42 KB
/
interact.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// Copyright (c) 2019-present Codist <countstarlight@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Written by Codist <countstarlight@gmail.com>, March 2019
//
package main
import (
"fmt"
"github.com/countstarlight/homo/cmd/interact/config"
"github.com/countstarlight/homo/module/baidu"
"github.com/countstarlight/homo/module/nlu"
"github.com/marcusolsson/tui-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"sync"
"time"
)
var flags = []cli.Flag{
cli.BoolFlag{
EnvVar: "HOMO_INTERACT_DEBUG",
Name: "debug, d",
Usage: "start homo in debug mode",
},
cli.BoolFlag{
EnvVar: "HOMO_INTENT_ONLY",
Name: "intent, i",
Usage: "start homo in intent only mode",
},
}
func interact(ctx *cli.Context) error {
// Set logrus format
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "15:04:05"
// Show colorful on windows
customFormatter.ForceColors = true
logrus.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
logrus.Infof("Homo Interact v0.0.1")
if ctx.Bool("debug") {
logrus.Infof("Running in debug mode")
config.DebugMode = true
} else if ctx.Bool("intent") {
logrus.Infof("Running homo in intent only mode")
config.IntentOnlyMode = true
}
config.NewContext()
//
//tui begin
//
sidebar := tui.NewVBox(
tui.NewLabel("Homo Interact v0.0.1"),
tui.NewLabel("Homo 交互模式"),
tui.NewSpacer(),
tui.NewLabel("语音合成[开启]"),
tui.NewLabel("语音合成API[百度]"),
tui.NewLabel("语音自动播放[开启]"),
)
if ctx.Bool("debug") {
sidebar.Append(
tui.NewLabel("DEBUG MODE"),
)
} else if ctx.Bool("intent") {
sidebar.Append(
tui.NewLabel("INTENT-ONLY MODE"),
)
}
sidebar.Append(
tui.NewLabel("按Esc退出"),
)
sidebar.SetBorder(true)
history := tui.NewVBox()
historyScroll := tui.NewScrollArea(history)
//cannot scroll after
historyScroll.SetAutoscrollToBottom(true)
historyBox := tui.NewVBox(historyScroll)
historyBox.SetTitle("消息")
historyBox.SetBorder(true)
input := tui.NewEntry()
input.SetFocused(true)
input.SetSizePolicy(tui.Expanding, tui.Maximum)
inputBox := tui.NewHBox(input)
inputBox.SetTitle("输入 按Enter提交")
inputBox.SetBorder(true)
inputBox.SetSizePolicy(tui.Expanding, tui.Maximum)
chat := tui.NewVBox(historyBox, inputBox)
chat.SetSizePolicy(tui.Expanding, tui.Expanding)
input.OnSubmit(func(e *tui.Entry) {
if len(e.Text()) > 0 {
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "你"))),
tui.NewLabel(e.Text()),
tui.NewSpacer(),
))
//Send text to homo core
var (
reply string
err error
)
playMutex := &sync.Mutex{}
if config.IntentOnlyMode {
replyMessage, err := nlu.ActionLocal(e.Text())
if err != nil {
reply = "错误: " + err.Error()
} else {
//display rank
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "homo"))),
tui.NewLabel(replyMessage[0]),
tui.NewSpacer(),
))
//Play voice
go func() {
playMutex.Lock()
er := baidu.TextToSpeech(replyMessage[0])
playMutex.Unlock()
if er != nil {
rep := "语音合成出错: " + er.Error()
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "homo"))),
tui.NewLabel(rep),
tui.NewSpacer(),
))
}
}()
//display entities
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "homo"))),
tui.NewLabel(replyMessage[1]),
tui.NewSpacer(),
))
//Play voice
go func() {
playMutex.Lock()
er := baidu.TextToSpeech(replyMessage[1])
playMutex.Unlock()
if er != nil {
rep := "语音合成出错: " + er.Error()
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "homo"))),
tui.NewLabel(rep),
tui.NewSpacer(),
))
}
}()
reply = replyMessage[2]
}
} else {
reply, err = nlu.ChatWithCore(e.Text())
if err != nil {
reply = "连接到Homo Core出错: " + err.Error()
}
}
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "homo"))),
tui.NewLabel(reply),
tui.NewSpacer(),
))
//Play voice
go func() {
playMutex.Lock()
er := baidu.TextToSpeech(reply)
playMutex.Unlock()
if er != nil {
rep := "语音合成出错: " + er.Error()
history.Append(tui.NewHBox(
tui.NewLabel(time.Now().Format("15:04")),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("%s:", "homo"))),
tui.NewLabel(rep),
tui.NewSpacer(),
))
}
}()
input.SetText("")
}
})
root := tui.NewHBox(sidebar, chat)
ui, err := tui.New(root)
if err != nil {
logrus.Fatal(err)
}
ui.SetKeybinding("Esc", func() { ui.Quit() })
//ui.SetKeybinding("Up", func() { historyScroll.Scroll(0, -1) })
//ui.SetKeybinding("Down", func() { historyScroll.Scroll(0, 1) })
if err := ui.Run(); err != nil {
logrus.Fatal(err)
}
return nil
}
func before(c *cli.Context) error { return nil }