Update client_chat.py
Browse files- client_chat.py +46 -78
client_chat.py
CHANGED
|
@@ -3,17 +3,17 @@ import requests
|
|
| 3 |
import json
|
| 4 |
from PyQt6.QtWidgets import (
|
| 5 |
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
|
| 6 |
-
QPushButton, QTextEdit, QLineEdit, QLabel, QComboBox
|
| 7 |
)
|
| 8 |
from PyQt6.QtCore import QTimer, Qt
|
| 9 |
-
from PyQt6.QtGui import QFont, QCursor, QIcon
|
| 10 |
|
| 11 |
API_BASE = "http://127.0.0.1:7860/api"
|
| 12 |
|
| 13 |
class ChatClient(QWidget):
|
| 14 |
-
def __init__(self):
|
| 15 |
super().__init__()
|
| 16 |
-
self.token =
|
| 17 |
self.ai_typing_timer = None
|
| 18 |
self.ai_text_buffer = ""
|
| 19 |
self.setWindowTitle("AI HUB Desktop Client")
|
|
@@ -21,41 +21,29 @@ class ChatClient(QWidget):
|
|
| 21 |
|
| 22 |
self.init_ui()
|
| 23 |
self.showMaximized()
|
| 24 |
-
self.setCursor(QCursor(Qt.CursorShape.
|
| 25 |
|
| 26 |
def init_ui(self):
|
| 27 |
self.layout = QVBoxLayout(self)
|
| 28 |
self.setLayout(self.layout)
|
| 29 |
|
| 30 |
-
# Login area
|
| 31 |
-
login_layout = QHBoxLayout()
|
| 32 |
-
self.email_input = QLineEdit()
|
| 33 |
-
self.email_input.setPlaceholderText("Email")
|
| 34 |
-
login_layout.addWidget(self.email_input)
|
| 35 |
-
|
| 36 |
-
self.password_input = QLineEdit()
|
| 37 |
-
self.password_input.setPlaceholderText("Password")
|
| 38 |
-
self.password_input.setEchoMode(QLineEdit.EchoMode.Password)
|
| 39 |
-
login_layout.addWidget(self.password_input)
|
| 40 |
-
|
| 41 |
-
self.login_button = QPushButton("Login")
|
| 42 |
-
self.login_button.clicked.connect(self.handle_login)
|
| 43 |
-
login_layout.addWidget(self.login_button)
|
| 44 |
-
|
| 45 |
-
self.signup_button = QPushButton("Signup")
|
| 46 |
-
self.signup_button.clicked.connect(self.handle_signup)
|
| 47 |
-
login_layout.addWidget(self.signup_button)
|
| 48 |
-
self.layout.addLayout(login_layout)
|
| 49 |
-
|
| 50 |
# Chat Display Area
|
| 51 |
self.chat_area = QTextEdit()
|
| 52 |
self.chat_area.setReadOnly(True)
|
| 53 |
-
self.chat_area.setFont(QFont("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
self.layout.addWidget(self.chat_area)
|
| 55 |
|
| 56 |
# Typing Indicator
|
| 57 |
self.typing_label = QLabel("")
|
| 58 |
-
self.typing_label.setStyleSheet("color:
|
| 59 |
self.layout.addWidget(self.typing_label)
|
| 60 |
|
| 61 |
# Input Area
|
|
@@ -63,15 +51,40 @@ class ChatClient(QWidget):
|
|
| 63 |
self.user_input = QLineEdit()
|
| 64 |
self.user_input.setPlaceholderText("Type your message here...")
|
| 65 |
self.user_input.returnPressed.connect(self.send_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
input_layout.addWidget(self.user_input)
|
| 67 |
|
| 68 |
self.send_button = QPushButton("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
self.send_button.clicked.connect(self.send_message)
|
| 70 |
input_layout.addWidget(self.send_button)
|
| 71 |
|
| 72 |
# Model selection
|
| 73 |
self.model_select = QComboBox()
|
| 74 |
self.model_select.addItems(["qwen", "deepseek"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
input_layout.addWidget(self.model_select)
|
| 76 |
|
| 77 |
self.layout.addLayout(input_layout)
|
|
@@ -79,7 +92,7 @@ class ChatClient(QWidget):
|
|
| 79 |
# --------------------
|
| 80 |
# Display AI typing animation
|
| 81 |
# --------------------
|
| 82 |
-
def type_ai_message(self, full_text, delay=
|
| 83 |
self.ai_text_buffer = full_text
|
| 84 |
self.typing_label.setText("🤖 AI is typing...")
|
| 85 |
self.ai_typing_timer = QTimer()
|
|
@@ -89,9 +102,9 @@ class ChatClient(QWidget):
|
|
| 89 |
def _type_next_char(self):
|
| 90 |
if self.ai_text_buffer:
|
| 91 |
char = self.ai_text_buffer[0]
|
|
|
|
| 92 |
self.chat_area.insertPlainText(char)
|
| 93 |
self.ai_text_buffer = self.ai_text_buffer[1:]
|
| 94 |
-
self.chat_area.moveCursor(Qt.TextCursor.MoveOperation.End)
|
| 95 |
else:
|
| 96 |
self.ai_typing_timer.stop()
|
| 97 |
self.typing_label.setText("")
|
|
@@ -101,7 +114,7 @@ class ChatClient(QWidget):
|
|
| 101 |
# --------------------
|
| 102 |
def send_message(self):
|
| 103 |
message = self.user_input.text().strip()
|
| 104 |
-
if not message
|
| 105 |
return
|
| 106 |
self.chat_area.append(f"🧑 {message}")
|
| 107 |
self.user_input.clear()
|
|
@@ -126,58 +139,13 @@ class ChatClient(QWidget):
|
|
| 126 |
except Exception as e:
|
| 127 |
self.chat_area.append(f"Error sending message: {e}")
|
| 128 |
|
| 129 |
-
# --------------------
|
| 130 |
-
# Login / Signup
|
| 131 |
-
# --------------------
|
| 132 |
-
def handle_login(self):
|
| 133 |
-
email = self.email_input.text().strip()
|
| 134 |
-
password = self.password_input.text().strip()
|
| 135 |
-
if self.login(email, password):
|
| 136 |
-
self.chat_area.append("✅ Logged in successfully!")
|
| 137 |
-
else:
|
| 138 |
-
self.chat_area.append("❌ Login failed!")
|
| 139 |
-
|
| 140 |
-
def handle_signup(self):
|
| 141 |
-
email = self.email_input.text().strip()
|
| 142 |
-
password = self.password_input.text().strip()
|
| 143 |
-
if not email or not password:
|
| 144 |
-
self.chat_area.append("⚠️ Enter email & password")
|
| 145 |
-
return
|
| 146 |
-
try:
|
| 147 |
-
res = requests.post(
|
| 148 |
-
f"{API_BASE}/signup",
|
| 149 |
-
headers={"Content-Type": "application/json"},
|
| 150 |
-
data=json.dumps({"email": email, "username": email.split('@')[0], "password": password})
|
| 151 |
-
)
|
| 152 |
-
data = res.json()
|
| 153 |
-
if "message" in data:
|
| 154 |
-
self.chat_area.append("✅ Signup successful! Verify your email.")
|
| 155 |
-
else:
|
| 156 |
-
self.chat_area.append("❌ Signup error: " + data.get("error", "Unknown"))
|
| 157 |
-
except Exception as e:
|
| 158 |
-
self.chat_area.append(f"Error signing up: {e}")
|
| 159 |
-
|
| 160 |
-
def login(self, email, password):
|
| 161 |
-
try:
|
| 162 |
-
res = requests.post(
|
| 163 |
-
f"{API_BASE}/login",
|
| 164 |
-
headers={"Content-Type": "application/json"},
|
| 165 |
-
data=json.dumps({"email": email, "password": password})
|
| 166 |
-
)
|
| 167 |
-
data = res.json()
|
| 168 |
-
if "token" in data:
|
| 169 |
-
self.token = data["token"]
|
| 170 |
-
return True
|
| 171 |
-
return False
|
| 172 |
-
except Exception as e:
|
| 173 |
-
print("Login exception:", e)
|
| 174 |
-
return False
|
| 175 |
-
|
| 176 |
# --------------------
|
| 177 |
# Run App
|
| 178 |
# --------------------
|
| 179 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 180 |
app = QApplication(sys.argv)
|
| 181 |
-
window = ChatClient()
|
| 182 |
window.show()
|
| 183 |
sys.exit(app.exec())
|
|
|
|
| 3 |
import json
|
| 4 |
from PyQt6.QtWidgets import (
|
| 5 |
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
|
| 6 |
+
QPushButton, QTextEdit, QLineEdit, QLabel, QComboBox
|
| 7 |
)
|
| 8 |
from PyQt6.QtCore import QTimer, Qt
|
| 9 |
+
from PyQt6.QtGui import QFont, QCursor, QIcon, QColor, QTextCursor
|
| 10 |
|
| 11 |
API_BASE = "http://127.0.0.1:7860/api"
|
| 12 |
|
| 13 |
class ChatClient(QWidget):
|
| 14 |
+
def __init__(self, token):
|
| 15 |
super().__init__()
|
| 16 |
+
self.token = token # Token comes from main.py login
|
| 17 |
self.ai_typing_timer = None
|
| 18 |
self.ai_text_buffer = ""
|
| 19 |
self.setWindowTitle("AI HUB Desktop Client")
|
|
|
|
| 21 |
|
| 22 |
self.init_ui()
|
| 23 |
self.showMaximized()
|
| 24 |
+
self.setCursor(QCursor(Qt.CursorShape.ArrowCursor))
|
| 25 |
|
| 26 |
def init_ui(self):
|
| 27 |
self.layout = QVBoxLayout(self)
|
| 28 |
self.setLayout(self.layout)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Chat Display Area
|
| 31 |
self.chat_area = QTextEdit()
|
| 32 |
self.chat_area.setReadOnly(True)
|
| 33 |
+
self.chat_area.setFont(QFont("Consolas", 12))
|
| 34 |
+
self.chat_area.setStyleSheet("""
|
| 35 |
+
QTextEdit {
|
| 36 |
+
background-color: #1e1e1e;
|
| 37 |
+
color: #f1f1f1;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
padding: 10px;
|
| 40 |
+
}
|
| 41 |
+
""")
|
| 42 |
self.layout.addWidget(self.chat_area)
|
| 43 |
|
| 44 |
# Typing Indicator
|
| 45 |
self.typing_label = QLabel("")
|
| 46 |
+
self.typing_label.setStyleSheet("color: #00ff00; font-style: italic;")
|
| 47 |
self.layout.addWidget(self.typing_label)
|
| 48 |
|
| 49 |
# Input Area
|
|
|
|
| 51 |
self.user_input = QLineEdit()
|
| 52 |
self.user_input.setPlaceholderText("Type your message here...")
|
| 53 |
self.user_input.returnPressed.connect(self.send_message)
|
| 54 |
+
self.user_input.setStyleSheet("""
|
| 55 |
+
QLineEdit {
|
| 56 |
+
border-radius: 8px;
|
| 57 |
+
padding: 6px;
|
| 58 |
+
font-size: 14px;
|
| 59 |
+
}
|
| 60 |
+
""")
|
| 61 |
input_layout.addWidget(self.user_input)
|
| 62 |
|
| 63 |
self.send_button = QPushButton("Send")
|
| 64 |
+
self.send_button.setStyleSheet("""
|
| 65 |
+
QPushButton {
|
| 66 |
+
background-color: #0078d7;
|
| 67 |
+
color: white;
|
| 68 |
+
border-radius: 8px;
|
| 69 |
+
padding: 6px 12px;
|
| 70 |
+
}
|
| 71 |
+
QPushButton:hover {
|
| 72 |
+
background-color: #005a9e;
|
| 73 |
+
}
|
| 74 |
+
""")
|
| 75 |
self.send_button.clicked.connect(self.send_message)
|
| 76 |
input_layout.addWidget(self.send_button)
|
| 77 |
|
| 78 |
# Model selection
|
| 79 |
self.model_select = QComboBox()
|
| 80 |
self.model_select.addItems(["qwen", "deepseek"])
|
| 81 |
+
self.model_select.setStyleSheet("""
|
| 82 |
+
QComboBox {
|
| 83 |
+
border-radius: 8px;
|
| 84 |
+
padding: 4px;
|
| 85 |
+
font-size: 14px;
|
| 86 |
+
}
|
| 87 |
+
""")
|
| 88 |
input_layout.addWidget(self.model_select)
|
| 89 |
|
| 90 |
self.layout.addLayout(input_layout)
|
|
|
|
| 92 |
# --------------------
|
| 93 |
# Display AI typing animation
|
| 94 |
# --------------------
|
| 95 |
+
def type_ai_message(self, full_text, delay=30):
|
| 96 |
self.ai_text_buffer = full_text
|
| 97 |
self.typing_label.setText("🤖 AI is typing...")
|
| 98 |
self.ai_typing_timer = QTimer()
|
|
|
|
| 102 |
def _type_next_char(self):
|
| 103 |
if self.ai_text_buffer:
|
| 104 |
char = self.ai_text_buffer[0]
|
| 105 |
+
self.chat_area.moveCursor(QTextCursor.MoveOperation.End)
|
| 106 |
self.chat_area.insertPlainText(char)
|
| 107 |
self.ai_text_buffer = self.ai_text_buffer[1:]
|
|
|
|
| 108 |
else:
|
| 109 |
self.ai_typing_timer.stop()
|
| 110 |
self.typing_label.setText("")
|
|
|
|
| 114 |
# --------------------
|
| 115 |
def send_message(self):
|
| 116 |
message = self.user_input.text().strip()
|
| 117 |
+
if not message:
|
| 118 |
return
|
| 119 |
self.chat_area.append(f"🧑 {message}")
|
| 120 |
self.user_input.clear()
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
self.chat_area.append(f"Error sending message: {e}")
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# --------------------
|
| 143 |
# Run App
|
| 144 |
# --------------------
|
| 145 |
if __name__ == "__main__":
|
| 146 |
+
# Example: Pass token from main.py login
|
| 147 |
+
token = "YOUR_JWT_TOKEN_HERE"
|
| 148 |
app = QApplication(sys.argv)
|
| 149 |
+
window = ChatClient(token)
|
| 150 |
window.show()
|
| 151 |
sys.exit(app.exec())
|