Spaces:
Runtime error
Runtime error
# 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 | |
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}) | |
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}) | |
def deletechat(request): | |
data = DataManage() | |
roomid = request.GET.get('id') | |
data.DeleteRoom(roomid) | |
return redirect('/chat') | |
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) |