Spaces:
Sleeping
Sleeping
change message protocol
Browse files- chat/consumers.py +11 -1
- chat/database_manage.py +0 -1
- chat/templates/index.html +78 -37
- chat/urls.py +1 -1
- chat/views.py +31 -2
- chatbot_django/settings.py +1 -1
- db.sqlite3 +0 -0
- oldindex.html +1 -1
chat/consumers.py
CHANGED
@@ -2,17 +2,27 @@ import json
|
|
2 |
from . import model_manage as md
|
3 |
from channels.generic.websocket import WebsocketConsumer
|
4 |
from .database_manage import DataManage
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
class ChatConsumer(WebsocketConsumer):
|
7 |
def connect(self):
|
|
|
8 |
self.user = self.scope["user"]
|
|
|
9 |
if self.user.is_authenticated:
|
10 |
self.accept()
|
|
|
11 |
self.model, self.session = md.init_model("auto")
|
12 |
else:
|
13 |
self.close()
|
14 |
|
15 |
def disconnect(self, close_code):
|
|
|
16 |
del self.model, self.session
|
17 |
pass
|
18 |
|
@@ -34,4 +44,4 @@ class ChatConsumer(WebsocketConsumer):
|
|
34 |
md.print_history(history_state)
|
35 |
self.send(text_data=json.dumps({"message": response}))
|
36 |
self.database.Add_prompt_response(roomid,question,response)
|
37 |
-
|
|
|
2 |
from . import model_manage as md
|
3 |
from channels.generic.websocket import WebsocketConsumer
|
4 |
from .database_manage import DataManage
|
5 |
+
from django.views.decorators.csrf import csrf_exempt
|
6 |
+
from django.utils.decorators import method_decorator
|
7 |
+
from django.views import View
|
8 |
+
import json
|
9 |
+
from django.http import JsonResponse
|
10 |
+
|
11 |
|
12 |
class ChatConsumer(WebsocketConsumer):
|
13 |
def connect(self):
|
14 |
+
print("Starting connect socket")
|
15 |
self.user = self.scope["user"]
|
16 |
+
print(f"User: {self.user}")
|
17 |
if self.user.is_authenticated:
|
18 |
self.accept()
|
19 |
+
print(f"User accepted")
|
20 |
self.model, self.session = md.init_model("auto")
|
21 |
else:
|
22 |
self.close()
|
23 |
|
24 |
def disconnect(self, close_code):
|
25 |
+
print("End, close code: ",close_code)
|
26 |
del self.model, self.session
|
27 |
pass
|
28 |
|
|
|
44 |
md.print_history(history_state)
|
45 |
self.send(text_data=json.dumps({"message": response}))
|
46 |
self.database.Add_prompt_response(roomid,question,response)
|
47 |
+
|
chat/database_manage.py
CHANGED
@@ -30,7 +30,6 @@ class DataManage:
|
|
30 |
def LoadRoomDetail(self, roomid):
|
31 |
room = Room.objects.get(RoomID=roomid)
|
32 |
print(room)
|
33 |
-
print(room==None)
|
34 |
chat_details = ChatDetails.objects.filter(RoomID=room.RoomID).order_by('order')
|
35 |
print(chat_details)
|
36 |
mes = []
|
|
|
30 |
def LoadRoomDetail(self, roomid):
|
31 |
room = Room.objects.get(RoomID=roomid)
|
32 |
print(room)
|
|
|
33 |
chat_details = ChatDetails.objects.filter(RoomID=room.RoomID).order_by('order')
|
34 |
print(chat_details)
|
35 |
mes = []
|
chat/templates/index.html
CHANGED
@@ -5,6 +5,7 @@
|
|
5 |
<title>Python Project</title>
|
6 |
<meta charset="UTF-8">
|
7 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
8 |
<script src="https://cdn.tailwindcss.com"></script>
|
9 |
<!-- <script>
|
10 |
// const roomdata = JSON.parse('{{ roomdata|safe }}');
|
@@ -151,11 +152,11 @@
|
|
151 |
<div class="w-full flex flex-row border border-1 border-[#919191] rounded-xl px-4 py-6 items-center">
|
152 |
<textarea
|
153 |
class="w-full h-full outline-0 bg-transparent border-none text-white resize-none text-md max-h-[100px]"
|
154 |
-
v-on:keyup.enter="
|
155 |
v-model="message"></textarea>
|
156 |
<div class="flex flex-col justify-end gap-4 text-[#2E2E2E] rounded-md p-1"
|
157 |
:class="{'bg-white':message!='','bg-white/10':message==''}">
|
158 |
-
<button @click="
|
159 |
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"
|
160 |
xmlns="http://www.w3.org/2000/svg">
|
161 |
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
@@ -241,7 +242,12 @@
|
|
241 |
if (this.id == null) {
|
242 |
return
|
243 |
}
|
244 |
-
this.connect()
|
|
|
|
|
|
|
|
|
|
|
245 |
},
|
246 |
methods: {
|
247 |
refresh() {
|
@@ -251,54 +257,89 @@
|
|
251 |
window.location.href = '/chat/delete?id=' + id;
|
252 |
return;
|
253 |
},
|
254 |
-
|
|
|
255 |
if (this.id == null) {
|
256 |
window.location.href = '/chat/newchat?ques=' + this.message;
|
257 |
return;
|
258 |
}
|
|
|
|
|
259 |
this.messages.push({
|
260 |
role: "user",
|
261 |
-
content: this.
|
262 |
});
|
263 |
-
|
264 |
-
'
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
266 |
'messages': this.messages,
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
-
connect() {
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
}
|
302 |
},
|
303 |
}).mount('#app')
|
304 |
</script>
|
|
|
5 |
<title>Python Project</title>
|
6 |
<meta charset="UTF-8">
|
7 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
8 |
+
<meta name="csrf-token" content="{{ csrf_token }}">
|
9 |
<script src="https://cdn.tailwindcss.com"></script>
|
10 |
<!-- <script>
|
11 |
// const roomdata = JSON.parse('{{ roomdata|safe }}');
|
|
|
152 |
<div class="w-full flex flex-row border border-1 border-[#919191] rounded-xl px-4 py-6 items-center">
|
153 |
<textarea
|
154 |
class="w-full h-full outline-0 bg-transparent border-none text-white resize-none text-md max-h-[100px]"
|
155 |
+
v-on:keyup.enter="sendMessageAPI" placeholder="Type a message" :rows="message.split('\n').length"
|
156 |
v-model="message"></textarea>
|
157 |
<div class="flex flex-col justify-end gap-4 text-[#2E2E2E] rounded-md p-1"
|
158 |
:class="{'bg-white':message!='','bg-white/10':message==''}">
|
159 |
+
<button @click="sendMessageAPI">
|
160 |
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"
|
161 |
xmlns="http://www.w3.org/2000/svg">
|
162 |
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
242 |
if (this.id == null) {
|
243 |
return
|
244 |
}
|
245 |
+
// this.connect()
|
246 |
+
this.ques = params.get('ques');
|
247 |
+
if (this.ques != null) {
|
248 |
+
this.message = this.ques;
|
249 |
+
this.sendMessageAPI();
|
250 |
+
}
|
251 |
},
|
252 |
methods: {
|
253 |
refresh() {
|
|
|
257 |
window.location.href = '/chat/delete?id=' + id;
|
258 |
return;
|
259 |
},
|
260 |
+
sendMessageAPI() {
|
261 |
+
console.log("call sendMesageAPI");
|
262 |
if (this.id == null) {
|
263 |
window.location.href = '/chat/newchat?ques=' + this.message;
|
264 |
return;
|
265 |
}
|
266 |
+
this.ques = this.message;
|
267 |
+
this.message = "";
|
268 |
this.messages.push({
|
269 |
role: "user",
|
270 |
+
content: this.ques
|
271 |
});
|
272 |
+
fetch('/chat/messages/', {
|
273 |
+
method: 'POST',
|
274 |
+
headers: {
|
275 |
+
'Content-Type': 'application/json'
|
276 |
+
},
|
277 |
+
body: JSON.stringify({
|
278 |
+
'roomid': this.id,
|
279 |
+
'message': this.ques,
|
280 |
'messages': this.messages,
|
281 |
+
})
|
282 |
+
})
|
283 |
+
.then(response => response.json())
|
284 |
+
.then(data => {
|
285 |
+
this.messages.push({
|
286 |
+
role: "model",
|
287 |
+
content: data.message
|
288 |
+
});
|
289 |
+
})
|
290 |
+
.catch(error => {
|
291 |
+
console.error('Error sending or fetching messages:', error);
|
292 |
+
});
|
293 |
},
|
294 |
+
|
295 |
+
// sendMessage() {
|
296 |
+
// if (this.id == null) {
|
297 |
+
// window.location.href = '/chat/newchat?ques=' + this.message;
|
298 |
+
// return;
|
299 |
+
// }
|
300 |
+
// this.messages.push({
|
301 |
+
// role: "user",
|
302 |
+
// content: this.message
|
303 |
+
// });
|
304 |
+
// this.chatSocket.send(JSON.stringify({
|
305 |
+
// 'roomid':this.id,
|
306 |
+
// 'message': this.message,
|
307 |
+
// 'messages': this.messages,
|
308 |
+
// }));
|
309 |
+
// console.log(JSON.stringify({
|
310 |
+
// 'messages': this.messages
|
311 |
+
// }))
|
312 |
+
// this.message = "";
|
313 |
+
// },
|
314 |
|
315 |
+
// connect() {
|
316 |
+
// this.chatSocket = new WebSocket(
|
317 |
+
// 'wss://' + window.location.host + '/wss/chat'
|
318 |
+
// );
|
319 |
|
320 |
+
// this.chatSocket.onopen = () => {
|
321 |
+
// this.connectionStatus = 'Connected';
|
322 |
+
// console.log('WebSocket connected');
|
323 |
+
// };
|
324 |
|
325 |
+
// this.chatSocket.onmessage = (event) => {
|
326 |
+
// var mes = JSON.parse(event.data)
|
327 |
+
// this.messages.push({
|
328 |
+
// role: "model",
|
329 |
+
// content: mes.message
|
330 |
+
// });
|
331 |
+
// console.log('Message received: ', event.data);
|
332 |
+
// };
|
333 |
|
334 |
+
// this.chatSocket.onclose = () => {
|
335 |
+
// this.connectionStatus = 'Disconnected';
|
336 |
+
// console.log('WebSocket disconnected');
|
337 |
+
// };
|
338 |
|
339 |
+
// this.chatSocket.onerror = (error) => {
|
340 |
+
// console.error('WebSocket Error: ', error);
|
341 |
+
// };
|
342 |
+
// }
|
343 |
},
|
344 |
}).mount('#app')
|
345 |
</script>
|
chat/urls.py
CHANGED
@@ -5,7 +5,7 @@ app_name = 'chat'
|
|
5 |
|
6 |
urlpatterns = [
|
7 |
path('', views.index, name='index'),
|
|
|
8 |
path('newchat/',views.newchat,name = 'newchat'),
|
9 |
path('delete/',views.deletechat,name = 'delete'),
|
10 |
-
path("<str:room_name>/", views.room, name="room"),
|
11 |
]
|
|
|
5 |
|
6 |
urlpatterns = [
|
7 |
path('', views.index, name='index'),
|
8 |
+
path('messages/', views.MessageView.as_view(), name='messages'),
|
9 |
path('newchat/',views.newchat,name = 'newchat'),
|
10 |
path('delete/',views.deletechat,name = 'delete'),
|
|
|
11 |
]
|
chat/views.py
CHANGED
@@ -3,7 +3,11 @@ from django.shortcuts import render,redirect
|
|
3 |
from django.http import HttpResponse
|
4 |
from django.contrib.auth.decorators import login_required
|
5 |
from .database_manage import DataManage
|
6 |
-
import
|
|
|
|
|
|
|
|
|
7 |
import json
|
8 |
@login_required
|
9 |
def index(request):
|
@@ -43,4 +47,29 @@ def deletechat(request):
|
|
43 |
data = DataManage()
|
44 |
roomid = request.GET.get('id')
|
45 |
data.DeleteRoom(roomid)
|
46 |
-
return redirect('/chat')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from django.http import HttpResponse
|
4 |
from django.contrib.auth.decorators import login_required
|
5 |
from .database_manage import DataManage
|
6 |
+
from django.views.decorators.csrf import csrf_exempt
|
7 |
+
from django.utils.decorators import method_decorator
|
8 |
+
from django.views import View
|
9 |
+
from django.http import JsonResponse
|
10 |
+
from . import model_manage as md
|
11 |
import json
|
12 |
@login_required
|
13 |
def index(request):
|
|
|
47 |
data = DataManage()
|
48 |
roomid = request.GET.get('id')
|
49 |
data.DeleteRoom(roomid)
|
50 |
+
return redirect('/chat')
|
51 |
+
|
52 |
+
@method_decorator(csrf_exempt, name='dispatch')
|
53 |
+
class MessageView(View):
|
54 |
+
def __init__(self) -> None:
|
55 |
+
super().__init__()
|
56 |
+
self.model = None
|
57 |
+
self.session = None
|
58 |
+
self.database = None
|
59 |
+
|
60 |
+
def post(self, request):
|
61 |
+
if not self.model:
|
62 |
+
self.model, self.session = md.init_model("auto")
|
63 |
+
text_data_json = json.loads(request.body)
|
64 |
+
self.database = DataManage()
|
65 |
+
print(text_data_json)
|
66 |
+
roomid = text_data_json["roomid"]
|
67 |
+
message = text_data_json["messages"]
|
68 |
+
print(message)
|
69 |
+
question = message[-1]['content']
|
70 |
+
response, history_state = md.full_chain_history_question(question, self.session, mode="auto")
|
71 |
+
# print("First answer: ",response)
|
72 |
+
print("Session history:")
|
73 |
+
md.print_history(history_state)
|
74 |
+
self.database.Add_prompt_response(roomid,question,response)
|
75 |
+
return JsonResponse({"message": response}, status=201)
|
chatbot_django/settings.py
CHANGED
@@ -28,7 +28,7 @@ LOGIN_URL = '/users/login/'
|
|
28 |
# SECURITY WARNING: don't run with debug turned on in production!
|
29 |
DEBUG = True
|
30 |
|
31 |
-
ALLOWED_HOSTS = ['arxivbot-arxiv-chatbot.hf.space']
|
32 |
|
33 |
CSRF_TRUSTED_ORIGINS = ['https://arxivbot-arxiv-chatbot.hf.space']
|
34 |
|
|
|
28 |
# SECURITY WARNING: don't run with debug turned on in production!
|
29 |
DEBUG = True
|
30 |
|
31 |
+
ALLOWED_HOSTS = ['artteiv-arxiv-chatbot.hf.space','arxivbot-arxiv-chatbot.hf.space','localhost','127.0.0.1']
|
32 |
|
33 |
CSRF_TRUSTED_ORIGINS = ['https://arxivbot-arxiv-chatbot.hf.space']
|
34 |
|
db.sqlite3
CHANGED
Binary files a/db.sqlite3 and b/db.sqlite3 differ
|
|
oldindex.html
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
<html>
|
3 |
|
4 |
<head>
|
5 |
-
<title>Python
|
6 |
<meta charset="UTF-8">
|
7 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
8 |
<script src="https://cdn.tailwindcss.com"></script>
|
|
|
2 |
<html>
|
3 |
|
4 |
<head>
|
5 |
+
<title>Python Projec</title>
|
6 |
<meta charset="UTF-8">
|
7 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
8 |
<script src="https://cdn.tailwindcss.com"></script>
|