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 25, 2024
1 parent be56c76 commit e78ca3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions gradio/helpers.py
Original file line number Diff line number Diff line change
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
3 changes: 2 additions & 1 deletion gradio/utils.py
Original file line number Diff line number Diff line change
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,12 @@ 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 e78ca3a

Please sign in to comment.