Window Not Opening PySimpleGUI Python

I have been trying to make a ChatGPT extension that creates a window which takes in your voice as input and prints the response on the window and also says it using text-to-speech. When I run it, it takes in input AND gives a response, but no window gets opened, and no messages are printed on the window.

I didn’t know what to do, so here is the code:

# Python program to translate
# speech to text and text to speech
import os 
import sys
import speech_recognition as sr
import pyttsx3 
import openai
import PySimpleGUI as sg

 
# Initialize the recognizer 
r = sr.Recognizer() 
 
# Function to convert text to
# speech
def SpeakText(command):
     
    # Initialize the engine
    engine = pyttsx3.init()
    engine.say(command) 
    engine.runAndWait()
class ChatApp:
    def __init__(self, api_key):
        openai.api_key = api_key
        self.messages = [
            {"role": "system", "content": "You are a helpful assistant."},
        ]

    def chat(self, prompt):
        self.messages.append({"role": "user", "content": prompt})
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=self.messages
        )
        self.messages.append({"role": "assistant", "content": response.choices[0].message["content"]})
        return response.choices[0].message["content"]

app = ChatApp("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")

layout = [
    [sg.Text("Vocal AI", text_color = "black")],
    [sg.Text("")],
    [sg.Text("", key = "input", text_color = "black")],
    [sg.Text("", key = "output", text_color = "black")],
    [sg.Text("")],
    [sg.Text("", key = "error", text_color = "red")]
]

window = sg.Window("Vocal AI v2.3 non-Vocal", layout)


# Loop infinitely for user to
# speak
SpeakText("Waiting for question...")
while(1):    
     
    # Exception handling to handle
    # exceptions at the runtime
    try:
         
        # use the microphone as source for input.
        with sr.Microphone() as source2:
             
            # wait for a second to let the recognizer
            # adjust the energy threshold based on
            # the surrounding noise level 
            r.adjust_for_ambient_noise(source2, duration=0.2)
             
            #listens for the user's input 
            audio2 = r.listen(source2)
             
            # Using google to recognize audio
            MyText = r.recognize_google(audio2)
            MyText = MyText.lower()
            print("<YOU> {0}".format(MyText))
            event, values = window.read()            

            if MyText == "cancel" or event == sg.WIN_CLOSED:
                SpeakText("Closing Vocal AI")
                window.close()
                sys.exit(0)
            elif MyText == "refresh":
                clear = lambda: os.system('cls')
                clear()
            try:
                api_key = "I REMOVED THE API KEY FOR SAFETY"
                prompt = MyText
                window["input"].update("<User> " + prompt)
                SpeakText(MyText)
                response = app.chat(prompt)
                window["output"].update("<ChatGPT> {0}".format(response))
                SpeakText(response)
            except Exception as e:
                window["error"].update("Could not obtain response from Chat-GPT : {0}".format(e))
                SpeakText("Could not obtain response from Chat-GPT : {0}".format(e))
                continue
            
             
    except sr.RequestError as e:
        window["error"].update("Could not request results; {0}".format(e))
        SpeakText("Could not request results; {0}".format(e))
         
    except sr.UnknownValueError:
        window["error"].update("Unknown Value")
        SpeakText("unknown value")

I was expecting that a window would pop up, and when I said something, my sentence and the response to it would be printed on the window.

  • 1

    Should trace your code by adding some prints to find where and how it executed and what the root cause for no GUI displayed.

    – 




  • Could you tell me where I maybe should place the prints? @JasonYang

    – 

  • See following post.

    – 

Most of time, it is caused by the programming logic.

Should trace your code by adding some prints to find where and how it executed and what the root cause for no GUI displayed.

Example for lot of prints added to trace the execution.

# Python program to translate
# speech to text and text to speech
import os
import sys
import speech_recognition as sr
import pyttsx3
import openai
import PySimpleGUI as sg


# Initialize the recognizer
r = sr.Recognizer()

# Function to convert text to
# speech
def SpeakText(command):

    # Initialize the engine
    engine = pyttsx3.init()
    engine.say(command)
    engine.runAndWait()

class ChatApp:
    def __init__(self, api_key):
        openai.api_key = api_key
        self.messages = [
            {"role": "system", "content": "You are a helpful assistant."},
        ]

    def chat(self, prompt):
        self.messages.append({"role": "user", "content": prompt})
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=self.messages
        )
        self.messages.append({"role": "assistant", "content": response.choices[0].message["content"]})
        return response.choices[0].message["content"]

app = ChatApp("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")

layout = [
    [sg.Text("Vocal AI", text_color = "black")],
    [sg.Text("")],
    [sg.Text("", key = "input", text_color = "black")],
    [sg.Text("", key = "output", text_color = "black")],
    [sg.Text("")],
    [sg.Text("", key = "error", text_color = "red")]
]

window = sg.Window("Vocal AI v2.3 non-Vocal", layout)


# Loop infinitely for user to
# speak
SpeakText("Waiting for question...")
while(1):

    # Exception handling to handle
    # exceptions at the runtime

    print(">>> While Loop")

    try:

        # use the microphone as source for input.
        with sr.Microphone() as source2:

            # wait for a second to let the recognizer
            # adjust the energy threshold based on
            # the surrounding noise level
            r.adjust_for_ambient_noise(source2, duration=0.2)

            #listens for the user's input
            print(">>> Listening")
            audio2 = r.listen(source2)

            # Using google to recognize audio
            MyText = r.recognize_google(audio2)
            print(">>> Recognize")
            MyText = MyText.lower()
            print("<YOU> {0}".format(MyText))

            print(">>> Window event read")
            event, values = window.read()

            print(">>> Window event check")
            if MyText == "cancel" or event == sg.WIN_CLOSED:
                print(">>> Window closing")
                SpeakText("Closing Vocal AI")
                window.close()
                sys.exit(0)
            elif MyText == "refresh":
                clear = lambda: os.system('cls')
                clear()
                print(">>> Refreshed")
            try:
                print(">>> Remove API key")
                api_key = "I REMOVED THE API KEY FOR SAFETY"
                prompt = MyText
                print(">>> Update GUI and speech")
                window["input"].update("<User> " + prompt)
                SpeakText(MyText)
                response = app.chat(prompt)
                window["output"].update("<ChatGPT> {0}".format(response))
                SpeakText(response)
                print(">>> Update done")
            except Exception as e:
                print(f">>> Exception {e}")
                window["error"].update("Could not obtain response from Chat-GPT : {0}".format(e))
                SpeakText("Could not obtain response from Chat-GPT : {0}".format(e))
                continue


    except sr.RequestError as e:
        print(f">>> Exception {e}")
        window["error"].update("Could not request results; {0}".format(e))
        SpeakText("Could not request results; {0}".format(e))

    except sr.UnknownValueError as e:
        print(f">>> Exception {e}")
        window["error"].update("Unknown Value")
        SpeakText("unknown value")

Leave a Comment