-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
107 lines (89 loc) · 2.5 KB
/
http.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
package main
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"golang.org/x/exp/slog"
)
// MureaderMurecomHandler handles murecom request from mureader:
//
// Request:
//
// POST /murecom
//
// {
// "prevPages": ["昨天很伤心", "昨天很难过],
// "currentPages": ["今天很开心"],
// "nextPages": ["明天很迷茫", "明天很无助", "明天很绝望"]
// }
//
// Response:
//
// {
// "music": {
// "Title": "昨天很难过",
// "Artist": "张三",
// "CoverImage": "http://example.com/cover.jpg",
// "SourceUrl": "http://example.com/source.mp3"
// }
func MureaderMurecomHandler(c *gin.Context) {
var req MureaderMurecomRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
resp, err := murecom4reader(&req)
if err != nil {
c.JSON(422, gin.H{"error": err.Error()})
return
}
c.JSON(200, resp)
}
func murecom4reader(req *MureaderMurecomRequest) (*MureaderMurecomResponse, error) {
logger := slog.With("func", "murecom4reader")
if req == nil {
return nil, errors.New("request is nil")
}
logger = logger.With("req", req.LogValue())
emotions := []Emotion{}
for _, pages := range [][]string{req.PrevPages, req.CurrentPages, req.NextPages} {
for _, page := range pages {
emotion, err := emotext(page)
if err != nil {
logger.Error("murecom4reader: emotext error. Try to continue.", "err", err)
continue
}
emotions = append(emotions, emotion)
}
}
avgEmotion := Emotion{}
if len(emotions) == 0 {
emotions = append(emotions, Emotion{Valence: 0.5, Arousal: 0.5})
} // assert: len(emotions) > 0
for _, emotion := range emotions {
avgEmotion.Valence += emotion.Valence
avgEmotion.Arousal += emotion.Arousal
}
avgEmotion.Valence /= float64(len(emotions))
avgEmotion.Arousal /= float64(len(emotions))
tracks, err := murecom(avgEmotion)
if err != nil {
logger.Warn("murecom4reader: murecom error.", "err", err)
return nil, fmt.Errorf("murecom4reader: murecom error: %w", err)
}
if len(tracks) == 0 {
logger.Warn("murecom4reader: murecom returns no track.")
return nil, fmt.Errorf("murecom4reader: murecom returns no track")
} // assert: len(tracks) > 0
var resp MureaderMurecomResponse
resp.Music = &Music{
Title: tracks[0].Name,
Artist: tracks[0].Artist,
CoverImage: tracks[0].CoverImageURL,
SourceUrl: tracks[0].AudioFileURL,
TrackEmotion: tracks[0].Emotion,
}
resp.TextEmotion = avgEmotion
logger.Info("murecom4reader: success.", "resp", resp)
return &resp, nil
}