feat: stream speech audio in supported formats
This commit is contained in:
@ -188,10 +188,30 @@ async fn fake_backend() -> u16 {
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
}
|
||||
let format = req["response_format"].as_str().unwrap_or("FEHLT");
|
||||
let content_type = match format {
|
||||
"mp3" => "audio/mpeg",
|
||||
"opus" => "audio/ogg",
|
||||
"flac" => "audio/flac",
|
||||
"pcm" => "audio/pcm",
|
||||
_ => "audio/wav",
|
||||
};
|
||||
if req["input"] == "STROM" {
|
||||
let chunks = ["ERSTES", "ZWEITES", "DRITTES"];
|
||||
let audio = stream::unfold(0usize, move |index| async move {
|
||||
let chunk = chunks.get(index)?;
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
Some((Ok::<_, Infallible>(Bytes::from(*chunk)), index + 1))
|
||||
});
|
||||
return Response::builder()
|
||||
.header(CONTENT_TYPE, content_type)
|
||||
.body(Body::from_stream(audio))
|
||||
.unwrap();
|
||||
}
|
||||
Response::builder()
|
||||
.header(CONTENT_TYPE, "audio/wav")
|
||||
.header(CONTENT_TYPE, content_type)
|
||||
.body(Body::from(format!(
|
||||
"RIFF|{}|{}",
|
||||
"RIFF|{}|{}|{format}",
|
||||
req["model"].as_str().unwrap_or(""),
|
||||
req["input"].as_str().unwrap_or("")
|
||||
)))
|
||||
@ -536,7 +556,9 @@ async fn chat_ohne_streaming_startet_die_instanz_und_reicht_die_antwort_durch()
|
||||
#[tokio::test]
|
||||
async fn sprachausgabe_reicht_audio_durch_und_verlangt_key_und_modell() {
|
||||
let gw = gateway(0, false).await;
|
||||
let body = json!({"model": "bonsai", "input": "Hallo", "voice": "Vivian"}).to_string();
|
||||
let body =
|
||||
json!({"model": "bonsai", "input": "Hallo", "voice": "Vivian", "response_format": "wav"})
|
||||
.to_string();
|
||||
let post = |key: Option<&str>, body: String| {
|
||||
let mut req = gw
|
||||
.client
|
||||
@ -552,7 +574,7 @@ async fn sprachausgabe_reicht_audio_durch_und_verlangt_key_und_modell() {
|
||||
assert_eq!(resp.status(), 200);
|
||||
assert_eq!(resp.headers()[CONTENT_TYPE], "audio/wav");
|
||||
// Die Attrappe gibt zurück, was sie gesehen hat: Modellname und Text unverändert.
|
||||
assert_eq!(resp.text().await.unwrap(), "RIFF|bonsai|Hallo");
|
||||
assert_eq!(resp.text().await.unwrap(), "RIFF|bonsai|Hallo|wav");
|
||||
assert_eq!(gw.engine.starts.load(Ordering::SeqCst), 1);
|
||||
|
||||
assert_eq!(post(None, body.clone()).await.status(), 401);
|
||||
@ -564,6 +586,91 @@ async fn sprachausgabe_reicht_audio_durch_und_verlangt_key_und_modell() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sprachausgabe_formate_standard_und_unbekanntes_format() {
|
||||
let gw = gateway(0, false).await;
|
||||
for (format, expected_type) in [
|
||||
(None, "audio/mpeg"),
|
||||
(Some("mp3"), "audio/mpeg"),
|
||||
(Some("wav"), "audio/wav"),
|
||||
(Some("opus"), "audio/ogg"),
|
||||
(Some("flac"), "audio/flac"),
|
||||
] {
|
||||
let mut body = json!({"model": "bonsai", "input": "Ton", "voice": "Vivian"});
|
||||
if let Some(format) = format {
|
||||
body["response_format"] = json!(format);
|
||||
}
|
||||
let resp = gw
|
||||
.client
|
||||
.post(format!("{}/v1/audio/speech", gw.base))
|
||||
.bearer_auth(&gw.key)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(body.to_string())
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), 200, "{format:?}");
|
||||
assert_eq!(resp.headers()[CONTENT_TYPE], expected_type, "{format:?}");
|
||||
assert_eq!(
|
||||
resp.text().await.unwrap(),
|
||||
format!("RIFF|bonsai|Ton|{}", format.unwrap_or("mp3"))
|
||||
);
|
||||
}
|
||||
for format in [
|
||||
json!("aac"),
|
||||
json!("pcm"),
|
||||
json!("unbekannt"),
|
||||
json!(null),
|
||||
json!(42),
|
||||
] {
|
||||
let resp = gw.client.post(format!("{}/v1/audio/speech", gw.base))
|
||||
.bearer_auth(&gw.key)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(json!({"model": "bonsai", "input": "Ton", "voice": "Vivian", "response_format": format}).to_string())
|
||||
.send().await.unwrap();
|
||||
assert_eq!(resp.status(), 400);
|
||||
let body: Value = serde_json::from_str(&resp.text().await.unwrap()).unwrap();
|
||||
assert!(
|
||||
body["error"]["message"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.contains("Audioformat")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sprachausgabe_strom_kommt_stueckweise_und_ist_als_datei_lesbar() {
|
||||
let gw = gateway(0, false).await;
|
||||
let started = Instant::now();
|
||||
let mut resp = gw
|
||||
.client
|
||||
.post(format!("{}/v1/audio/speech", gw.base))
|
||||
.bearer_auth(&gw.key)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(json!({"model": "bonsai", "input": "STROM", "voice": "Vivian"}).to_string())
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), 200);
|
||||
assert_eq!(resp.headers()[CONTENT_TYPE], "audio/mpeg");
|
||||
let first = resp.chunk().await.unwrap().unwrap();
|
||||
assert_eq!(first, "ERSTES");
|
||||
assert!(
|
||||
started.elapsed() < Duration::from_millis(500),
|
||||
"Antwort wartet nicht auf das Ende der Erzeugung"
|
||||
);
|
||||
let mut file = first.to_vec();
|
||||
while let Some(chunk) = resp.chunk().await.unwrap() {
|
||||
file.extend_from_slice(&chunk);
|
||||
}
|
||||
assert!(
|
||||
started.elapsed() >= Duration::from_millis(400),
|
||||
"Stücke wurden gepuffert"
|
||||
);
|
||||
assert_eq!(file, b"ERSTESZWEITESDRITTES");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sprachausgabe_ohne_ton_wird_zu_502_statt_leerem_200() {
|
||||
let gw = gateway(0, false).await;
|
||||
@ -2359,10 +2466,10 @@ async fn playground_liest_vor_nur_mit_sprachmodell_und_sitzung() {
|
||||
let gut = json!({"model": "sprecher", "input": "Hallo", "voice": "Ryan"});
|
||||
let resp = sprechen(cookie.clone(), Some(csrf.clone()), gut.clone()).await;
|
||||
assert_eq!(resp.status(), 200);
|
||||
assert_eq!(resp.headers()[CONTENT_TYPE], "audio/wav");
|
||||
assert_eq!(resp.headers()[CONTENT_TYPE], "audio/pcm");
|
||||
// Die Attrappe gibt zurück, was sie gesehen hat (die Umschreibung des Modellnamens auf den des
|
||||
// Studios macht `StudioEngine::endpoint`, getestet im Adapter, nicht die FixedEngine).
|
||||
assert_eq!(resp.text().await.unwrap(), "RIFF|sprecher|Hallo");
|
||||
assert_eq!(resp.text().await.unwrap(), "RIFF|sprecher|Hallo|pcm");
|
||||
// Ohne Token 403, ohne Sitzung 401, ein Chatmodell ist kein Sprachmodell.
|
||||
assert_eq!(
|
||||
sprechen(cookie.clone(), None, gut.clone()).await.status(),
|
||||
|
||||
Reference in New Issue
Block a user