llmservice_flows/
audio.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use reqwest::multipart;
use serde::Deserialize;

use crate::LLMApi;
use crate::Retry;

pub struct TranscribeInput {
    pub audio: Vec<u8>,
    pub audio_format: String,
    pub language: String,
}

impl LLMApi for TranscribeInput {
    type Output = TranscriptionOutput;
    async fn api(&self, endpoint: &str, api_key: &str) -> Retry<Self::Output> {
        transcribe_inner(endpoint, api_key, &self).await
    }
}

#[derive(Debug, Deserialize)]
pub struct TranscriptionOutput {
    pub text: String,
}
pub struct TranslateInput {
    pub audio: Vec<u8>,
    pub audio_format: String,
    pub language: String,
}

impl LLMApi for TranslateInput {
    type Output = TranslationOutput;
    async fn api(&self, endpoint: &str, api_key: &str) -> Retry<Self::Output> {
        translate_inner(endpoint, api_key, &self).await
    }
}

#[derive(Debug, Deserialize)]
pub struct TranslationOutput {
    pub text: String,
}

impl<'a> crate::LLMServiceFlows<'a> {
    /// Transcribe audio into the input language.
    ///
    /// `input` is an [TranscribeInput] object.
    ///
    ///```rust
    ///   // This code snippet transcribe input audio into English, the audio is collected in previous step.
    ///   // Prepare the TranscribeInput struct.
    ///   let input = TranscribeInput {
    ///      audio: audio,
    ///      audio_format: "wav".to_string(),
    ///      language: "en".to_string(),
    ///   };
    ///   // Call the transcribe function.
    ///   let transcription = match llm.transcribe(input).await {
    ///       Ok(r) => r.text,
    ///       Err(e) => {your error handling},
    ///   };
    /// ```
    pub async fn transcribe(&self, input: TranscribeInput) -> Result<TranscriptionOutput, String> {
        self.keep_trying(input).await
    }

    /// Translate audio into English.
    ///
    /// `input` is an [TranslateInput] object.
    ///
    ///```rust
    ///   // This code snippet translate input audio into English, the audio is collected in previous step.
    ///   // Prepare the TranslateInput struct.
    ///   let input = TranslateInput {
    ///      audio: audio,
    ///      audio_format: "wav".to_string(),
    ///      language: "zh".to_string(),
    ///   };
    ///   // Call the translate function.
    ///   let translation = match llm.translate(input).await {
    ///       Ok(r) => r.text,
    ///       Err(e) => {your error handling},
    ///   };
    /// ```
    pub async fn translate(&self, input: TranslateInput) -> Result<TranslationOutput, String> {
        self.keep_trying(input).await
    }
}

async fn transcribe_inner(
    endpoint: &str,
    _api_key: &str,
    input: &TranscribeInput,
) -> Retry<TranscriptionOutput> {
    let uri = format!("{}/audio/transcriptions", endpoint);

    let form = multipart::Form::new()
        .part(
            "file",
            multipart::Part::bytes(input.audio.clone())
                .file_name(format!("audio.{}", input.audio_format)),
        )
        .part("language", multipart::Part::text(input.language.clone()));

    match reqwest::Client::new()
        .post(uri)
        .multipart(form)
        .send()
        .await
    {
        Ok(res) => {
            let status = res.status();
            let body = res.bytes().await.unwrap();
            match status.is_success() {
                true => Retry::No(
                    serde_json::from_slice::<TranscriptionOutput>(&body.as_ref())
                        .or(Err(String::from("Unexpected error"))),
                ),
                false => {
                    match status.into() {
                        409 | 429 | 503 => {
                            // 409 TryAgain 429 RateLimitError
                            // 503 ServiceUnavailable
                            Retry::Yes(String::from_utf8_lossy(&body.as_ref()).into_owned())
                        }
                        _ => Retry::No(Err(String::from_utf8_lossy(&body.as_ref()).into_owned())),
                    }
                }
            }
        }
        Err(e) => Retry::No(Err(e.to_string())),
    }
}

async fn translate_inner(
    endpoint: &str,
    _api_key: &str,
    input: &TranslateInput,
) -> Retry<TranslationOutput> {
    let uri = format!("{}/audio/translations", endpoint);

    let form = multipart::Form::new()
        .part(
            "file",
            multipart::Part::bytes(input.audio.clone())
                .file_name(format!("audio.{}", input.audio_format)),
        )
        .part("language", multipart::Part::text(input.language.clone()));

    match reqwest::Client::new()
        .post(uri)
        .multipart(form)
        .send()
        .await
    {
        Ok(res) => {
            let status = res.status();
            let body = res.bytes().await.unwrap();
            match status.is_success() {
                true => Retry::No(
                    serde_json::from_slice::<TranslationOutput>(&body.as_ref())
                        .or(Err(String::from("Unexpected error"))),
                ),
                false => {
                    match status.into() {
                        409 | 429 | 503 => {
                            // 409 TryAgain 429 RateLimitError
                            // 503 ServiceUnavailable
                            Retry::Yes(String::from_utf8_lossy(&body.as_ref()).into_owned())
                        }
                        _ => Retry::No(Err(String::from_utf8_lossy(&body.as_ref()).into_owned())),
                    }
                }
            }
        }
        Err(e) => Retry::No(Err(e.to_string())),
    }
}