internet_ml/web/internet_ml_server/api/question_answer/views.py

49 lines
1.3 KiB
Python
Raw Normal View History

2022-12-28 16:37:10 +00:00
import json
2022-12-28 10:41:27 +00:00
from django.views.generic.base import TemplateView
2022-12-28 16:37:10 +00:00
from dotenv import load_dotenv
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from internet_ml.NLP.no_context import QA
2022-12-28 10:41:27 +00:00
2022-12-28 16:37:10 +00:00
load_dotenv()
2022-12-28 10:41:27 +00:00
2022-12-28 16:37:10 +00:00
class QAView(APIView):
def post(self, request, format=None):
"""
{"question": "Who is Elon Musk?"}
{
"error": "",
"response": {
'score': VAL,
'start': VAL,
'end': VAL,
'answer': 'THE_ANSWER'
},
"resources": [
'SOME_LINKS_HERE'
]
}
or
{
"error": "",
"status": "",
"detail": "",
}
so check error if it exists first and then for other stuff
"""
try:
answer = QA.answer(request.POST.get("question"))
content = json.dumps(
{"error": "", "response": answer[0], "resources": answer[1]}
)
return Response(content, status=status.HTTP_200_OK)
except:
content = json.dumps(
{"error": "Google API key not present in .env or environment"}
)
return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)