0

When I used the SAPI alone, it works perfectly fine.

import win32com.client as wincl

speak = wincl.Dispatch("SAPI.SpVoice")
sentence = "This is a voice from Windows"
speak.Speak(sentence)

However, when I put it inside the Gradio function, it does not work anymore and gives an error.

import gradio as gr
import win32com.client as wincl

speak = wincl.Dispatch("SAPI.SpVoice")

def greet(name):
    global speak

    sentence = "Hello " + name + "!"
    speak.Speak(sentence)

    return sentence

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()

Error Messages

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\venv\lib\site-packages\gradio\routes.py", line 384, in run_predict
    output = await app.get_blocks().process_api(
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\venv\lib\site-packages\gradio\blocks.py", line 1032, in process_api
    result = await self.call_function(
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\venv\lib\site-packages\gradio\blocks.py", line 844, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\venv\lib\site-packages\anyio\to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "C:\Users\User\PycharmProjects\Voice_Chat_Bot\bot.py", line 11, in greet
    speak.Speak(sentence)
  File "<COMObject SAPI.SpVoice>", line 2, in Speak
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147221008), None)

I've tried to Googled around, but I still cannot find any clue. Can anyone please help?

  • 1
    Take a look at this answer: https://stackoverflow.com/questions/26764978/using-win32com-with-multithreading It looks like your `greet` function is running in a different thread. You cannot create an object in the main thread and use it in a worker thread without some extra work. – DS_London Mar 05 '23 at 08:49

0 Answers0