Added exception handling
This commit is contained in:
parent
bc098077cf
commit
6c75e6a911
@ -10,32 +10,38 @@ def count_punct(text):
|
|||||||
return round(count/(len(text) - text.count(" ")), 3)*100
|
return round(count/(len(text) - text.count(" ")), 3)*100
|
||||||
|
|
||||||
def createLabels(data):
|
def createLabels(data):
|
||||||
id_to_labels = load('prototype/filehandler/labels.pkl')
|
try:
|
||||||
df = DataFrame(data['messages'], columns = ['body_text'])
|
id_to_labels = load('prototype/filehandler/labels.pkl')
|
||||||
|
df = DataFrame(data['messages'], columns = ['body_text'])
|
||||||
|
|
||||||
model = load('prototype/filehandler/model.pkl')
|
model = load('prototype/filehandler/model.pkl')
|
||||||
|
|
||||||
df['body_len'] = df['body_text'].apply(lambda x: len(x) - x.count(" "))
|
df['body_len'] = df['body_text'].apply(lambda x: len(x) - x.count(" "))
|
||||||
df['punct%'] = df['body_text'].apply(lambda x: count_punct(x))
|
df['punct%'] = df['body_text'].apply(lambda x: count_punct(x))
|
||||||
|
|
||||||
transformer = TfidfTransformer()
|
transformer = TfidfTransformer()
|
||||||
loaded_vec = CountVectorizer(decode_error = "replace", vocabulary = load('prototype/filehandler/vocabulary.pkl'))
|
loaded_vec = CountVectorizer(decode_error = "replace", vocabulary = load('prototype/filehandler/vocabulary.pkl'))
|
||||||
transformed = transformer.fit_transform(loaded_vec.fit_transform(df.body_text).toarray())
|
transformed = transformer.fit_transform(loaded_vec.fit_transform(df.body_text).toarray())
|
||||||
|
|
||||||
features = concat([df[['body_len', 'punct%']], DataFrame(transformed.toarray())], axis=1)
|
features = concat([df[['body_len', 'punct%']], DataFrame(transformed.toarray())], axis=1)
|
||||||
|
|
||||||
pred = model.predict(features)
|
pred = model.predict(features)
|
||||||
labels = list(map(id_to_labels.get, pred))
|
labels = list(map(id_to_labels.get, pred))
|
||||||
|
|
||||||
for id, label in zip(data['para_id'], labels):
|
for id, label in zip(data['para_id'], labels):
|
||||||
Paragraph.objects.filter(pk = id).update(label = label)
|
Paragraph.objects.filter(pk = id).update(label = label)
|
||||||
|
|
||||||
return(True)
|
return(True)
|
||||||
|
except:
|
||||||
|
return(False)
|
||||||
|
|
||||||
def updateLabelsByParagraphId(data):
|
def updateLabelsByParagraphId(data):
|
||||||
for paragraph_ in data:
|
try:
|
||||||
Paragraph.objects.filter(pk = paragraph_['id']).update(label = paragraph_['label'], user_updated = True)
|
for paragraph_ in data:
|
||||||
return(True)
|
Paragraph.objects.filter(pk = paragraph_['id']).update(label = paragraph_['label'], user_updated = True)
|
||||||
|
return(True)
|
||||||
|
except:
|
||||||
|
return(False)
|
||||||
|
|
||||||
def addToDatabase(data, file_id):
|
def addToDatabase(data, file_id):
|
||||||
out = {}
|
out = {}
|
||||||
|
@ -20,10 +20,13 @@ def model_form_upload(request):
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = DocumentForm(request.POST, request.FILES)
|
form = DocumentForm(request.POST, request.FILES)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
data = parseData(request.FILES['file'])
|
try:
|
||||||
|
data = parseData(request.FILES['file'])
|
||||||
|
except:
|
||||||
|
return HttpResponse('Error: Couldn\'t parse data', status=406)
|
||||||
file_id = (form.save()).pk
|
file_id = (form.save()).pk
|
||||||
if not (createLabels(addToDatabase(data, file_id))):
|
if not (createLabels(addToDatabase(data, file_id))):
|
||||||
return HttpResponse('Błąd przy dodawaniu informacji do bazy danych/tworzeniu etykiet', status=406)
|
return HttpResponse('Error: Couln\'t get labels or contact database', status=406)
|
||||||
output = listDiscussionsFromFile(file_id)
|
output = listDiscussionsFromFile(file_id)
|
||||||
return JsonResponse(output, safe=False)
|
return JsonResponse(output, safe=False)
|
||||||
else:
|
else:
|
||||||
@ -37,22 +40,34 @@ def model_form_upload(request):
|
|||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def discussions(request, id):
|
def discussions(request, id):
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
output = listParagraphsFromDiscussion(id)
|
try:
|
||||||
return JsonResponse(output, safe=False)
|
|
||||||
|
|
||||||
elif request.method == 'PATCH':
|
|
||||||
received_data = json.loads(request.body.decode("utf-8"))
|
|
||||||
if updateLabelsByParagraphId(received_data):
|
|
||||||
output = listParagraphsFromDiscussion(id)
|
output = listParagraphsFromDiscussion(id)
|
||||||
return JsonResponse(output, safe=False)
|
return JsonResponse(output, safe=False)
|
||||||
|
except:
|
||||||
|
return HttpResponse('Error: Couldn\'t list paragraphs', status=406)
|
||||||
|
|
||||||
|
elif request.method == 'PATCH':
|
||||||
|
try:
|
||||||
|
received_data = json.loads(request.body.decode("utf-8"))
|
||||||
|
except:
|
||||||
|
return HttpResponse('Error: Couldn\'t receive data', status=406)
|
||||||
|
if updateLabelsByParagraphId(received_data):
|
||||||
|
try:
|
||||||
|
output = listParagraphsFromDiscussion(id)
|
||||||
|
return JsonResponse(output, safe=False)
|
||||||
|
except:
|
||||||
|
return HttpResponse('Error: Couldn\'t list paragraphs', status=406)
|
||||||
else:
|
else:
|
||||||
return HttpResponse('Błąd przy dodwaniu informacji do bazy danych', status=406)
|
return HttpResponse('Error: Couldn\'t update database', status=406)
|
||||||
else:
|
else:
|
||||||
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
|
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
|
||||||
|
|
||||||
def visualize(request, id):
|
def visualize(request, id):
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
output = listPostsFromDiscussion(id)
|
try:
|
||||||
return JsonResponse(output, safe=False)
|
output = listPostsFromDiscussion(id)
|
||||||
|
return JsonResponse(output, safe=False)
|
||||||
|
except:
|
||||||
|
return HttpResponse('Error: Couldn\'t list posts', status=406)
|
||||||
else:
|
else:
|
||||||
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
|
return HttpResponse('Error: Unacceptable HTTP method', status=406)
|
||||||
|
Loading…
Reference in New Issue
Block a user