Exemplo n.º 1
0
    async def get_audio_response(self):
        """Download audio data then send to speech-to-text API for answer"""
        try:
            audio_url = await self.image_frame.evaluate(
                'jQuery("#audio-source").attr("src")')
            if not isinstance(audio_url, str):
                raise DownloadError(f"Audio url is not valid, expected `str`"
                                    "instead received {type(audio_url)}")
        except CancelledError:
            raise DownloadError("Audio url not found, aborting")

        self.log("Downloading audio file")
        try:
            audio_data = await self.loop.create_task(
                util.get_page(audio_url,
                              proxy=self.proxy,
                              proxy_auth=self.proxy_auth,
                              binary=True,
                              timeout=self.page_load_timeout))
        except ClientError as e:
            self.log(f"Error `{e}` occured during audio download, retrying")
        else:
            answer = None
            service = self.speech_service.lower()
            if service in [
                    "azure", "pocketsphinx", "deepspeech", "azurespeech"
            ]:
                if service == "azurespeech":
                    speech = AzureSpeech()
                elif service == "azure":
                    speech = Azure()
                elif service == "pocketsphinx":
                    speech = Sphinx()
                else:
                    speech = DeepSpeech()
                tmpd = tempfile.mkdtemp()
                tmpf = os.path.join(tmpd, "audio.mp3")
                await util.save_file(tmpf, data=audio_data, binary=True)
                answer = await self.loop.create_task(speech.get_text(tmpf))
                shutil.rmtree(tmpd)
            else:
                speech = Amazon()
                answer = await self.loop.create_task(
                    speech.get_text(audio_data))
            if answer:
                self.log(f'Received answer "{answer}"')
                return answer

            self.log("No answer, reloading")
            await self.click_reload_button()
            func = (
                f'"{audio_url}" !== '
                f'jQuery(".rc-audiochallenge-tdownload-link").attr("href")')
            try:
                await self.image_frame.waitForFunction(
                    func, timeout=self.animation_timeout)
            except TimeoutError:
                raise ReloadError("Download link never updated")
Exemplo n.º 2
0
async def solve(service, audio_file):
    answer = None
    service = service.lower()
    if service in ["azure", "pocketsphinx", "deepspeech"]:
        if service == "azure":
            speech = Azure()
        elif service == "pocketsphinx":
            speech = Sphinx()
        else:
            speech = DeepSpeech()
        answer = await speech.get_text(audio_file)
    else:
        speech = Amazon()
        answer = await speech.get_text(audio_file)
    if answer:
        print(f"audio file: {audio_file}")
        print(f"{service}'s best guess: {answer}")
    else:
        print(f"{service}'s failed to decipher: {audio_file}")
    if post_play_audio:
        await play_audio(audio_file)
    print("--------" * 15)