Update translation after changing original transcript in Speech Studio

Chatzopoulou, Eirini 0 Reputation points
2024-05-24T09:14:26.8866667+00:00

Hello,

I would like to update automatically the translation after changing the original transcript in Speech Studio.

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,531 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. dupammi 7,750 Reputation points Microsoft Vendor
    2024-05-24T11:20:15.2833333+00:00

    Hi @Chatzopoulou, Eirini

    Thank you for your question.

    To automatically update the translation after changing the original transcript in Azure Speech Studio, set up Azure Speech SDK and Translator Text API, fetch the updated transcript using speech recognition, and translate it using the Translator Text API. Replace placeholders with your Azure keys and adjust the audio file path as necessary.

    Below is a quick repro I tried at my end. Try to modify as per your use case requirements.

    import os
    import requests
    from azure.cognitiveservices.speech import SpeechConfig, SpeechRecognizer, AudioConfig, ResultReason
    # Azure Cognitive Services Speech subscription key and region
    speech_key = "YOUR_SPEECH_KEY"
    service_region = "YOUR_SPEECH_REGION"
    # Azure Translator Text API endpoint and key
    endpoint = "https://api.cognitive.microsofttranslator.com"
    path = "/translate?api-version=3.0"
    translation_key = "YOUR_TRANSLATION_KEY"
    location = "TRANSLATION_REGION"
    # Initialize speech recognizer
    speech_config = SpeechConfig(subscription=speech_key, region=service_region)
    audio_config = AudioConfig(filename="C://Users//Downloads//audio1.wav")
    recognizer = SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    # Fetch the updated transcript
    def get_updated_transcript():
        result = recognizer.recognize_once()
        if result.reason == ResultReason.RecognizedSpeech:
            return result.text
        else:
            print("No speech could be recognized")
            return None
    # Translate the updated transcript
    def translate_text(text, target_language="es"):  # Target language is Spanish (es) by default
        headers = {
            "Ocp-Apim-Subscription-Key": translation_key,
            "Ocp-Apim-Subscription-Region": location,
            "Content-Type": "application/json"
        }
        body = [{
            "text": text
        }]
        request = requests.post(f"{endpoint}{path}&to={target_language}", headers=headers, json=body)
        if request.status_code == 200:
            response = request.json()
            return response[0]["translations"][0]["text"]
        else:
            print(f"Error: {request.status_code} - {request.text}")
            return None
    # Main function to update and translate
    def main():
        updated_transcript = get_updated_transcript()
        if updated_transcript:
            translation = translate_text(updated_transcript)
            if translation:
                print("Updated Translation:", translation)
    if __name__ == "__main__":
        main()
    

    Output:

    User's image

    I hope this helps. Thank you.

    0 comments No comments