forestcalled/text-generation-webui
0
1import builtins2import io3 4import requests5 6from modules.logging_colors import logger7 8original_open = open9original_get = requests.get10 11 12class RequestBlocker:13 14 def __enter__(self):15 requests.get = my_get16 17 def __exit__(self, exc_type, exc_value, traceback):18 requests.get = original_get19 20 21class OpenMonkeyPatch:22 23 def __enter__(self):24 builtins.open = my_open25 26 def __exit__(self, exc_type, exc_value, traceback):27 builtins.open = original_open28 29 30def my_get(url, **kwargs):31 logger.info('Unwanted HTTP request redirected to localhost :)')32 kwargs.setdefault('allow_redirects', True)33 return requests.api.request('get', 'http://127.0.0.1/', **kwargs)34 35 36# Kindly provided by our friend WizardLM-30B37def my_open(*args, **kwargs):38 filename = str(args[0])39 if filename.endswith('index.html'):40 with original_open(*args, **kwargs) as f:41 file_contents = f.read()42 43 file_contents = file_contents.replace(b'\t\t<script\n\t\t\tsrc="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.7/iframeResizer.contentWindow.min.js"\n\t\t\tasync\n\t\t></script>', b'')44 file_contents = file_contents.replace(b'cdnjs.cloudflare.com', b'127.0.0.1')45 return io.BytesIO(file_contents)46 else:47 return original_open(*args, **kwargs)48 