From e78bcc919001df9a58f47c6f591506d3bceff253 Mon Sep 17 00:00:00 2001 From: projects-g <63178974+projects-g@users.noreply.github.com> Date: Thu, 28 Sep 2023 18:16:39 +0530 Subject: [PATCH] Scaleai Translation (#258) * hotfix * remove assert from translation * review comments * reflector.media change targetLang to en --- .../processors/transcript_translator.py | 21 +++++++++---------- www/app/transcripts/useTranscript.ts | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/server/reflector/processors/transcript_translator.py b/server/reflector/processors/transcript_translator.py index 354b1bd8..f6f2e521 100644 --- a/server/reflector/processors/transcript_translator.py +++ b/server/reflector/processors/transcript_translator.py @@ -1,7 +1,6 @@ from time import monotonic import httpx - from reflector.processors.base import Processor from reflector.processors.types import Transcript, TranslationLanguages from reflector.settings import settings @@ -43,27 +42,26 @@ class TranscriptTranslatorProcessor(Processor): self.transcript = data await self.flush() - async def get_translation(self, text: str) -> str: - self.logger.debug(f"Try to translate {text=}") + async def get_translation(self, text: str) -> str | None: # FIXME this should be a processor after, as each user may want # different languages + source_language = self.get_pref("audio:source_language", "en") target_language = self.get_pref("audio:target_language", "en") + if source_language == target_language: + return languages = TranslationLanguages() - # Only way to set the target should be the UI element like dropdown. # Hence, this assert should never fail. assert languages.is_supported(target_language) - assert target_language != source_language - source_language = self.get_pref("audio:source_language", "en") - target_language = self.get_pref("audio:target_language", "en") + self.logger.debug(f"Try to translate {text=}") json_payload = { "text": text, "source_language": source_language, "target_language": target_language, } - translation = None + async with httpx.AsyncClient() as client: response = await retry(client.post)( settings.TRANSCRIPT_URL + "/translate", @@ -75,7 +73,7 @@ class TranscriptTranslatorProcessor(Processor): result = response.json()["text"] # Sanity check for translation status in the result - if source_language != target_language and target_language in result: + if target_language in result: translation = result[target_language] self.logger.debug(f"Translation response: {text=}, {translation=}") return translation @@ -83,6 +81,7 @@ class TranscriptTranslatorProcessor(Processor): async def _flush(self): if not self.transcript: return - translation = await self.get_translation(text=self.transcript.text) - self.transcript.translation = translation + self.transcript.translation = await self.get_translation( + text=self.transcript.text + ) await self.emit(self.transcript) diff --git a/www/app/transcripts/useTranscript.ts b/www/app/transcripts/useTranscript.ts index 314b85f8..7dbc969a 100644 --- a/www/app/transcripts/useTranscript.ts +++ b/www/app/transcripts/useTranscript.ts @@ -56,7 +56,7 @@ const useTranscript = ( const requestParameters: V1TranscriptsCreateRequest = { createTranscript: { name: "Weekly All-Hands", // Hardcoded for now - targetLanguage: "fr", // Hardcoded for now + targetLanguage: "en", // Hardcoded for now }, };