speechmatics / speechmatics-python

Python library and CLI for Speechmatics
https://speechmatics.github.io/speechmatics-python/
MIT License
58 stars 14 forks source link

asyncio.run() cannot be called from a running event loop #60

Closed ItriOne closed 1 year ago

ItriOne commented 1 year ago

Describe the bug

File D:\Python\Spyder\pkgs\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals)

File d:\workspace\soundtranscribe\speechrecognition.py:99 ws.run_synchronously(file, conf, settings)

File D:\Python\Spyder\Python\lib\site-packages\speechmatics\client.py:505 in run_synchronously asyncio.run(asyncio.wait_for(self.run(*args, **kwargs), timeout=timeout))

File asyncio\runners.py:33 in run

RuntimeError: asyncio.run() cannot be called from a running event loop

To Reproduce

import speechmatics

LANGUAGE = "ar"
AUDIO_FILE_PATH = wav_path
CONNECTION_URL = f"wss://[eu2.rt.speechmatics.com/v2/{LANGUAGE}](http://eu2.rt.speechmatics.com/v2/%7BLANGUAGE%7D)"
AUTH_TOKEN = "My secret Token" # hiden

Create a transcription client

ws = speechmatics.client.WebsocketClient(
    speechmatics.models.ConnectionSettings(
        url=CONNECTION_URL,
        auth_token=AUTH_TOKEN,
        generate_temp_token=True, # Enterprise customers don't need to provide this parameter
    )
)

Define an event handler to print the partial transcript

def print_partial_transcript(msg):
    print(f"(PART) {msg['metadata']['transcript']}")

Define an event handler to print the full transcript

def print_transcript(msg):
    print(f"(FULL) {msg['metadata']['transcript']}")

Register the event handler for partial transcript

ws.add_event_handler(
    event_name=speechmatics.models.ServerMessageType.AddPartialTranscript,
    event_handler=print_partial_transcript,
)

Register the event handler for full transcript

ws.add_event_handler(
    event_name=speechmatics.models.ServerMessageType.AddTranscript,
    event_handler=print_transcript,
)

settings = speechmatics.models.AudioSettings()

Define transcription parameters

conf = speechmatics.models.TranscriptionConfig(
    language=LANGUAGE,
    enable_partials=True,
)

with open(AUDIO_FILE_PATH, 'rb') as file:
    ws.run_synchronously(file, conf, settings)

Expected behavior A clear and concise description of what you expected to happen.

Screenshots/Logs If applicable, add screenshots or logs to help explain your problem.

Additional context Add any other context about the problem here.

TudorCRL commented 1 year ago

Hi @ItriOne, thanks for the message, and thanks for using the SDK! We're looking into this bug and will get back to as soon as we have an answer. Just so you're aware I lightly edited your comment to make sure all the code snippets were correctly placed in code snippet blocks

TudorCRL commented 1 year ago

@ItriOne, I've just copied the code snippets you provided and was not able to replicate the issue. I do have a couple of suggestions that you might want to follow up on:

  1. Are you running your code in a Jupyter notebook? According to this issue in stackoverflow, running asyncio processes in Jupyter notebooks can cause the exact error that you're seeing. If that is the case, then you might possibly find more luck using the ws.run function rather than ws.run_synchronously - run_synchronously wraps a call to ws.run in a asyncio.run(asyncio.wait_for()).], which are the kinds of function calls that can create issues in Jupyter.
  2. If that isn't the issue, then the other thing we've seen cause people issues in the past is weird unicode characters that have arisen from copy and pasting into different text editors, so that's always worth a check (though seems very unlikely in this case given the stack trace)

If neither of those suggestions help, then maybe you could share a few more details about your system so we can help debug, like python version, operating system, whether you're calling this from within some other piece of code and if so what that code might be.

Hopefully, that's enough to help you fix your issue, and as I said above, if it's still a problem, tell us and we can take another look

TudorCRL commented 1 year ago

This issue is over 2 months old so I'm going to close it, we can reopen if there's any further need for dicussion