aliabd HF staff commited on
Commit
707aedb
1 Parent(s): 54d39fa

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +1 -1
  2. audio/cantina.wav +0 -0
  3. requirements.txt +1 -1
  4. run.ipynb +1 -1
  5. run.py +49 -18
README.md CHANGED
@@ -5,7 +5,7 @@ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
- sdk_version: 3.40.1
9
  app_file: run.py
10
  pinned: false
11
  hf_oauth: true
 
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
audio/cantina.wav ADDED
Binary file (132 kB). View file
 
requirements.txt CHANGED
@@ -1 +1 @@
1
- https://gradio-main-build.s3.amazonaws.com/2e720c0c47ea94923ab689f928308157f2c30b2d/gradio-3.40.1-py3-none-any.whl
 
1
+ https://gradio-main-build.s3.amazonaws.com/d76d50112328711b1a692b80c1e88a085b15b301/gradio-3.41.2-py3-none-any.whl
run.ipynb CHANGED
@@ -1 +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": ["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}
 
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 CHANGED
@@ -1,24 +1,55 @@
1
  import gradio as gr
2
  from pydub import AudioSegment
 
3
 
4
- def stream_audio(audio_file):
5
- audio = AudioSegment.from_mp3(audio_file)
6
- i = 0
7
- chunk_size = 3000
8
-
9
- while chunk_size*i < len(audio):
10
- chunk = audio[chunk_size*i:chunk_size*(i+1)]
11
- i += 1
12
- if chunk:
13
- file = f"/tmp/{i}.mp3"
14
- chunk.export(file, format="mp3")
15
- yield file
16
-
17
- demo = gr.Interface(
18
- fn=stream_audio,
19
- inputs=gr.Audio(type="filepath", label="Audio file to stream"),
20
- outputs=gr.Audio(autoplay=True, streaming=True),
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  if __name__ == "__main__":
24
  demo.queue().launch()
 
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()