14
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local Parsers = require "luan:Parsers.luan"
|
|
4 local json_string = Parsers.json_string or error()
|
|
5 local Io = require "luan:Io.luan"
|
|
6 local uri = Io.uri or error()
|
|
7 local Http = require "luan:http/Http.luan"
|
|
8 local Config = require "site:/private/Config.luan"
|
|
9 local key = Config.chatgpt.key or error()
|
|
10 local Logging = require "luan:logging/Logging.luan"
|
46
|
11 local logger = Logging.logger "tts.js"
|
14
|
12
|
|
13
|
46
|
14 -- https://platform.openai.com/docs/guides/text-to-speech
|
26
|
15
|
46
|
16 local url = "https://api.openai.com/v1/audio/speech"
|
14
|
17 local headers = {
|
|
18 Authorization = "Bearer "..key
|
46
|
19 ["Content-Type"] = "application/json"
|
14
|
20 }
|
|
21
|
46
|
22 local function text_to_speech(voice,instructions,text)
|
14
|
23 local options = {
|
|
24 method = "POST"
|
|
25 headers = headers
|
46
|
26 content = json_string{
|
|
27 model = "gpt-4o-mini-tts"
|
|
28 voice = voice
|
|
29 input = text
|
|
30 instructions = instructions
|
14
|
31 }
|
|
32 }
|
46
|
33 return uri(url,options)
|
14
|
34 end
|
|
35
|
|
36 return function()
|
46
|
37 local voice = Http.request.parameters.voice or error()
|
|
38 local instructions = Http.request.parameters.instructions or error()
|
|
39 local text = Http.request.parameters.text or error()
|
|
40 local input = text_to_speech(voice,instructions,text)
|
|
41 Http.response.binary_writer().write_from(input)
|
14
|
42 end
|