forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_translator.py
88 lines (73 loc) · 3.34 KB
/
language_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
#install speech_recognition , translate and pyttsx3 libraries
#make sure there are no microphone or hardware issues
import speech_recognition as sr
import pyttsx3
from translate import Translator
# Initialize the TTS engine
engine = pyttsx3.init()
# Function to translate using translate-python
def translate_text(text, target_language):
try:
translator = Translator(to_lang=target_language)
translation = translator.translate(text)
return translation
except Exception as e:
return f"Sorry, the translation service encountered an error: {e}"
# Function to speak the translated text
def speak(text):
engine.say(text)
engine.runAndWait()
# Function to capture voice commands and convert speech to text
def voice_to_text():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening for your command...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
print(f"Captured voice command: {command}")
return command
except sr.UnknownValueError:
print("Sorry, I didn't understand that.")
return "Sorry, I didn't understand that."
except sr.RequestError:
print("Speech recognition service is unavailable.")
return "Sorry, the speech recognition service is not available."
# Function to process translation commands via voice
def process_translation_command(command):
if "translate" in command.lower():
try:
print(f"Command received for translation: {command}")
# Parse the command to extract the text and target language
if "'" not in command or "to " not in command:
speak("Please provide a valid command, for example: Translate 'Hello' to French.")
return
text_to_translate = command.split("'")[1]
target_language = command.split("to ")[1].strip().lower()
# Provide feedback on extracted text and language
print(f"Text to translate: {text_to_translate}")
print(f"Target language: {target_language}")
# Perform the translation
translated_text = translate_text(text_to_translate, target_language)
print(f"Translated Text: {translated_text}")
speak(f"The translation is: {translated_text}")
except IndexError:
speak("Sorry, I couldn't process the command format. Please try again.")
except Exception as e:
speak(f"Sorry, I couldn't process that translation: {e}")
print(f"Error: {e}")
else:
speak("Please give a translation command that includes 'Translate'.")
# Main function for the voice assistant
if __name__ == "__main__":
speak("Hello! I'm your translation assistant. How can I help you?")
while True:
command = voice_to_text()
print(f"Command received: {command}")
if "exit" in command.lower() or "stop" in command.lower():
speak("Goodbye!")
break
elif command.lower() == "sorry, i didn't understand that.":
continue # If speech recognition fails, re-listen for a command
else:
process_translation_command(command)