This Python program uses the tkinter library to create a chat interface between two people. The program loads a pre-written conversation between two characters and displays it in a GUI. Additionally, it utilizes text-to-speech technology to speak the messages out loud as they are displayed.
The GUI includes a text box where users can type their responses, and a button to submit the response to the conversation. The program then generates a response based on the user's input using the OpenAI API, and adds it to the conversation.
The conversation between the two characters is pre-written in a separate file, and the program loads it when it is run. The user can continue the conversation as long as they like, with the program generating new responses based on the conversation history.
Overall, this program provides an interactive and engaging chat experience using Python and the OpenAI API.
- Python 3.x
- tkinter
- PIL
- pyttsx3
- dotenv
- Clone the repository to your local machine.
- Create a new virtual environment for the project and activate it.
- Install the dependencies using
pip install -r requirements.txt
. - Create an
.env
from the.env.example
provided. - Add your OpenAI API key to a
.env
file in the root directory. - Run the program using
python chat_interface.py
.
launch.py
: The main file that implements the chat interface.data/*
: Contains test dialogues and prompts for the program to use.openai_api.py
: Defines the function that calls the OpenAI API to get conversation data.
-
The
ImageLabel
class is a custom tkinter label that can display animated gifs. -
The
ChatInterface
class handles the GUI and the conversation. -
The
send_dialogue
method sends the conversation messages and displays them on the GUI. -
The
print_message
method displays the messages in the text box. -
The
speak_message
method uses text-to-speech to speak the messages out loud. -
The
main
method loads the test dialogues or gets the dialogues from the OpenAI API and creates the main window and chat interface. -
openai_request(language)
that takes a language parameter and returns a dictionary with two lists: the sender's and receiver's response lists, which are generated by having a conversation between two fictional characters, Cartman and Kyle, about the Harry Potter universe. The conversation is carried out by using a prompt-based approach where the initial prompts are defined in theinit_prompts.py
file. The function uses thedelete_line_break()
function to remove line breaks from the response lists and theopenai_connection()
function to make requests to the OpenAI API with retries. Theopenai_request()
function generates the responses from Cartman and Kyle iteratively by concatenating their prompts with each other's responses until a specified number of iterations is reached.
We have an initial prompt for both characters, which will have a brief description providing context for their personality and situation. As the conversation progresses, the prompt accumulates context from the responses and is sent in its entirety with each iteration.
sequenceDiagram
Cartman ->> Kyle: Hello Kyle, how are you?
Kyle--x Cartman : I am good thanks!
Kyle-->Cartman : Checking responses...
And this will produce a flow chart:
graph TB
A[OpenAI API]
A --> C(Cartman prompt)
B --> A
C --> A
B --> B
C --> C
A --> B(Kyle prompt)
B --> D{Kyle prompt}
C --> D(TkInter UI)
This chatbot was created by Fernando Gonzalez (nandodev) as a little experiment with the OpenAI API. Feel free to Fork or Clone the repository.