-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.py
340 lines (286 loc) · 7.96 KB
/
translator.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from dotenv import load_dotenv
from datetime import datetime
import os
from pathlib import Path
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
# Import namespaces
import azure.cognitiveservices.speech as speech_sdk
global targetLanguage
targetLanguage = ''
def gui():
# from tkinter import *
# Explicit imports to satisfy Flake8
OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path("./assets")
def relative_to_assets(path: str) -> Path:
return ASSETS_PATH / Path(path)
window = Tk()
window.geometry("843x491")
window.configure(bg="#FE724D")
canvas = Canvas(
window,
bg="#FE724D",
height=491,
width=843,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas.place(x=0, y=0)
button_image_1 = PhotoImage(
file=relative_to_assets("button_1.png"))
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=lambda: makeFR(targetLanguage),
relief="flat"
)
button_1.place(
x=591.6849365234375,
y=53.99999999999994,
width=214.3150634765625,
height=186.14793395996094
)
button_image_2 = PhotoImage(
file=relative_to_assets("button_2.png"))
button_2 = Button(
image=button_image_2,
borderwidth=0,
highlightthickness=0,
command=lambda: makeES(targetLanguage),
relief="flat"
)
button_2.place(
x=359.0,
y=53.99999999999994,
width=214.3150634765625,
height=186.14793395996094
)
button_image_3 = PhotoImage(
file=relative_to_assets("button_3.png"))
button_3 = Button(
image=button_image_3,
borderwidth=0,
highlightthickness=0,
command=lambda: makeHI(targetLanguage),
relief="flat"
)
button_3.place(
x=359.0,
y=257.2931518554687,
width=214.3150634765625,
height=186.14793395996094
)
button_image_4=PhotoImage(
file=relative_to_assets("button_4.png"))
button_4=Button(
image=button_image_4,
borderwidth=0,
highlightthickness=0,
command=lambda: makeDE(targetLanguage),
relief="flat"
)
button_4.place(
x=591.6849365234375,
y=257.2931518554687,
width=214.3150634765625,
height=186.14793395996094
)
canvas.create_rectangle(
37.0,
40.99999999999994,
306.0,
442.99999999999994,
fill="#FFFFFF",
outline="")
button_image_5=PhotoImage(
file=relative_to_assets("button_5.png"))
button_5=Button(
image=button_image_5,
borderwidth=0,
highlightthickness=0,
command=lambda: makeqt(targetLanguage),
relief="flat"
)
button_5.place(
x=57.0,
y=383.99999999999994,
width=230.0,
height=39.0
)
canvas.create_text(
54.0,
70.99999999999994,
anchor="nw",
text="Captraduire",
fill="#8A00BB",
font=("Comfortaa Regular", 34 * -1)
)
canvas.create_text(
60.0,
360.99999999999994,
anchor="nw",
text=" to End the program, click below\n",
fill="#000000",
font=("Comfortaa Regular", 13 * -1)
)
canvas.create_text(
58.0,
207.99999999999994,
anchor="nw",
text="Instructions :",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
61.0,
217.99999999999994,
anchor="nw",
text="",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
75.0,
269.99999999999994,
anchor="nw",
text="∘ Speak in English",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
75.0,
324.99999999999994,
anchor="nw",
text="∘ You're good to go",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
75.0,
235.99999999999994,
anchor="nw",
text="∘ Click on the desired",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
75.0,
249.99999999999994,
anchor="nw",
text=" language button",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
75.0,
290.99999999999994,
anchor="nw",
text="∘ Wait for the program to",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
75.0,
305.99999999999994,
anchor="nw",
text=" translate",
fill="#6D6D6D",
font=("FontAwesome5Brands Regular", 15 * -1)
)
canvas.create_text(
60.0,
125.99999999999994,
anchor="nw",
text="Welcome to Captraduire,",
fill="#FE724D",
font=("Comfortaa Regular", 18 * -1)
)
canvas.create_text(
60.0,
146.99999999999994,
anchor="nw",
text="your on the go",
fill="#FE724D",
font=("Comfortaa Regular", 18 * -1)
)
canvas.create_text(
60.0,
167.99999999999994,
anchor="nw",
text="translator.",
fill="#FE724D",
font=("Comfortaa Regular", 18 * -1)
)
window.resizable(False, False)
window.mainloop()
def main(targetLanguage):
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
targetLanguag=''
while targetLanguag != 'quit':
targetLanguag=gui()
except Exception as ex:
print(ex)
def makeFR(targetLanguage):
targetLanguage="fr"
Translate(targetLanguage)
return targetLanguage
def makeES(targetLanguage):
targetLanguage="es"
Translate(targetLanguage)
return targetLanguage
def makeHI(targetLanguage):
targetLanguage="hi"
Translate(targetLanguage)
return targetLanguage
def makeDE(targetLanguage):
targetLanguage="de"
Translate(targetLanguage)
return targetLanguage
def makeqt(targetLanguage):
return exit()
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(targetLanguage)