-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve import patching performance
- Loading branch information
1 parent
6c6024f
commit 33100a9
Showing
1 changed file
with
26 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
# LLM tends to generate `.show()` which does not work in a headless environment | ||
import matplotlib.pyplot | ||
matplotlib.pyplot.show = lambda *_args, **_kwargs: matplotlib.pyplot.savefig("plot.png") | ||
|
||
# Disable progressbar for MoviePy which fills up the context window | ||
import moviepy.editor | ||
|
||
old_moviepy_editor_VideoClip_write_videofile = moviepy.editor.VideoClip.write_videofile | ||
moviepy.editor.VideoClip.write_videofile = ( | ||
lambda self, *args, **kwargs: old_moviepy_editor_VideoClip_write_videofile( | ||
self, *args, verbose=False, logger=None, **kwargs | ||
) | ||
) | ||
import sys | ||
|
||
original_import = __import__ | ||
|
||
|
||
def patched_import(name, globals=None, locals=None, fromlist=(), level=0): | ||
module = original_import(name, globals, locals, fromlist, level) | ||
|
||
if name == "matplotlib.pyplot": | ||
sys.modules["matplotlib.pyplot"].show = lambda: sys.modules[ | ||
"matplotlib.pyplot" | ||
].savefig("plot.png") | ||
elif name == "moviepy.editor": | ||
original_write_videofile = sys.modules[ | ||
"moviepy.editor" | ||
].VideoClip.write_videofile | ||
sys.modules["moviepy.editor"].VideoClip.write_videofile = ( | ||
lambda self, *args, **kwargs: original_write_videofile( | ||
self, *args, verbose=False, logger=None, **kwargs | ||
) | ||
) | ||
|
||
return module | ||
|
||
|
||
__builtins__["__import__"] = patched_import |