AiForklift/magazine/views.py

82 lines
2.1 KiB
Python
Raw Normal View History

2019-05-05 17:34:26 +02:00
import math
import json
2019-03-25 15:52:35 +01:00
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
2019-05-05 17:34:26 +02:00
import tensorflow as tf
import numpy as np
2019-03-25 15:52:35 +01:00
# Create your views here.
def index(request):
return HttpResponse('It lives!')
@csrf_exempt
def classify(request):
2019-05-05 17:34:26 +02:00
loaded_request = json.load(request)
sw = loaded_request['sepalWidth']
sl = loaded_request['sepalLength']
pw = loaded_request['petalWidth']
pl = loaded_request['petalLength']
model = tf.keras.models.load_model('iris_model.h5')
output = model.predict(np.array([[sw, sl, pw, pl]]))
if output[0][0] > output[0][1] and output[0][0] > output[0][1]:
guess = 1
elif output[0][1] > output[0][0] and output[0][1] > output[0][2]:
guess = 2
else:
guess = 3
return HttpResponse(guess)
2019-03-26 17:03:38 +01:00
@csrf_exempt
2019-03-26 17:03:38 +01:00
def shortestPath(request):
loaded_request = json.load(request)
graph = loaded_request["graph"]
graph = {int(k): v for k, v in graph.items()}
for node in graph:
graph[node] = {
int(k): v for k, v in graph[node].items()}
start_node = loaded_request["start_node"]
dest_node = loaded_request["dest_node"]
2019-03-26 17:03:38 +01:00
distance = {}
predecessor = {}
path = {}
unseen_nodes = graph.copy()
for node in graph:
distance[node] = math.inf
distance[start_node] = 0
while unseen_nodes:
min_node = None
for node in unseen_nodes:
if min_node is None:
min_node = node
elif distance[node] < distance[min_node]:
min_node = node
for childNode, weight in graph[min_node].items():
if distance[min_node] + weight < distance[childNode]:
distance[childNode] = distance[min_node] + weight
predecessor[childNode] = min_node
unseen_nodes.pop(min_node)
for node in graph:
current = node
p = [current]
2019-03-26 17:03:38 +01:00
while current != start_node:
p.append(predecessor[current])
2019-03-26 17:03:38 +01:00
current = predecessor[current]
path[node] = p[::-1]
2019-05-05 17:34:26 +02:00
print(path)
return HttpResponse(path[dest_node][1:])