gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.71k stars 2.55k forks source link

Audio component: gradio RuntimeError: Response content shorter than Content-Length #8878

Open JackismyShephard opened 3 months ago

JackismyShephard commented 3 months ago

Describe the bug

I am developing a gradio app with many instantiations of the Audio components (think 10+). I have observed that Audio components serving as output for event listener functions load new audio (in my case represented using file paths) very slowly and in some cases will not be able to load the new audio at all. In these cases I am getting a RuntimeError: Response content shorter than Content-Length in my terminal. A similar issue has been reported before at https://github.com/gradio-app/gradio/issues/8351 and (partially) at https://github.com/gradio-app/gradio/issues/7155.

Have you searched existing issues? πŸ”Ž

Reproduction

This is a bit hard at the moment as the relevant code is quite large and complex, but I can work on trying to reproduce the behaviour in a simple script if that is necessary.

Screenshot

image

Logs

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 399, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\gradio\route_utils.py", line 714, in __call__
    await self.app(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 75, in app
    await response(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\responses.py", line 352, in __call__
    await send(
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 50, in sender
    await send(message)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 50, in sender
    await send(message)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\errors.py", line 161, in _send
    await send(message)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 534, in send
    raise RuntimeError("Response content shorter than Content-Length")
RuntimeError: Response content shorter than Content-Length
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 399, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\gradio\route_utils.py", line 714, in __call__
    await self.app(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\routing.py", line 75, in app
    await response(scope, receive, send)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\responses.py", line 352, in __call__
    await send(
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 50, in sender
    await send(message)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\_exception_handler.py", line 50, in sender
    await send(message)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\starlette\middleware\errors.py", line 161, in _send
    await send(message)
  File "C:\Users\Jacki\ai_audio\ultimate-rvc\dependencies\.venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 534, in send
    raise RuntimeError("Response content shorter than Content-Length")
RuntimeError: Response content shorter than Content-Length

System Info

Gradio 4.38.1

Python 3.11 (using miniconda)

Windows 11 (also tested on Ubuntu 22.04 and 24.04)

Severity

I can work around it

abidlabs commented 3 months ago

Thanks @JackismyShephard yes a repro would be very helpful here!

JackismyShephard commented 3 months ago

@abidlabs So I have finally managed to write a small script that reproduces the error in one scenario: when downloading audio from youtube and trying to load this audio:

import gradio as gr
import yt_dlp

def _yt_download(link: str) -> str:
    link = link.split("&")[0]
    outtmpl = "0_%(title)s_Original"
    ydl_opts = {
        "quiet": True,
        "no_warnings": True,
        "format": "bestaudio",
        "outtmpl": outtmpl,
        "ignoreerrors": True,
        "nocheckcertificate": True,
        "postprocessors": [
            {
                "key": "FFmpegExtractAudio",
                "preferredcodec": "wav",
                "preferredquality": 0,
            }
        ],
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        result = ydl.extract_info(link, download=True)
        download_path = ydl.prepare_filename(result, outtmpl=f"{outtmpl}.wav")

    return download_path

with gr.Blocks(title="Ultimate RVC") as app:

    audio_link = gr.Text(
        label="Song link",
    )
    audio_output = gr.Audio(label="audio output", type="filepath", interactive=False)
    audio_input = gr.Audio(label="audio_input", type="filepath")
    retrieve_audio_btn = gr.Button("Retrieve audio", variant="primary")

    retrieve_audio_btn.click(
        _yt_download,
        inputs=[audio_link],
        outputs=[audio_output],
    ).success(
        lambda v: gr.Audio(value=v),
        inputs=[audio_output],
        outputs=[audio_input],
    )

if __name__ == "__main__":
    app.launch()

The first time I try to load audio with this script, it is successful, but upon trying to load new audio, usually the second (and sometimes also the first) audio component do(es) not update. In this case the error seems to be due to chaining event listeners (the error also occurs when using the .then method).

abidlabs commented 3 months ago

thanks @JackismyShephard! let me cc @hannahblair who is looking into audio component bugs

JackismyShephard commented 2 months ago

@abidlabs @hannahblair Are there any updates on this issue?

kmanninen commented 2 months ago

Hi. I am also looking for a solution to this issue. I am experiencing it locally - running Gradio on Windows and Ubuntu. My use case involves users either recording audio or uploading their own .wav files.

Specifically, this bug occurs when I add example .wav files to the interface and users select examples to load. The examples are usually no longer than 2 minutes each. After the user selects two or three examples the Gradio UI becomes very sluggish uploading or recording any new audio files. A peek into the browser console log shows numerous pending calls to "upload". In my command line, where Gradio is running, I receive a "Response content shorter than Content-Length" error, similar to the original post of this issue.

mauricio-fernandez-l commented 3 days ago

Any update on this? I get the same error already with the minimal code

import gradio as gr

with gr.Blocks() as demo:
    audio = gr.Audio(sources="microphone", type="filepath", scale=1)

demo.launch(inbrowser=True)

Errors are particularly likely to be triggered by short audio.