Debug School

rakesh kumar
rakesh kumar

Posted on

How to implement question answer to predict answer in django

The question-answering pipeline answers questions using information from a given context:

from transformers import pipeline

question_answerer = pipeline("question-answering")
question_answerer(
    question="Where do I work?",
    context="My name is Sylvain and I work at Hugging Face in Brooklyn",
)
Enter fullscreen mode Exit fullscreen mode
{'score': 0.6385916471481323, 'start': 33, 'end': 45, 'answer': 'Hugging Face'}
Enter fullscreen mode Exit fullscreen mode

Install the required dependencies. Make sure you have the transformers library installed.

pip install transformers
Enter fullscreen mode Exit fullscreen mode

Import the necessary libraries in your Django view.

from django.shortcuts import render
from transformers import pipeline
Enter fullscreen mode Exit fullscreen mode

Define a view function to handle the POST request with the dynamic text input.

def question_answering(request):
    if request.method == 'POST':
        # Get the input text from the POST request
        text = request.POST['text']
        question = request.POST['question']

        # Load the pre-trained question answering model
        model = pipeline('question-answering')

        # Perform question answering on the input text
        answer = model(question=question, context=text)

        # Render the result in the template
        return render(request, 'answer.html', {'answer': answer})

    # Render the initial form
    return render(request, 'question.html')
Enter fullscreen mode Exit fullscreen mode

Create two HTML templates, question.html and answer.html, to display the input form and the answer respectively.
question.html:

html
Copy code
<!DOCTYPE html>
<html>
<head>
    <title>Question Answering</title>
</head>
<body>
    <h1>Enter the Question and Text</h1>
    <form method="post" action="{% url 'question_answering' %}">
        {% csrf_token %}
        <label for="question">Question:</label>
        <input type="text" id="question" name="question" required><br><br>
        <label for="text">Text:</label><br>
        <textarea id="text" name="text" rows="4" cols="50" required></textarea><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

answer.html:

html
Copy code
<!DOCTYPE html>
<html>
<head>
    <title>Question Answering Result</title>
</head>
<body>
    <h1>Answer:</h1>
    <p>{{ answer }}</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Add the URL mapping in your Django urls.py file.

from django.urls import path
from .views import question_answering

urlpatterns = [
    path('question-answering/', question_answering, name='question_answering'),
]
Enter fullscreen mode Exit fullscreen mode

Run your Django development server.

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Now you can access the question answering form at http://localhost:8000/question-answering/. Enter a question and the corresponding text, submit the form, and you will see the answer displayed on the result page.

Top comments (0)