-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-side.py
More file actions
78 lines (62 loc) · 2.89 KB
/
client-side.py
File metadata and controls
78 lines (62 loc) · 2.89 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
import tkinter as tk
from tkinter import scrolledtext
import requests
import json
from threading import Thread
class ChatbotClient:
def __init__(self, root):
self.root = root
self.root.title("DeepSeek Chatbot nih cuyy")
self.setup_ui()
base_url = "YOUR_SERVER_URL_HERE"
self.server_url = base_url.rstrip('/') + '/chat'
def setup_ui(self):
# Chatdisplay
self.chat_display = scrolledtext.ScrolledText(self.root, wrap=tk.WORD, width=50, height=20)
self.chat_display.pack(padx=10, pady=10, expand=True, fill='both')
# Inputframe
input_frame = tk.Frame(self.root)
input_frame.pack(padx=10, pady=(0, 10), fill='x')
self.input_field = tk.Entry(input_frame)
self.input_field.pack(side='left', expand=True, fill='x')
self.input_field.bind("<Return>", lambda e: self.send_message())
send_button = tk.Button(input_frame, text="Kirim", command=self.send_message)
send_button.pack(side='right', padx=(10, 0))
def send_message(self):
message = self.input_field.get().strip()
if not message:
return
self.input_field.delete(0, tk.END)
self.display_message(f"Anda: {message}")
Thread(target=self.send_request, args=(message,), daemon=True).start()
def send_request(self, message):
try:
print(f"Sending request to: {self.server_url}")
response = requests.post(
self.server_url,
json={"prompt": message},
timeout=60,
headers={"Content-Type": "application/json"}
)
if response.status_code == 404:
self.root.after(0, self.display_message,
"Bot: Error: Server endpoint not found. Please check the URL configuration.")
return
response.raise_for_status()
bot_response = response.json().get('response', 'Error: No response from server')
self.root.after(0, self.display_message, f"Bot: {bot_response}")
except requests.exceptions.ConnectionError:
self.root.after(0, self.display_message,
"Bot: Error: Could not connect to server. Please check your internet connection and the server URL.")
except requests.exceptions.Timeout:
self.root.after(0, self.display_message,
"Bot: Error: Request timed out. Please try again.")
except Exception as e:
self.root.after(0, self.display_message, f"Bot: Error: {str(e)}")
def display_message(self, message):
self.chat_display.insert(tk.END, message + "\n")
self.chat_display.see(tk.END)
if __name__ == "__main__":
root = tk.Tk()
app = ChatbotClient(root)
root.mainloop()