50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
import os
|
|
|
|
import dotenv
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
import internet_ml.NLP.no_context.QA
|
|
|
|
dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
|
|
dotenv.load_dotenv(dotenv_path)
|
|
|
|
|
|
class QAView(APIView):
|
|
def post(self, request):
|
|
"""
|
|
{"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
|
|
"""
|
|
answer = internet_ml.NLP.no_context.QA.answer(
|
|
str(request.data["question"]),
|
|
str(os.environ.get("INTERNET_ML_GOOGLE_API")),
|
|
str(os.environ.get("INTERNET_ML_GOOGLE_SEARCH_ENGINE_ID")),
|
|
)
|
|
content = {
|
|
"error": "",
|
|
"question": str(request.data["question"]),
|
|
"response": answer[0],
|
|
"resources": answer[1],
|
|
}
|
|
return Response(content)
|