Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.8 kB
1
Indexable
Never
import tkinter as tk
import openai

openai.api_key = "YOUR_API_KEY"

def get_response(text):
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt='User: ' + text,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    ).choices[0].text

    return response

class ChatBotGUI:
    def __init__(self, master):
        self.master = master
        self.master.title("ChatGPT Lookalike")

        self.user_input = tk.StringVar()
        self.user_input.set("Enter your message here...")

        self.input_field = tk.Entry(self.master, textvariable=self.user_input)
        self.input_field.pack()
        self.input_field.bind("<FocusIn>", self.clear_placeholder)
        self.input_field.bind("<FocusOut>", self.add_placeholder)
        self.input_field.bind("<Return>", self.send_message)

        self.scrollbar = tk.Scrollbar(self.master)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.conversation = tk.Text(self.master, yscrollcommand=self.scrollbar.set)
        self.conversation.pack()
        self.scrollbar.config(command=self.conversation.yview)

    def clear_placeholder(self, event):
        if self.user_input.get() == "Enter your message here...":
            self.user_input.set("")

    def add_placeholder(self, event):
        if self.user_input.get() == "":
            self.user_input.set("Enter your message here...")

    def send_message(self, event):
        user_message = self.user_input.get()
        self.conversation.insert(tk.END, "User: " + user_message + "\n")
        response = get_response(user_message)
        self.conversation.insert(tk.END, "Bot: " + response + "\n")
        self.user_input.set("")

if __name__ == "__main__":
    root = tk.Tk()
    chatbot_gui = ChatBotGUI(root)
    root.mainloop()