<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mahdi Ghelichi on Medium]]></title>
        <description><![CDATA[Stories by Mahdi Ghelichi on Medium]]></description>
        <link>https://medium.com/@mahdi04?source=rss-43b0bef45698------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hxLPbpnY_arQ697RIU86Fg.png</url>
            <title>Stories by Mahdi Ghelichi on Medium</title>
            <link>https://medium.com/@mahdi04?source=rss-43b0bef45698------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 11:54:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mahdi04/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Train/predict simple machine learning models with Django REST.]]></title>
            <link>https://medium.com/@mahdi04/train-predict-simple-machine-learning-models-with-django-rest-76ce46bf2868?source=rss-43b0bef45698------2</link>
            <guid isPermaLink="false">https://medium.com/p/76ce46bf2868</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Mahdi Ghelichi]]></dc:creator>
            <pubDate>Wed, 06 Feb 2019 18:35:18 GMT</pubDate>
            <atom:updated>2019-02-06T20:52:42.842Z</atom:updated>
            <content:encoded><![CDATA[<h3>Train/predict simple machine learning model with Django REST.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*tUK-KJC07zwhRrYOvNARrQ.png" /></figure><p><a href="https://www.django-rest-framework.org/">Django REST</a> Framework allows machine learning models to be used as RESTFul web services. Complex machine learning models can be readily used simply by calling an API endpoint. In this tutorial, we are going to use REST framework to train different <a href="https://en.wikipedia.org/wiki/Random_forest">Random forest</a> classifiers on iris dataset and invoke these models when we call prediction endpoints.</p><h3>Prerequisite and installations</h3><p>We use python3.6 and Django 2.1 and djangorestframework 3.8.2. <br>To install major requirements: <br>pip install django==2.1 djangorestframework==3.8.2<br> pip install sklearn pandas numpy</p><h3>Part 1. Basic setup</h3><ol><li>First, cd to the directory where we want to store our code, then run django-admin startproject DjangoRestML<br>This will create a DjangoRestML directory in our current directory.</li><li>Then: cd DjangoRestML</li><li>Create an empty directory named ‘models’: mkdir models This is the directory where we store our trained models (treated here as static files)</li><li>Now, to create our app, we run: python manage.py startapp App</li><li>Our entire directory should now look like this:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*5fxzUI0Qj5pHKre-noeC-g.png" /></figure><h3>Part 2. Django setup</h3><p>First, we need to setup our <a href="https://github.com/mahdi-ghelichi/DjangoRestML/blob/master/DjangoRestML/settings.py">settings.py</a>. We add the ‘models’ directory to our settings followed by our ‘App’ and ‘rest_framework’ to INSTALLED_APPS:</p><pre>...<br>MODEL_ROOT = os.path.join(BASE_DIR, &#39;models&#39;)<br>....<br>INSTALLED_APPS = [<br>    ...<br>    &#39;rest_framework&#39;,<br>    &#39;App&#39;,<br>]<br>...</pre><h3>Part 3. Urls and patters</h3><p>Now, we need to specify our endpoints, both on the project level and the app level:</p><p><em>DjangoRestML/urls.py</em></p><pre>from django.contrib import admin<br>from django.conf.urls import url, include</pre><pre>urlpatterns = [<br>    url(https://medium.com/r&#39;^admin/&#39;,%20admin.site.urls),<br>    url(https://medium.com/r&#39;^App/&#39;,%20include(&#39;App.urls&#39;)),<br>]</pre><p><em>App/urls.py</em></p><pre>from App.views import Train, Predict<br>from django.conf.urls import url<br><br>app_name = &#39;App&#39;<br><br>urlpatterns = [<br>    url(https://medium.com/r&#39;^train/r&#39;^train/$&#39;, Train.as_view(#39;,%20Train.as_view(), name=&quot;train&quot;),<br>    url(https://medium.com/r&#39;^predict/r&#39;^predict/$&#39;, Predict.as_view(#39;,%20Predict.as_view(), name=&quot;predict&quot;),<br>]</pre><h3>Part 3. Views</h3><p>Our endpoint calls are directed to specific functions/classes in the views of our app. Here, we use the POST method for both the training and prediction calls. The reasons for choosing POST is that it can encompass lots of necessary parameters (plus security/authentication variables) as json payload which we need to train the model with or authenticate a user.</p><p><em>App/views.py</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c0bc6b0a62e6a9bc1b4ac4a4872c676/href">https://medium.com/media/4c0bc6b0a62e6a9bc1b4ac4a4872c676/href</a></iframe><p>As you can see in theTrainclass, first we load the iris dataset from sklearn’s datasets library and grab the model_name that the user wants to assign to the model. The trained model is pickled and stored in the MODEL_ROOT that we defined in our settings.py.<br>For prediction purpose, we use the Predictclass. The user can specify the model_name plus the necessary features for prediction/classification. The code goes through each element of the json payload, grab the stored model and performs the prediction using that model. The results are sent back as a list of predicted labels.</p><h3>Part 4. Results</h3><p>We run the app using: python manage.py runserver<br>Here we show how to use this REST framework to call train/predictions from the browser. We train a Random forest classifier named ‘model_1’ with the following parameters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0jbKzJ4oIrzU_NChZczdcw.png" /></figure><p>Now, we have ‘model_1’ as pickled object in our models directory.</p><p>For prediction, we provide a list of dictionaries each containing a specific model_name and a set of data points which we want to do prediction on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ydkg8gkaxyu3l2ZZyPfrOw.png" /></figure><p>The outputs is simply a list of predicted classes. The entire code cat be found at <a href="https://github.com/mahdi-ghelichi/DjangoRestML">this repository.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76ce46bf2868" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Async Django App with Celery and RabbitMQ]]></title>
            <link>https://medium.com/@mahdi04/simple-async-django-app-with-celery-and-rabbitmq-ad4b1692c380?source=rss-43b0bef45698------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad4b1692c380</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[celery]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Mahdi Ghelichi]]></dc:creator>
            <pubDate>Thu, 31 Jan 2019 19:01:40 GMT</pubDate>
            <atom:updated>2019-02-05T16:26:33.113Z</atom:updated>
            <content:encoded><![CDATA[<p>We will be building a simple <a href="https://www.djangoproject.com/">Django</a> application to run async tasks in the background using Celery and RabbitMQ. <a href="http://www.celeryproject.org/">Celery</a> is a powerful asynchronous task queue/job queue based on distributed message passing. <a href="https://www.rabbitmq.com/">RabbitMQ</a> is an open-source message-broker. A software where queues can be defined, applications may connect to the queue and transfer a message onto it. Monitoring/canceling/deleting of the background jobs are other capabilities of this basic app.</p><h3>Prerequisites and installations:</h3><p>This tutorial uses Python3.6, Django 2.1, Celery 4.2.1.<br>To install Django and Celery:</p><pre>pip install django==2.1 celery==4.2.1</pre><p>(<strong><em>if </em></strong>you are using multiple versions of python and want these libraries to be installed only on a specific version of python, lets say, 3.6, then consider this command):</p><pre>python3.6 -m pip install django==2.1 celery==4.2.1</pre><p>To install RabbitMQ (Mac): brew install rabbitmq <br>The RabbitMQ codes are installed in /usr/local/sbin. You need to add this path to your .bash_profile . nano ~/.bash_profile and add it to the bottom of the .bash_profile, finally, export PATH=$PATH:/usr/local/sbin.</p><h3>Part 1. Basic setup</h3><ol><li>First, cd to the directory where we want to store our code, then run django-admin startproject AsyncDjangoApp<br>This will create a AsyncDjangoApp directory in our current directory.</li><li>Then: cd AsyncDjangoApp.</li><li>Now, to create our app, we run: python3.6 manage.py startapp App</li><li>Inside AsyncDjangoApp package (containing __init__.py) create an empty celery.py file.</li><li>Inside the App package, create empty forms.py and tasks.py. The task.py is where we put the functions/codes that need to be put in the background. These are usually slow or time-consuming tasks such as training machine learning models. The forms.py contain the <a href="https://docs.djangoproject.com/en/2.1/topics/forms/">Django Forms</a> that will be receiving input information from the user.</li><li>We create a template directory that will contain our simple html pages.</li><li>Run rabbitmq-server start .</li><li>Our entire directory should now look like this:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/152/1*gZnLefeSFMMXEZDVmUjGGw.png" /></figure><h3>Part 2. Django setup</h3><p>First, we need to setup our <a href="https://github.com/mahdi-ghelichi/AsyncDjangoApp/blob/master/AsyncDjangoApp/settings.py">settings.py</a>. We need to specify the templates configuration and add our App to the installed app list:</p><pre>import os</pre><pre>BASE_DIR = os.path.dirname(...)<br>TEMPLATES_DIR = os.path.join(BASE_DIR, ‘templates’)<br>...</pre><pre>INSTALLED_APPS = [<br>     ...   <br>     &#39;App&#39;,<br>]</pre><pre>...<br>TEMPLATES = [<br>    {<br>      ...<br>      &#39;DIRS&#39;: [TEMPLATES_DIR, ],<br>      ...<br>    }<br> ]<br>...</pre><p>Second, we need to specify the configurations for celery in settings.py according to this at the bottom of our settings file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/444/1*Br9l_WZk9R3Ax5ITtpubZA.png" /></figure><h3>Part 3. Filling the blanks</h3><p>Now, it is the time to fill out our celery.py and forms.py plus design our models and url patterns.</p><p><em>celery.py:</em></p><pre>from __future__ import absolute_import<br>import os<br>from celery import Celery</pre><pre>os.environ.setdefault(&#39;DJANGO_SETTINGS_MODULE&#39;, &#39;AsyncDjangoApp.settings&#39;)<br><br>app = Celery(&#39;AsyncDjangoApp&#39;)<br>app.config_from_object(&#39;django.conf:settings&#39;, namespace=&#39;CELERY&#39;)<br>app.autodiscover_tasks()</pre><p>Now run: celery -A AsyncDjangoApp worker -l info to launch Celery workers (being inside AsyncDjangoAppdirectory).</p><p><em>forms.py:</em></p><pre>from django import forms<br><br>class JobForm(forms.Form):<br>    job_name = forms.CharField(label=&#39;Job name&#39;, required=True)</pre><p>As we can see, our forms only contain one entry which is the name of the job that we are giving to it.</p><p>To get track of our jobs, we need to store them in our database upon their call/creation. A <a href="https://docs.djangoproject.com/en/2.1/topics/db/models/">Django model</a> is a class that represents table or collection in our DB, and where every attribute of the class is a field of the table or collection. We only need to store the task_id that Celery gives to each task and the job_name that user assigns.</p><p><em>models.py:</em></p><pre>from django.db import models<br><br>class Tasks(models.Model):<br>    task_id = models.CharField(max_length=50)<br>    job_name = models.CharField(max_length=50)</pre><pre>    def __str__(self):<br>        return f&#39;{self.task_id} {self.job_name}&#39;</pre><p><em>AsyncDjangoApp/urls.py:</em></p><pre>from django.contrib import admin<br>from django.conf.urls import include, url<br><br>urlpatterns = [<br>    url(https://medium.com/r&#39;admin/&#39;,%20admin.site.urls),<br>    url(https://medium.com/r&#39;^App/&#39;,%20include(&#39;App.urls&#39;)), # our application !<br>]</pre><p><em>App/urls.py:</em></p><pre>from django.conf.urls import url<br>from App import views<br><br>app_name = &#39;App&#39;<br><br>urlpatterns = [<br>    url(https://medium.com/r&#39;^run/r&#39;^run/$&#39;, views.run, name=&#39;run&#39;#39;,%20views.run,%20name=&#39;run&#39;),<br>    url(https://medium.com/r&#39;^monitor/r&#39;^monitor/$&#39;, views.monitor, name=&#39;monitor&#39;#39;,%20views.monitor,%20name=&#39;monitor&#39;),<br>    url(https://medium.com/r&#39;^delete_job/(?P&lt;task_id&gt;.+)/$&#39;, views.delete_job,<br>       name=&#39;delete_job&#39;),<br>    url(https://medium.com/r&#39;^cancel_job/(?P&lt;task_id&gt;.+)/$&#39;, views.cancel_job,<br>       name=&#39;cancel_job&#39;)<br>]</pre><p>delete_job and cancel_job are the endpoints for removing a job from our DB and revoking/canceling a celery task. The first two endpoints are the major endpoints we will deal with today.</p><h3>Part 4. Views and tasks</h3><p>In our backend, when /App/run/ is called, views.run will be called. Upon a GET call, this view returns an html page displaying JobForm and asking for input information (in this case only a job_name). Submitting this form, results in a POST call, triggering our Celery task, and landing back on the same page while the task is running in the background .</p><p><em>views.py</em></p><pre>from App.tasks import process<br>from App.forms import JobForm<br>from django.shortcuts import render<br>from django.views.decorators.http import require_http_methods<br><br>@require_http_methods([&quot;GET&quot;, &quot;POST&quot;])<br>def run(request):<br>    if request.method == &quot;POST&quot;:<br>        form = JobForm(request.POST)<br>        if form.is_valid():<br>            data = form.cleaned_data<br>            job_name = data[&#39;job_name&#39;]<br>            process.delay(job_name=job_name)<br>            return render(request, &#39;job.html&#39;,<br>                   context={&#39;form&#39;: JobForm,<br>                       &#39;message&#39;: f&#39;{job_name} dispatched...&#39;})<br>    else:<br>        return render(request, &#39;job.html&#39;, <br>               context={&#39;form&#39;: JobForm})</pre><p>Our background task/function is called process. To run this celery function in the background we just need to call it with process.delay(*args, **kwargs) instead of regular python process(*args, **kwargs). In this case, our process function only needs the job_name as its input keyword argument.</p><p><em>tasks.py</em></p><pre>from AsyncDjangoApp.celery import app<br>from App.models import Tasks<br>from time import sleep<br>import random<br><br>@app.task(bind=True)<br>def process(self, job_name=None):<br><br>    b = Tasks(task_id=self.request.id, job_name=job_name)<br>    b.save()<br><br>    self.update_state(state=&#39;Dispatching&#39;, meta={&#39;progress&#39;: &#39;33&#39;})<br>    sleep(random.randint(5, 10)) <br><br>    self.update_state(state=&#39;Running&#39;, meta={&#39;progress&#39;: &#39;66&#39;})<br>    sleep(random.randint(5, 10))  </pre><pre>    self.update_state(state=&#39;Finishing&#39;, meta={&#39;progress&#39;: &#39;100&#39;})<br>    sleep(random.randint(5, 10)) </pre><p>Here, b = Tasks(…) creates and then saves the invoked celery job, which we retrieve later on for tracking and monitoring purposes. <br>We use a dummy function and random sleeps to simulate a long-running job. Each of the steps can simulate multiple steps of a slow process. For instance, in data science, one can divide the entire process of training a model into three steps: <br><strong>1.</strong> <strong>Pre-processing the dataset.</strong><br><strong>2.</strong> <strong>Training the algorithm.<br>3.</strong> <strong>Saving the model and reporting metrics</strong>.</p><p>We are also customizing the Celery’s task state (Dispatching, Running, Finishing) and providing additional information such as the level of job progress.</p><h3>Part 5. Adding HTML templates</h3><p>Our app has only two pages, a page that asks for a job_name (using JobForm) and runs that job in the background. We call this page run.html. Here we use <a href="https://getbootstrap.com/docs/4.0/getting-started/introduction/">Bootstrap 4</a> for basic styling (using its <a href="https://getbootstrap.com/docs/4.0/getting-started/introduction/">CDN</a> in the &lt;head&gt; tag of our html).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57c8133a46f198ead3a9bbe99127fe0f/href">https://medium.com/media/57c8133a46f198ead3a9bbe99127fe0f/href</a></iframe><p>For the details of Django templates, I refer you to its <a href="https://docs.djangoproject.com/en/2.1/topics/templates/">official documentation</a>. Line 18, 19 of the above gist provide a direct link to our job tracking page. <br>Now, we need to run our app using (before that applying migrations):<br>python3.6 manage.py migrate<br>python3.6 manage.py makemigrations<br>python3.6 manage.py runserver<br>This is how our run.html looks like after typing in Job name and hitting Run:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*kfLbHSmbZXt-BX0olFDIng.png" /></figure><h3>Part 6. Job management/monitoring</h3><p>For monitoring purpose, we make a query to grab all stored Tasks, then call Celery’s AsyncResult(task_id) and return a list containing 4 elements: job_name, state, progress, and task_id. The monitor.html displays the level of job progress using basic Bootstrap progress bars.</p><p><em>views.py</em></p><pre>from celery.result import AsyncResult<br>from App.models import Tasks<br>from django.shortcuts import render<br>from django.views.decorators.http import require_GET</pre><pre>def track_jobs():<br>    entries = Tasks.objects.all()<br>    information = []<br>    for item in entries:<br>        progress = 100  # max value for bootstrap progress <br>                        # bar, when  the job is finished<br>        result = AsyncResult(item.task_id)<br>        if isinstance(result.info, dict):<br>            progress = result.info[&#39;progress&#39;]<br>        information.append([item.job_name, result.state, <br>                          progress, item.task_id])<br>    return information<br><br>@require_GET<br>def monitor(request):<br>    info = track_jobs()<br>    return render(request, &#39;monitor.html&#39;, context={&#39;info&#39;: info})<br></pre><pre>@require_GET<br>def cancel_job(request, task_id=None):<br>    result = AsyncResult(task_id)<br>    result.revoke(terminate=True)<br>    info = track_jobs()<br>    return render(request, &#39;monitor.html&#39;, context={&#39;info&#39;: info})<br><br><br>@require_GET<br>def delete_job(request, task_id=None):<br>    a = Tasks.objects.filter(task_id=task_id)<br>    a.delete()<br>    info = track_jobs()<br>    return render(request, &#39;monitor.html&#39;, context={&#39;info&#39;: info})</pre><p>Canceling a job means getting the AsyncResult(task_id) object and then calling its revoke() method. Deleting a job simply translates to removing a job from DB. The following shows an snapshot of the monitoring page. The complete monitor.html template can be found <a href="https://github.com/mahdi-ghelichi/AsyncDjangoApp/blob/master/templates/monitor.html">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*pUWJfUbq6yip57aMRI8J3w.png" /></figure><p>The complete code can be found <a href="https://github.com/mahdi-ghelichi/AsyncDjangoApp">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad4b1692c380" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>