Jul-10-2023, 08:06 AM
Hi! I'm still quite new to Python, so apologies if I'm missing something really basic..
I have this code, which has speech recognition and then sends recognized text to an API which will give a response (chatbot).
Now there are some things wrong in my flow. Where the biggest issue is that I want to remain listening to TCP commands while get_speech() is active.
As you can see in code, I'm listening to TCP-commands; with the "openMic" command you should be able to basically start the chatbot.
This will gives a welcome message from the API and then listens to your input. With the "closeMic" command I want to stop the chatbot <-- which doesn't work.
The get_speech(), function is running infinitely. The function it self runs perfect. However, I always want to be able to stop the function with the closeMic command.
It is also important that I don't want to break the program, since the user should be able to start the function again with the "openMic" command.
I've tried several things like adding booleans, passing booleans.. But I'm stuck.
Is there anyone who can help me out?
I've hidden my API and charID
I have this code, which has speech recognition and then sends recognized text to an API which will give a response (chatbot).
Now there are some things wrong in my flow. Where the biggest issue is that I want to remain listening to TCP commands while get_speech() is active.
As you can see in code, I'm listening to TCP-commands; with the "openMic" command you should be able to basically start the chatbot.
This will gives a welcome message from the API and then listens to your input. With the "closeMic" command I want to stop the chatbot <-- which doesn't work.
The get_speech(), function is running infinitely. The function it self runs perfect. However, I always want to be able to stop the function with the closeMic command.
It is also important that I don't want to break the program, since the user should be able to start the function again with the "openMic" command.
I've tried several things like adding booleans, passing booleans.. But I'm stuck.
Is there anyone who can help me out?
I've hidden my API and charID
import requests import json import socket import base64 import speech_recognition as sr import pyttsx3 from playsound import playsound TCP_IP = "127.0.0.1" TCP_PORT = 9999 url = "https://api.convai.com/character/getResponse" def get_speech(recognizer, microphone): with microphone as source: print("Adjusting for ambient noise...") recognizer.adjust_for_ambient_noise(source) print("Listening for your voice...") audio = recognizer.listen(source) try: print("Recognizing your speech...") return recognizer.recognize_google(audio) except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") except sr.RequestError as e: print(f"Could not request results from Google Speech Recognition service; {e}") def get_response(sentence): payload = { 'userText': [sentence], 'charID': '', 'sessionID': '-1', 'voiceResponse': 'True' } headers = { 'CONVAI-API-KEY': '' } response = requests.request("POST", url, headers=headers, data=payload) data = response.json() character_response = data["text"] print("Text response: " + data["text"]) decode_string = base64.b64decode(data["audio"]) with open('audioResponse.wav', 'wb') as f: f.write(decode_string) def main(): recognizer = sr.Recognizer() microphone = sr.Microphone() server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((TCP_IP, TCP_PORT)) server_socket.listen(1) print(f"Server listening on {TCP_IP}:{TCP_PORT}") chatbotEnabled = False connected = False client_socket, addr = server_socket.accept() connected = True print(f"Client connected: {addr}") data = client_socket.recv(1024).decode().strip() while connected: if data == "openMic": chatbotEnabled = True # user_input = get_speech(recognizer, microphone, chatbotEnabled) elif data == "closeMic": chatbotEnabled = False print("chatbot disabled") get_response("Bye") playsound("audioResponse.wav") continue elif data == "stopProgram": connected = False break if chatbotEnabled: get_response("Please introduce yourself") playsound("audioResponse.wav") user_input = get_speech(recognizer, microphone) if user_input is None: continue elif user_input in ["quit", "exit", "bye"]: break elif not user_input is None: response = get_response(user_input) playsound("audioResponse.wav") if __name__ == "__main__": main()