Python Forum
Controlling program with TCP commands
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Controlling program with TCP commands
#1
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

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()
Reply
#2
Actually, you are not listening for TCP commands. You listen for 1 TCP command.

You'll want to put this inside the while loop.
 data = client_socket.recv(1024).decode().strip()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Controlling text-to-speech pauses with pyttsx3 pmac1300 4 8,990 Mar-14-2022, 06:47 AM
Last Post: Coricoco_fr
  Controlling what get outputted to stdout when running external commands Daring_T 4 4,783 Jan-30-2022, 05:40 PM
Last Post: bowlofred
  Looking For Help On Controlling Philips Hue From PyCharm RickyRay333 4 6,330 Aug-24-2020, 08:33 PM
Last Post: Nickd12
  Controlling Python Program From Django Front End sourabhjaiswal92 3 4,756 May-21-2018, 06:07 AM
Last Post: wavic
  controlling multiple server simultaneously. caligola 3 5,285 May-11-2018, 05:44 PM
Last Post: wavic
  Controlling trailing zeros with rounding? RedSkeleton007 1 29,084 Jan-25-2018, 07:23 AM
Last Post: j.crater

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.