-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot_app.py
More file actions
88 lines (78 loc) · 3.2 KB
/
chatbot_app.py
File metadata and controls
88 lines (78 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import tkinter as tk
from tkinter import messagebox, scrolledtext
import requests
# Replace with your OpenAI API key
OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"
OPENAI_API_URL = "https://api.openai.com/v1/chat/completions"
class LoginWindow(tk.Tk):
def __init__(self):
super().__init__()
self.title("Login")
self.geometry("400x250")
self.resizable(False, False)
self.create_widgets()
def create_widgets(self):
tk.Label(self, text="Username:", font=("Arial", 14)).pack(pady=10)
self.username_entry = tk.Entry(self, font=("Arial", 14))
self.username_entry.pack(pady=5)
tk.Label(self, text="Password:", font=("Arial", 14)).pack(pady=10)
self.password_entry = tk.Entry(self, font=("Arial", 14), show="*")
self.password_entry.pack(pady=5)
tk.Button(self, text="Login", font=("Arial", 14), command=self.login).pack(pady=20)
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
# Simple check, you can add real authentication here
if username and password:
self.destroy()
ChatWindow(username)
else:
messagebox.showerror("Error", "Please enter both username and password.")
class ChatWindow(tk.Tk):
def __init__(self, username):
super().__init__()
self.title(f"Chatbot - {username}")
self.geometry("600x600")
self.resizable(True, True)
self.username = username
self.create_widgets()
def create_widgets(self):
self.chat_area = scrolledtext.ScrolledText(self, font=("Arial", 14), state='disabled', wrap='word')
self.chat_area.pack(padx=10, pady=10, fill='both', expand=True)
self.entry = tk.Entry(self, font=("Arial", 14))
self.entry.pack(padx=10, pady=10, fill='x')
self.entry.bind('<Return>', self.send_message)
tk.Button(self, text="Send", font=("Arial", 14), command=self.send_message).pack(pady=5)
def send_message(self, event=None):
user_message = self.entry.get().strip()
if not user_message:
return
self.entry.delete(0, tk.END)
self.append_chat(f"{self.username}: {user_message}")
bot_reply = self.get_bot_reply(user_message)
self.append_chat(f"Bot: {bot_reply}")
def append_chat(self, message):
self.chat_area.config(state='normal')
self.chat_area.insert(tk.END, message + '\n')
self.chat_area.config(state='disabled')
self.chat_area.see(tk.END)
def get_bot_reply(self, user_message):
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": user_message}
]
}
try:
response = requests.post(OPENAI_API_URL, headers=headers, json=data)
response.raise_for_status()
reply = response.json()['choices'][0]['message']['content']
return reply.strip()
except Exception as e:
return f"Error: {e}"
if __name__ == "__main__":
LoginWindow().mainloop()