Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer importing matplotlib #7157

Merged
merged 1 commit into from Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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