This code is not opening the app: “””
Import necessary modules – openai module will help to connect with local server, gtts will convert text to speech and playsound will play the audio output from the assistant response.
“””
import os
from tkinter import *
from gtts import gTTS
from playsound import playsound
from autogen import OpenAIWrapper
OPENAI_API_KEY=”1111111111111111″
# Common configuration for Lilith
llm_config = {
“model”: “Lilith”,
“base_url”: “http://127.0.0.1:5151/v1/”,
“api_key”: “None-needed”,
“timeout”: 6000, # Set your default timeout value here
“temperature”: 0, # Set your default temperature value here
“cache_seed”: 42 # Set your default cache_seed value here
}
# OpenAIWrapper instantiation for Lilith
client = OpenAIWrapper(config_list=[llm_config])
def speak(text): # Using gtts convert text into speech and playsound play the audio output.
tts = gTTS(text=text, lang=’en’)
filename = ‘voice.mp3’
tts.save(filename)
playsound(filename)
os.remove(filename)
root = Tk() # Create a GUI window using Tkinter
root.geometry(“400×500″)
text_speech = Text(root, height=2, width =16) # Define the text widget for displaying assistant’s response.
text_speech.pack()
def submit(): # This function is activated when user clicks on ‘Submit’. It gets the user input and sends it to LLM via OpenAI client.
text_input = text_entry.get()
completion = client.chat.completions.create(
model=”local-model”, # this field is currently unused
messages=[{“role”: “user”, “content”: text_input}],
temperature=0.7,
)
assistant_text = completion[“choices”][0][“message”][“content”].strip()
speak(assistant_text) # The assistant’s response is converted to speech using gtts and playsound plays it out for the user.
text_speech.insert(END, f’nYou: {text_input}nAssistant: {assistant_text}’)
def clear(): # This function clears the input field in case if user wants to start a new conversation.
text_entry.delete(0, END)
button = Button(root, text=”Submit”, command=submit) # Define ‘Submit’ button.
button.pack()
clearButton = Button(root, text=”Clear”, command=clear) # Define ‘Clear’ button to clear the input field.
clearButton.pack()
text_entry = Entry(root) # This is where user inputs his query or statement.
text_entry.pack()
The code you provided seems to be missing a few crucial components. It appears that you’re trying to create a simple GUI application where a user inputs text, and the Lilith model responds both in text and audio. Here are some suggestions to help you fix the issues:
1. **Initialize Tkinter Window:**
Ensure that you start the Tkinter event loop by adding the line `root.mainloop()` at the end of your script. This is necessary for the GUI to be displayed and respond to user interactions.
“`python
root.mainloop()
“`
2. **Import Missing Modules:**
Make sure you have installed the required modules by running:
“`
pip install gtts playsound
“`
3. **Correct OpenAI Configuration:**
The `OpenAIWrapper` configuration should include the API key. Update the configuration as follows:
“`python
llm_config = {
“model”: “Lilith”,
“base_url”: “http://127.0.0.1:5151/v1/”,
“api_key”: OPENAI_API_KEY, # Use the API key you obtained from OpenAI
“timeout”: 6000,
“temperature”: 0,
“cache_seed”: 42
}
“`
4. **Add Entry for User Input:**
It seems you are using `text_entry` for user input, but it’s not defined in the code. Add the following line to create an entry widget for user input:
“`python
text_entry = Entry(root)
text_entry.pack()
“`
Ensure that you call `text_entry.get()` in the `submit` function to retrieve user input.
5. **Handle Missing Dependencies:**
Ensure that the necessary dependencies and files are present. The `autogen` module and Lilith model should be available. The Lilith model needs to be running on the specified URL.
After making these adjustments, your GUI application should function as intended, allowing users to input text, receive responses both in the GUI and as speech.
This is suposed to be a TTS-STT system
I understand that you’re working on a Text-to-Speech (TTS) and Speech-to-Text (STT) system. Thank you for clarifying. To make your system work as intended, you’ll need to integrate a Speech-to-Text component. Here are the steps to modify your code accordingly:
1. **Install SpeechRecognition Module:**
You’ll need the SpeechRecognition module for Speech-to-Text. Install it using:
“`bash
pip install SpeechRecognition
“`
2. **Modify Code for Speech-to-Text:**
Add the SpeechRecognition code to recognize speech input. Replace your existing `submit` function with the modified one below:
“`python
import speech_recognition as sr
recognizer = sr.Recognizer()
def submit():
with sr.Microphone() as source:
print(“Say something:”)
audio = recognizer.listen(source)
try:
user_input = recognizer.recognize_google(audio)
text_entry.delete(0, END) # Clear the existing text in the entry widget
text_entry.insert(END, user_input) # Display the recognized speech in the entry widget
completion = client.chat.completions.create(
model=”local-model”,
messages=[{“role”: “user”, “content”: user_input}],
temperature=0.7,
)
assistant_text = completion[“choices”][0][“message”][“content”].strip()
speak(assistant_text)
text_speech.insert(END, f’nYou: {user_input}nAssistant: {assistant_text}’)
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}”)
“`
This modification uses the `recognizer` to capture audio input from the microphone and converts it to text using Google Speech Recognition.
3. **Modify GUI Components:**
Add a button for voice input. Update your GUI components accordingly:
“`python
voice_button = Button(root, text=”Voice Input”, command=submit)
voice_button.pack()
“`
This button triggers the `submit` function when clicked, capturing voice input from the user.
After making these changes, your system should allow users to input text through the entry widget or by clicking the “Voice Input” button, and it will provide responses in both text and speech.