【问题标题】:AttributeError: module '__main__' has no attribute 'cleaner'AttributeError: 模块 \'__main__\' 没有属性 \'cleaner\'
【发布时间】:2022-11-19 16:10:27
【问题描述】:

我们正在使用 ai 助手创建网站。我们在 Google Colab 中训练了我们的模型,现在我们正在尝试将其上传到我们的项目中。但是我们得到以下错误:

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

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

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

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

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

视图.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'))

【问题讨论】:

  • 你如何从views.py导入cleanerPipeline的文件在哪里?
  • 1)pipeline是机器学习的方法,我们现在不用。这对训练我们的模型很重要。所以,我们现在不使用这部分代码。 2)我们不会在代码中的任何地方导入更清洁的功能。这是姜戈。我们的模型(ai)需要此方法。我们没有明确地调用这个方法

标签: python django machine-learning scikit-learn joblib


【解决方案1】:

为了解决这个问题,我刚刚添加了清洁器函数到 manage.py,因为有模块主要的.它解决了这个问题。

【讨论】:

    【解决方案2】:

    只需将模块的名称从“main”更改为其他任何名称,它就可以工作

    【讨论】:

      猜你喜欢
      • 2019-09-14
      • 2017-12-19
      • 1970-01-01
      • 2019-04-10
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2019-02-18
      相关资源
      最近更新 更多