rig_core/providers/minimax/
voice_clone.rs1use serde::{Deserialize, Serialize};
22use serde_json::Value;
23
24use crate::driver::Model;
25use crate::error::{EncodeError, ProviderError};
26use crate::operation::Whole;
27use crate::providers::openai::OpenAI;
28use crate::providers::openai::wire::OpenAIConfig;
29use crate::wire::{
30 Body, Call, Decoder, Descriptor, Encoded, Flow, Free, Mode, Operation, Out, Wire, WireEvent,
31 WireFrame,
32};
33
34const FILES_UPLOAD_PATH: &str = "/files/upload";
35const VOICE_CLONE_PATH: &str = "/voice_clone";
36
37pub fn voice_cloning(client: &OpenAI) -> Model<VoiceClones> {
40 client.model(VoiceClones::new(client.config().clone()))
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
45pub enum AudioPurpose {
46 VoiceClone,
48 PromptAudio,
50}
51
52impl AudioPurpose {
53 pub fn as_str(self) -> &'static str {
55 match self {
56 Self::VoiceClone => "voice_clone",
57 Self::PromptAudio => "prompt_audio",
58 }
59 }
60}
61
62#[derive(Clone, Debug, PartialEq, Eq)]
65pub struct CloneAudio {
66 pub purpose: AudioPurpose,
68 pub filename: String,
70 pub data: Vec<u8>,
72}
73
74impl CloneAudio {
75 pub fn sample(filename: impl Into<String>, data: impl Into<Vec<u8>>) -> Self {
77 Self {
78 purpose: AudioPurpose::VoiceClone,
79 filename: filename.into(),
80 data: data.into(),
81 }
82 }
83
84 pub fn prompt(filename: impl Into<String>, data: impl Into<Vec<u8>>) -> Self {
86 Self {
87 purpose: AudioPurpose::PromptAudio,
88 filename: filename.into(),
89 data: data.into(),
90 }
91 }
92}
93
94#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
96pub struct UploadedFile {
97 pub file_id: i64,
99 pub bytes: Option<u64>,
101 pub created_at: Option<u64>,
103 pub filename: Option<String>,
105 pub purpose: Option<String>,
107}
108
109#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
112pub struct ClonePrompt {
113 pub prompt_audio: i64,
115 pub prompt_text: String,
117}
118
119#[derive(Clone, Debug, PartialEq, Serialize)]
121pub struct VoiceClone {
122 pub file_id: i64,
124 pub voice_id: String,
127 #[serde(skip_serializing_if = "Option::is_none")]
129 pub clone_prompt: Option<ClonePrompt>,
130 #[serde(skip_serializing_if = "Option::is_none")]
132 pub text: Option<String>,
133 #[serde(skip_serializing_if = "Option::is_none")]
135 pub model: Option<String>,
136 #[serde(skip)]
139 pub additional_params: Option<Value>,
140}
141
142impl VoiceClone {
143 pub fn new(file_id: i64, voice_id: impl Into<String>) -> Self {
145 Self {
146 file_id,
147 voice_id: voice_id.into(),
148 clone_prompt: None,
149 text: None,
150 model: None,
151 additional_params: None,
152 }
153 }
154
155 pub fn prompt(mut self, prompt_audio: i64, prompt_text: impl Into<String>) -> Self {
158 self.clone_prompt = Some(ClonePrompt {
159 prompt_audio,
160 prompt_text: prompt_text.into(),
161 });
162 self
163 }
164
165 pub fn preview(mut self, text: impl Into<String>, model: impl Into<String>) -> Self {
167 self.text = Some(text.into());
168 self.model = Some(model.into());
169 self
170 }
171
172 pub fn created_voice_id(&self) -> &str {
175 self.additional_params
176 .as_ref()
177 .and_then(|params| params.get("voice_id"))
178 .and_then(Value::as_str)
179 .unwrap_or(&self.voice_id)
180 }
181
182 pub fn additional_params(mut self, params: impl Into<Option<Value>>) -> Self {
185 self.additional_params =
186 crate::json_utils::merge_params(self.additional_params.take(), params.into());
187 self
188 }
189}
190
191#[derive(Clone, Debug, PartialEq)]
193pub struct ClonedVoice {
194 pub voice_id: String,
196 pub demo_audio: Option<String>,
198 pub extra_info: Option<Value>,
200}
201
202#[derive(Clone, Debug, PartialEq)]
204pub enum VoiceCloneRequest {
205 Upload(CloneAudio),
207 Clone(VoiceClone),
209}
210
211#[derive(Clone, Debug, Default, Deserialize, Serialize)]
214pub struct VoiceCloneReply {
215 pub file: Option<UploadedFile>,
217 pub demo_audio: Option<String>,
219 #[serde(default, skip_serializing_if = "Option::is_none")]
221 pub extra_info: Option<Value>,
222}
223
224#[derive(Debug, Clone, Copy, PartialEq, Eq)]
226pub struct VoiceCloning;
227
228impl Operation for VoiceCloning {
229 type Request = VoiceCloneRequest;
230 type Event = std::convert::Infallible;
231 type End = VoiceCloneReply;
232 type Response = VoiceCloneReply;
233 type Fold = Whole<Self>;
234 type Emit = Free;
235
236 fn fold(_request: &Self::Request, _call: &mut Call<'_>) -> Self::Fold {
237 Whole::new()
238 }
239}
240
241#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
244pub struct VoiceClones {
245 pub provider: OpenAIConfig,
247}
248
249impl VoiceClones {
250 pub fn new(provider: OpenAIConfig) -> Self {
252 Self { provider }
253 }
254}
255
256impl Wire for VoiceClones {
257 type Op = VoiceCloning;
258 type Event = Vec<u8>;
259 type Payload = Encoded;
260 type Frame = WireFrame;
261 type Decoder<'id> = VoiceClonesDecoder;
262 type Reassembler = crate::wire::document::Unreassembled;
263
264 fn describe(&self) -> Descriptor<'_> {
265 Descriptor::new(self.provider.dialect.name)
266 }
267
268 fn encode(&self, request: VoiceCloneRequest, _mode: Mode) -> Result<Encoded, EncodeError> {
269 if self.provider.dialect.name != super::PROVIDER_NAME {
270 return Err(EncodeError::request(format!(
271 "voice cloning is a MiniMax endpoint, not one {} serves",
272 self.provider.dialect.name
273 )));
274 }
275 let provider = &self.provider;
276 match request {
277 VoiceCloneRequest::Upload(audio) => {
278 use crate::http_client::MultipartForm;
279 use crate::http_client::multipart::Part;
280
281 let format = std::path::Path::new(&audio.filename)
282 .extension()
283 .and_then(std::ffi::OsStr::to_str)
284 .map(str::to_ascii_lowercase);
285 if !matches!(format.as_deref(), Some("mp3" | "m4a" | "wav")) {
286 return Err(EncodeError::request(format!(
287 "MiniMax clones voices from mp3, m4a or wav files, not `{}`",
288 audio.filename
289 )));
290 }
291
292 let form = MultipartForm::new()
293 .text("purpose", audio.purpose.as_str())
294 .part(Part::bytes("file", audio.data).filename(audio.filename));
295 let builder = http::Request::post(provider.uri(FILES_UPLOAD_PATH, None));
296 crate::providers::openai::wire::encoded(provider, builder, Body::Multipart(form))
297 }
298 VoiceCloneRequest::Clone(mut clone) => {
299 let additional_params = clone.additional_params.take();
300 let mut body = serde_json::to_value(clone)?;
301 if let Some(additional_params) = additional_params {
302 body = crate::json_utils::merge(body, additional_params);
303 }
304 crate::providers::openai::wire::json_post(provider, VOICE_CLONE_PATH, None, &body)
305 }
306 }
307 }
308
309 fn decoder<'id>(&self) -> Self::Decoder<'id> {
310 VoiceClonesDecoder
311 }
312}
313
314pub struct VoiceClonesDecoder;
316
317impl<'id> Decoder<'id, VoiceCloning> for VoiceClonesDecoder {
318 type Event = Vec<u8>;
319
320 fn classify(&self, frame: WireFrame) -> WireEvent<Self::Event> {
321 WireEvent::Known(frame.into_bytes())
322 }
323
324 fn decode(
325 &mut self,
326 body: Vec<u8>,
327 out: Out<'id, VoiceCloning>,
328 ) -> Result<Flow, ProviderError> {
329 Ok(out.end(super::decode_reply(&body)?))
330 }
331}