mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-22 05:09:05 +00:00
server: pass source and target language from api to pipeline
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
from reflector.processors.base import Processor
|
||||
import importlib
|
||||
|
||||
from reflector.processors.audio_transcript import AudioTranscriptProcessor
|
||||
from reflector.processors.base import Pipeline, Processor
|
||||
from reflector.processors.types import AudioFile
|
||||
from reflector.settings import settings
|
||||
import importlib
|
||||
|
||||
|
||||
class AudioTranscriptAutoProcessor(AudioTranscriptProcessor):
|
||||
@@ -35,6 +36,10 @@ class AudioTranscriptAutoProcessor(AudioTranscriptProcessor):
|
||||
self.processor = self.get_instance(settings.TRANSCRIPT_BACKEND)
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def set_pipeline(self, pipeline: Pipeline):
|
||||
super().set_pipeline(pipeline)
|
||||
self.processor.set_pipeline(pipeline)
|
||||
|
||||
def connect(self, processor: Processor):
|
||||
self.processor.connect(processor)
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ API will be a POST request to TRANSCRIPT_URL:
|
||||
from time import monotonic
|
||||
|
||||
import httpx
|
||||
|
||||
from reflector.processors.audio_transcript import AudioTranscriptProcessor
|
||||
from reflector.processors.audio_transcript_auto import AudioTranscriptAutoProcessor
|
||||
from reflector.processors.types import AudioFile, Transcript, TranslationLanguages, Word
|
||||
@@ -54,14 +53,10 @@ class AudioTranscriptModalProcessor(AudioTranscriptProcessor):
|
||||
"file": (data.name, data.fd),
|
||||
}
|
||||
|
||||
# TODO: Get the source / target language from the UI preferences dynamically
|
||||
# Update code here once this is possible.
|
||||
# i.e) extract from context/session objects
|
||||
source_language = "en"
|
||||
|
||||
# TODO: target lang is set to "fr" for demo purposes
|
||||
# Revert back once language selection is implemented
|
||||
target_language = "fr"
|
||||
# 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")
|
||||
languages = TranslationLanguages()
|
||||
|
||||
# Only way to set the target should be the UI element like dropdown.
|
||||
@@ -87,8 +82,8 @@ class AudioTranscriptModalProcessor(AudioTranscriptProcessor):
|
||||
result = response.json()
|
||||
|
||||
# Sanity check for translation status in the result
|
||||
translation = ""
|
||||
if target_language in result["text"]:
|
||||
translation = None
|
||||
if source_language != target_language and target_language in result["text"]:
|
||||
translation = result["text"][target_language]
|
||||
text = result["text"][source_language]
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from reflector.logger import logger
|
||||
from uuid import uuid4
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import asyncio
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from reflector.logger import logger
|
||||
|
||||
|
||||
class Processor:
|
||||
@@ -17,9 +19,11 @@ class Processor:
|
||||
self.uid = uuid4().hex
|
||||
self.flushed = False
|
||||
self.logger = (custom_logger or logger).bind(processor=self.__class__.__name__)
|
||||
self.pipeline = None
|
||||
|
||||
def set_pipeline(self, pipeline: "Pipeline"):
|
||||
# if pipeline is used, pipeline logger will be used instead
|
||||
self.pipeline = pipeline
|
||||
self.logger = pipeline.logger.bind(processor=self.__class__.__name__)
|
||||
|
||||
def connect(self, processor: "Processor"):
|
||||
@@ -54,6 +58,14 @@ class Processor:
|
||||
"""
|
||||
self._callbacks.remove(callback)
|
||||
|
||||
def get_pref(self, key: str, default: Any = None):
|
||||
"""
|
||||
Get a preference from the pipeline prefs
|
||||
"""
|
||||
if self.pipeline:
|
||||
return self.pipeline.get_pref(key, default)
|
||||
return default
|
||||
|
||||
async def emit(self, data):
|
||||
for callback in self._callbacks:
|
||||
await callback(data)
|
||||
@@ -191,6 +203,7 @@ class Pipeline(Processor):
|
||||
self.logger.info("Pipeline created")
|
||||
|
||||
self.processors = processors
|
||||
self.prefs = {}
|
||||
|
||||
for processor in processors:
|
||||
processor.set_pipeline(self)
|
||||
@@ -220,3 +233,17 @@ class Pipeline(Processor):
|
||||
for processor in self.processors:
|
||||
processor.describe(level + 1)
|
||||
logger.info("")
|
||||
|
||||
def set_pref(self, key: str, value: Any):
|
||||
"""
|
||||
Set a preference for this pipeline
|
||||
"""
|
||||
self.prefs[key] = value
|
||||
|
||||
def get_pref(self, key: str, default=None):
|
||||
"""
|
||||
Get a preference for this pipeline
|
||||
"""
|
||||
if key not in self.prefs:
|
||||
self.logger.warning(f"Pref {key} not found, using default")
|
||||
return self.prefs.get(key, default)
|
||||
|
||||
@@ -47,7 +47,7 @@ class Word(BaseModel):
|
||||
|
||||
class Transcript(BaseModel):
|
||||
text: str = ""
|
||||
translation: str = ""
|
||||
translation: str | None = None
|
||||
words: list[Word] = None
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user