-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscribe.js
174 lines (114 loc) · 4.82 KB
/
transcribe.js
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
import path from 'path'
import * as fs from 'node:fs'
import { exec } from 'child_process'
import * as openai from '../services/openai'
//import * as openai from '../services/openai'
import Router from '../lib/router'
import { CORS_HEADERS } from '../lib/cors'
//import { trim_array, stream_loop } from '../lib/utils'
//import get_weather from '../tools/get_weather.json'
const route = new Router('/transcribe')
route.post('/', async (req) => {
try {
const formdata = await req.formData()
const name = formdata.get('name')
const file = formdata.get('file')
if (!file) throw new Error('Audio data not found')
let filename = `tmp-${Date.now()}-${name}`
let filepath = path.join('public', 'uploads', filename)
await Bun.write(filepath, file)
// remove silent parts
let outpath = `${path.join('public', 'uploads', `out-${filename}`)}`
const retval = await new Promise((resolve, reject) => {
const sCommand = `ffmpeg -i ${filepath} -af silenceremove=stop_periods=-1:stop_duration=1:stop_threshold=-50dB ${outpath}`
exec(sCommand, (error, stdout, stderr) => {
if (error) {
resolve({
status: 'error',
})
} else {
resolve({
status: 'success',
error: stderr,
out: stdout,
})
}
})
})
// if successful, use the output file
if(retval.status === 'success') {
filepath = outpath
}
// check file size
const minFileSize = 18000 // bytes
const check_file = Bun.file(filepath)
console.log('file size', check_file.size) // number of bytes
if(check_file.size < minFileSize) {
console.log('Audio data has probably no detectable speech data')
return new Response(JSON.stringify({
text: 'Good morning...', // test
}),
{ status: 200, headers: CORS_HEADERS }
)
}
const isAPIMode = false
const language = 'en' // en, ja
const temperature = 0
let text_data = ''
if(isAPIMode) {
//filepath = path.join('public', 'uploads', 'test-file170753061013795735.webm')
const result = await openai.transcribe({
file: fs.createReadStream(filepath),
language: language, //en
temperature: temperature //0.2,
})
console.log('whisper api', result, (new Date()).toLocaleTimeString())
text_data = result.text
} else {
///// use local whisper using whisper 2 api
const outdir = path.join('public', 'uploads')
let whisper_command = `whisper './${filepath}' --task --language ${language} --temperature ${temperature} --model tiny --output_dir '${outdir}'`
const whisper_retval = await new Promise((resolve, reject) => {
exec(whisper_command, (error, stdout, stderr) => {
if (error) {
resolve({
status: 'error',
error: 'Failed to transcribe'
})
} else {
resolve({
status: 'ok',
error: stderr,
out: stdout
})
}
})
})
console.log('output...', whisper_retval, (new Date()).toLocaleTimeString())
if(whisper_retval.status === "error" || whisper_retval.out.length === 0) {
console.log('No transcription')
return new Response(JSON.stringify({
text: '',
}), {
status: 200, headers: CORS_HEADERS
})
}
// read the output text file instead of the out property which contains timeline
try {
text_data = fs.readFileSync(`${filepath}.txt`, 'utf8')
} catch(error) {
console.log(error.message)
}
}
return new Response(
JSON.stringify({
text: 'Hello there' //text_data
}),
{ status: 200, headers: CORS_HEADERS }
)
} catch(error) {
console.log(error.message)
throw new Error(error.message)
}
})
export default route.handler