-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* changes * add changeset * changes * changes * changes * changes * changes * changes * add changeset * changes * changes * changes * changes * changes * changes --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
- Loading branch information
1 parent
cd1353f
commit 667875b
Showing
10 changed files
with
187 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
"@gradio/upload": patch | ||
"gradio": patch | ||
--- | ||
|
||
fix:Live audio streaming output | ||
|
||
highlight: | ||
|
||
#### Now supports loading streamed outputs | ||
|
||
Allows users to use generators to stream audio out, yielding consecutive chunks of audio. Requires `streaming=True` to be set on the output audio. | ||
|
||
```python | ||
import gradio as gr | ||
from pydub import AudioSegment | ||
|
||
def stream_audio(audio_file): | ||
audio = AudioSegment.from_mp3(audio_file) | ||
i = 0 | ||
chunk_size = 3000 | ||
|
||
while chunk_size*i < len(audio): | ||
chunk = audio[chunk_size*i:chunk_size*(i+1)] | ||
i += 1 | ||
if chunk: | ||
file = f"/tmp/{i}.mp3" | ||
chunk.export(file, format="mp3") | ||
yield file | ||
|
||
demo = gr.Interface( | ||
fn=stream_audio, | ||
inputs=gr.Audio(type="filepath", label="Audio file to stream"), | ||
outputs=gr.Audio(autoplay=True, streaming=True), | ||
) | ||
|
||
demo.queue().launch() | ||
``` | ||
|
||
From the backend, streamed outputs are served from the `/stream/` endpoint instead of the `/file/` endpoint. Currently just used to serve audio streaming output. The output JSON will have `is_stream`: `true`, instead of `is_file`: `true` in the file data object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: stream_audio_out"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from pydub import AudioSegment\n", "\n", "def stream_audio(audio_file):\n", " audio = AudioSegment.from_mp3(audio_file)\n", " i = 0\n", " chunk_size = 3000\n", " \n", " while chunk_size*i < len(audio):\n", " chunk = audio[chunk_size*i:chunk_size*(i+1)]\n", " i += 1\n", " if chunk:\n", " file = f\"/tmp/{i}.mp3\"\n", " chunk.export(file, format=\"mp3\") \n", " yield file\n", " \n", "demo = gr.Interface(\n", " fn=stream_audio,\n", " inputs=gr.Audio(type=\"filepath\", label=\"Audio file to stream\"),\n", " outputs=gr.Audio(autoplay=True, streaming=True),\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.queue().launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import gradio as gr | ||
from pydub import AudioSegment | ||
|
||
def stream_audio(audio_file): | ||
audio = AudioSegment.from_mp3(audio_file) | ||
i = 0 | ||
chunk_size = 3000 | ||
|
||
while chunk_size*i < len(audio): | ||
chunk = audio[chunk_size*i:chunk_size*(i+1)] | ||
i += 1 | ||
if chunk: | ||
file = f"/tmp/{i}.mp3" | ||
chunk.export(file, format="mp3") | ||
yield file | ||
|
||
demo = gr.Interface( | ||
fn=stream_audio, | ||
inputs=gr.Audio(type="filepath", label="Audio file to stream"), | ||
outputs=gr.Audio(autoplay=True, streaming=True), | ||
) | ||
|
||
if __name__ == "__main__": | ||
demo.queue().launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
667875b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
gradio – ./
gradio-hugging-face.vercel.app
www.gradio.app
gradio-git-main-hugging-face.vercel.app
gradio-two.vercel.app
gradio.app