Django and REST APIs for servicing computational-based queries

I wrote a machine learning application in Django so that the user can specify some parameters in the form and prepare the model. Once the model has been trained, I want to execute queries like:

curl http://localhost:8000/.../?model_input='XYZ' 

and I want Django to return the result of the model based on XYZ input. Each example that I saw from the Tastypie or REST framework builds its answer from a set of queries. How can I continue if the answer is not the result of a set of queries, but the result of pure computing in memory? In my case, the answer is the result of multiplying the matrix (the trained model) by a vector (input), and this result is not stored in the table.

What is the recommended way to manage such requests? Any help is appreciated. Regards, Patrick

+6
source share
1 answer

The Django REST Framework does not require a model source or set of queries, although it works best when working with any of them. For this reason, it provides a basic Serializer , as well as a basic APIView classes , to allow content negotiation over standard Django class views.

You most likely will not need to use Serializer if you do not want to serialize the result object. Another common use for Serializer is to validate incoming data and convert it to the expected format.

If you just wanted to return the base value (you did not indicate that the “result of matrix multiplication” can actually be), then even just using the basic representations is a step from doing all this manually. The Response object provided by the Django REST Framework allows you to return arbitrary data and convert it to comparable JSON or XML views automatically. You never need to call json.dumps or force data to a specific view, the Response object does all this for you.

 from rest_framework.response import Response from rest_framework import serializers, views class IncredibleInputSerializer(serializers.Serializer): model_input = serializers.CharField() class IncredibleView(views.APIView): def get(self, request): # Validate the incoming input (provided through query parameters) serializer = IncredibleInputSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) # Get the model input data = serializer.validated_data model_input = data["model_input"] # Perform the complex calculations complex_result = model_input + "xyz" # Return it in your custom format return Response({ "complex_result": complex_result, }) 

In the above example, we create an IncredibleInputSerializer that checks the request parameter of model_input to make sure it is included in the request. This is a very simple example, because the Django REST Framework supports the execution of additional functions for input, for example, converting it to a number or confirming that it corresponds to a specific format.

Of course, if you need to serialize an object or a list of objects, what sets the Django REST Framework apart. It does not have to be a model object, it can be an object with an attribute or methods for receiving data, or even just a basic dictionary, and the Django REST Framework should be able to serialize it for you.

+5
source

Source: https://habr.com/ru/post/980542/


All Articles