|
import os |
|
import sys |
|
from pathlib import Path |
|
import speech_recognition as sr |
|
import pyttsx3 |
|
import pygame |
|
from src.components.voicesynth import VoiceSynthesizer |
|
from src.logger.logger import logging |
|
from src.exception.exception import customexception |
|
|
|
|
|
r = sr.Recognizer() |
|
|
|
def play_speech(text): |
|
try: |
|
|
|
converter = VoiceSynthesizer() |
|
converter.synthesize_speech(text) |
|
|
|
|
|
pygame.mixer.init() |
|
|
|
|
|
pygame.mixer.music.load("artifacts/Audio.mp3") |
|
logging.info("Generated audio file loaded") |
|
|
|
|
|
pygame.mixer.music.play() |
|
pygame.mixer.music.get_endevent() |
|
while pygame.mixer.music.get_busy(): |
|
continue |
|
pygame.mixer.music.load("artifacts/Audio_copy.mp3") |
|
os.remove("artifacts/Audio.mp3") |
|
logging.info("Created audio file removed for entry of new file.") |
|
|
|
except Exception as e: |
|
raise customexception(e,sys) |
|
|
|
|
|
|
|
def SpeakText(command): |
|
try: |
|
|
|
engine = pyttsx3.init() |
|
engine.say(command) |
|
engine.runAndWait() |
|
except Exception as e: |
|
raise customexception(e,sys) |
|
|
|
def listen(): |
|
|
|
r = sr.Recognizer() |
|
with sr.Microphone() as source: |
|
print("Say something!") |
|
audio = r.listen(source) |
|
|
|
|
|
try: |
|
text = r.recognize_google(audio) |
|
print("Google Speech Recognition thinks you said " + text) |
|
|
|
return text |
|
|
|
except sr.UnknownValueError: |
|
raise ("Google Speech Recognition could not understand audio", sys) |
|
except sr.RequestError as e: |
|
raise customexception("Could not request results from Google Speech Recognition service; {0}".format(e), sys) |
|
except Exception as e: |
|
raise customexception(e, sys) |
|
|
|
def save_output(output_data, output_dir="artifacts"): |
|
try: |
|
Path(output_dir).mkdir(exist_ok=True) |
|
|
|
|
|
with open(f"{output_dir}/response.txt", "w") as f: |
|
f.write(output_data['response_text']) |
|
logging.info("Generated response stored in response.txt file in artifacts folder.") |
|
except Exception as e: |
|
raise customexception(e,sys) |
|
|
|
def load_output(output_dir="artifacts"): |
|
try: |
|
|
|
with open(f"{output_dir}/response.txt", "r") as f: |
|
answer = f.read() |
|
logging.info("Stored text loaded.") |
|
|
|
return answer |
|
except Exception as e: |
|
raise customexception(e,sys) |