Pavel Demeshkevich
Pavel Demeshkevich

Reputation: 37

AttributeError: module '__main__' has no attribute 'cleaner'

We are creating web-site with ai assistant. We trained our model in Google Colab and now we are trying to upload it to our project. But we get the following error:

AttributeError: module '__main__' has no attribute 'cleaner'

In our file views.py declared the class VoiceAssistant and the function cleaner for pipeline. The problem is hidden on the line:

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

While training our model we used the following code:

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'))

Upvotes: 0

Views: 1215

Answers (2)

modarwis
modarwis

Reputation: 1

Just change the name of your module from "main" to anything else, and it should work

Upvotes: 0

Pavel Demeshkevich
Pavel Demeshkevich

Reputation: 37

To solve this problem, I just added cleaner function to the manage.py, because there is the module main. It solved the problem.

Upvotes: 1

Related Questions