internet_ml/web/internet_ml_server/api/nlp/nocontext/views.py

48 lines
1.2 KiB
Python
Raw Normal View History

2022-12-28 16:37:10 +00:00
import json
2022-12-30 06:50:36 +00:00
import os
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
2022-12-30 06:50:36 +00:00
from .tools import question_answer
2022-12-28 10:41:27 +00:00
2022-12-28 16:37:10 +00:00
load_dotenv()
2022-12-30 06:50:36 +00:00
import internet_ml.NLP.no_context.QA
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
"""
2022-12-30 06:50:36 +00:00
answer = internet_ml.NLP.no_context.QA.answer(
request.POST.get("question"),
str(os.getenv("INTERNET_ML_GOOGLE_API")),
str(os.getenv("INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID")),
)
content = json.dumps(
{"error": "", "response": answer[0], "resources": answer[1]}
)
return content