Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Wiki

    Автоматическое добавление в задачи ссылок на коммиты

    Programming

    Уроки React. Урок 7

    Node.js

    Create simple POS with React.js, Node.js, and MongoDB #8: CRUD POS Machine

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Thursday, September 11
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Python / Adding backend to CodePen snippet using Python/Django | Question-Answering Application
    Python

    Adding backend to CodePen snippet using Python/Django | Question-Answering Application

    coderashaplBy coderashaplApril 28, 2020Updated:May 26, 2024No Comments10 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Adding backend to CodePen snippet using Python/Django | Question-Answering Application
    Question-Answering Application with Python/Django. Thumbnail
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Question-Answering Application with Python/Django. Cover
    Question-Answering Application with Python/Django. Cover

    In this article, I am going to show you one of the best ways to improve your Back-End skills without caring about Front-End. The great designed apps always make a positive impact on me, so I used to spend too much time on the design part of my applications.  However, I like to work with the complexity of Back-End so I don’t have time to learn the inner workings of Front-End development. At the same time, I can’t start to build Back-End without finishing Front-End for some reason. As a solution, I started to build random apps by using CodePen snippets and it was really amazing with those UI/UX designs. Additionally, you’ll improve your creative thinking skills while trying to find app ideas by using CodePen designs.

    A few days ago, I used a simple UI “search-box” design to create a Question-Answering Application. Even this kind of small designs can be turned into an application. By building this app you’ll learn a new Python package to implement a QA System on your own private data, become familiar with parser libraries of Python such as BeautifulSoup and integration of AJAX with Django. Let me give you a spoiler of the algorithm, we are going to crawl data for a particular question from the internet and pass them into the pre-trained Deep Learning model to find the exact answer.

    Before we start you need a basic understanding of Django and Ubuntu to run some important commands. If you’re using other operating systems, you can download Anaconda to make your work easier.

    Installation and Configuration

    To get started, create and activate a virtual environment by following commands:

    virtualenv env
    . env/bin/activate

    Once the environment activated, install Django, then create a new project named answerfinder and inside this project create an app named search:

    pip install django
    django-admin startproject answerfinder
    cd answerfinder
    django-admin startapp search

    Don’t forget to configure your templates and static files inside settings.py

    Ajax Request with Django

    Create new a new HTML file named search.html and copy-paste everything inside following CodePen snippet.

    Animated Search Box – Click to see the Pen

    Now, let’s make small changes in the HTML file and add Ajax in the JavaScript file to get the input value.  Initially, we need to define the method of the form and add id into search input to get data by id. Then, we are going to implement the ajax function inside the submitFn() function that handles the submission of the form.

    search.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="/static/css/main.css">
        <title>QA Application</title>
    </head>
    <body>
        <form method="POST" onsubmit="submitFn(this, event);">
            {% csrf_token %}
            <div class="search-wrapper">
                <div class="input-holder">
                    <input type="text" id="question" class="search-input" placeholder="Ask me anything.." />
                    <button class="search-icon" onclick="searchToggle(this, event);"><span></span></button>
                </div>
                <span class="close" onclick="searchToggle(this, event);"></span>
                <div class="result-container">
                </div>
            </div>
        </form>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
        <script src="/static/js/main.js"></script>
    </body>
    </html>

    main.js

    function searchToggle(obj, evt){
        var container = $(obj).closest('.search-wrapper');
    
        if(!container.hasClass('active')){
                container.addClass('active');
                evt.preventDefault();
        }
        else if(container.hasClass('active') && $(obj).closest('.input-holder').length == 0){
                container.removeClass('active');
                // clear input
                container.find('.search-input').val('');
                // clear and hide result container when we press close
                container.find('.result-container').fadeOut(100, function(){$(this).empty();});
        }
    }
    
    function submitFn(obj, evt){
              
        $.ajax({
                type:'POST',
                url:'/',
                data:{
                  question:$('#question').val(), // get value inside the search input
                  csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
                },
                success:function(json){
                console.log(json)
                $(obj).find('.result-container').html('<span>Answer: ' + json.answer + '</span>');
                $(obj).find('.result-container').fadeIn(100);
                
            
                },
                error : function(xhr,errmsg,err) {
                console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
                console.log($('#total').text())
            }
        });
    
        evt.preventDefault();
    }

    As you see, I removed the body of the submitFn() function and implemented ajax to get the input value. Since we are making POST request it is important to include csrf token to make it secure. Once the request succeeds, we’ll get a response as a JSON to display it in the template.

    Read More:  Optimizing Database Interactions in Python: SQLAlchemy Best Practices

    Next, we should create a view that checks if a given input or question is taken, and return a response as JSON.

    views.py

    from django.http import JsonResponse
    
    def search_view(request):
        if request.POST:
            question = request.POST.get('question')
            data = {
            'question': question
            }
            return JsonResponse(data)
        return render(request, 'search.html')

    urls.py

    from django.contrib import admin
    from django.urls import path
    from search.views import search_view
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', search_view, name="search"),
    ]
    

    Now, if you submit the form, the question will show up in the console of your browser with 200 status code which means the request was successful.

    Question-Answering System

    I found an article in Medium that explains the Question-Answering system with Python. It’s an easy-to-use python package to implement a QA System on your own private data. You can go and check for more explanation from the link below.

    How to create your own Question-Answering system easily with python

    The main reason for using this package is to display the exact answer to the user, even you can create a chatbot by using this deep learning model. Let’s first install the package then we’ll look closer to the program.

    pip install cdqa pandas

    Once installation completed, create a new python file just for testing and download the pre-trained models and data manually by using the code block below:

    import pandas as pd
    from ast import literal_eval
    
    from cdqa.utils.filters import filter_paragraphs
    from cdqa.utils.download import download_model, download_bnpp_data
    from cdqa.pipeline.cdqa_sklearn import QAPipeline
    
    # Download data and models
    download_bnpp_data(dir='./data/bnpp_newsroom_v1.1/')
    download_model(model='bert-squad_1.1', dir='./models')
    
    # Loading data and filtering / preprocessing the documents
    df = pd.read_csv('data/bnpp_newsroom_v1.1/bnpp_newsroom-v1.1.csv', converters={'paragraphs': literal_eval})                       
    df = filter_paragraphs(df)
    
    # Loading QAPipeline with CPU version of BERT Reader pretrained on SQuAD 1.1                   
    cdqa_pipeline = QAPipeline(reader='models/bert_qa.joblib')
    
    # Fitting the retriever to the list of documents in the dataframe
    cdqa_pipeline.fit_retriever(df)
    
    # Sending a question to the pipeline and getting prediction
    query = 'Since when does the Excellence Program of BNP Paribas exist?'
    prediction = cdqa_pipeline.predict(query)
    
    print('query: {}n'.format(query))
    print('answer: {}n'.format(prediction[0]))
    print('title: {}n'.format(prediction[1]))
    print('paragraph: {}n'.format(prediction[2]))
    

    the output should like this:

    Question-answering system's code output
    Question-answering system’s code output

    It prints the exact answer and paragraph that includes the answer.

    We are going to crawl the content of a few webpages later in this post to use them as data.

    Basically, when a question sent to the system, the Retriever will select a list of documents from the crawled data that are the most likely to contain the answer. It computes the cosine similarity between the question and each document in the crawled data.

    After selecting the most probable documents, the system divides each document into paragraphs and send them with the question to the Reader, which is basically a pre-trained Deep Learning model. The model used was the Pytorch version of the well known NLP model BERT.

    Then, the Reader outputs the most probable answer it can find in each paragraph. After the Reader, there is a final layer in the system that compares the answers by using an internal score function and outputs the most likely one according to the scores which will the answer of our question.

    Here is the schema of the system mechanism.

    Question-answering system's schema
    Question-answering system’s schema

    You have to include pre-trained models in your Django project and to achieve that you can run download functions directly from views or just simply copy-paste the models directory into your project.

    Crawling Data with Beautiful Soup

    We are going to use Beautiful Soup to crawl the content of the first 3 webpages from Google’s search results to get some information about the question because the answer probably locates in one of them. Basically, we need to perform a google search to get these links, and instead of putting so much effort for such a trivial task, google package has one dependency on Beautiful Soup library that can find links of all the google search result directly.

    Read More:  Build a Python Command-line Program Using OOP

    Install the beautiful soup and google package by following commands:

    pip install beautifulsoup4
    pip install google

    Try to run the following code snippet in a different file to see the result:

    from googlesearch import search 
    
    for url in search("Coffee", tld="com", num=10, stop=5, pause=2): 
    	print(url) 
    

    Once we get the links it’s time to create a new function that will crawl the content of these webpages. The data frame must be (CSV) in a specific structure so it can be sent to the cdQA pipeline. At this point, we can use the pdf_converter function of the cdQA library, to create an input data frame from a directory of PDF files. Therefore, I am going to save all crawled data in PDF files for each webpage. Hopefully, we’ll have 3 pdf files in total (can be 1 or 2 as well). Additionally, we need to name these pdf files, so by using enumerate() function, these files will name by their index numbers.

    If I summarize the algorithm it will get the question form the input, search it on google, crawl the first 3 results, create 3 pdf files from the crawled data and finally find the answer by using a question answering system.

    views.py

    import os, io
    import errno
    import urllib
    import urllib.request
    from time import sleep
    from urllib.request import urlopen, Request
    from django.shortcuts import render
    from django.http import JsonResponse
    from bs4 import BeautifulSoup
    from googlesearch import search 
    import pandas as pd
    from cdqa.utils.filters import filter_paragraphs
    from cdqa.utils.download import download_model, download_bnpp_data
    from cdqa.pipeline.cdqa_sklearn import QAPipeline
    from cdqa.utils.converters import pdf_converter
    
    
    def search_view(request):
        if request.POST:
            question = request.POST.get('question')
            # uncomment the line below to download models 
            # download_model(model='bert-squad_1.1', dir='./models')
            for idx, url in enumerate(search(question, tld="com", num=10, stop=3, pause=2)): 
                crawl_result(url, idx) 
            # change path to pdfs folder
            df = pdf_converter(directory_path='/path/to/pdfs/')
            cdqa_pipeline = QAPipeline(reader='models/bert_qa.joblib')
            cdqa_pipeline.fit_retriever(df)
            prediction = cdqa_pipeline.predict(question)
            data = {
            'answer': prediction[0]
            }
            return JsonResponse(data)
        return render(request, 'search.html')
       
    
    def crawl_result(url, idx):
        try:
            req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
            html = urlopen(req).read()
            bs = BeautifulSoup(html, 'html.parser')
            # change path to pdfs folder
            filename = "/path/to/pdfs/" + str(idx) + ".pdf"
            if not os.path.exists(os.path.dirname(filename)):
                try:
                    os.makedirs(os.path.dirname(filename))
                except OSError as exc: # Guard against race condition
                    if exc.errno != errno.EEXIST:
                        raise
            with open(filename, 'w') as f:
                for line in bs.find_all('p')[:5]:
                    f.write(line.text + 'n')
        except (urllib.error.HTTPError, AttributeError) as e:
            pass
    
    
    

    Note that you must change filename path to your pdfs folder otherwise cdQA can’t create an input data frame. Remember that you have to download pre-trained models, simply uncomment the download function inside search_view(). Once models downloaded you can remove this line.

    Final Result

    Finally, you can run the project and type any question in the search box. It’ll take about 20 seconds to display the answer.

    Question-answering system's result
    Question-answering system’s result

    The question was “What is programming?” and the answer was “designing and building an executable computer program to accomplish a specific computing result”. Sometimes the crawlers can’t find enough information so the answer will empty. Additionally, don’t forget to remove pdf files for new questions, if you wish you can add a new function to handle this task.

    I know some of you going to tell me that Flask is better for this kind of applications but I prefer Django anyway.

    You can find this project in GitHub link below:

    GitHub – Question-Answering Application

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    coderashapl

      Related Posts

      Flask Development Made Easy: A Comprehensive Guide to Test-Driven Development

      January 4, 2024

      Creating Our Own Chat GPT

      July 27, 2023

      The Ultimate Guide to Pip

      June 12, 2023
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Programming October 19, 2016

      19. Уроки Node.js. Безопасный Путь к Файлу в fs и path.

      В этой статье мы рассмотрим, как при помощи Node.js создать веб-сервер, который будет возвращать файл пользователю из директории public. Может возникнуть вопрос: зачем здесь Node.js? почему бы не сделать это на другом сервере? Вопрос совершенно уместен. Да, для отдачи файлов, как правило, другие сервера будут более эффективны. С другой стороны, Node.js, во-первых, тоже работает весьма неплохо, а во-вторых, перед отдачей файла может совершить какие-то интеллектуальные действия, например, обратиться к базе данных, проверить, имеет ли пользователь право на доступ к данному файлу, и только если имеет, тогда уже отдавать.

      С днем программиста!

      September 12, 2016

      Leveraging Interactive Content for Effective B2B Lead Generation

      December 1, 2024

      Are Coding Bootcamps Worth Your Time? Yes — with the Right Approach

      July 23, 2019

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Уроки React. Урок 1, Введение.

      Programming September 5, 2016

      NLP Preprocessing using Spacy

      Machine Learning April 5, 2023

      Optimizing LinkedIn: A Strategic Lead Generation Funnel Approach

      LinkedIn November 27, 2024

      Implementing Machine Learning in Web Applications with Python and TensorFlow

      Flask April 7, 2023

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Entrepreneurship

      Essential Steps to Identify and Validate Your Business Ideas

      Programming

      Effective Strategies for Utilizing Frameworks in Web Development

      LinkedIn

      Maximizing LinkedIn: Top Tools for Lead Generation and Automation

      Most Popular

      Basic Principles and Rules of Our Team

      JavaScript

      Build Real-World React Native App #9 : Implementing Remove Ads Feature

      JavaScript

      Overview of Natural Language Processing Using Python Libraries

      Programming
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.