AttributeError: модуль '__main__' не имеет атрибута 'cleaner'

Мы создаем веб-сайт с помощью ai assistant. Мы обучили нашу модель в Google Colab и теперь пытаемся загрузить ее в наш проект. Но мы получаем следующую проблему: AttributeError: модуль 'main' не имеет атрибута 'cleaner'.

В нашем файле "views.py" объявлен класс VoiceAssistant и функция "cleaner" для конвейера. Проблема скрыта в строке:

talk_model = joblib.load(r'artifitial_assistant/model.pkl')

В процессе обучения нашей модели мы использовали следующий код:

Pipeline(steps=[('bow',
                 CountVectorizer(analyzer = cleaner)),
                ('tfidf', TfidfTransformer()),
                ('classifier', DecisionTreeClassifier())])

Views.py:

import string
import traceback
import webbrowser
import joblib
import pyttsx3
import speech_recognition
import wikipedia
from django.shortcut import render


def cleaner(x):
    """
    cleaning function required for neural model
    """
    return [a for a in (''.join([a for a in x if a not in string.punctuation])).lower().split()]


class VoiceAssistant:
    """
    Settings of our voice assistant
    """
    name = ""
    sex = ""
    speech_lang = ""
    is_talking = False
    recognition_lang = ""
    # initializing speech recognition and input tools
    recognizer = speech_recognition.Recognizer()
    microphone = speech_recognition.Microphone()

    # initialization of the speech synthesis tool
    ttsEngine = pyttsx3.init()

    def assistant_answer(self, voice):
        """
        a function that loads user input into the neural model and predicts the response
        """
        answer = self.talk_model.predict([voice])[0]
        return answer


    # loading a neural model from disk
    talk_model = joblib.load(r'artifitial_assistant/model.pkl') # !!!<-Problem uppears here
    
    ... 

    
from django.shortcuts import render
from django.http import HttpResponse

#initializing voice_assistant
voice_assistant = VoiceAssistant()
voice_assistant.sex = "female"
voice_assistant.speech_lang = "en"
voice_assistant.name = "blonde"
voice_assistant.setup_assistant_voice()


def first_view(request): #just want to get the simplest response from voice_assistant
    return HttpResponse(voice_assistant.assistant_answer('Hi'))

Для решения этой проблемы я просто добавил функцию cleaner в manage.py, потому что там есть модуль main. Это решило проблему.

Просто измените имя вашего модуля с "main" на любое другое, и он должен работать

Вернуться на верх