首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:模块“__main__”没有属性“更干净”

AttributeError:模块“__main__”没有属性“更干净”
EN

Stack Overflow用户
提问于 2022-08-02 14:46:13
回答 2查看 205关注 0票数 0

我们正在创建与人工智能助理的网站。我们在Google中训练了我们的模型,现在我们正在尝试将它上传到我们的项目中。但是我们得到了以下错误:

代码语言:javascript
复制
AttributeError: module '__main__' has no attribute 'cleaner'

在我们的文件中,views.py声明了管道的类VoiceAssistant和函数cleaner。问题就隐藏在这条线上:

代码语言:javascript
复制
talk_model = joblib.load(r'artifitial_assistant/model.pkl')

在培训我们的模型时,我们使用了以下代码:

代码语言:javascript
复制
Pipeline(steps=[('bow',
                 CountVectorizer(analyzer = cleaner)),
                ('tfidf', TfidfTransformer()),
                ('classifier', DecisionTreeClassifier())])

Views.py:

代码语言:javascript
复制
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'))
EN

回答 2

Stack Overflow用户

发布于 2022-08-05 09:02:57

为了解决这个问题,我只是在manage.py中添加了更干净的函数,因为有模块。它解决了问题。

票数 1
EN

Stack Overflow用户

发布于 2022-11-19 07:44:01

只需将模块的名称从"main“更改为任何其他内容,它就会正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73209533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档