Skip to content

Commit

Permalink
Defer importing matplotlib
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jan 26, 2024
1 parent 40e6ddf commit ab2dcd0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-rice-laugh.md
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Defer importing matplotlib
7 changes: 4 additions & 3 deletions gradio/helpers.py
Expand Up @@ -15,13 +15,11 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, Optional

import matplotlib.pyplot as plt
import numpy as np
import PIL
import PIL.Image
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
from matplotlib import animation

from gradio import components, oauth, processing_utils, routes, utils, wasm_utils
from gradio.context import Context, LocalContext
Expand Down Expand Up @@ -903,6 +901,9 @@ def make_waveform(
Returns:
A filepath to the output video in mp4 format.
"""
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

if isinstance(audio, str):
audio_file = audio
audio = processing_utils.audio_from_file(audio)
Expand Down Expand Up @@ -1024,7 +1025,7 @@ def _animate(_):
b.set_y((-rand_height * samples)[idx])

frames = int(duration * 10)
anim = animation.FuncAnimation(
anim = FuncAnimation(
fig, # type: ignore
_animate,
repeat=False,
Expand Down
5 changes: 4 additions & 1 deletion gradio/utils.py
Expand Up @@ -39,7 +39,6 @@

import anyio
import httpx
import matplotlib
from typing_extensions import ParamSpec

import gradio
Expand Down Expand Up @@ -881,10 +880,14 @@ def __str__(self):

class MatplotlibBackendMananger:
def __enter__(self):
import matplotlib

self._original_backend = matplotlib.get_backend()
matplotlib.use("agg")

def __exit__(self, exc_type, exc_val, exc_tb):
import matplotlib

matplotlib.use(self._original_backend)


Expand Down

0 comments on commit ab2dcd0

Please sign in to comment.