AI speech code stopping after the first speech command

Hello there, i am working on an AI for a scholarship and i am connecting my app to my code in my computer using uplink. After connecting to uplink, the text to speech is not exactly working. the module I am using for text to speech is pyttsx3 . Usually to make my code say something, I type //speak(text)// , and when I run it, the text is spoken. The problem really starts when i use more than one of this speak function… Example-
// speak(“Hello, this is the first sentance”)
speak(“Hello, this is the second sentance”)//
The code works when i run it just as a python code but when i run this is anvil, it just says the first sentence and just stops but the app would be still running.
For the sake of simplicity, i have not shown the code of my AI instead i have shown just a small code featuring just the text to speech :point_down:.

The python code in my system:

import pyttsx3 # pip install pyttsx3… the text to speech module
import anvil.server
anvil.server.connect(uplink key)

engine = pyttsx3.init(‘sapi5’)
voices = engine.getProperty(‘voices’)
engine.setProperty(‘voice’, voices[0].id)

def speak(text):
# This function will pronounce the string which u passed to it
engine.say(text)
engine.runAndWait()
engine.stop()
@anvil.server.callable
def say(): # the function i would be calling with anvil

speak("hello. testing. this is the first sentence")
speak("hello. this is the second sentence")
speak("hey. this is the third sentance")

anvil.server.wait_forever()

The Anvil code:

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server

class Form1(Form1Template):

def init(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)

# Any code you write here will run when the form opens.

def button_1_click(self, **event_args):
anvil.server.call(“say”)
“”“This method is called when the button is clicked”""
pass

So, when button_1 is clicked, it should say the 3 sentences but as you know, it says the first sentance and just stops…does it have anything to do with the anvil.server.wait_forever function? Please help me with this problem… Thank You!!

Welcome to the Anvil forum!

Just a guess, from a human-language perspective: A “sentence” is supposed to end with punctuation, e.g., a period, exclamation mark, or a question mark. Text-to-speech engines may rely on such punctuation in order to decide on how to enunciate the sentence as a whole.

Each of your speak calls ends without punctuation. Could this be part of the problem?

TIP: To help people reading your code, to get it formatted correctly in this forum, please put a line
(```)
(without the parentheses) before and after your code. This will preserve the meaningful indentation.

Thank You for replying but it seems that is not the issue. It works fine just as a python code but when i use it with anvil, there is this problem…i wonder whats wrong

I’m not experienced with sending things like this, but generally functions get returned all at once from what I understand. How does your uplink code return the audio? Is it an audio file?

Actually its not an audio file…In pyttsx3, it converts the text to speech right then itself

I’m looking at the pyttsx3 documentation, and it appears to rely on built in speech synthesizers on Windows and Mac to produce the voice. This might be part of the issue. I would also mention that this probably makes this app non-functional on a mobile device without a speech synthesizer.

It appears that you are able to export speech to an audio file with pyttsx3. I would recommend that on your uplink server you convert the synthesized speech to an audio file, which is then returned to the app. The app can then play the audio file in the browser.

Here is a SO thread on saving pyttsx3 speech to a file: https://stackoverflow.com/questions/53935096/how-can-i-convert-a-text-file-to-mp3-file-using-python-pyttsx3-and-sapi5

And here is an example of playing a sound on click: Play sound when called

1 Like

Oh…Thank you soo much… but i am not familiar with returning an audio file to the app using uplink, so, is there any example I should refer?

In Python everything is an object, so just return the file object. Then pass it through to something that will actually know what to do with it on the front end (i.e. the html audio player).

1 Like

Okay Sir. Once again, thank you for the answer

See
https://anvil.works/docs/server#valid-arguments-and-return-values

Note that this excludes returning opened files. What you’ll want to send, instead, is the contents of the file. For that, see Media objects.

2 Likes

Ah yes thank you. @p.colbert is very right. Wasn’t thinking Anvil-ly there.

Again, thank you guys… :smiley: