Mandy00 commited on
Commit
55cd0e8
·
verified ·
1 Parent(s): 14b93d1

Create translate.py

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