Hello, guys, i am using GTTS (Google Text To Speech), for text to speech, for an AI which i have been making for a scholarship…For simplicity, i have made a new code and anvil app just specifying The text to speech… In this code, using GTTS, a string is taken and is turn into speech and stored in an audio file and this is all in a function called speak. By using uplink, i am calling that function and returning the audio-file into the variable ‘audio’…So, can somebody please tell me how I can play the audio, step-by-step, because i just don’t get how other people are doing it.
Here is my server code-
import random
from gtts import gTTS
import anvil.server
anvil.server.connect("my uplink key")
@anvil.server.callable()
def speak(audio_string):
tts=gTTS(text=audio_string, lang='hi')
r= random.randint(1,1000000)
audio_file= 'audio-' + str(r)+ '.mp3'
tts.save(audio_file)
return audio_file
anvil.server.wait_forever()
And here is my client 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 text_box_1_pressed_enter(self, **event_args):
text=self.text_box_1.text
audio=anvil.server.call("speak",text)
"""This method is called when the user presses Enter in this text box"""
pass
So, can somebody please tell me, how to play the audio-file…Thank You
As I mentioned in my other post to you, when posting, please can you wrap your code inside triple back ticks to format it properly?
For example :
```
<code here>
```
You can edit your own post to do this. It makes it easier to read and for someone to help you.
If you do this I’ll look into your issue for you.
I see you edited it but not in the right way. I have done it for you - please take the time to see what I’ve done and apply it to your future posts.
Whatever your TTS library produces (an mp3 by the looks of it), you need to return it to Anvil as a “media object”. Then, in your client side code you can tell the browser to download the media object. Your browser will then play it in whatever the default mp3 player your browser uses.
The relevant documentation is here : https://anvil.works/docs/media
You are looking particularly to create a BlobMedia object on the server (uplink) side code, and you are looking to download it in the client side code. It’s all there.
If you are looking for any other way to play the audio in the browser, then you may have to get your hands dirty with <audio>
tags in HTML, but that it beyond my expertise. Maybe someone else can help with that.
edit - additionally, it looks like you are trying to save the result to a file with this code :
(tts.save(audio_file)
You need to turn tts
into the BlobMedia object. Something like this :
# instead of tts.save(audio_file)
tts_blob = anvil.BlobMedia('audio/mpeg', tts, name=audio_file)
return tts_blob
then in the browser, soething like this :
tts_blob = anvil.server.call("speak",text)
anvil.media.download(tts_blob)
Please note this is all untested code.
Yeah I’m So sorry for the inconvenience… I was in a bit of a hurry… Also ill test it out. And is there also a way to delete the downloaded audio file after speaking… And also is the edit okay?
Using my method you’re not saving it anywhere, except maybe in your browser cache (which you can clear using your browser management tools). Garbage collector will destroy the tts_blob when it goes out of scope.
If you save the file first (like your original code) then just google python file management or deletion. There’ll be tons of stuff on that.
Sir, i have tested it out but it just keeps on loading and there is no response…it not speaking…was the code to make it say included?
In the client code, before the download, put this :
print(type(tts_blob))
and check that is a media type.
Also, try changing this :
anvil.media.download(tts_blob)
to this
anvil.media.download(tts_blob.get_bytes())
Ok, looking at the docs here :
https://gtts.readthedocs.io/en/latest/module.html#examples
You cannot return the tts object directly. You must create a file-like object like this example from that documentation link :
from gtts import gTTS
from io import BytesIO
mp3_fp = BytesIO()
tts = gTTS('hello', lang='en')
tts.write_to_fp(mp3_fp)
# return this to the client side.
return mp3_fp
You may or may not have to convert it to a BlobMedia object first - you’ll need to experiment.
I got his error while doing this-
‘’’
anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize <class ‘_io.BytesIO’> object at msg[‘response’] at [Form1, line 15](javascript:void(0))
‘’’
This was the server code when I got the Error-
‘’’
from gtts import gTTS
import anvil.server
from io import BytesIO
anvil.server.connect(“WSYGGBM752KUNTVZTJE3N7VF-YUGJDTELSRVEWFUK”)
@anvil.server.callable()
def speak(audio_string):
mp3_fp = BytesIO()
tts = gTTS(audio_string, lang=‘en’)
tts.write_to_fp(mp3_fp)
# return this to the client side.
return mp3_fp
anvil.server.wait_forever()
‘’’
Ok, you do need to convert that into a BlobMedia using the code I gave before before returning it (it might require a bit of experimentation)
edit Actually - what is the line in the form that it fails on? The code at like 15.
And, please, for the third time of asking, format the code correctly using the backticks.
Oh my, i thought the format was right this time… ![:thinking: :thinking:](https://anvil.works/forum/images/emoji/apple/thinking.png?v=9)
You need to look at how I edited your original question’s code. You use back ticks (usually found on the key to the left of the “1” key at the top of the keyboard), and not the “>” symbol.
edit and you need to now remove the “>” symbols else it won’t recognise it as code.
edit 2 - you’ve used single quotes instead of back ticks. That won’t work. The back tick looks a lot like a single quote but it is usually slightly accented from top left to bottom right.
And also could you tell me what exactly to delete from the code to add this-
tts_blob = anvil.BlobMedia('audio/mpeg', tts, name=audio_file)
return tts_blob
because i noticed there are some undefined variables in the new code…
Ok, we’re gettting a little disorderly here ![:slight_smile: :slight_smile:](https://anvil.works/forum/images/emoji/apple/slight_smile.png?v=9)
Let’s get it back on track - though please try to find the back tick and adjust your code posts accordingly.
Ok, in your server code, try this :
from gtts import gTTS
from io import BytesIO
import anvil.server
anvil.server.connect("my uplink key")
@anvil.server.callable()
def speak(audio_string):
mp3_fp = BytesIO()
tts = gTTS(audio_string, lang='en')
tts.write_to_fp(mp3_fp)
tts_blob = anvil.BlobMedia('audio/mpeg', mp3_fp, name='audio.mp3')
return tts_blob
anvil.server.wait_forever()
Then in your client code, put this :
def text_box_1_pressed_enter(self, **event_args):
text=self.text_box_1.text
tts_blob = anvil.server.call("speak",text)
anvil.media.download(tts_blob)
and tell me what happens.
1 Like
Still no luck…Its just loading and loading
Have there been any errors printed?
Ok, I have just found something interesting. The return never seems to happen if I try to return the media object. No errors or anything, it just sits there.
This might be one for a mind greater than mine. Here is a clone of the project I’m using, along with a listing of my server code :
https://anvil.works/build#clone:AWECSIXLTEG7Q3JH=YXRPRFMPJDSS3NM6YW43EQOT
from gtts import gTTS
from io import BytesIO
import anvil.server
anvil.server.connect("xxx")
@anvil.server.callable()
def speak(audio_string):
print("in speak ...")
mp3_fp = BytesIO()
tts = gTTS(audio_string, lang='en')
tts.write_to_fp(mp3_fp)
# The file saved is fine. I can play it.
#tts.save("dave.mp3")
print("converting to blob")
tts_blob = anvil.BlobMedia('audio/mpeg', mp3_fp, name='audio.mp3')
# This prints.
print("converted to blob")
# This return never happens, or at least seems not to.
return tts_blob
anvil.server.wait_forever()
soooo… now what am i exactly supposed to do now ![:thinking: :thinking:](https://anvil.works/forum/images/emoji/apple/thinking.png?v=9)