New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option for autoplay
in Audio component
#1349
Comments
Seems reasonable to me. I think this would make sense as a parameter in the "style()" function of the "Audio()" component. |
Auto playing audio is not allowed in chrome, although works in other browsers. Auto playing videos work in all browsers as long as the video is muted. There are some workarounds but they are pretty heavy and can be inconsistent. Will see if we can any other options here since there have already been user interactions. |
I would recommend against using any work around to enforce auto play and to use the HTML |
In most cases, it is a slightly different case to normal autoplaying videos. It is similar to a user uploading a video via a file input and that video then autoplaying (as opposed to autoplaying on page load) but the main issue still stands. We would need to focus an appropriate part of the screen (possibly the video elements itself). On balance, I think autoplaying with the video muted (chrome default) is probably the best approach if we decide to support this.
I don't agree with this though, this isn't a stylistic choice but changes the behaviour of the UI. It should be a kwarg and updatable via |
I needed this for a small internal demo, where we wanted to output text-to-speech without the user pressing an additional button. My workaround looks like this: audio_el = gr.Audio(type="numpy", elem_id="speaker")
autoplay_audio = """async () => {
console.log('playing audio in 2 seconds')
let gradioEl = document.querySelector('body > gradio-app').shadowRoot;
setTimeout(() => {
let audioplayer = gradioEl.querySelector('#speaker > audio');
audioplayer.play();
}, 2000)
}"""
transcription.change(fn=update_audio, inputs=state, outputs=audio_el, _js=autoplay_audio) It works fine, but obviously the 2 seconds delay are hard coded because this is how long our system approximately needs to display the audio player. Is there a way to get the state of the audio component? |
I'm in same satuation to showcase an ai chat demo with TTS feature and no user. |
Agreeed. It would be great to enable this, as well as https://github.com/gradio-app/gradio/discussions/3316. cc @aliabid94 |
@pi43r and/or @Arcadia822 it sounds like you got a workaround running? @pi43r thank you for the code snippet. I unfortunately am having trouble getting it to work. I either end up with a |
It's been a while since I wrote the code and gradio might have changed. It should work in any browser (maybe not in safari because apple is different). I might be able to make a minimal reproduction in the future on huggingface... |
Strictly as a quick workaround, when I launched the interface of a chatbot I was playing with, I set debug=True and the audio played as the text was returned. An audio player box popped up below the gradio input/output boxes, but the audio played automatically. I also suppressed all warnings, to keep them from displaying. |
Thanks for the tip. I tried |
in launch function.
…On Mon, Mar 13, 2023 at 8:38 PM Tom Szumowski ***@***.***> wrote:
@robjm16 <https://github.com/robjm16>
Strictly as a quick workaround, when I launched the interface of a chatbot
I was playing with, I set debug=True and the audio played as the text was
returned.
Thanks for the tip. I tried debug=True in both the launch function and in
the audio block. No luck on my end. Though it may be because I'm using
blocks instead? Where did you place your debug=True?
—
Reply to this email directly, view it on GitHub
<#1349 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGFOR2GNCF2Y5SDAC7UWRU3W364ZNANCNFSM5WPTK5MA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I was trying to get this voice-enabled chatbot to work (code below). Wasn't using gradio blocks. import openai import warnings Create list of messages, starting with initial message to the systemmessages = [{"role": "system", "content": 'You are a therapist. Respond to all input in 25 words or less.'}] def transcribe(audio): Parameters: Returns:
def make_into_speech(words): Launch the interfaceui = gr.Interface(fn=transcribe, inputs=gr.Audio(source="microphone", type="filepath", label="Record Here"), outputs=[gr.Text(label="Chat Transcript")]) |
Thanks again for the additional info. I was able to get it to work for my use case with the usage of
|
I now use this method to add autoplay, which can be referred to below:
|
Thanks. This worked for me.
…On Thu, Mar 30, 2023 at 5:52 AM CsqTom ***@***.***> wrote:
I now use this method to add autoplay, which can be referred to below:
`
import gradio as gr
from gtts import gTTS
from io import BytesIO
import base64
def text_to_speech(text):
tts = gTTS(text)
tts.save('hello_world.mp3')
audio_bytes = BytesIO()
tts.write_to_fp(audio_bytes)
audio_bytes.seek(0)
audio = base64.b64encode(audio_bytes.read()).decode("utf-8")
audio_player = f'<audio src="data:audio/mpeg;base64,{audio}" controls autoplay></audio>'
return audio_player
with gr.Blocks() as demo:
html = gr.HTML()
# html.visible = False
text = gr.Text()
btn = gr.Button("OK")
btn.click(text_to_speech, inputs=[text], outputs=[html])
demo.launch()
`
—
Reply to this email directly, view it on GitHub
<#1349 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGFOR2EBUQLGNQ455SJW7B3W6VJUVANCNFSM5WPTK5MA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Thank you! It works! def audio_to_html(audio):
audio_bytes = BytesIO()
wavio.write(audio_bytes, audio[1].astype(np.float32), audio[0], sampwidth=4)
audio_bytes.seek(0)
audio_base64 = base64.b64encode(audio_bytes.read()).decode("utf-8")
audio_player = f'<audio src="data:audio/mpeg;base64,{audio_base64}" controls autoplay></audio>'
return audio_player and adding: import wavio |
Thanks the above solution worked for me, For a more beginner friendly description of what's going on:
Use the |
Thank you! Your code worked perfectly when I integrated it into my codebase, and cleaned it up a lot too! I appreciate the reproducible example you provided as well! |
🙌🏼 |
Is your feature request related to a problem? Please describe.
I'd like to be able to auto play a sound when it comes back from the inference.
Describe the solution you'd like
An new attribute,
False
by default, in theAudio
component to enable/disable autoplay of the HTML audio element.The text was updated successfully, but these errors were encountered: