ehForwarderBot / efb-telegram-master

EFB Telegram Master Channel, a channel for EH Forwarder Bot.
GNU Affero General Public License v3.0
223 stars 79 forks source link

An error during voice recog #25

Closed catbaron0 closed 5 years ago

catbaron0 commented 6 years ago

Here is the log:

2018-07-14 00:13:48,454 [ERROR]: telegram.ext.dispatcher (dispatcher.process_update; dispatcher.py:301)
    An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/telegram/ext/dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "/usr/local/lib/python3.6/dist-packages/telegram/ext/commandhandler.py", line 173, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "/usr/local/lib/python3.6/dist-packages/efb_telegram_master/voice_recognition.py", line 74, in recognize_speech
    file, _, _ = self._download_file(update.message, update.message.reply_to_message.voice, MsgType.Audio)
AttributeError: 'VoiceRecognitionManager' object has no attribute '_download_file'
catbaron0 commented 6 years ago

I think we got this problem because the script voice_recognition.py has not been updated for ver.2.0b, maybe.(There is one miss-spelling of recognise so I wonder if this function really works in ver.1.0). So I tried to fix it. Update the def recognize_speech(self, bot, update, args=[] block as the code below:

   def recognize_speech(self, bot, update, args=[]):
        """
        Recognise voice message. Triggered by `/recog`.

        Args:
            bot: Telegram Bot instance
            update: Message update
            args: Arguments from message
        """

        if not getattr(update.message, "reply_to_message", None):
            text = self._("/recog lang_code\n" \
                          "Reply to a voice with this command to recognize it.\n" \
                          "examples:\n/recog zh\n/recog en-US\n\nSupported languages:\n")
            text += "\n".join("%s: %r" % (i.engine_name, i.lang_list) for i in self.voice_engines)
            return self._reply_error(bot, update, text)
        if not getattr(update.message.reply_to_message, "voice"):
            return self._reply_error(bot, update,
                                     self._("Reply only to a voice with this command to recognize it. (RS02)"))

        if update.message.reply_to_message.voice.duration > 60:
            return self._reply_error(bot, update, self._("Only voice shorter than 60s is supported. (RS04)"))

        f = bot.get_file(update.message.reply_to_message.voice.file_id)
        file = tempfile.NamedTemporaryFile()
        f.download(out=file)
        file.seek(0)

        results = OrderedDict()
        if not args:
            args=['zh']
        for i in self.voice_engines:
            results["%s (%s)" % (i.engine_name, args[0])] = i.recognize(file.name, args[0])

        msg = ""
        for i in results:
            msg += "\n<b>%s</b>:\n" % html.escape(i)
            for j in results[i]:
                msg += "%s\n" % html.escape(j)
        msg = self._("Results:\n{0}").format(msg)
        bot.send_message(update.message.reply_to_message.chat.id, msg,
                              reply_to_message_id=update.message.reply_to_message.message_id,
                              parse_mode=telegram.ParseMode.HTML)
        file.close()

Baiduspeech works well for me, but I got some problem with Bingspeech:

/recog

Results:

Bing (zh):
因为之前看过很多那种跟你说过了嘛就算了嘛然后呢没想到明天下班了。
因为之前看过很多那种跟你说过了就算了嘛然后呢没想到明天下班了。
因为之前看过很多那种跟你说过了嘛就算了嘛然后呢没想到你然后下班了。
因为之前看过很多那种跟你说过了嘛就算了嘛然后呢没想到你天他换了。
因为之前看过很多那种跟你说过了就算了嘛然后呢没想到你然后下班了。

Baidu (zh):
真的受不了了就算了嘛,

Seems some coding problem happened. I just want to recognize speech for wechat so Baidu is enough for now.

blueset commented 6 years ago

Thank you for the report. Actually I'm considering isolating this voice recognition module as a middleware. I will try to fix this when the middleware is released.

blueset commented 5 years ago

Voice recognition implemented as middleware by @catbaron0. Thank you for your effort!