From 564c0823c06f1c8d2a5c986527f5198b0ff78077 Mon Sep 17 00:00:00 2001 From: Nurfog Date: Thu, 29 Jan 2026 14:31:45 -0300 Subject: [PATCH] feat: Implement bilingual transcription using Ollama and rename the internal translation helper function. --- services/cms-service/src/handlers.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/services/cms-service/src/handlers.rs b/services/cms-service/src/handlers.rs index c74ddf9..7f517f5 100644 --- a/services/cms-service/src/handlers.rs +++ b/services/cms-service/src/handlers.rs @@ -666,7 +666,7 @@ pub async fn process_transcription( Ok(Json(updated_lesson)) } -async fn _translate_text(text: &str, target_lang: &str) -> Result { +async fn translate_text(text: &str, target_lang: &str) -> Result { let provider = env::var("AI_PROVIDER").unwrap_or_else(|_| "openai".to_string()); let client = reqwest::Client::new(); @@ -796,12 +796,32 @@ pub async fn run_transcription_task(pool: PgPool, lesson_id: Uuid) -> Result<(), return Err(err); } - let transcription_result: serde_json::Value = response.json().await + let mut transcription_result: serde_json::Value = response.json().await .map_err(|e| format!("Failed to parse Whisper response: {}", e))?; tracing::info!("Transcription received successfully for lesson {}", lesson_id); - // 5. Update lesson with transcription + // 5. Bilingual translation with Ollama + let text = transcription_result["text"].as_str().unwrap_or("").to_string(); + let detected_lang = transcription_result["language"].as_str().unwrap_or("es").to_string(); + + // Ensure the detected language text is stored in its own key + transcription_result[detected_lang.clone()] = serde_json::json!(text); + + let target_lang = if detected_lang == "es" { "en" } else { "es" }; + tracing::info!("Translating transcription from {} to {} using Ollama...", detected_lang, target_lang); + + if !text.is_empty() { + match translate_text(&text, target_lang).await { + Ok(translated) => { + tracing::info!("Translation to {} successful", target_lang); + transcription_result[target_lang] = serde_json::json!(translated); + }, + Err(e) => tracing::error!("Translation failed: {}", e), + } + } + + // 6. Update lesson with bilinguial transcription sqlx::query("UPDATE lessons SET transcription = $1, transcription_status = 'completed' WHERE id = $2") .bind(&transcription_result) .bind(lesson_id)