File size: 1,270 Bytes
3017a3b
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: stream_audio"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio numpy "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import numpy as np\n", "import time\n", "\n", "def add_to_stream(audio, instream):\n", "    time.sleep(1)\n", "    if audio is None:\n", "        return gr.Audio(), instream\n", "    if instream is None:\n", "        ret = audio\n", "    else:\n", "        ret = (audio[0], np.concatenate((instream[1], audio[1])))\n", "    return ret, ret\n", "\n", "with gr.Blocks() as demo:\n", "    inp = gr.Audio(sources=[\"microphone\"])\n", "    out = gr.Audio()\n", "    stream = gr.State()\n", "    clear = gr.Button(\"Clear\")\n", "\n", "    inp.stream(add_to_stream, [inp, stream], [out, stream])\n", "    clear.click(lambda: [None, None, None], None, [inp, out, stream])\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}