|
| 1 | +from typing import TYPE_CHECKING |
| 2 | + |
| 3 | +import io |
| 4 | +import soundfile as sf |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +from besser.agent.core.processors.processor import Processor |
| 9 | +from besser.agent.core.session import Session |
| 10 | +from besser.agent.nlp.nlp_engine import NLPEngine |
| 11 | +from besser.agent.nlp.llm.llm import LLM |
| 12 | + |
| 13 | +from besser.agent.exceptions.logger import logger |
| 14 | + |
| 15 | +from besser.agent.nlp.speech2text.speech2text import Speech2Text |
| 16 | + |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from besser.agent.core.agent import Agent |
| 20 | + |
| 21 | + |
| 22 | +class AudioLanguageDetectionProcessor(Processor): |
| 23 | + """The AudioLanguageDetectionProcessor detects the spoken language in a given audio message. |
| 24 | +
|
| 25 | + This processor uses a speech-to-text model to transcribe audio and then leverages an LLM to predict the language. |
| 26 | + Ideally, you use a model that is trained for language detection, such as OpenAI's GPT-4o-mini |
| 27 | + or anything that works well on a plethora of languages. |
| 28 | +
|
| 29 | + Args: |
| 30 | + agent (Agent): The agent the processor belongs to. |
| 31 | + transcription_model (Speech2Text): The speech-to-text model to use for transcription. |
| 32 | + llm_name (str): The name of the LLM to use for language detection. |
| 33 | +
|
| 34 | + Attributes: |
| 35 | + agent (Agent): The agent the processor belongs to. |
| 36 | + _transcription_model_name (str): The speech-to-text model to use for transcription. |
| 37 | + _llm_name (str): The name of the LLM used for language detection. |
| 38 | + _nlp_engine (NLPEngine): The NLP Engine the Agent uses. |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__(self, agent: "Agent", transcription_model: Speech2Text, llm_name: str): |
| 42 | + super().__init__(agent=agent, user_messages=True, agent_messages=False) |
| 43 | + self._llm_name: str = llm_name |
| 44 | + self._transcription_model: Speech2Text = transcription_model |
| 45 | + self._nlp_engine: NLPEngine = agent.nlp_engine |
| 46 | + |
| 47 | + def process(self, session: Session, message: bytes) -> bytes: |
| 48 | + """Method to process a message and predict the message's language. |
| 49 | +
|
| 50 | + The detected language will be stored as a session parameter. The key is "user_language". |
| 51 | +
|
| 52 | + Args: |
| 53 | + session (Session): the current session |
| 54 | + message (str): the message to be processed |
| 55 | +
|
| 56 | + Returns: |
| 57 | + str: the original message |
| 58 | + """ |
| 59 | + # transcribe audio bytes |
| 60 | + llm: LLM = self._nlp_engine._llms.get(self._llm_name) |
| 61 | + |
| 62 | + try: |
| 63 | + raw_audio = io.BytesIO(message) |
| 64 | + |
| 65 | + # Convert raw bytes to NumPy array |
| 66 | + audio_data = np.frombuffer(raw_audio.read(), dtype=np.int16) |
| 67 | + |
| 68 | + # Save as WAV in memory |
| 69 | + wav_file = io.BytesIO() |
| 70 | + sf.write( |
| 71 | + wav_file, audio_data, samplerate=44100, format="WAV", subtype="PCM_16" |
| 72 | + ) |
| 73 | + wav_file.name = "audio.wav" |
| 74 | + wav_file.seek(0) |
| 75 | + |
| 76 | + # Use the transcription model to transcribe the audio |
| 77 | + transcription = self._transcription_model.speech2text(wav_file.getvalue()) |
| 78 | + |
| 79 | + prompt = ( |
| 80 | + f"Identify the language based on the following message: {transcription}. " |
| 81 | + f"Only return the ISO 639-1 standard language code of the " |
| 82 | + f"language you recognized." |
| 83 | + ) |
| 84 | + # Use the LLM to detect the language based on the transcription |
| 85 | + # this might not work with all LLMs |
| 86 | + detected_lang = llm.predict(prompt, session=session) |
| 87 | + |
| 88 | + logger.info(f"Detected language (ISO 639-1): {detected_lang}") |
| 89 | + |
| 90 | + session.set("user_language", detected_lang) |
| 91 | + except Exception as e: |
| 92 | + logger.error(f"Error during language detection: {e}") |
| 93 | + |
| 94 | + return message |
0 commit comments