Spaces:
Runtime error
Runtime error
File size: 1,646 Bytes
75df934 f3de9b9 75df934 46d747a 204dd4d 75df934 204dd4d 46d747a 204dd4d 46d747a 204dd4d 46d747a 9235b6d 75df934 204dd4d 3826b3b 75df934 3826b3b 75df934 46d747a 75df934 46d747a f7e0e10 75df934 3826b3b 46d747a 204dd4d |
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 |
import json
from . import model_manage as md
from channels.generic.websocket import WebsocketConsumer
from .database_manage import DataManage
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.views import View
import json
from django.http import JsonResponse
class ChatConsumer(WebsocketConsumer):
def connect(self):
print("Starting connect socket")
self.user = self.scope["user"]
print(f"User: {self.user}")
if self.user.is_authenticated:
self.accept()
print(f"User accepted")
self.model, self.session = md.init_model("auto")
else:
self.close()
def disconnect(self, close_code):
print("End, close code: ",close_code)
del self.model, self.session
pass
def receive(self, text_data):
"""
After socket create with:
username: self.user
roomid: self.roomid
"""
text_data_json = json.loads(text_data)
self.database = DataManage()
roomid = text_data_json["roomid"]
message = text_data_json["messages"]
print(message)
question = message[-1]['content']
response, history_state = md.full_chain_history_question(question, self.session, mode="auto")
# print("First answer: ",response)
print("Session history:")
md.print_history(history_state)
self.send(text_data=json.dumps({"message": response}))
self.database.Add_prompt_response(roomid,question,response)
|