Spaces:
Sleeping
Sleeping
File size: 3,011 Bytes
55cd0e8 35c4674 55cd0e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import os
from gtts import gTTS
from speechmatics.models import ConnectionSettings
from speechmatics.batch_client import BatchClient
from httpx import HTTPStatusError
# Supported input and output languages
LANGUAGES = {
"bg": "bulgarian",
"ca": "catalan",
"zh-cn": "chinese (simplified)",
"hr": "croatian",
"cs": "czech",
"da": "danish",
"nl": "dutch",
"en": "english",
"et": "estonian",
"fi": "finnish",
"fr": "french",
"gl": "galician",
"de": "german",
"el": "greek",
"hi": "hindi",
"hu": "hungarian",
"id": "indonesian",
"it": "italian",
"ja": "japanese",
"ko": "korean",
"lv": "latvian",
"lt": "lithuanian",
"ms": "malay",
"no": "norwegian",
"pl": "polish",
"pt": "portuguese",
"ro": "romanian",
"ru": "russian",
"sk": "slovak",
"sl": "slovenian",
"es": "spanish",
"sv": "swedish",
"tr": "turkish",
"uk": "ukrainian",
"vi": "vietnamese",
}
# Function to translate and create audio file
def translate_and_speak(input_text, input_language, output_language):
# Get Speechmatics API key from environment variable
API_KEY = os.environ.get("thx2xVuc7jjNpoUipaFTGXDjzQRVNHot")
if not API_KEY:
raise ValueError("SPEECHMATICS_API_KEY environment variable not set!")
# Perform transcription and translation using Speechmatics
settings = ConnectionSettings(
url="https://asr.api.speechmatics.com/v2",
auth_token=thx2xVuc7jjNpoUipaFTGXDjzQRVNHot,
)
conf = {
"type": "transcription",
"transcription_config": {
"language": input_language
},
"translation_config": {
"target_languages": [output_language]
}
}
# Open the client using a context manager
with BatchClient(settings) as client:
try:
# Submit the job using the provided text
job_id = client.submit_job(
content=input_text,
transcription_config=conf,
)
# Wait for the transcription to complete
transcript = client.wait_for_completion(job_id, transcription_format='json-v2')
translation = ""
for translated_segment in transcript["translations"][output_language]:
translation += translated_segment["content"] + " "
# Create an audio file from the translated text
audio_file = "translated_audio.mp3"
tts = gTTS(text=translation, lang=output_language)
tts.save(audio_file)
# Return the translation and the path to the audio file
return translation, audio_file
except HTTPStatusError as e:
if e.response.status_code == 401:
return 'Invalid API key - Check your SPEECHMATICS_API_KEY secret!', None
elif e.response.status_code == 400:
return e.response.json()['detail'], None
else:
raise e |