Spaces:
Runtime error
Runtime error
File size: 2,670 Bytes
75df934 46d747a 204dd4d 46d747a 75df934 46d747a 75df934 4b81092 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 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 |
# chat/views.py
from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
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
from django.http import JsonResponse
from . import model_manage as md
import json
@login_required
def index(request):
roomid = request.GET.get('id')
data = DataManage()
rooms = data.LoadRooms(username=request.user.username)
if roomid:
# try:
roomdata = data.LoadRoomDetail(roomid=roomid)
if roomdata==None:
raise("Room not found")
context = {
'rooms': rooms,
'roomdata': json.dumps(roomdata),
}
return render(request, 'index.html',context=context)
# except Exception as e:
# print(e)
# return HttpResponse("Room not found", status=404)
else:
roomdata = {'messages':[]}
return render(request, "index.html",{"rooms":rooms,'roomdata':roomdata})
@login_required
def newchat(request):
data = DataManage()
user = request.user.username
ques = request.GET.get('ques')
room = data.InsertRoom(user,ques[:23])
return redirect(f'/chat?id={room}&ques={ques}')
def room(request, room_name):
return render(request, "chat/room.html", {"room_name": room_name})
@login_required
def deletechat(request):
data = DataManage()
roomid = request.GET.get('id')
data.DeleteRoom(roomid)
return redirect('/chat')
@method_decorator(csrf_exempt, name='dispatch')
class MessageView(View):
def __init__(self) -> None:
super().__init__()
self.model = None
self.session = None
self.database = None
def post(self, request):
if not self.model:
self.model, self.session = md.init_model("auto")
text_data_json = json.loads(request.body)
self.database = DataManage()
print(text_data_json)
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.database.Add_prompt_response(roomid,question,response)
return JsonResponse({"message": response}, status=201) |