File size: 2,237 Bytes
af0c0e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
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

# Initialize the voice recognizer
r = sr.Recognizer()

def play_speech(text):
    # Voice syntesizer initiation
    converter = VoiceSynthesizer()
    converter.synthesize_speech(text)

    # Initialize the mixer module
    pygame.mixer.init()

    # Load the mp3 file
    pygame.mixer.music.load("artifacts/Audio.mp3")
    logging.info("Generated audio file loaded")

    # Play the loaded mp3 file
    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.")

# Function to convert text to
# speech
def SpeakText(command):
    # Initialize the engine
    engine = pyttsx3.init()
    engine.say(command) 
    engine.runAndWait()

def listen():
    # obtain audio from the microphone
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        audio = r.listen(source)

    # recognize speech using Google Speech Recognition
    try:
        text = r.recognize_google(audio)
        print("Google Speech Recognition thinks you said " + text)

        return text
    
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

def save_output(output_data, output_dir="artifacts"):
    Path(output_dir).mkdir(exist_ok=True)
    
    # Save text response
    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.")

def load_output(output_dir="artifacts"):   
    # Load text response
    with open(f"{output_dir}/response.txt", "r") as f:
        answer = f.read()
    logging.info("Stored text loaded.")

    return answer