Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 3.
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
-
|
| 11 |
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
|
| 2 |
---
|
| 3 |
+
title: stream_audio_out
|
| 4 |
+
emoji: 🔥
|
| 5 |
+
colorFrom: indigo
|
| 6 |
+
colorTo: indigo
|
| 7 |
sdk: gradio
|
| 8 |
+
sdk_version: 3.41.2
|
| 9 |
+
app_file: run.py
|
| 10 |
pinned: false
|
| 11 |
+
hf_oauth: true
|
| 12 |
---
|
|
|
|
|
|
audio/cantina.wav
ADDED
|
Binary file (132 kB). View file
|
|
|
run.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 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": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('audio')\n", "!wget -q -O audio/cantina.wav https://github.com/gradio-app/gradio/raw/main/demo/stream_audio_out/audio/cantina.wav"]}, {"cell_type": "code", "execution_count": null, "id": 44380577570523278879349135829904343037, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from pydub import AudioSegment\n", "from time import sleep\n", "\n", "with gr.Blocks() as demo:\n", " input_audio = gr.Audio(label=\"Input Audio\", type=\"filepath\", format=\"mp3\")\n", " with gr.Row():\n", " with gr.Column():\n", " stream_as_file_btn = gr.Button(\"Stream as File\")\n", " format = gr.Radio([\"wav\", \"mp3\"], value=\"wav\", label=\"Format\")\n", " stream_as_file_output = gr.Audio(streaming=True)\n", "\n", " def stream_file(audio_file, format):\n", " audio = AudioSegment.from_file(audio_file)\n", " i = 0\n", " chunk_size = 1000\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}.{format}\"\n", " chunk.export(file, format=format)\n", " yield file\n", " sleep(0.5)\n", "\n", " stream_as_file_btn.click(\n", " stream_file, [input_audio, format], stream_as_file_output\n", " )\n", "\n", " gr.Examples(\n", " [[\"audio/cantina.wav\", \"wav\"], [\"audio/cantina.wav\", \"mp3\"]],\n", " [input_audio, format],\n", " fn=stream_file,\n", " outputs=stream_as_file_output,\n", " cache_examples=True,\n", " )\n", "\n", " with gr.Column():\n", " stream_as_bytes_btn = gr.Button(\"Stream as Bytes\")\n", " stream_as_bytes_output = gr.Audio(format=\"bytes\", streaming=True)\n", "\n", " def stream_bytes(audio_file):\n", " chunk_size = 20_000\n", " with open(audio_file, \"rb\") as f:\n", " while True:\n", " chunk = f.read(chunk_size)\n", " if chunk:\n", " yield chunk\n", " sleep(1)\n", " else:\n", " break\n", " stream_as_bytes_btn.click(stream_bytes, input_audio, stream_as_bytes_output)\n", "\n", "if __name__ == \"__main__\":\n", " demo.queue().launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
+
from time import sleep
|
| 4 |
+
|
| 5 |
+
with gr.Blocks() as demo:
|
| 6 |
+
input_audio = gr.Audio(label="Input Audio", type="filepath", format="mp3")
|
| 7 |
+
with gr.Row():
|
| 8 |
+
with gr.Column():
|
| 9 |
+
stream_as_file_btn = gr.Button("Stream as File")
|
| 10 |
+
format = gr.Radio(["wav", "mp3"], value="wav", label="Format")
|
| 11 |
+
stream_as_file_output = gr.Audio(streaming=True)
|
| 12 |
+
|
| 13 |
+
def stream_file(audio_file, format):
|
| 14 |
+
audio = AudioSegment.from_file(audio_file)
|
| 15 |
+
i = 0
|
| 16 |
+
chunk_size = 1000
|
| 17 |
+
while chunk_size * i < len(audio):
|
| 18 |
+
chunk = audio[chunk_size * i : chunk_size * (i + 1)]
|
| 19 |
+
i += 1
|
| 20 |
+
if chunk:
|
| 21 |
+
file = f"/tmp/{i}.{format}"
|
| 22 |
+
chunk.export(file, format=format)
|
| 23 |
+
yield file
|
| 24 |
+
sleep(0.5)
|
| 25 |
+
|
| 26 |
+
stream_as_file_btn.click(
|
| 27 |
+
stream_file, [input_audio, format], stream_as_file_output
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
gr.Examples(
|
| 31 |
+
[["audio/cantina.wav", "wav"], ["audio/cantina.wav", "mp3"]],
|
| 32 |
+
[input_audio, format],
|
| 33 |
+
fn=stream_file,
|
| 34 |
+
outputs=stream_as_file_output,
|
| 35 |
+
cache_examples=True,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
with gr.Column():
|
| 39 |
+
stream_as_bytes_btn = gr.Button("Stream as Bytes")
|
| 40 |
+
stream_as_bytes_output = gr.Audio(format="bytes", streaming=True)
|
| 41 |
+
|
| 42 |
+
def stream_bytes(audio_file):
|
| 43 |
+
chunk_size = 20_000
|
| 44 |
+
with open(audio_file, "rb") as f:
|
| 45 |
+
while True:
|
| 46 |
+
chunk = f.read(chunk_size)
|
| 47 |
+
if chunk:
|
| 48 |
+
yield chunk
|
| 49 |
+
sleep(1)
|
| 50 |
+
else:
|
| 51 |
+
break
|
| 52 |
+
stream_as_bytes_btn.click(stream_bytes, input_audio, stream_as_bytes_output)
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
demo.queue().launch()
|