-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscription-handler.go
116 lines (96 loc) · 2.52 KB
/
transcription-handler.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
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/samber/lo"
)
type Job struct {
ID string `json:"id"`
Path string `json:"path"`
Language string `json:"language"`
OutputFormat string `json:"format"`
OutputPath string `json:"output_path"`
Progress int `json:"progress"`
Status string `json:"status"`
Result string `json:"result"`
Callback string `json:"callback"`
Model string `json:"model"`
Duration string `json:"duration"`
Priority int `json:"priority"`
}
type handlers struct {
queuedJobs []*Job
processedJobs []*Job
queueChan chan bool
}
func NewHandlers() *handlers {
return &handlers{
queuedJobs: []*Job{},
processedJobs: []*Job{},
queueChan: make(chan bool, 2),
}
}
const (
JobStatusQueued = "QUEUED"
JobStatusRunning = "RUNNING"
JobStatusCompleted = "COMPLETED"
JobStatusFailed = "FAILED"
)
func (h *handlers) SubmitJob(c *gin.Context) {
job := &Job{}
err := c.BindJSON(job)
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
job.ID = uuid.New().String()
job.Status = JobStatusQueued
if job.Priority <= 0 {
// This is the default value. We want the default to be 1000 now so the jobs get appended to the front of the queue.
job.Priority = 1000
}
if job.OutputFormat == "" {
job.OutputPath = "txt"
}
if job.Model == "" {
job.Model = "openai/whisper-large-v3"
}
if job.Priority >= 500 {
// High prio. Put in the front
h.queuedJobs = append([]*Job{job}, h.queuedJobs...)
} else {
// Low prio, put in the back
h.queuedJobs = append(h.queuedJobs, job)
}
// Attempt to send. If buffer is full, ignore
select {
case h.queueChan <- true:
case <-time.After(50 * time.Millisecond):
}
c.JSON(http.StatusAccepted, job)
}
func (h *handlers) ListJobs(c *gin.Context) {
c.JSON(http.StatusOK, append(h.queuedJobs, h.processedJobs...))
}
func (h *handlers) DeleteJob(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.AbortWithStatus(http.StatusNotFound)
return
}
if _, index, ok := lo.FindIndexOf(h.queuedJobs, func(i *Job) bool { return i.ID == id }); ok {
h.queuedJobs = remove(h.queuedJobs, index)
}
c.Status(http.StatusNoContent)
}
func (h *handlers) GetJob(c *gin.Context) {
id := c.Param("id")
jobs := append(h.queuedJobs, h.processedJobs...)
if _, index, ok := lo.FindIndexOf(jobs, func(i *Job) bool { return i.ID == id }); ok {
c.JSON(http.StatusOK, jobs[index])
return
}
c.Status(http.StatusNotFound)
}