Spaces:
Sleeping
Sleeping
Download src/js_loader.py from Slep/CondViT-LRVSF-Demo: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/Slep/CondViT-LRVSF-Demo/resolve/main/src/js_loader.py
- Command line
-
hf download hf://spaces/Slep/CondViT-LRVSF-Demo/src/js_loader.py
-
curl -L -o js_loader.py https://huggingface.co/spaces/Slep/CondViT-LRVSF-Demo/resolve/main/src/js_loader.py
1.04 kB
| import gradio | |
| class JavaScriptLoader: | |
| def __init__(self, target): | |
| #Copy the template response | |
| self.original_template = gradio.routes.templates.TemplateResponse | |
| #Prep the js files | |
| self.load_js(target) | |
| #reassign the template response to your method, so gradio calls your method instead | |
| gradio.routes.templates.TemplateResponse = self.template_response | |
| def load_js(self, target): | |
| with open(target, 'r', encoding="utf-8") as file: | |
| self.loaded_script = f"<script>\n{file.read()}\n</script>" | |
| def template_response(self, *args, **kwargs): | |
| """Once gradio calls your method, you call the original, you modify it to include | |
| your scripts and you return the modified version | |
| """ | |
| response = self.original_template(*args, **kwargs) | |
| response.body = response.body.replace( | |
| '</head>'.encode('utf-8'), (self.loaded_script + "\n</head>").encode("utf-8") | |
| ) | |
| response.init_headers() | |
| return response |