Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Kvadratni committed Dec 21, 2024
1 parent 6d654cb commit 27bb51c
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions crates/goose-server/src/routes/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::process::Command;
use std::sync::Arc;
use tempfile::Builder;
use tokio::fs;
use tokio::process::Command as TokioCommand;
use tokio::sync::{OnceCell, RwLock};
use tower_http::cors::{Any, CorsLayer};

Expand Down Expand Up @@ -52,7 +51,7 @@ async fn get_status() -> Arc<RwLock<WhisperStatus>> {
async fn ensure_whisper() {
INIT.get_or_init(|| async {
let status = get_status().await;

// Get the project root directory
let mut project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
project_dir.pop(); // go up from goose-server
Expand Down Expand Up @@ -118,7 +117,7 @@ async fn check_whisper_ready() -> Result<(), (StatusCode, Json<serde_json::Value
ensure_whisper().await;
let status = get_status().await;
let status = status.read().await;

if !status.is_ready() {
Err((
StatusCode::SERVICE_UNAVAILABLE,
Expand All @@ -130,14 +129,16 @@ async fn check_whisper_ready() -> Result<(), (StatusCode, Json<serde_json::Value
"built": status.built,
"model_downloaded": status.model_downloaded
}
}))
})),
))
} else {
Ok(())
}
}

async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
async fn transcribe(
mut multipart: Multipart,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<serde_json::Value>)> {
// Check if whisper is ready
check_whisper_ready().await?;

Expand All @@ -155,7 +156,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": "Received empty audio data"
}))
})),
));
}

Expand All @@ -169,7 +170,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Failed to create temporary WebM file: {}", e)
}))
})),
));
}
};
Expand All @@ -184,7 +185,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Failed to create temporary WAV file: {}", e)
}))
})),
));
}
};
Expand All @@ -200,7 +201,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Failed to write WebM data: {}", e)
}))
})),
));
}
}
Expand Down Expand Up @@ -236,7 +237,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": "Whisper executable not found"
}))
})),
));
}

Expand All @@ -248,7 +249,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": "Whisper model not found"
}))
})),
));
}

Expand All @@ -262,7 +263,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Failed to verify WebM file: {}", e)
}))
})),
));
}
};
Expand Down Expand Up @@ -293,7 +294,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Invalid WebM file: {}", String::from_utf8_lossy(&ffprobe_webm.stderr))
}))
})),
));
}

Expand Down Expand Up @@ -330,7 +331,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("FFmpeg conversion failed: {}", String::from_utf8_lossy(&ffmpeg_output.stderr))
}))
})),
));
}

Expand All @@ -344,7 +345,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Failed to verify WAV file: {}", e)
}))
})),
));
}
};
Expand All @@ -358,7 +359,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": "WAV conversion failed - output file is empty"
}))
})),
));
}

Expand Down Expand Up @@ -387,7 +388,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Invalid WAV file: {}", String::from_utf8_lossy(&ffprobe_wav.stderr))
}))
})),
));
}

Expand Down Expand Up @@ -440,7 +441,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Failed to read transcription output: {}", e)
}))
})),
));
}
}
Expand All @@ -456,7 +457,7 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": format!("Whisper failed: {}", String::from_utf8_lossy(&output.stderr))
}))
})),
));
}
} else {
Expand All @@ -470,6 +471,6 @@ async fn transcribe(mut multipart: Multipart) -> Result<Json<serde_json::Value>,
Json(json!({
"success": false,
"error": "Failed to process audio"
}))
})),
))
}
}

0 comments on commit 27bb51c

Please sign in to comment.