50 lines
2.0 KiB
C++
50 lines
2.0 KiB
C++
#include "vendors/azure/audio.h"
|
|
|
|
#include "vendors/azure/caps.h"
|
|
#include "vendors/azure/check.h"
|
|
|
|
namespace molo::azure {
|
|
|
|
using clients::body::Json;
|
|
|
|
llm::RawResponse AudioService::speech(const std::string& deployment, const Json& body) {
|
|
Check::deployment(deployment);
|
|
Check::object("body", body);
|
|
Check::has_string(body, "input");
|
|
Check::has_string(body, "voice");
|
|
Check::max_chars("input", body.find("input")->as_string(), AzureCaps::kMaxSpeechChars);
|
|
return call_json_raw(Op::kCreateSpeech, {deployment, {}}, body);
|
|
}
|
|
|
|
llm::RawResponse AudioService::upload(Op op, const std::string& deployment, const UploadFile& file,
|
|
const std::map<std::string, std::string>& fields) {
|
|
Check::deployment(deployment);
|
|
Check::non_empty("audio filename", file.filename);
|
|
Check::no_control("filename", file.filename);
|
|
Check::require(!file.data.empty(), "audio file is empty");
|
|
Check::require(file.data.size() <= AzureCaps::kMaxAudioFileBytes, "audio file over 25 MiB");
|
|
clients::body::MultipartBody form;
|
|
form.add_file("file", file.filename, file.content_type, file.data);
|
|
for (const auto& [k, v] : fields) form.add_text(k, v);
|
|
return call_multipart_raw(op, {deployment, {}}, form);
|
|
}
|
|
|
|
llm::RawResponse AudioService::transcribe(const std::string& deployment, const UploadFile& file,
|
|
const std::map<std::string, std::string>& fields) {
|
|
return upload(Op::kCreateTranscription, deployment, file, fields);
|
|
}
|
|
|
|
llm::RawResponse AudioService::translate(const std::string& deployment, const UploadFile& file,
|
|
const std::map<std::string, std::string>& fields) {
|
|
return upload(Op::kCreateTranslation, deployment, file, fields);
|
|
}
|
|
|
|
Json AudioService::as_json(const llm::RawResponse& answer) {
|
|
if (answer.content_type.find("json") != std::string::npos) return Json::parse(answer.body);
|
|
Json o = Json::object();
|
|
o.set("text", Json(answer.body));
|
|
return o;
|
|
}
|
|
|
|
} // namespace molo::azure
|