From 09c781b16f972a3a1fd80ac00dc4f0298f7d68bf Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Sun, 18 Jun 2023 16:31:14 -0300 Subject: [PATCH] Add modules/block_requests.py This has become unnecessary, but it could be useful in the future for other libraries. --- modules/block_requests.py | 19 +++++++++++++++++++ server.py | 17 +++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 modules/block_requests.py diff --git a/modules/block_requests.py b/modules/block_requests.py new file mode 100644 index 00000000..4358a820 --- /dev/null +++ b/modules/block_requests.py @@ -0,0 +1,19 @@ +import requests + +from modules.logging_colors import logger + + +class RequestBlocker: + + def __enter__(self): + self.original_get = requests.get + requests.get = my_get + + def __exit__(self, exc_type, exc_value, traceback): + requests.get = self.original_get + + +def my_get(url, **kwargs): + logger.info('Unwanted HTTP request redirected to localhost :)') + kwargs.setdefault('allow_redirects', True) + return requests.api.request('get', 'http://127.0.0.1/', **kwargs) diff --git a/server.py b/server.py index 9cdd33d1..4198dd69 100644 --- a/server.py +++ b/server.py @@ -1,26 +1,15 @@ import os import warnings -import requests - from modules.logging_colors import logger +from modules.block_requests import RequestBlocker os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False' os.environ['BITSANDBYTES_NOWELCOME'] = '1' warnings.filterwarnings('ignore', category=UserWarning, message='TypedStorage is deprecated') - -# This is a hack to prevent Gradio from phoning home when it gets imported -def my_get(url, **kwargs): - logger.info('Gradio HTTP request redirected to localhost :)') - kwargs.setdefault('allow_redirects', True) - return requests.api.request('get', 'http://127.0.0.1/', **kwargs) - - -original_get = requests.get -requests.get = my_get -import gradio as gr -requests.get = original_get +with RequestBlocker(): + import gradio as gr import matplotlib matplotlib.use('Agg') # This fixes LaTeX rendering on some systems