views.py
from django.shortcuts import render
from .models import questions
from .serializers import approvalSerializers
from django.http import JsonResponse
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from rest_framework.decorators import api_view
import os
from avasyu import settings
import pickle
import requests
import numpy as np
from django.core.cache import cache
# Create your views here.
data = []
def landing_views(request):
return render(request, "avasyuapp/landing.html")
def ques_views(request):
cache.clear()
return render(request, "avasyuapp/ques.html")
def store_db(request):
if request.method == "POST":
ans1 = request.POST["answer1"]
ans2 = request.POST["answer2"]
ans3 = request.POST["answer3"]
ans4 = request.POST["answer4"]
ans5 = request.POST["answer5"]
ans6 = request.POST["answer6"]
ans7 = request.POST["answer7"]
ans8 = request.POST["answer8"]
ans9 = request.POST["answer9"]
data.append(ans1)
data.append(ans2)
data.append(ans3)
data.append(ans4)
data.append(ans5)
data.append(ans6)
o1 = questions(
ques='Have you motivated yourself to become a good communicator?', ans=ans1)
o1.save()
o2 = questions(
ques='Can you speak in front of group without any nervousness?', ans=ans2)
o2.save()
o3 = questions(
ques='Can you justify you as a good communicator?', ans=ans3)
o3.save()
o4 = questions(
ques='Are you really happy to make communication as your future passion?', ans=ans4)
o4.save()
o5 = questions(
ques='Is your english vocabulary and comprehension strong?', ans=ans5)
o5.save()
o6 = questions(ques='Are you good at grammar?', ans=ans6)
o6.save()
o7 = questions(
ques='Have you achieved anything till date as a good communicator ?', ans=ans7)
o7.save()
o8 = questions(
ques='Are you a good listener,good reader and are you clear in your communication when communicating with others ?', ans=ans8)
o8.save()
o9 = questions(ques='Are you spending any time on reading ?', ans=ans9)
o9.save()
print("Stored in DB..")
# print(data)
cache.clear()
return render(request, 'avasyuapp/result.html')
else:
return redirect('ques/')
class ApprovalsView(viewsets.ModelViewSet):
queryset = questions.objects.all()
serializer_class = approvalSerializers
answer = []
def input_con(ans):
for i in range(len(data)):
if ans[i] == 'Yes':
answer.insert(i, 3)
elif ans[i] == 'Partially Yes':
answer.insert(i, 2)
elif ans[i] == 'Partially No':
answer.insert(i, 1)
else:
answer.insert(i, 0)
return answer
def output_con(classifier):
if classifier == 3:
result = 'Yes'
elif classifier == 2:
result = 'Partially Yes'
elif classifier == 1:
result = 'Partially No'
else:
result = 'No'
return result
@api_view(["POST"])
def approvereject(request):
files = os.path.join(settings.MODELS, 'classifierfinal.pkl')
with open(files, 'rb') as file:
classifier = pickle.load(file)
#classifier = joblib.load(file)
converted_data = input_con(data)
# print(data)
output = classifier.predict([converted_data])
print(output)
dis_out = output_con(output)
print(dis_out)
cache.clear()
return render(request, 'avasyuapp/output.html', {'output': dis_out})
Первоначально, когда я пу sh код для репозитория, я получаю ожидаемый результат, но когда я перезагружаю приложение, я получаю либо 0 функций, либо функции. удваивается после каждого пробега. После того, как я перезагружаю приложение, я получаю эту ошибку, мне нужно снова выложить ее sh на heroku, чтобы получить вывод. Как я могу очистить буфер после каждой отправки и перестать получать эту ошибку 0 функций.
ValueError at /output/
Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.
Request Method: POST
Request URL: https://avasyu.herokuapp.com/output/
Django Version: 3.0.4
Exception Type: ValueError
Exception Value:
Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.
Exception Location: /app/.heroku/python/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array, line 594
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.6.10
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python36.zip',
'/app/.heroku/python/lib/python3.6',
'/app/.heroku/python/lib/python3.6/lib-dynload',
'/app/.heroku/python/lib/python3.6/site-packages']
Server time: Thu, 12 Mar 2020 19:45:41 +0000