feat: Modellarten und persönliche Stimmenfreigabe ergänzen
This commit is contained in:
119
crates/nexus-api/src/ui/stimmen.rs
Normal file
119
crates/nexus-api/src/ui/stimmen.rs
Normal file
@ -0,0 +1,119 @@
|
||||
//! Sprachmodelle und Freigabe der Stimmen in Nexus; Audio bleibt beim Studio.
|
||||
use super::{audit, csrf_field, esc, form_field, guard, redirect};
|
||||
use crate::state::AppState;
|
||||
use axum::{
|
||||
body::Bytes,
|
||||
extract::State,
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::Response,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(super) async fn page(state: &AppState, user: &super::Viewer) -> String {
|
||||
let models: String = state.store().list_models(false).unwrap_or_default().into_iter()
|
||||
.filter_map(|m| {
|
||||
let details = state.store().model_details(&m.name).ok().flatten()?;
|
||||
if details.engine != "tts" { return None; }
|
||||
let installed = crate::ui::chat::speech_offered(&state.runtime, &details.path);
|
||||
let mode = match super::chat::voice_art(&details.path) {
|
||||
"gespeichert" => "klonen", "eingebaut" => "eingebaut", _ => "entwerfen",
|
||||
};
|
||||
Some(format!("<tr><td>{}</td><td>Deutsch und weitere (Studio)</td><td>{mode}</td><td>{}</td></tr>",
|
||||
esc(&m.name), if installed { "ja" } else { "nein" }))
|
||||
}).collect();
|
||||
let mut body = format!(
|
||||
"<section class=\"card\"><h2>Sprachmodelle</h2><div class=\"breit\"><table><thead><tr><th>Modell</th><th>Sprachen</th><th>Modus</th><th>Installiert</th></tr></thead><tbody>{models}</tbody></table></div><p class=\"klein\">Die genaue Sprachenliste meldet das Studio derzeit nicht je Modell.</p></section>"
|
||||
);
|
||||
let voices = state.runtime.engine_voices("tts").await;
|
||||
let options: String = state
|
||||
.store()
|
||||
.list_models(true)
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.filter_map(|m| {
|
||||
let d = state.store().model_details(&m.name).ok().flatten()?;
|
||||
if d.engine != "tts" || !crate::ui::chat::speech_offered(&state.runtime, &d.path) {
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"<option value=\"{}\" data-art=\"{}\">{}</option>",
|
||||
esc(&m.name),
|
||||
super::chat::voice_art(&d.path),
|
||||
esc(&m.name)
|
||||
))
|
||||
})
|
||||
.collect();
|
||||
body.push_str("<section class=\"card\"><h2>Stimmen</h2><p class=\"klein\">Eine Stimme darf ohne Freigabe nur probegehört werden. Die erste Freigabe beansprucht die Stimme für diesen Benutzer.</p>");
|
||||
if options.is_empty() {
|
||||
body.push_str("<p class=\"leer\">Kein installiertes Sprachmodell verfügbar.</p>");
|
||||
}
|
||||
for v in voices {
|
||||
let approved = state
|
||||
.store()
|
||||
.voice_approved(&v.id, v.saved, &user.user)
|
||||
.unwrap_or(false);
|
||||
let art = if v.saved { "gespeichert" } else { "eingebaut" };
|
||||
body.push_str(&format!("<div class=\"zeile\"><strong>{}</strong><span class=\"klein\">({art})</span><select aria-label=\"Sprachmodell für {}\" class=\"stimmen-modell\">{options}</select><button type=\"button\" class=\"sm probe\" data-stimme=\"{}\" data-gespeichert=\"{}\">Probehören</button><form method=\"post\" action=\"/ui/modelle/stimme\">{}<input type=\"hidden\" name=\"stimme\" value=\"{}\"><input type=\"hidden\" name=\"gespeichert\" value=\"{}\"><input type=\"hidden\" name=\"freigabe\" value=\"{}\"><button class=\"sm\" type=\"submit\">{}</button></form></div>",
|
||||
esc(&v.name), esc(&v.name), esc(&v.id), if v.saved { "1" } else { "0" }, csrf_field(user), esc(&v.id), if v.saved { "1" } else { "0" }, if approved { "0" } else { "1" }, if approved { "Freigabe zurücknehmen" } else { "Freigeben" }));
|
||||
}
|
||||
body.push_str("<p id=\"probe-status\" role=\"status\"></p></section><script src=\"/ui/stimmen.js\"></script>");
|
||||
body
|
||||
}
|
||||
|
||||
pub(super) async fn save(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
body: Bytes,
|
||||
) -> Response {
|
||||
let user = match guard(
|
||||
&state,
|
||||
&headers,
|
||||
form_field(&body, "csrf").as_deref(),
|
||||
false,
|
||||
) {
|
||||
Ok(user) => user,
|
||||
Err(r) => return *r,
|
||||
};
|
||||
let voice = form_field(&body, "stimme").unwrap_or_default();
|
||||
let saved = form_field(&body, "gespeichert").as_deref() == Some("1");
|
||||
let approved = form_field(&body, "freigabe").as_deref() == Some("1");
|
||||
if voice.is_empty()
|
||||
|| !state
|
||||
.runtime
|
||||
.engine_voices("tts")
|
||||
.await
|
||||
.iter()
|
||||
.any(|v| v.id == voice && v.saved == saved)
|
||||
{
|
||||
return super::refused(StatusCode::BAD_REQUEST, "Die Stimme ist nicht verfügbar.");
|
||||
}
|
||||
match state
|
||||
.store()
|
||||
.set_voice_approval(&voice, saved, &user.user, approved)
|
||||
{
|
||||
Ok(true) => {
|
||||
audit(&state, &user, "voice.approval", &voice);
|
||||
redirect("/ui/modelle?art=sprache")
|
||||
}
|
||||
Ok(false) => super::refused(
|
||||
StatusCode::FORBIDDEN,
|
||||
"Nur der Besitzer kann diese Stimme freigeben.",
|
||||
),
|
||||
Err(_) => super::refused(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Die Freigabe konnte nicht gespeichert werden.",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn script() -> Response {
|
||||
super::secure(Response::builder())
|
||||
.header(
|
||||
axum::http::header::CONTENT_TYPE,
|
||||
"text/javascript; charset=utf-8",
|
||||
)
|
||||
.body(axum::body::Body::from(include_str!(
|
||||
"../../assets/stimmen.js"
|
||||
)))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
Reference in New Issue
Block a user