#include "vendors/audio/elevenlabs/stt.h" #include "vendors/audio/caps.h" namespace molo::audio::elevenlabs { using C = ElevenCalls; clients::body::MultipartBody SpeechToText::build(const SttRequest& r) const { require_text("model_id", r.model_id, 128); int sources = (!r.audio.empty()) + (!r.source_url.empty()) + (!r.cloud_storage_url.empty()); if (sources != 1) bad("give exactly one of audio, source_url and cloud_storage_url"); if (r.audio.size() > ElevenCaps::kMaxFileBytes) bad("audio is over the byte cap"); require_text("source_url", r.source_url, AudioCaps::kMaxFieldChars, false); require_text("cloud_storage_url", r.cloud_storage_url, AudioCaps::kMaxFieldChars, false); require_text("language_code", r.language_code, 16, false); if (r.num_speakers < 0 || r.num_speakers > 32) bad("num_speakers must be 0 or 1..32"); const std::string& g = r.timestamps_granularity; if (!g.empty() && g != "none" && g != "word" && g != "character") bad("timestamps_granularity must be none, word or character"); if (!r.file_format.empty() && r.file_format != "pcm_s16le_16" && r.file_format != "other") bad("file_format must be pcm_s16le_16 or other"); if (r.keyterms.size() > AudioCaps::kMaxKeywords) bad("too many keyterms"); if (r.temperature > 2.0) bad("temperature must be 0..2"); clients::body::MultipartBody form; form.add_text("model_id", r.model_id); if (!r.audio.empty()) form.add_file("file", r.filename, r.content_type, r.audio); if (!r.source_url.empty()) form.add_text("source_url", r.source_url); if (!r.cloud_storage_url.empty()) form.add_text("cloud_storage_url", r.cloud_storage_url); if (!r.language_code.empty()) form.add_text("language_code", r.language_code); if (r.diarize) form.add_text("diarize", "true"); if (r.num_speakers > 0) form.add_text("num_speakers", std::to_string(r.num_speakers)); if (!g.empty()) form.add_text("timestamps_granularity", g); if (r.tag_audio_events >= 0) form.add_text("tag_audio_events", r.tag_audio_events ? "true" : "false"); for (const auto& k : r.keyterms) { require_text("keyterm", k, 100); form.add_text("keyterms", k); } if (!r.file_format.empty()) form.add_text("file_format", r.file_format); if (r.use_multi_channel >= 0) form.add_text("use_multi_channel", r.use_multi_channel ? "true" : "false"); if (r.seed >= 0) form.add_text("seed", std::to_string(r.seed)); if (r.temperature >= 0) form.add_text("temperature", std::to_string(r.temperature)); return form; } Query SpeechToText::query(const SttRequest& r) const { Query q; if (r.enable_logging >= 0) q.add_bool("enable_logging", r.enable_logging == 1); return q; } Json SpeechToText::transcribe(const SttRequest& r) { return call_form(C::kStt, {}, build(r), query(r)); } Json SpeechToText::get(const std::string& id) { check_id("transcription", id); return call(C::kSttGet, {id}); } Json SpeechToText::remove(const std::string& id, bool confirm) { check_id("transcription", id); return call_destructive(C::kSttDelete, {id}, nullptr, Query(), confirm); } } // namespace molo::audio::elevenlabs