gradio icon indicating copy to clipboard operation
gradio copied to clipboard

front-end responses slowly with markdown

Open blairstar opened this issue 10 months ago • 2 comments

Describe the bug

I wrote a simple interactive program, which contains a button, a picture and several markdowns. The markdown has a lot of formulas. After sending more than 10 requests, the front-end response was extremely slow, especially expanding accordions. After debugging, it was initially discovered that it seems that every request will be re-rendered using katex.

Have you searched existing issues? 🔎

  • [X] I have searched and found no existing issues

Reproduction

import gradio as gr

import matplotlib.pyplot as plt import numpy as np

g_latex_del = [ {"left": "$$", "right": "$$", "display": True}, {"left": "$", "right": "$", "display": False}, {"left": "\(", "right": "\)", "display": False}, {"left": "\begin{equation}", "right": "\end{equation}", "display": True}, {"left": "\begin{align}", "right": "\end{align}", "display": True}, {"left": "\begin{aligned}", "right": "\end{aligned}", "display": True} ]

def markdown(): with gr.Accordion(label="markdown1") as accord: gr.Markdown( r""" \begin{align} Z_1 &= \sqrt{\alpha_1} X + \sqrt{1-\alpha_1}\epsilon_1 \tag{4.1} \newline Z_2 &= \sqrt{\alpha_2} Z_1 + \sqrt{1-\alpha_2}\epsilon_2 \tag{4.2} \newline &\dots \notag \newline Z_{t} &= \sqrt{\alpha_t}Z_{t-1} + \sqrt{1-\alpha_t}\epsilon_{t} \tag{4.3} \newline &\dots \notag \newline Z_{T} &= \sqrt{\alpha_T}Z_{T-1} + \sqrt{1-\alpha_T}\epsilon_{T} \tag{4.4} \newline &where \quad \alpha_t < 1 \qquad t\in {1,2,\dots,T} \notag \end{align}

        \begin{align}
            z_2 &= \sqrt{\alpha_2}(\sqrt{\alpha_1}x + \sqrt{1-\alpha_1}\epsilon_1) + \sqrt{1-\alpha_2}\epsilon_2	    \tag{4.5}   \newline
                &= \sqrt{\alpha_2\alpha_1}x + \sqrt{\alpha_2-\alpha_2\alpha_1}\epsilon_1 + \sqrt{1-\alpha_2}\epsilon_2  \tag{4.6}   \newline
                &= \mathcal{N}(\sqrt{\alpha_1\alpha_2}x,\ 1-\alpha_1\alpha_2)                                           \tag{4.7}
        \end{align}
        \begin{align}
            q(z_t|x) &= \mathcal{N}(\sqrt{\alpha_1\alpha_2\cdots\alpha_t}x,\ 1-\alpha_1\alpha_2\cdots\alpha_t) = \mathcal{N}(\sqrt{\bar{\alpha_t}}x,\ 1-\bar{\alpha_t})  \qquad where\ \bar{\alpha_t} \triangleq \prod_{j=1}^t\alpha_j      \tag{4.8}
        \end{align}
        """, latex_delimiters=g_latex_del)

with gr.Accordion(label="markdown2") as accord:
    gr.Markdown(
        r"""
        \begin{align}
            q(x|z) = \frac{q(z|x)q(x)}{q(z)}    \tag{3.1}
        \end{align}

        \begin{align}
            q(x|z)  \propto q(z|x)q(x) 	\qquad where\ z\ is\ fixed  \tag{3.2}
        \end{align}
        \begin{align}
            q(x|z)  &\propto \frac{1}{\sqrt{2\pi(1-\alpha)}}\exp{\frac{-(z-\sqrt{\alpha}x)^2}{2(1-\alpha)}}\ q(x)& 	\qquad &where\ z\ is\ fixed      \tag{3.3}   \newline
                    &=	\frac{1}{\sqrt{\alpha}} \underbrace{\frac{1}{\sqrt{2\pi}\sigma}\exp{\frac{-(x-\mu)^2}{2\sigma^2}}}_{GaussFun}\ q(x)& \qquad &where\ \mu=\frac{z}{\sqrt{\alpha}}\quad \sigma=\sqrt{\frac{1-\alpha}{\alpha}}   \tag{3.4}
        \end{align}
        """, latex_delimiters=g_latex_del)

with gr.Accordion(label="markdown3") as accord:
    gr.Markdown(
        r""" 
        \begin{align}
            Z_1   &= \sqrt{\alpha_1} X + \sqrt{1-\alpha_1}\epsilon_1 			\tag{4.1}   \newline
            Z_2   &= \sqrt{\alpha_2} Z_1 + \sqrt{1-\alpha_2}\epsilon_2 			\tag{4.2}   \newline
                  &\dots														\notag      \newline
            Z_{t} &= \sqrt{\alpha_t}Z_{t-1} + \sqrt{1-\alpha_t}\epsilon_{t}	    \tag{4.3}   \newline
                  &\dots													    \notag      \newline
            Z_{T} &= \sqrt{\alpha_T}Z_{T-1} + \sqrt{1-\alpha_T}\epsilon_{T}	    \tag{4.4}   \newline
                  &where \quad \alpha_t < 1   \qquad t\in {1,2,\dots,T}         \notag
        \end{align}

        \begin{align}
            z_2 &= \sqrt{\alpha_2}(\sqrt{\alpha_1}x + \sqrt{1-\alpha_1}\epsilon_1) + \sqrt{1-\alpha_2}\epsilon_2	    \tag{4.5}   \newline
                &= \sqrt{\alpha_2\alpha_1}x + \sqrt{\alpha_2-\alpha_2\alpha_1}\epsilon_1 + \sqrt{1-\alpha_2}\epsilon_2  \tag{4.6}   \newline
                &= \mathcal{N}(\sqrt{\alpha_1\alpha_2}x,\ 1-\alpha_1\alpha_2)                                           \tag{4.7}
        \end{align}
        \begin{align}
            q(z_t|x) &= \mathcal{N}(\sqrt{\alpha_1\alpha_2\cdots\alpha_t}x,\ 1-\alpha_1\alpha_2\cdots\alpha_t) = \mathcal{N}(\sqrt{\bar{\alpha_t}}x,\ 1-\bar{\alpha_t})  \qquad where\ \bar{\alpha_t} \triangleq \prod_{j=1}^t\alpha_j      \tag{4.8}
        \end{align}
        """, latex_delimiters=g_latex_del)
return

def draw(num): x = np.arange(0, 10) y = num*x fig = plt.figure(figsize=(3, 3.2)) ax = fig.add_subplot(111) ax.plot(x, y, label="line") fig.tight_layout()

return fig

def click_plot(): with gr.Accordion("click plot") as accord: num = gr.Number(value=0, label="number") plot = gr.Plot()

    num.change(draw, [num], [plot])
    
return

def run_app(): with gr.Blocks() as demo: markdown() click_plot()

demo.launch()

return

if name == "main": run_app()

Screenshot

No response

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Windows
gradio version: 4.12.0
gradio_client version: 0.8.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
altair: 5.2.0
fastapi: 0.109.2
ffmpy: 0.3.1
gradio-client==0.8.0 is not installed.
httpx: 0.26.0
huggingface-hub: 0.21.4
importlib-resources: 6.1.1
jinja2: 3.1.3
markupsafe: 2.1.5
matplotlib: 3.8.3
numpy: 1.22.4+mkl
orjson: 3.9.13
packaging: 22.0
pandas: 1.5.2
pillow: 9.3.0
pydantic: 2.6.1
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.9.0
typing-extensions: 4.9.0
uvicorn: 0.27.1
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.


gradio_client dependencies in your environment:

fsspec: 2024.2.0
httpx: 0.26.0
huggingface-hub: 0.21.4
packaging: 22.0
typing-extensions: 4.9.0
websockets: 11.0.3

Severity

Blocking usage of gradio

blairstar avatar Mar 29 '24 07:03 blairstar

source code: https://colab.research.google.com/drive/1Ht-eJWLA_zpxd-I2VRp69-d-cpEAcF8F?usp=sharing

blairstar avatar Mar 29 '24 07:03 blairstar