Untitled

 avatar
unknown
plain_text
a year ago
1.2 kB
5
Indexable
def chatbot_view(request):
    intents = get_intents()  # Load the intents
    response = None  # Default response

    if request.method == 'POST':
        user_input = request.POST.get('user_input')
        ints = predict_class(user_input)
        found_translation_intent = False
        res = None

        for intent in ints:
            if intent['intent'] == 'translate':
                found_translation_intent = True
                if 'batangueno_word' in intent:
                    response = intent['batangueno_word']
                else:
                    response = translate_text(user_input)
                break
            else:
                res = get_response(ints, intents)  # Pass the intents variable

        if not found_translation_intent and res:
            response = res

        # Check if the request is AJAX
        if request.headers.get('x-requested-with') == 'XMLHttpRequest':
            return JsonResponse({'response': response})

    # If not an AJAX request or AJAX request failed to provide a response, render the HTML template
    return render(request, 'chatbot.html', {'response': response})

Editor is loading...
Leave a Comment