liveBook · Manning

Target language

As explained in Data Analysis with LLMs

A target language is the language or language variant into which source content is translated.

A target language is the language, dialect, or requested style that translated content should use. In a spoken translator, audio is transcribed into text, translated into the target language, and turned back into speech. section 7.4.1 section 7.4.3

A target-language request can specify more than a language name:

Choosing the output

A translator can accept the string "French" and produce “Bonjour mes collègues à Paris.” Replacing it with "English in the style of Shakespeare" requests a stylized English rendering instead. section 7.4.4

Listing 7.3 Translating spoken input into a different language
import argparse  #1
import openai
import playsound
import requests
import scipy.io.wavfile
import sounddevice
import time

client = openai.OpenAI()

def record(output_path):                 #2
    """ Record audio and store in .wav file. 
    
    Args:
        output_path: store audio recording there.
    """
    sample_rate = 44100
    nr_frames = 5 * sample_rate
    recording = sounddevice.rec(
        nr_frames, samplerate=sample_rate, channels=1)
    sounddevice.wait()
    scipy.io.wavfile.write(output_path, sample_rate, recording)

def transcribe(audio_path):        #3
    """ Transcribe audio file to text.
    
    Args:
        audio_path: path to audio file.
    
    Returns:
        transcribed text.
    """
    with open(audio_path, 'rb') as audio_file:
        transcription = client.audio.transcriptions.create(
            file=audio_file, model='whisper-1')
        return transcription.text
#4
def create_prompt(to_translate, to_language):
    """ Generate prompt to translate text to target language.
    
    Args:
        to_translate: translate this text.
        to_language: translate text to this language.
    
    Returns:
        Translated text.
    """
    parts = []
    parts += [f'Translate this text to {to_language}:']
    parts += [to_translate]
    parts += ['Translated text:']
    return '\n'.join(parts)

def call_llm(prompt):                             #5
    """ Query large language model and return answer.
    
    Args:
        prompt: input prompt for language model.
    
    Returns:
        Answer by language model.
    """
    for nr_retries in range(1, 4):
        try:
            response = client.chat.completions.create(
                model='gpt-4o',
                messages=[
                    {'role':'user', 'content':prompt}
                    ]
                )
            return response.choices[0].message.content
        except:
            time.sleep(nr_retries * 2)
    raise Exception('Cannot query OpenAI model!')

def generate_speech(speech_text):    #6
    """ Generates speech for given text.
    
    Args:
        speech_text: generate speech for this text.
    
    Returns:
        query result.
    """
    response = client.audio.speech.create(
        model='tts-1', voice='alloy', 
        input=speech_text)
    return response.content

if __name__ == '__main__':        #7
    
    parser = argparse.ArgumentParser()
    parser.add_argument('tolanguage', type=str, help='Target language')
    args = parser.parse_args()
    
    while True:  #8
        
        user_input = input('Press enter to record (type quit to quit).')
        if user_input == 'quit':
            break
        
        audio_path = 'to_translate.wav'  #9
        record(audio_path)
        to_translate = transcribe(audio_path)
        print(f'Original text: {to_translate}')
         #10
        prompt = create_prompt(to_translate, args.tolanguage)
        translated = call_llm(prompt)
        print(f'Translated text: {translated}')

        speech = generate_speech(translated)    #11
        with open('translation.mp3', 'wb') as file:
            file.write(speech)

        playsound.playsound('translation.mp3')  #12

The implementation shows how a target-language string becomes a prompt parameter and drives the translation step in an audio application. section 7.4.3

How Data Analysis with LLMs covers it

How deep it goes
Defines the term and demonstrates its use as a free-form prompt parameter, without examining translation-model internals.
Examples
Uses French, German with Swabian dialect, and English in the style of Shakespeare as target-language requests.
What it assumes
Assumes basic use of a microphone-driven Python application while leaving input-language recognition to the language model.
What you can do after
Operate a spoken translator by supplying a target-language string that replaces a prompt placeholder.
How it connects
Connects target language with speech recognition, prompts, language models, translation, and text-to-speech.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.