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
|
||||
|
||||
def createLabels(data):
|
||||
id_to_labels = load('prototype/filehandler/labels.pkl')
|
||||
df = DataFrame(data['messages'], columns = ['body_text'])
|
||||
try:
|
||||
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['punct%'] = df['body_text'].apply(lambda x: count_punct(x))
|
||||
df['body_len'] = df['body_text'].apply(lambda x: len(x) - x.count(" "))
|
||||
df['punct%'] = df['body_text'].apply(lambda x: count_punct(x))
|
||||
|
||||
transformer = TfidfTransformer()
|
||||
loaded_vec = CountVectorizer(decode_error = "replace", vocabulary = load('prototype/filehandler/vocabulary.pkl'))
|
||||
transformed = transformer.fit_transform(loaded_vec.fit_transform(df.body_text).toarray())
|
||||
transformer = TfidfTransformer()
|
||||
loaded_vec = CountVectorizer(decode_error = "replace", vocabulary = load('prototype/filehandler/vocabulary.pkl'))
|
||||
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)
|
||||
labels = list(map(id_to_labels.get, pred))
|
||||
pred = model.predict(features)
|
||||
labels = list(map(id_to_labels.get, pred))
|
||||
|
||||
for id, label in zip(data['para_id'], labels):
|
||||
Paragraph.objects.filter(pk = id).update(label = label)
|
||||
for id, label in zip(data['para_id'], labels):
|
||||
Paragraph.objects.filter(pk = id).update(label = label)
|
||||
|
||||
return(True)
|
||||
return(True)
|
||||
except:
|
||||
return(False)
|
||||
|
||||
def updateLabelsByParagraphId(data):
|
||||
for paragraph_ in data:
|
||||
Paragraph.objects.filter(pk = paragraph_['id']).update(label = paragraph_['label'], user_updated = True)
|
||||
return(True)
|
||||
try:
|
||||
for paragraph_ in data:
|
||||
Paragraph.objects.filter(pk = paragraph_['id']).update(label = paragraph_['label'], user_updated = True)
|
||||
return(True)
|
||||
except:
|
||||
return(False)
|
||||
|
||||
def addToDatabase(data, file_id):
|
||||
out = {}
|
||||
|
@ -20,10 +20,13 @@ def model_form_upload(request):
|
||||
if request.method == 'POST':
|
||||
form = DocumentForm(request.POST, request.FILES)
|
||||
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
|
||||
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)
|
||||
return JsonResponse(output, safe=False)
|
||||
else:
|
||||
@ -37,22 +40,34 @@ def model_form_upload(request):
|
||||
@csrf_exempt
|
||||
def discussions(request, id):
|
||||
if request.method == 'GET':
|
||||
output = listParagraphsFromDiscussion(id)
|
||||
return JsonResponse(output, safe=False)
|
||||
|
||||
elif request.method == 'PATCH':
|
||||
received_data = json.loads(request.body.decode("utf-8"))
|
||||
if updateLabelsByParagraphId(received_data):
|
||||
try:
|
||||
output = listParagraphsFromDiscussion(id)
|
||||
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:
|
||||
return HttpResponse('Błąd przy dodwaniu informacji do bazy danych', status=406)
|
||||
return HttpResponse('Error: Couldn\'t update database', status=406)
|
||||
else:
|
||||
return HttpResponse('Nieobsługiwana metoda HTTP', status=406)
|
||||
|
||||
def visualize(request, id):
|
||||
if request.method == 'GET':
|
||||
output = listPostsFromDiscussion(id)
|
||||
return JsonResponse(output, safe=False)
|
||||
try:
|
||||
output = listPostsFromDiscussion(id)
|
||||
return JsonResponse(output, safe=False)
|
||||
except:
|
||||
return HttpResponse('Error: Couldn\'t list posts', status=406)
|
||||
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