from collections import namedtuple
import gradio as gr
import os
ScriptFile = namedtuple("ScriptFile", ["basedir", "filename", "path"])
GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse
current_dir = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.dirname(current_dir)
def get_character_html(text):
return f"""\
{text}
"""
def webpath(fn):
if fn.startswith(root_path):
web_path = os.path.relpath(fn, root_path).replace('\\', '/')
else:
web_path = os.path.abspath(fn)
return f'file={web_path}?{os.path.getmtime(fn)}'
def list_scripts(scriptdirname, extension):
scripts_list = []
scripts_dir = os.path.join(root_path, scriptdirname)
if os.path.exists(scripts_dir):
for filename in sorted(os.listdir(scripts_dir)):
scripts_list.append(ScriptFile(f"{root_path}/javascript", filename, os.path.join(scripts_dir, filename)))
scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
return scripts_list
def javascript_html():
head = ""
for script in list_scripts("javascript", ".js"):
head += f'\n'
for script in list_scripts("javascript", ".mjs"):
head += f'\n'
return head
def reload_javascript():
js = javascript_html()
js += """
"""
corner = """
"""
def template_response(*args, **kwargs):
res = GradioTemplateResponseOriginal(*args, **kwargs)
# res.body = res.body.replace(b'', f'{meta}{js}'.encode("utf8"))
res.body = res.body.replace(b'', f'{js}'.encode("utf8"))
res.body = res.body.replace(b'