Skip to content

Commit

Permalink
added two files
Browse files Browse the repository at this point in the history
  • Loading branch information
HemanthSai7 committed Sep 6, 2021
1 parent 3ef447b commit b2e343e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COG_SERVICE_KEY=your_cognitive_services_key
COG_SERVICE_REGION=your_cognitive_services_location
75 changes: 75 additions & 0 deletions translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from dotenv import load_dotenv
from datetime import datetime
import os

# Import namespaces
import azure.cognitiveservices.speech as speech_sdk


def main():
try:
global speech_config
global translation_config

# Get Configuration Settings
load_dotenv()
cog_key = os.getenv('COG_SERVICE_KEY')
cog_region = os.getenv('COG_SERVICE_REGION')

# Configure translation
translation_config = speech_sdk.translation.SpeechTranslationConfig(
cog_key, cog_region)
translation_config.speech_recognition_language = 'en-US'
translation_config.add_target_language('fr')
translation_config.add_target_language('es')
translation_config.add_target_language('hi')
translation_config.add_target_language('de')
print('ready to translate from',
translation_config.speech_recognition_language)

# Configure speech
speech_config = speech_sdk.SpeechConfig(cog_key, cog_region)

# Get user input
targetLanguage = ''
while targetLanguage != 'quit':
targetLanguage = input(
'\nEnter a target language\n fr = French\n es = Spanish\n hi = Hindi\n de = German\n Enter anything else to stop\n').lower()
if targetLanguage in translation_config.target_languages:
Translate(targetLanguage)
else:
targetLanguage = 'quit'

except Exception as ex:
print(ex)


def Translate(targetLanguage):
translation = ''

# Translate speech
audio_config = speech_sdk.AudioConfig(use_default_microphone=True)
translator = speech_sdk.translation.TranslationRecognizer(
translation_config, audio_config)
print("Speak now...")
result = translator.recognize_once_async().get()
print(f'Translating {result.text}')
translation = result.translations[targetLanguage]
print(translation)

# Synthesize translation
voices = {
"fr": "fr-FR-Julie",
"es": "es-ES-Laura",
"hi": "hi-IN-Kalpana",
"de": "de-DE-HeddaRUS"
}
speech_config.speech_synthesis_voice_name = voices.get(targetLanguage)
speech_synthesizer = speech_sdk.SpeechSynthesizer(speech_config)
speak = speech_synthesizer.speak_text_async(translation).get()
if speak.reason != speech_sdk.ResultReason.SynthesizingAudioCompleted:
print(speak.reason)


if __name__ == "__main__":
main()

0 comments on commit b2e343e

Please sign in to comment.