view src/tts.mp3.luan @ 53:6c78fd83518f

add delete course
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 Aug 2025 16:59:37 +0900
parents cc20eebaa74a
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local Parsers = require "luan:Parsers.luan"
local json_string = Parsers.json_string or error()
local Io = require "luan:Io.luan"
local uri = Io.uri or error()
local Http = require "luan:http/Http.luan"
local Config = require "site:/private/Config.luan"
local key = Config.chatgpt.key or error()
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "tts.js"


-- https://platform.openai.com/docs/guides/text-to-speech

local url = "https://api.openai.com/v1/audio/speech"
local headers = {
	Authorization = "Bearer "..key
	["Content-Type"] = "application/json"
}

local function text_to_speech(voice,instructions,text)
	local options = {
		method = "POST"
		headers = headers
		content = json_string{
			model = "gpt-4o-mini-tts"
			voice = voice
			input = text
			instructions = instructions
		}
	}
	return uri(url,options)
end

return function()
	local voice = Http.request.parameters.voice or error()
	local instructions = Http.request.parameters.instructions or error()
	local text = Http.request.parameters.text or error()
	local input = text_to_speech(voice,instructions,text)
	Http.response.binary_writer().write_from(input)
end