Spaces:
Sleeping
Sleeping
File size: 2,350 Bytes
9d3162f |
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 |
from googletrans import Translator, constants
from pprint import pprint
#import library
import warnings
#from gtts import gTTS
import os
import time
import pyttsx3
warnings.filterwarnings("ignore")
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import speech_recognition as sr
class CaesarLangTranslate:
def __init__(self) -> None:
self.translator = Translator()
@classmethod
def all_languages():
print("Total supported languages:", len(constants.LANGUAGES))
print("Languages:")
pprint(constants.LANGUAGES)
def translate(self,text,src="fr",lang="en",verbose=False):
translation = self.translator.translate(text,src=src,dest=lang)
print(f"{translation.origin} ({translation.src}) --> {translation.text} ({translation.dest})")
if verbose == True:
pprint(translation.extra_data)
return translation.origin,translation.text,translation.dest
# Initialize recognizer class (for recognizing the speech)
engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
engine.setProperty('voice',voices[1].id)
recognizer = sr.Recognizer()
def speak(text,whisper_mode=None):
if whisper_mode == 0:
engine.say(text)
engine.runAndWait()
def caesar_recognition(language="en-US"):
r = sr.Recognizer()
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source,duration=1)
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio,language=language)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said.lower()
WAKE = "hello caesar"
while True:
print("Listening")
text = caesar_recognition()
if WAKE in text:
print("How can I help you sir?")
speak("How can I help you sir?",0)
text = caesar_recognition()
TRANSLATION_MODE = "translate"
if TRANSLATION_MODE in text:
print("What is your translation?")
speak("What is your translation?",0)
text = caesar_recognition(language="fr-FR")
caesarlangtranslate = CaesarLangTranslate()
tranlationinfo = caesarlangtranslate.translate(text)
print(tranlationinfo)
speak(tranlationinfo[1],0)
else:
print(text) |