Debug School

rakesh kumar
rakesh kumar

Posted on

How to implement text translation to predict answer in django

For translation, you can use a default model if you provide a language pair in the task name (such as "translation_en_to_fr"), but the easiest way is to pick the model you want to use on the Model Hub. Here we’ll try translating from French to English:

from transformers import pipeline

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
translator("Ce cours est produit par Hugging Face.")
Enter fullscreen mode Exit fullscreen mode
[{'translation_text': 'This course is produced by Hugging Face.'}]
Enter fullscreen mode Exit fullscreen mode

Implement text translation

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 text_translation(request):
    if request.method == 'POST':
        # Get the input text and target language from the POST request
        text = request.POST['text']
        target_lang = request.POST['target_lang']

        # Load the pre-trained translation model
        model = pipeline('translation', model='Helsinki-NLP/opus-mt-en-{target_lang}')

        # Translate the input text to the target language
        translated_text = model(text, max_length=512)

        # Extract the translated text from the response
        translated_text = translated_text[0]['translation_text']

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

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

Create two HTML templates, text.html and translation.html, to display the input form and the translated text respectively.
text.html:

<html>
<head>
    <title>Text Translation</title>
</head>
<body>
    <h1>Enter the Text for Translation</h1>
    <form method="post" action="{% url 'text_translation' %}">
        {% csrf_token %}
        <label for="text">Text:</label><br>
        <textarea id="text" name="text" rows="4" cols="50" required></textarea><br><br>
        <label for="target_lang">Target Language:</label>
        <select id="target_lang" name="target_lang" required>
            <option value="fr">French</option>
            <option value="es">Spanish</option>
            <option value="de">German</option>
            <!-- Add more language options as needed -->
        </select><br><br>
        <input type="submit" value="Translate">
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

translation.html:

html
Copy code
<!DOCTYPE html>
<html>
<head>
    <title>Text Translation Result</title>
</head>
<body>
    <h1>Translated Text:</h1>
    <p>{{ translated_text }}</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 text_translation

urlpatterns = [
    path('text-translation/', text_translation, name='text_translation'),
]
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 text translation form at http://localhost:8000/text-translation/. Enter the desired input text and select the target language, submit the form, and you will see the translated text displayed on the result page.

Top comments (0)