lenML/ChatTTS-Forge
301
1# based on https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/v1.6.0/modules/ui_gradio_extensions.py2 3import os4from pathlib import Path5 6import gradio as gr7 8from modules import config9 10from .localization import localization_js11 12GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse13 14WEBUI_DIR_PATH = Path(os.path.dirname(os.path.realpath(__file__)))15 16 17def read_file(fp):18 with open(WEBUI_DIR_PATH / fp, "r", encoding="utf-8") as f:19 return f.read()20 21 22def javascript_html():23 def s(text: str):24 return f'<script type="text/javascript">{text}</script>\n'25 26 def src(src: str):27 return f"<script src='{src}'></script>\n"28 29 def sf(fp: str):30 return s(read_file(fp))31 32 head = ""33 head += src("https://jsd.onmicrosoft.cn/npm/marked@12.0.2")34 head += s(localization_js(config.runtime_env_vars.language))35 head += sf("js/index.js")36 head += sf("js/localization.js")37 38 if config.runtime_env_vars.theme:39 head += s(f"set_theme('{config.runtime_env_vars.theme}');")40 41 return head42 43 44def css_html():45 head = f'<style>{read_file("css/style.css")}</style>'46 return head47 48 49def reload_javascript():50 js = javascript_html()51 css = css_html()52 53 def template_response(*args, **kwargs):54 res = GradioTemplateResponseOriginal(*args, **kwargs)55 res.body = res.body.replace(b"</head>", f"{js}</head>".encode("utf8"))56 res.body = res.body.replace(b"</body>", f"{css}</body>".encode("utf8"))57 res.init_headers()58 return res59 60 gr.routes.templates.TemplateResponse = template_response61 